I am building an application with Djnago and MySql. I want to use docker for the deployment of my application. I have prepared a requirement.txt
, docker-compose.yml
and a Dockerfile
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
Dockerfile
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,
}
}
I have been trying to run the command docker build .
to build an image from docker file but I get this error. Looks like there is some issue with the MySql connector. I have tried searching for the solution but couldn't found any thing to fix this. I am able to build the image if I remove the mysql_db service from the 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
Just run pip install requirements after apt-get install because mysqlclient requires libmysqlclient-dev:
You're using apt package manager with alpine base linux image which is incompatible. I recommend to take python3.7-slim with debian os which supports apt.
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
If you do need alpine modify Dockerfile like this:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With