python - 如何允许 Django 容器在没有权限错误的情况下使用 Docker SDK?
问题描述
我有一个运行 Django 应用程序的容器,我需要从中在主机上生成其他容器。我在我的一个视图中使用 Docker SDK for Python,但是在运行应用程序时出现 Permission Denied 错误。
Dockerfile:
# syntax = docker/dockerfile:experimental
FROM python:slim-buster
MAINTAINER "" ""
RUN mkdir -p /opt/app && \
mkdir -p /opt/app/pip_cache
WORKDIR /opt/app
COPY ./config ./src /opt/app/
RUN chmod +x web/start-server.sh && \
chmod +x web/wait-for-it.sh && \
chmod +x web/setup.sh && \
python3 -m pip install --upgrade pip && \
python3 -m pip install -r requirements.txt --cache-dir pip_cache && \
chown -R www-data:www-data /opt
CMD ./web/start-server.sh
STOPSIGNAL SIGTERM
码头工人-compose.yaml:
version: "3"
services:
nginx:
build: ./config/nginx
container_name: nginx
restart: always
ports:
- "8084:80"
- "443:443"
command: nginx -g "daemon off;"
volumes:
- static:/static/
- media:/media/
links:
- web:web
depends_on:
- web
web:
build: .
container_name: web
restart: always
expose:
- "8010"
links:
- db:db
volumes:
- static:/static/
- media:/media/
- /var/run/docker.sock:/var/run/docker.sock
env_file:
- config/web/django.env
depends_on:
- db
stdin_open: true
tty: true
db:
image: postgres:latest
container_name: postgres
restart: always
ports:
- "5432:5432"
env_file:
- config/postgres/database.env
volumes:
static:
media:
视图.py:
import docker
from django.http import HttpResponse
def container_view(request):
if request.method == 'GET':
client = docker.from_env()
client.images.pull('alpine')
client.containers.run('alpine', 'echo Hello, World!')
return HttpResponse('Success!')
加载时出错localhost:8084
:
DockerException at /
Error while fetching server API version: ('Connection aborted.', PermissionError(13, 'Permission denied'))
我很感激你们能给的任何意见/建议!
解决方案
推荐阅读
- r - 如何从 R 中的数据框中提取每个条目名称的中点位置?
- java - 使用 pdfbox 替换 pdf 中的字符串时获取 /u0000
- python - 如何在python中按顺序对列表进行排序?
- python - 制作数据集以在 Sklearn 中测试 PCA?
- java - Android Studio 我无法在课堂上获得回报。(火库)
- r - 通过预定义的协方差矩阵计算逐行马氏距离
- python - 如何保留自动添加的 argparse 帮助参数,但从中删除 -h 以便我可以将该选项用于另一个参数
- python - 将数据框保存为python中的另一个值
- apache-kafka - 从 Postgres 表到具有更新跟踪的 KSQL 表
- pine-script - 首支蜡烛收盘价