I am attempting to dockerize a Django project that features a MySQL database and nginx. When I run the docker-compose up --build
command, I receive the error message django.db.utils.OperationalError: (2002, "Can't connect to local server through socket '/run/mysqld/mysqld.sock' (2)").
Here's my docker-compose.yml
file:
version: '3.7'
services:
app:
build: ./app
env_file:
- .env
container_name: app
restart: always
expose:
- 8000
command: bash -c "python3 manage.py collectstatic --noinput && python3 manage.py migrate --noinput --fake-initial && \
gunicorn -b 0.0.0.0:8000 veblog.wsgi"
environment:
- MYSQL_DATABASE=${NAME}
- MYSQL_USER=${USER_NAME}
- MYSQL_PASSWORD=${PASSWORD}
- MYSQL_HOST=sqldb
mem_limit: 1g
depends_on:
- sqldb
# - nginx
volumes:
- ./volumes/app:/app
- type: bind
source: ./volumes/media
target: /app/media
- type: bind
source: ./volumes/static
target: /app/static
nginx:
build: ./nginx
container_name: nginx
restart: always
ports:
- 8000:80
sqldb:
image: mysql:latest
container_name: sqldb
restart: always
depends_on:
- nginx
expose:
- 3306
environment:
- MYSQL_DATABASE=${NAME}
- MYSQL_USER=${USER_NAME}
- MYSQL_PASSWORD=${PASSWORD}
- MYSQL_ROOT_PASSWORD=${ROOT_PASSWORD}
- MYSQL_INITDB_SKIP_TZINFO=true
- MYSQL_INIT_COMMAND=SET GLOBAL host_cache_size=0;
volumes:
- type: bind
source: ./volumes/dbdata
target: /var/lib/mysql
- /usr/share/zoneinfo:/usr/share/zoneinfo:ro
This is my Dockerfile
in the app
directory:
FROM python:3
ENV PYTHONDONTWRITEBYCODE 1
ENV PYTHONUNBUFFERED 1
RUN mkdir -p /app
WORKDIR /app
COPY . .
RUN apt update
RUN apt install gcc python3-dev musl-dev -y
RUN pip install --upgrade pip
RUN pip install mysqlclient
COPY ./requirement.txt .
RUN pip install -r requirement.txt
ADD . /app/
RUN pip install gunicorn
Here's the Dockerfile for nginx:
FROM nginx:alpine
RUN rm /etc/nginx/conf.d/default.conf
ADD default.conf /etc/nginx/conf.d/default.conf
And here's my default.conf
file:
upstream app {
server app:8000;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://app;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /media/ {
alias /app/media;
}
location /static/ {
alias /app/static;
}
}
This is my settings.py
file:
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': config('NAME'),
'USER': config('USER_NAME'),
'PASSWORD': config('PASSWORD'),
'HOST': 'localhost', # host.docker.internal
'PORT': '3306',
MySQL and Apache2 are started and active. I can connect to my database in the terminal. When I set HOST
to 127.0.0.1
, I get the following error: (2002, "Can't connect to MySQL server on '127.0.0.1' (115)").
When I set HOST
to sqldb
, I get the following error: (2002, "Can't connect to MySQL server on 'sqldb' (115)").
/var/run/mysqld/mysqld.sock
and /etc/mysql/my.cnf
are empty. I have tried the following commands: sudo chmod -R 755 /var/run/mysqld
, ln -s /var/lib/mysql/mysql.sock /var/run/mysqld/mysqld.sock
, SQL uninstall and install, set IP Docker to bind-address. I use command 'docker build' separately instead of docker-compose up --build
.
I tried the solutions listed in this link: ERROR 2002 (HY000): Can't connect to MySQL server on '192.168.1.15' (115), but they didn't work. Specifically, I ran the command mysql --help | grep -A 1 "Default options" Default options are read from the following files in the given order: /etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf
. I then set the bind-address
to 0.0.0.0
in /etc/my.cnf
, /etc/mysql/my.cnf
, and ~/.my.cnf
, but when I logged into mysql and ran SHOW VARIABLES LIKE 'bind-address'
, I received the error 'Empty set (0.01 sec)'. I also gave access to my firewall. Can anyone guide me on this?