Docker : Alpine : Django - Error installing python mysqlclient library [duplicate] - python

This question already has answers here:
Docker Alpine: Error loading MySQLdb module
(2 answers)
Closed 1 year ago.
I am building an Alpine based image of a Django application to connect with a MySQL db. For connecting with the database, I am using mysqlclient. For building the image, I am using docker-compose. When I do docker-compose build I get the respective error:
#15 7.366 Downloading mysqlclient-1.3.0.tar.gz (76 kB)
#15 7.403 Preparing metadata (setup.py): started
#15 7.545 Preparing metadata (setup.py): finished with status 'error'
#15 7.545 ERROR: Command errored out with exit status 1:
#15 7.545 command: /usr/local/bin/python -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-3fyj3dl9/mysqlclient_4a600f29b1334b47a39251a3f24d8315/setup.py'"'"'; __file__='"'"'/tmp/pip-install-3fyj3dl9/mysqlclient_4a600f29b1334b47a39251a3f24d8315/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-wh30qihi
#15 7.545 cwd: /tmp/pip-install-3fyj3dl9/mysqlclient_4a600f29b1334b47a39251a3f24d8315/
#15 7.545 Complete output (10 lines):
#15 7.545 /bin/sh: mysql_config: not found
#15 7.545 Traceback (most recent call last):
#15 7.545 File "<string>", line 1, in <module>
#15 7.545 File "/tmp/pip-install-3fyj3dl9/mysqlclient_4a600f29b1334b47a39251a3f24d8315/setup.py", line 17, in <module>
#15 7.545 metadata, options = get_config()
#15 7.545 File "/tmp/pip-install-3fyj3dl9/mysqlclient_4a600f29b1334b47a39251a3f24d8315/setup_posix.py", line 47, in get_config
#15 7.545 libs = mysql_config("libs_r")
#15 7.545 File "/tmp/pip-install-3fyj3dl9/mysqlclient_4a600f29b1334b47a39251a3f24d8315/setup_posix.py", line 29, in mysql_config
#15 7.545 raise EnvironmentError("%s not found" % (mysql_config.path,))
#15 7.545 OSError: mysql_config not found
#15 7.545 ----------------------------------------
#15 7.545 WARNING: Discarding https://files.pythonhosted.org/packages/6a/91/bdfe808fb5dc99a5f65833b370818161b77ef6d1e19b488e4c146ab615aa/mysqlclient-1.3.0.tar.gz#sha256=06eb5664e3738b283ea2262ee60ed83192e898f019cc7ff251f4d05a564ab3b7 (from https://pypi.org/simple/mysqlclient/). Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
#15 7.545 ERROR: Could not find a version that satisfies the requirement mysqlclient (from versions: 1.3.0, 1.3.1, 1.3.2, 1.3.3, 1.3.4, 1.3.5, 1.3.6, 1.3.7, 1.3.8, 1.3.9, 1.3.10, 1.3.11rc1, 1.3.11, 1.3.12, 1.3.13, 1.3.14, 1.4.0rc1, 1.4.0rc2, 1.4.0rc3, 1.4.0, 1.4.1, 1.4.2, 1.4.2.post1, 1.4.3, 1.4.4, 1.4.5, 1.4.6, 2.0.0, 2.0.1, 2.0.2, 2.0.3, 2.1.0rc1)
#15 7.546 ERROR: No matching distribution found for mysqlclient
------
failed to solve: rpc error: code = Unknown desc = executor failed running [/bin/sh -c python -m pip install mysqlclient]: exit code: 1
This is my Django Dockerfile
# Base image for newly image. Alpine version is lightweigther.
FROM python:3.9-alpine
ENV PYTHONUNBUFFERED 1
# Set app directory
WORKDIR /app
COPY . /app/
# install python dependencies
RUN python -m pip install --upgrade pip
# dependencies
RUN python -m pip install Django djangorestframework django-extensions
RUN python -m pip install mysqlclient
RUN python -m pip install python-dotenv
And this is my docker-compose.yaml
# Docker Compose file Reference (https://docs.docker.com/compose/compose-file/)
version: '3.8'
# Different Components of the Application
services:
# Database Service (Mysql)
app-db:
container_name: theater-platform-db
image: mysql:5.7
ports:
- '2003:3306'
restart: always
environment:
MYSQL_DATABASE: theater_platform
MYSQL_USER: ${DB_USER}
MYSQL_PASSWORD: ${DB_USER_PW}
MYSQL_ROOT_PASSWORD: ${DB_ROOT_PW}
volumes:
- ./volumes/db-data:/var/lib/mysql
networks:
- backend
healthcheck:
test: 'mysqladmin ping -h theater-platform-db -P 3306 -u ${DB_USER} --password="${DB_USER_PW}"'
timeout: 10s
retries: 3
app-backend:
container_name: theater-platform-django-api-server
build:
context: backend
dockerfile: Dockerfile
ports:
- '2004:8000'
command: >
sh -c 'python manage.py makemigrations &&
python manage.py migrate &&
python manage.py runserver 8000'
restart: on-failure
environment:
DEBUG: '1'
DB_HOST: theater-platform-db
DB_PORT: 3306
DB_NAME: theater_platform
DB_USER: ${DB_USER}
DB_PASSWORD: ${DB_USER_PW}
volumes:
- ./volumes/app-data:/app/appdata
networks:
- backend
- frontend
depends_on:
app-db:
condition: service_healthy
app-frontend:
container_name: theater-platform-web-ui
build:
context: frontend
dockerfile: Dockerfile
ports:
- '2005:8080'
networks:
- frontend
volumes:
db-data:
app-data:
networks:
backend:
frontend:
Does anyone know how to fix it? I have tried to see proper documentation but with no success.

