I have a Docker composition of an Express application and MySQL database.
When I run docker-compose up –build, everything works perfectly.
However, if I try to run the created image separately, the MySQL server is not recognized.
Here is my docker-compose.yml file:
version: '3.8'
services:
mysql:
image: mysql
container_name: mysql-for-compose
environment:
MYSQL_ROOT_PASSWORD: 'password'
MYSQL_DATABASE: 'testdb'
volumes:
- mysql-data:/var/lib/mysql
ports:
- "3306:3306"
networks:
- express-mysql-network
express:
build: .
container_name: express-for-compose
ports:
- "4000:4000"
networks:
- express-mysql-network
depends_on:
- mysql
networks:
express-mysql-network:
volumes:
mysql-data:
When I run the following command:
docker-compose up –build
Everything works perfect.
But when I run afterward the created image:
docker run -p 4000:4000 –network express-mysql-network
If the app tries to talk to MySQL, it fails with the following error:
Error: getaddrinfo EAI_AGAIN mysql
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:109:26) {
errno: -3001,
code: 'EAI_AGAIN',
syscall: 'getaddrinfo',
hostname: 'mysql',
fatal: true
}
Can you help explain why?