【发布时间】:2020-06-01 06:09:14
【问题描述】:
大家好,我正在尝试将 django 应用程序与数据库连接。当我运行 docker compose up 时,当 dockerfile 进行迁移时出现此错误django.db.utils.OperationalError: (2005, "Unknown MySQL server host 'db' (-2)")
ERROR: Service 'web' failed to build: The command '/bin/sh -c python manage.py makemigrations' returned a non-zero code: 1
这是我的 Dockerfile
FROM python:3.6
ENV PYTHONUNBUFFERED 1
RUN mkdir /SFP_ingestion
WORKDIR /SFP_ingestion
COPY . /SFP_ingestion
RUN pip install -r requirements.txt
RUN python generatemodel.py
RUN python generateapp.py
RUN python manage.py makemigrations
RUN python manage.py migrate
RUN python manage.py migrate easyaudit
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
这是我的 docker-compose.yml
services:
db:
image: mysql
restart: always
command: --default-authentication-plugin=mysql_native_password --mysqlx=0
environment:
- MYSQL_HOST=localhost
- MYSQL_PORT=3306 # cannot change this port to other number
- MYSQL_DATABASE=sfp # name you want for the database
- MYSQL_USER=root # change to whatever username you want
- MYSQL_PASSWORD=password #change to the password you want for user
- MYSQL_ROOT_PASSWORD=password #change to good root password
ports:
- "3306:3306"
expose:
- "3306"
volumes:
- "./db:/var/lib/mysql"
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/SFP_ingestion
restart: always
ports:
- "8000:8000"
depends_on:
- db
【问题讨论】:
标签: python mysql django docker docker-compose