error when docker build python django app in alpine linux
Asked Answered
C

5

5
  1. Dockerfile
FROM python:3.6.5-alpine3.7
WORKDIR /app
ADD . /app
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 10021
CMD ["python", "manage.py", "runserver", "0.0.0.0:10021"]

  1. Requirements

    • openpyxl==2.4.11
    • requests==2.18.4
    • Django==2.0.2
    • mysqlclient==1.3.12

  1. Build Error
Collecting mysqlclient==1.3.12 (from -r requirements.txt (line 4))
  Downloading https://mirrors.aliyun.com/pypi/packages/6f/86/bad31f1c1bb0cc99e88ca2adb7cb5c71f7a6540c1bb001480513de76a931/mysqlclient-1.3.12.tar.gz (89kB)
    Complete output from command python setup.py egg_info:
    /bin/sh: mysql_config: not found
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-install-er3m_9t6/mysqlclient/setup.py", line 17, in <module>
        metadata, options = get_config()
      File "/tmp/pip-install-er3m_9t6/mysqlclient/setup_posix.py", line 44, in get_config
        libs = mysql_config("libs_r")
      File "/tmp/pip-install-er3m_9t6/mysqlclient/setup_posix.py", line 26, in mysql_config
        raise EnvironmentError("%s not found" % (mysql_config.path,))
    OSError: mysql_config not found

Thank @danblack for mariadb-connector-c-dev which helped me solve the OSError: mysql_config not found error.


  1. New Dockerfile
FROM python:3.6.5-alpine3.7
WORKDIR /app
ADD . /app
RUN apk add --no-cache build-base && apk add --no-cache mariadb-connector-c-dev && pip install --no-cache-dir -r requirements.txt && apk del build-base
EXPOSE 10021
CMD ["python", "manage.py", "runserver", "0.0.0.0:10021"]

  1. New Problem

But a new problem occurred, Docker printed nothing when docker run ... ran, no logs, no errors. It should have printed logs like the following

Performing system checks... 
System check identified no issues (0 silenced). 
September 04, 2018 - 15:56:26 
Django version 2.0.2, using settings 'xxx.settings' 
Starting development server at http://0.0.0.0:10021/ 
Quit the server with CTRL-BREAK.

Can someone show me a complete Dockerfile? Please help! Thank you so much.

Cathee answered 4/9, 2018 at 4:44 Comment(2)
Something must be wrong with my old and new Dockerfile, because I run this django app with FROM centos/python-36-centos7 successfully. But I want to use FROM python:3.6.5-alpine3.7 due to the basic image size, please help me, thank you all.Cathee
That's a bit of a new question and should be asked as such. I hope you worked it out. No-one notices major changes to questions once answered.Cns
C
11

You'd need to install the mariadb-connector-c-dev alpine package, which provides mysql_config that the python packages uses to identify which libraries it needs link against.

Cns answered 4/9, 2018 at 5:1 Comment(4)
Thanks, docker build ran successfully, but docker run -p ..., without -d, didn't work and printed nothing. PS, The souce code can be run successfully in my host machine.Cathee
You'll have to explain "didn't work" a bit more however it sounds like its a new problem.Cns
I'm sorry, but when I run docker run -p ... in the host machine(Centos7.4), it just printed nothing so that I don't know what happened was. Docker should have printed just like Performing system checks... System check identified no issues (0 silenced). September 04, 2018 - 15:56:26 Django version 2.0.2, using settings 'xxx.settings' Starting development server at http://0.0.0.0:10021/ Quit the server with CTRL-BREAK. but, nothing. No logs. No errors.Cathee
docker exec -ti {containername} ash and look what is going on.Cns
C
8

As per @danblack, mainly you need to install mariadb-connector-c-dev package. But only this package will give compilation errors. So additionally you will need to add gcc and musl-dev packages into Dockerfile. This will make Django and MySQL work within alpine image.

FROM python:3.8-alpine

RUN apk add gcc musl-dev mariadb-connector-c-dev
Cabe answered 30/6, 2021 at 7:16 Comment(0)
S
2

Since mysql_config is required to compile python mysql dependencies and also it has 127MB in size so keeping that in Docker container will increase container size to overcome this problem we can install and after successfully running pip install we can safely remove

 apk add --no-cache mariadb-dev build-base

above command will install mysql dependencies then we can run pip install

pip3 install -r requirements.txt 

once pip is done we can safely remove

apk del mariadb-dev build-base
Sisterinlaw answered 17/10, 2022 at 17:21 Comment(0)
D
2

The trick is to temporarily install mariadb-dev which is only needed during build and then delete it. You can save some bandwitdh by installing the runtime deps before deleting the build deps.

Something like:

RUN apk add --no-cache --virtual .build-deps mariadb-dev ... \
    && pip install ...\
    && apk add --virtual .runtime-deps mariadb-client-libs \
    && apk del .build-deps
Documentary answered 17/10, 2022 at 17:24 Comment(0)
K
0

In my case run with:

FROM python:3.8-alpine

RUN apk add mysql mysql-client gcc musl-dev mariadb-connector-c-dev

Adding mysql and mysql-client

Kight answered 18/8, 2022 at 19:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.