As a DevOps engineer, you need to deploy a web application with a Node.js backend and a MySQL database using Docker. The Node.js app connects to MySQL on localhost:3306, but running docker run for each container separately fails because they can’t communicate. How would you set up these containers to work together?
Answer
To make the Node.js and MySQL containers communicate:
- Use Docker Compose: Create a
docker-compose.ymlfile to define and link both services:version: '3.8' services: node-app: image: my-node-app build: . ports: - "3000:3000" depends_on: - mysql-db environment: - DB_HOST=mysql-db - DB_PORT=3306 mysql-db: image: mysql:8.0 environment: - MYSQL_ROOT_PASSWORD=secret ports: - "3306:3306" - Run the Application: Execute
docker-compose up -dto start both containers. Thenode-appservice connects tomysql-dbusing the service name (mysql-db) as the hostname, notlocalhost. - Verify Connectivity: Check logs with
docker-compose logs node-appto ensure the Node.js app connects to MySQL. If it fails, verify the environment variables and MySQL’s readiness. - Alternative Without Compose: Use a custom network:
- Create a network:
docker network create my-app-network - Run MySQL:
docker run -d --name mysql-db --network my-app-network -e MYSQL_ROOT_PASSWORD=secret mysql:8.0 - Run Node.js:
docker run -d --name node-app --network my-app-network -p 3000:3000 -e DB_HOST=mysql-db my-node-app
- Create a network: