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.
2
Answers
I have tested your compose file and it works , i don’t know about your api and how it queries the database. the problem may be with that.
make sure that the name of the service
mysql
can be resolved by the other container , you can test it with :i don’t know your api image , you might need to replace
sh
with some other shell.then inside the container :
if the hostname get resolved its ok.
else you might need to see your docker networking or make a network manually and use that.
hope that helps!
I think the issue is with DNS resolution when trying to connect to the mysql container
this error message means that hostname "mysql" cannot be resolved to an ip address.
you shouldn’t use links in docker compose when you’re using networks.
make sure to recreate the containers using "docker-compose up –build"
use the service name you defined in docker-compose.yml as your hostname in Django configuration
and since you mentioned that you are new to docker I just want to mention that you should specify a version tag for mysql image instead of using the latest tag as best practice
I hope this helps you.