简体   繁体   中英

Unknown mysql server host on docker and python

I'm building an API that fetches data from a MySQL database using Docker. I've tried everything and I always get this error: 2005 (HY000): Unknown MySQL server host 'db' (-3) . Here is my docker compose file:

version: '3'
services:
  web:
    container_name: nginx
    image: nginx
    volumes:
      - ./nginx/nginx.conf:/tmp/nginx.conf
    environment: 
      - FLASK_SERVER_ADDR=backend:9091
      - DB_PASSWORD=password
      - DB_USER=user
      - DB_HOST=db 
    command: /bin/bash -c "envsubst < /tmp/nginx.conf > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'" 
    ports:
      - 80:80
    networks:
      - local
    depends_on:
      - backend
    
  backend:
    container_name: app
    build: flask
    environment:
      - FLASK_SERVER_PORT=9091
      - DB_PASSWORD=password
    volumes:
      - flask:/tmp/app_data
    restart: unless-stopped
    networks:
      - local
    depends_on:
      - db
    links:
      - db

  db:
    container_name: db
    image: mysql
    restart: unless-stopped
    volumes:
      - ./mysql:/docker-entrypoint-initdb.d
    environment:
       - MYSQL_ROOT_PASSWORD=password
       - MYSQL_DATABASE=database
       - MYSQL_USER=user
       - MYSQL_PASSWORD=password
    ports:
      - 3306:3306

networks:
  local:
      
volumes:
  flask:
    driver: local
  db:
    driver: local

Inside the flask directory I have my Dockerfile like so:

FROM ubuntu:latest
WORKDIR /src
RUN apt -y update
RUN apt -y upgrade
RUN apt install -y python3
RUN apt install -y python3-pip
COPY . .
RUN chmod +x -R .
RUN pip install -r requirements.txt --no-cache-dir
CMD ["python3","app.py"]

Finally, on my app.py file I try to connect to the database with the name of the Docker container. I have tried using localhost and it still gives me the same error. This is the part of the code I use to access it:

conn = mysql.connector.connect(
              host="db",
              port=3306,
              user="user",
              password="password",
              database="database")

What is it that I'm doing wrong?

The containers aren't on the same networks: , which could be why you're having trouble.

I'd recommend deleting all of the networks: blocks in the file, both the blocks at the top level and the blocks in the web and backend containers. Compose will create a network named default for you and attach all of the containers to that network. Networking in Compose in the Docker documentation has more details on this setup.

The links: block is related to an obsolete Docker networking mode, and I've seen it implicated in problems in other SO questions. You should remove it as well.

You also do not need to manually specify container_name: in most cases. For the Nginx container, the Docker Hub nginx image already knows how to do the envsubst processing so you do not need to override its command: .

This should leave you with:

version: '3.8'
services:
  web:
    image: nginx
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/templates/default.conf.template
    environment: { ... }
    ports:
      - 80:80
    depends_on:
      - backend
    
  backend:
    build: flask
    environment: { ... }
    volumes:
      - flask:/tmp/app_data
    restart: unless-stopped
    depends_on:
      - db

  db:
    image: mysql
    restart: unless-stopped
    volumes:
      - ./mysql:/docker-entrypoint-initdb.d
      - db:/var/lib/mysql
    environment: { ... }
    ports:
      - 3306:3306

volumes:
  flask:
  db:

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