[英]Can't connect to MySQL server on 'db' ([Errno -2] Name does not resolve)
I am trying to connect mysql with docker using python.我正在尝试使用 python 将 mysql 与 docker 连接起来。 but this error is occurring.
但这个错误正在发生。 I am using python3.
我正在使用python3。 I am new in docker so I am unable to find any solution.
我是 docker 的新手,所以我找不到任何解决方案。
Dockerfile: Dockerfile:
FROM python:3.8-alpine
MAINTAINER Fahim Ahmed Irfan
ENV PYTHONUNBUFFERED 1
COPY ./requirements.txt /requirements.txt
RUN apk add --update --no-cache jpeg-dev
RUN apk --update add --no-cache --virtual .tmp-build-deps \
gcc build-base libffi-dev freetype-dev libpng-dev openblas-dev linux-headers mysql-dev zlib zlib-dev
RUN pip install -r /requirements.txt
RUN mkdir /app
WORKDIR ./app
COPY ./app /app
RUN adduser -D user
User user
EXPOSE 8000
Docker-compose file: Docker-compose 文件:
version: '3'
services:
app:
restart: always
build:
context: .
ports:
- "8000:8000"
volumes:
- ./app:/app
command: >
sh -c "python check_db.py --service-name mysql --ip db --port 3306 &&
python manage.py makemigrations &&
python manage.py migrate &&
python manage.py runserver 0.0.0.0:8000"
environment:
- DB_HOST=db
- DB_NAME=app
- DB_USER=root
- DB_PASS=supersecretpassword
depends_on:
- db
db:
image: mysql:8.0
environment:
- MYSQL_HOST=localhost
- MYSQL_PORT=3306
- MYSQL_ROOT_HOST=%
- MYSQL_DATABASE=app
- MYSQL_USER=root
- MYSQL_PASSWORD=supersecretpassword
ports:
- "3306:3306"
check_db.py: check_db.py:
import socket
import time
import argparse
""" Check if port is open, avoid docker-compose race condition """
parser = argparse.ArgumentParser(description='Check if port is open, avoid\
docker-compose race condition')
parser.add_argument('--service-name', required=True)
parser.add_argument('--ip', required=True)
parser.add_argument('--port', required=True)
args = parser.parse_args()
# Get arguments
service_name = str(args.service_name)
port = int(args.port)
ip = str(args.ip)
# Infinite loop
while True:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((ip, port))
if result == 0:
print("{0} port is open! Bye!".format(service_name))
break
else:
print("{0} port is not open! I'll check it soon!".format(service_name))
time.sleep(3)
settings.py:设置.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': os.environ.get('DB_HOST'),
'NAME': os.environ.get('DB_NAME'),
'USER': os.environ.get('DB_USER'),
'PASSWORD': os.environ.get('DB_PASS'),
'PORT': '3306',
}
requirements.txt:要求.txt:
Django==2.1.3,<2.2.0
djangorestframework==3.9.0,<3.10.0
pymysql
The error is: django.db.utils.OperationalError: (2003, "Can't connect to MySQL server on 'db' ([Errno -2] Name does not resolve)")错误是: django.db.utils.OperationalError: (2003, "Can't connect to MySQL server on 'db' ([Errno -2] Name does not resolve)")
Dont know how to solve this.不知道如何解决这个问题。 Please advice.
请指教。
Finally I got this working, I have made a few important changes that are required for your code.最后我得到了这个工作,我对您的代码进行了一些重要的更改。
WORKDIR app
WORKDIR
does the creation of the directory app
and also moving into the directory app
so you can remove the statement RUN mkdir /app
As you also move to that directory so change the COPY
statement to WORKDIR
创建目录app
并移动到目录app
中,因此您可以删除语句RUN mkdir /app
当您也移动到该目录时,将COPY
语句更改为
COPY . .
first .
首先
.
is the place where the dockerfile exists and the second .
是dockerfile存在的地方,第二个
.
is the app folder in the container.是容器中的 app 文件夹。 So it is always good to have dockerfile in the app folder.
所以在 app 文件夹中有 dockerfile 总是好的。
volumes:
- ./app:/app
the above code from the docker-compose is not required as you are copying the contents directly so mounting again is of no use. docker-compose 中的上述代码不是必需的,因为您直接复制内容,因此再次安装没有用。
- MYSQL_ROOT_PASSWORD=supersecretpassword
Also, add the above environment variable to the db service in the docker-compose另外,将上述环境变量添加到docker-compose中的db服务中
- MYSQL_HOST=localhost
Please remove this environment variable as it is not required here.请删除此环境变量,因为此处不需要它。
image: mysql:5.7
I would recommend using version 5.7 as it is one of the stable ones I extensively use without many issues from the database server.我建议使用 5.7 版本,因为它是我广泛使用的稳定版本之一,数据库服务器没有很多问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.