首页 > 解决方案 > 如何允许 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'))

我很感激你们能给的任何意见/建议!

标签: pythondjangodocker

解决方案


推荐阅读