this link is the awsner
you forgot to install one library to compile mysqlclient

Related

Install python package in docker image from my local directory

My goal is to copy the python package from my local directory into docker image and execute an installation inside the image, but I got the error as what we can see below. The image link is for my project file tree.
My Docker File Below:
FROM python:3.10.1-slim
WORKDIR /copyTrading
COPY requirements.txt requirements.txt
COPY ccxt.pro-master/python/ /copyTrading
RUN pip install -e .
RUN pip3 install -r requirements.txt
COPY . .
EXPOSE 80
CMD [ "python", "server2.py"]
------------------------Docker Build Error--------------------------
=> ERROR [5/7] RUN pip install -e . 9.2s
------
> [5/7] RUN pip install -e .:
#9 4.734 Obtaining file:///copyTrading
#9 5.433 ERROR: Command errored out with exit status 1:
#9 5.433 command: /usr/local/bin/python -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/copyTrading/setup.py'"'"'; __file__='"'"'/copyTrading/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-leehc5bu
#9 5.433 cwd: /copyTrading/
#9 5.433 Complete output (7 lines):
#9 5.433 Traceback (most recent call last):
#9 5.433 File "<string>", line 1, in <module>
#9 5.433 File "/copyTrading/setup.py", line 25, in <module>
#9 5.433 with open(package_json, encoding='utf-8') as f:
#9 5.433 File "/usr/local/lib/python3.10/codecs.py", line 905, in open
#9 5.433 file = builtins.open(filename, mode, buffering)
#9 5.433 FileNotFoundError: [Errno 2] No such file or directory: '/package.json'
#9 5.433 ----------------------------------------
#9 5.433 WARNING: Discarding file:///copyTrading. Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
#9 5.433 ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
#9 8.817 WARNING: You are using pip version 21.2.4; however, version 22.2.1 is available.
#9 8.817 You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
------
executor failed running [/bin/sh -c pip install -e .]: exit code: 1
[Project File Tree][1]
[1]: https://i.stack.imgur.com/n2Zqb.png

Installing ruamel.yaml.clib with docker

I have a small project in django rest framework and I want to dockerize it. In my requirements.txt file there is a package called ruamel.yaml.clib==0.2.6. While downloading all other requirements is successfull, there is a problem when it tries to download this package.
#11 208.5 Collecting ruamel.yaml.clib==0.2.6
#11 208.7 Downloading ruamel.yaml.clib-0.2.6.tar.gz (180 kB)
#11 217.8 ERROR: Command errored out with exit status 1:
#11 217.8 command: /usr/local/bin/python -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-b8oectgw/ruamel-yaml-clib_517e9b3f18a94ebea71ec88fbaece43a/setup.py'"'"'; __file__='"'"'/tmp/pip-install-b8oectgw/ruamel-yaml-clib_517e9b3f18a94ebea71ec88fbaece43a/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-n2gr5j35
#11 217.8 cwd: /tmp/pip-install-b8oectgw/ruamel-yaml-clib_517e9b3f18a94ebea71ec88fbaece43a/
#11 217.8 Complete output (3 lines):
#11 217.8 sys.argv ['/tmp/pip-install-b8oectgw/ruamel-yaml-clib_517e9b3f18a94ebea71ec88fbaece43a/setup.py', 'egg_info', '--egg-base', '/tmp/pip-pip-egg-info-n2gr5j35']
#11 217.8 test compiling /tmp/tmp_ruamel_erx3efla/test_ruamel_yaml.c -> test_ruamel_yaml compile error: /tmp/tmp_ruamel_erx3efla/test_ruamel_yaml.c
#11 217.8 Exception: command 'gcc' failed: No such file or directory
#11 217.8 ----------------------------------------
#11 217.8 WARNING: Discarding https://files.pythonhosted.org/packages/8b/25/08e5ad2431a028d0723ca5540b3af6a32f58f25e83c6dda4d0fcef7288a3/ruamel.yaml.clib-0.2.6.tar.gz#sha256=4ff604ce439abb20794f05613c374759ce10e3595d1867764dd1ae675b85acbd (from https://pypi.org/simple/ruamel-yaml-clib/) (requires-python:>=3.5). Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
#11 217.8 ERROR: Could not find a version that satisfies the requirement ruamel.yaml.clib==0.2.6 (from versions: 0.1.0, 0.1.2, 0.2.0, 0.2.2, 0.2.3, 0.2.4, 0.2.6)
#11 217.8 ERROR: No matching distribution found for ruamel.yaml.clib==0.2.6
However, there is no problem when I download this package without docker. Any suggestions?
Here is Dockerfile:
FROM python:3-alpine
# set work directory
WORKDIR /usr/src/app
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# install dependencies
RUN pip install -U pip setuptools wheel ruamel.yaml ruamel.yaml.clib==0.2.6
COPY ./requirements.txt .
RUN pip install --default-timeout=100 -r requirements.txt
# copy project
COPY . .
Here is compose file
version: '3.8'
services:
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/usr/src/app
ports:
- 8000:8000
env_file:
- ./.env.dev
EDIT
As mentioned in comments by #Anthon, the problem was related to alpine. I used python:3.9-slim-buster instead in Dockerfile and problem solved!
I see that OP has moved on from Alpine to solve this issue, but for whatever reason you need/want/wish to stay on Alpine, you can install gcc, musl-dev, and python3-dev:
⋮
RUN apk add --no-cache gcc musl-dev python3-dev
⋮
RUN pip install ruamel.yaml.clib …
I think the problem is with the way your Dockerfile tries to install ruamel.yaml.clib. It should be installed using pip (just as documented for the ruamel.yaml).
I suggest you take it out of the requirements.txt and explicitly do a
pip install -U pip setuptools wheel ruamel.yaml.clib==0.2.6
in your Dockerfile instead. This should just get you the pre-compiled wheel instead of trying to compile ruamel.yaml.clib from source, which will not work if you don't have a C compiler installed (this is actually what docker complains about)
I have ruamel.yaml.clib running succesfully in multiple Docker containers (but I never use a requirements.txt)

Unable to deploy Flask App in AWS ElasticBeanstalk

