简体   繁体   中英

docker compose up generate (2005, "Unknown MySQL server host 'db' (-2)") error

Hi all i'm trying to dockerize a django application with a connexion to the database. when i run docker compose up i get this error when the dockerfile is making migrations 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

here is my 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"]

and here is my 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  

You have to specify network for your containers. https://docs.docker.com/compose/networking/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM