Search code examples
mysqldjangodockerdocker-composedns

Django container unable to connect to other container on the same network


I'm very new to docker, now I am trying to run django with mysql in docker through docker-compose, but I always get this error:

ping: mysql: Name or service not known

I am using Docker version 24.0.4, build 3713ee1 and Docker Compose version v2.17.3

I have read the documentation it says

When you run docker compose up, the following happens:

A network called myapp_default is created. A container is created using web’s configuration. It joins the network myapp_default under the name web. A container is created using db’s configuration. It joins the network myapp_default under the name db.

Here is my docker-compose file that I am using;

version: "3.8"

services:
  mysql:
    image: mysql
    restart: always
    env_file:
      - .env
    volumes:
      - db_data:/var/lib/mysql
    ports:
      - "3306:3306"

  backend:
    container_name: analytics_api
    build:
      context: .
    command: gunicorn analytics_api.wsgi --bind 0.0.0.0:8001 --timeout 120 --workers 4
    ports:
      - "8001:8001"
    depends_on:
      - mysql
    links:
      - "mysql:database"

volumes:
  db_data:
    driver: local

Also I have checked the network and it has both the containers in there

[
    {
        "Name": "radioapis_default",
        "Id": "7a3a7a38886f7e19bdd6e3642bc9cf0b48fb6376c171a3ccae2cacde00b3f329",
        "Created": "2023-07-24T12:58:10.131998722Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.22.0.0/16",
                    "Gateway": "172.22.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "3ff18b9f499a2fc8b0bbd6bc9d1d7e5316e54cb9a849445360a468d01467bcbe": {
                "Name": "radioapis-mysql-1",
                "EndpointID": "049578e968da8d1807ea4ac04ec06e0480833f08f365da184d30bfe36a8ccb4c",
                "MacAddress": "02:42:ac:16:00:02",
                "IPv4Address": "172.22.0.2/16",
                "IPv6Address": ""
            },
            "4b804645be9d383ca097b8186636aea03489eba3b29cf9fd50444ffe0137b78e": {
                "Name": "analytics_api",
                "EndpointID": "770c0d63d786bc851e961f1fa0143b582da0ae0be6c14988c089a4525a012408",
                "MacAddress": "02:42:ac:16:00:03",
                "IPv4Address": "172.22.0.3/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "default",
            "com.docker.compose.project": "radioapis",
            "com.docker.compose.version": "2.17.3"
        }
    }

I have tried using a network in docker-compose.yml file;

version: "3.8"

services:
  mysql:
    image: mysql
    restart: always
    env_file:
      - .env
    volumes:
      - db_data:/var/lib/mysql
    ports:
      - "3306:3306"
    networks:
      - shared_network

  backend:
    container_name: analytics_api
    build:
      context: .
    command: gunicorn analytics_api.wsgi --bind 0.0.0.0:8001 --timeout 120 --workers 4
    ports:
      - "8001:8001"
    depends_on:
      - mysql
    links:
      - "mysql:database"
    networks:
      - shared_network

volumes:
  db_data:
    driver: local

networks:
  shared_network:

But still to no use.


Solution

  • This has been resolved as I did not know the actual difference between docker run and docker exec.

    As docker run is the command you use to create a new container from an image in an isolated network separate from the already running container, whilst docker exec lets you run commands on an already running container.