一尘不染

无法为Django和Mysql触发Docker构建

docker

我正在用Djnago和MySql构建应用程序。我想使用docker来部署我的应用程序。我已经准备了一个requirement.txtdocker- compose.ymlDockerfile

docker-compose.yml

version: "3"

services:
  law-application:
    restart: always
    build:
      context: .
    ports:
      - "8000:8000"
    volumes:
      - ./app:/app
    command: >
      sh -c "python manage.py runserver 0.0.0.0:8000"
    depends_on:
      - mysql_db
  mysql_db:
    image: mysql:latest
    command: mysqld --default-authentication-plugin=mysql_native_password
    volumes:
      - "./mysql:/var/lib/mysql"
    ports:
      - "3306:3306"
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=root
      - MYSQL_USER=root
      - MYSQL_PASSWORD=root

requirements.txt

django>=2.1.3,<2.2.0
djangorestframework==3.11.0
mysqlclient==1.4.6

Docker文件

FROM python:3.7-alpine
MAINTAINER Intersources Inc.

ENV PYTHONUNBUFFERED 1

COPY ./requirements.txt /requirements.txt
RUN pip install -r /requirements.txt
RUN apt-get update
RUN apt-get install python3-dev default-libmysqlclient-dev  -y

RUN mkdir /app

WORKDIR /app
COPY ./app /app

RUN adduser -D jeet
USER jeet

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'my-app-db',
        'USER': 'root',
        'PASSWORD': 'root',
        'HOST': 'mysql_db',
        'PORT': 3307,
    }
}

我一直在尝试运行命令docker build .从docker文件构建映像,但出现此错误。看起来MySql连接器有问题。我尝试搜索解决方案,但找不到任何解决方法。如果从中删除 mysql_db
服务,则可以构建映像docker-compose.yml

Sending build context to Docker daemon  444.7MB
Step 1/12 : FROM python:3.7-alpine
 ---> 6c7f85a86cca
Step 2/12 : MAINTAINER Intersources Inc.
 ---> Using cache
 ---> 03b6fa5764d4
Step 3/12 : ENV PYTHONUNBUFFERED 1
 ---> Using cache
 ---> 22ecd91dcb55
Step 4/12 : COPY ./requirements.txt /requirements.txt
 ---> Using cache
 ---> e58c16108f20
Step 5/12 : RUN pip install -r /requirements.txt
 ---> Running in 8f3eb8240fce
Collecting django<2.2.0,>=2.1.3
  Downloading https://files.pythonhosted.org/packages/ff/82/55a696532518aa47666b45480b579a221638ab29d60d33ce71fcbd3cef9a/Django-2.1.15-py3-none-any.whl (7.3MB)
Collecting djangorestframework==3.11.0
  Downloading https://files.pythonhosted.org/packages/be/5b/9bbde4395a1074d528d6d9e0cc161d3b99bd9d0b2b558ca919ffaa2e0068/djangorestframework-3.11.0-py3-none-any.whl (911kB)
Collecting mysqlclient==1.4.6
  Downloading https://files.pythonhosted.org/packages/d0/97/7326248ac8d5049968bf4ec708a5d3d4806e412a42e74160d7f266a3e03a/mysqlclient-1.4.6.tar.gz (85kB)
    ERROR: Command errored out with exit status 1:
     command: /usr/local/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-e9kt1otq/mysqlclient/setup.py'"'"'; __file__='"'"'/tmp/pip-install-e9kt1otq/mysqlclient/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-install-e9kt1otq/mysqlclient/pip-egg-info
         cwd: /tmp/pip-install-e9kt1otq/mysqlclient/
    Complete output (12 lines):
    /bin/sh: mysql_config: not found
    /bin/sh: mariadb_config: not found
    /bin/sh: mysql_config: not found
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-install-e9kt1otq/mysqlclient/setup.py", line 16, in <module>
        metadata, options = get_config()
      File "/tmp/pip-install-e9kt1otq/mysqlclient/setup_posix.py", line 61, in get_config
        libs = mysql_config("libs")
      File "/tmp/pip-install-e9kt1otq/mysqlclient/setup_posix.py", line 29, in mysql_config
        raise EnvironmentError("%s not found" % (_mysql_config_path,))
    OSError: mysql_config not found
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
The command '/bin/sh -c pip install -r /requirements.txt' returned a non-zero code: 1

阅读 275

收藏
2020-06-17

共1个答案

一尘不染

在apt-get安装后只运行pip安装要求,因为mysqlclient需要libmysqlclient-dev:

您正在将apt软件包管理器与不兼容的alpine基本linux映像一起使用。我建议将python3.7-slim与支持apt的debian os一起使用。

FROM python:3.7-slim
MAINTAINER Intersources Inc.

ENV PYTHONUNBUFFERED 1

RUN apt-get update
RUN apt-get install python3-dev default-libmysqlclient-dev gcc  -y
COPY ./requirements.txt /requirements.txt
RUN pip install -r /requirements.txt

RUN mkdir /app

WORKDIR /app
COPY ./app /app

RUN adduser -D jeet
USER jeet

如果您确实需要像这样高山修改Dockerfile:

FROM python:3.7-alpine
MAINTAINER Intersources Inc.

RUN apk update
RUN apk add musl-dev mariadb-dev gcc
RUN pip install mysqlclient

RUN mkdir /app

WORKDIR /app
COPY ./app /app

RUN adduser -D jeet
USER jeet
2020-06-17