Reputation:
I'm trying to start the server through docker-compose up
I'm get an error:
2002, "Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2)"
docker-compose.yml
version: '3'
services:
db:
image: mysql:5.7
environment:
MYSQL_DATABASE: 'slack_bot1'
MYSQL_USER: 'root'
MYSQL_PASSWORD: ''
MYSQL_ROOT_PASSWORD: '****'
volumes:
- /opt/slack_bot/mysql_data:/var/lib/mysql
redis:
image: "redis:alpine"
volumes:
- /opt/slack_bot/redis_data:/var/lib/redis
web:
build: .
command: python manage.py runserver 0.0.0.0:8001
ports:
- "8001:8001"
depends_on:
- db
Dockerfile
FROM python:3.7-alpine
ENV PYTHONUNBUFFERED 1
WORKDIR /home/slack_bot
ADD requirements.txt /home/slack_bot/
RUN set -e; \
apk add --no-cache --virtual .build-deps \
gcc \
libc-dev \
linux-headers \
mariadb-dev \
python3-dev \
;
RUN pip install -r requirements.txt
ADD . /home/slack_bot/
EXPOSE 8001
CMD ["python", "manage.py", "runserver", "0.0.0.0:8001"]
docker ps log
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
68b61ca0ce74 slack_bot_web "python manage.py ru…" 8 minutes ago Up 32 seconds 0.0.0.0:8001->8001/tcp slack_bot_web_1
c5f254a527b0 mysql:5.7 "docker-entrypoint.s…" 8 minutes ago Up 34 seconds 3306/tcp, 33060/tcp slack_bot_db_1
4cbc1fa3765e redis:alpine "docker-entrypoint.s…" 15 minutes ago Up 33 seconds 6379/tcp slack_bot_redis_1
Django settings of database
'ENGINE': 'django.db.backends.mysql',
'NAME': 'slack_bot1',
'USER': 'root',
'PASSWORD': '',
'HOST': '',
'PORT': '',
Upvotes: 7
Views: 17922
Reputation: 371
I was able to connect django and db docker containers which are in the same machine but aren't in the same docker-compose file by the following steps:
Use host.docker.internal
as host in settings.py instead of localhost as following:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'dbName',
'USER': 'dbUser',
'PASSWORD': 'dbUserPassword',
'HOST': 'host.docker.internal',
'PORT': '3306'
}
}
Expose the port in db compose-file
ports:
- 3306:3306
Upvotes: 1
Reputation: 31664
In your django settings, you leave database host as EMPTY, then the default value would be localhost
. When use localhost
, mysql client driver will connect mysql server with unix socket
not tcp
.
So, for your case you need to export unix socket
in your mysql container to volume, then your django app container utilize this volume to share the unix socket
.
Next is what you needed to do:
Manual new a folder which later as volume to share your unix socket file
(IMPORTANT: you could not rely on docker-compose to new this folder, you will encountered permission error)
mkdir -p /tmp/slack_bot/mysqld && chmod -R 777 /tmp/slack_bot/mysqld
In your docker-compose.yaml
, add one more volume to mysql
service:
db:
image: mysql:5.7
environment:
MYSQL_DATABASE: 'slack_bot1'
MYSQL_USER: 'root'
MYSQL_PASSWORD: ''
MYSQL_ROOT_PASSWORD: '****'
volumes:
- /opt/slack_bot/mysql_data:/var/lib/mysql
- /tmp/slack_bot/mysqld:/var/run/mysqld
In your docker-compose.yaml
, add one more volume to web
service:
web:
build: .
command: python manage.py runserver 0.0.0.0:8001
ports:
- "8001:8001"
depends_on:
- db
volumes:
- /tmp/slack_bot/mysqld:/run/mysqld
NOTE: here you say you get the error /run/mysqld/mysqld.sock
as next, I'm not sure if you paste the all log as most offen it could be /var/run/mysqld/mysqld.sock
, nertheless, if the error is /var/run/mysqld/mysqld.sock
then you should modify above example volume to - /tmp/slack_bot/mysqld:/var/run/mysqld
2002, "Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2)"
With above, now your web service could share the unix socket in mysql container.
Additional, configure HOST
as db
I think also could be another solution, just unix socket
is much faster compared to use tcp
.
Upvotes: 10