我想对Web服务进行docker化,我配置了以下文件:
dockerfile
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/
docker-compose.yml
version: '3'
services:
db:
image: mysql
container_name: DB_test
environment:
MYSQL_DATABASE: 'test'
MYSQL_USER: 'user'
MYSQL_PASSWORD: 'password'
MYSQL_ROOT_PASSWORD: 'password'
ports:
- "3306:3306"
volumes:
- ./sql:/docker-entrypoint-initdb.d
web:
build: .
command: gunicorn --bind 0.0.0.0:8000 manage:app
ports:
- "8000:8000"
depends_on:
- db
sql / init.sql
GRANT ALL PRIVILEGES ON test TO 'user'@'%';
FLUSH PRIVILEGES;
sqlalchemy_conf
{
"HR_API_ID" : "api_id",
"HR_API_CODE" : "api_code",
"DB_HOST" : "0.0.0.0",
"DB_PORT" : "3306",
"DB_USERNAME" : "user",
"DB_PASSWORD" : "password",
"DB_NAME" : "test",
"DB_CONNECTION" : "mysql"
}
之后,我运行:
docker-compose build
docker-compose up
当我尝试访问http://0.0.0.0:8000/时,控制台中显示的日志错误是以下错误:
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on '0.0.0.0' ([Errno 111] Connection refused)") (Background on this error at: http://sqlalche.me/e/e3q8)
Internal Server Error
我在做什么错了?
更新:
DB_HOST" : "db"
新错误:
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (1045, "Access denied for user 'user'@'172.19.0.3' (using password: YES)") (Background on this error at: http://sqlalche.me/e/e3q8)
不确定,但是在开始时运行 init.sql 应该可以避免该错误
答案 0 :(得分:1)
根据https://docs.docker.com/compose/networking/
您的DB_HOST应该为db
,这是您的数据库服务名称,而不是0.0.0.0