I am deploying a flask app in the Elastic Beanstalk environment in AWS. It works completely fine in my local, but when I upload the same to Beanstalk, I get the following error in the Log Files.
[ERROR] An error occurred during execution of command [app-deploy] - [InstallDependency]. Stop running the command. Error: fail to install dependencies with requirements.txt file with error Command /bin/sh -c /var/app/venv/staging-LQM1lest/bin/pip install -r requirements.txt failed with error exit status 1. Stderr: ERROR: Command errored out with exit status 1:
command: /var/app/venv/staging-LQM1lest/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-tu8sxhxj/mysqlclient_f51fe93483714a68ae43574db1c80ed5/setup.py'"'"'; __file__='"'"'/tmp/pip-install-tu8sxhxj/mysqlclient_f51fe93483714a68ae43574db1c80ed5/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-pip-egg-info-abr_xfaw
cwd: /tmp/pip-install-tu8sxhxj/mysqlclient_f51fe93483714a68ae43574db1c80ed5/
Complete output (15 lines):
/bin/sh: mysql_config: command not found
/bin/sh: mariadb_config: command not found
/bin/sh: mysql_config: command not found
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-install-tu8sxhxj/mysqlclient_f51fe93483714a68ae43574db1c80ed5/setup.py", line 15, in <module>
metadata, options = get_config()
File "/tmp/pip-install-tu8sxhxj/mysqlclient_f51fe93483714a68ae43574db1c80ed5/setup_posix.py", line 70, in get_config
libs = mysql_config("libs")
File "/tmp/pip-install-tu8sxhxj/mysqlclient_f51fe93483714a68ae43574db1c80ed5/setup_posix.py", line 31, in mysql_config
raise OSError("{} not found".format(_mysql_config_path))
OSError: mysql_config not found
mysql_config --version
mariadb_config --version
mysql_config --libs
----------------------------------------
WARNING: Discarding https://files.pythonhosted.org/packages/3c/df/59cd2fa5e48d0804d213bdcb1acb4d08c403b61c7ff7ed4dd4a6a2deb3f7/mysqlclient-2.0.3.tar.gz#sha256=f6ebea7c008f155baeefe16c56cd3ee6239f7a5a9ae42396c2f1860f08a7c432 (from https://pypi.org/simple/mysqlclient/) (requires-python:>=3.5). Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
ERROR: Could not find a version that satisfies the requirement mysqlclient==2.0.3
ERROR: No matching distribution found for mysqlclient==2.0.3
2021/04/04 16:02:21.681275 [INFO] Executing cleanup logic
2021/04/04 16:02:21.681366 [INFO] CommandService Response: {"status":"FAILURE","api_version":"1.0","results":[{"status":"FAILURE","msg":"Engine execution has encountered an error.","returncode":1,"events":[{"msg":"Instance deployment failed to install application dependencies. The deployment failed.","timestamp":1617552141,"severity":"ERROR"},{"msg":"Instance deployment failed. For details, see 'eb-engine.log'.","timestamp":1617552141,"severity":"ERROR"}]}]}
My requirements.txt file is as below:
aniso8601==8.0.0
click==7.1.2
Flask==1.1.2
Flask-JWT==0.3.2
Flask-MySQLdb==0.2.0
Flask-RESTful==0.3.8
Flask-SQLAlchemy==2.4.3
itsdangerous==1.1.0
Jinja2==2.11.2
MarkupSafe==1.1.1
mysqlclient==2.0.3
PyJWT==1.4.2
pytz==2020.1
six==1.15.0
SQLAlchemy==1.3.18
Werkzeug==1.0.1
Even when I tried doing pip install -r requirements.txt for the first time in my local, I faced the same issue.
However, I resolved it using the following steps
1. $ brew install mysql
2. $ pip install mysqlclient
I assume something similar needs to be done in the server. Can someone help me figure out how can this be run in the server?

Unable to install pygame in GitHubActions workflow

In my GitHub Actions workflow, I have the following step:
run: |
pip3 install pygame
which results in following error:
Collecting pygame
Downloading pygame-1.9.6.tar.gz (3.2 MB)
ERROR: Command errored out with exit status 1:
command: /opt/hostedtoolcache/Python/3.9.0/x64/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-kswqgf01/pygame/setup.py'"'"'; __file__='"'"'/tmp/pip-install-kswqgf01/pygame/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-pip-egg-info-zlsswybj
cwd: /tmp/pip-install-kswqgf01/pygame/
Complete output (12 lines):
WARNING, No "Setup" File Exists, Running "buildconfig/config.py"
Using UNIX configuration...
/bin/sh: 1: sdl-config: not found
/bin/sh: 1: sdl-config: not found
/bin/sh: 1: sdl-config: not found
Unable to run "sdl-config". Please make sure a development version of SDL is installed.
Hunting dependencies...
WARNING: "sdl-config" failed!
----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
Error: Process completed with exit code 1.
What is causing that error, and how can that be fixed so I can perform unit tests for my Python program?
This works for me:
name: install-pygame
on: [push]
jobs:
check-pygame:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- uses: actions/setup-python#v2
with:
python-version: '3.6' # Version range or exact version of a Python version to use, using SemVer's version range syntax
architecture: 'x64' # optional x64 or x86. Defaults to x64 if not specified
- run: |
python -m pip install --upgrade pip
python3 -m pip install pygame==2.0.0.dev12
Please check this topic and this instruction

How to import pygame in gitpod?

This is a simple SudokuSolver App.
Project on GitHub
It's working perfectly. But when I try to run it in gitpod it's unable to import pygame.
Error:
gitpod /workspace/SudokuVizualizationBacktracking $ /home/gitpod/.pyenv/versions/3.8.2/bin/python /workspace/SudokuVizualizationBacktracking/GUI.py
Traceback (most recent call last):
File "/workspace/SudokuVizualizationBacktracking/GUI.py", line 1, in <module>
import pygame
ModuleNotFoundError: No module named 'pygame'
Can you please help me out? Thanks You.
In your .gitpod.yml you have configured the following init task:
$ pip3 install -r requirements.txt
Start your workspace by opening https://gitpod.io/#https://github.com/Deepak-dash007/SudokuVizualizationBacktracking and run this command in the terminal. You'll get the following output:
$ pip3 install -r requirements.txt
Collecting pygame
Downloading pygame-1.9.6.tar.gz (3.2 MB)
|████████████████████████████████| 3.2 MB 6.8 MB/s
ERROR: Command errored out with exit status 1:
command: /home/gitpod/.pyenv/versions/3.8.2/bin/python3 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-fgexue3e/pygame/setup.py'"'"'; __file__='"'"'/tmp/pip-install-fgexue3e/pygame/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-pip-egg-info-9lyba20o
cwd: /tmp/pip-install-fgexue3e/pygame/
Complete output (12 lines):
WARNING, No "Setup" File Exists, Running "buildconfig/config.py"
Using UNIX configuration...
/bin/sh: 1: sdl-config: not found
/bin/sh: 1: sdl-config: not found
/bin/sh: 1: sdl-config: not found
Hunting dependencies...
WARNING: "sdl-config" failed!
Unable to run "sdl-config". Please make sure a development version of SDL is installed.
----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
Here you can see that sdl-config is missing. Google found this Stack Overflow answer that lists dependencies needed for pygame: https://stackoverflow.com/a/60990677/1364435
Change your image config in your .gitpod.yml to:
image:
file: .gitpod.Dockerfile
and added a new file .gitpod.Dockerfile that installs the dependencies linked in the other Stack Overflow post:
FROM gitpod/workspace-full-vnc
RUN sudo apt-get update && sudo apt-get install -y \
python-dev libsdl-image1.2-dev libsdl-mixer1.2-dev libsdl-ttf2.0-dev libsdl1.2-dev libsmpeg-dev python-numpy subversion libportmidi-dev ffmpeg libswscale-dev libavformat-dev libavcodec-dev
Push everything and create a new Gitpod workspace. That's it.
You'll find a running repo here https://github.com/corneliusludmann/SudokuVizualizationBacktracking
I also created a Pull Request for you: https://github.com/Deepak-dash007/SudokuVizualizationBacktracking/pull/1

Categories

Resources