This example shows how to use docker-compose
to help deploy multiple containers at once.
We need to create a docker-compose.yml
which is in the YAML format.
This file describes the docker engine how to create the Node.js and MongoDB containers.
The Node.js container will be build from its Dockerfile
inside the application directory.
While the MongoDB container will be created from the MongodB official image.
Note: we will work on this example in the directory of project docker-node-sample3
.
Previously, we have build a custom Node.js app image in example#3.
Please review the docker-node-sample3/Dockerfile
in the following link:
https://gist.github.com/potikanond/3f5f258da5290bf959f1b3d72ef3453f
We willl use that Dockerfile
again in this example.
Compose file version 3 reference: https://docs.docker.com/compose/compose-file/
We will create node and mongo services. The mongo container is created from an official mongo image.
The node container, however, is build from the Dockerfile
as specified by the instruction build: .
.
The container also links to the mongo service as specified with the instruction links:
.
docker-compose.yml:
version: '3'
services:
node:
container_name: my_node2
restart: always
build: .
ports:
- '8080:3000'
links:
- mongo
mongo:
container_name: my_mongo2
image: mongo:4.0.1-xenial
ports:
- '27017:27017'
Below, We can ask docker to create a virtual network net1
and attach both containers to the network.
The network is defined at the bottom of the YAML file. It is also configured with specified subnet IP addresses.
docker-compose.yml (with net1):
version: '3'
services:
node:
container_name: my_node2
restart: always
build: .
ports:
- '8080:3000'
links:
- mongo
networks:
- net1
mongo:
container_name: my_mongo2
image: mongo:4.0.1-xenial
ports:
- '27017:27017'
networks:
- net1
networks:
net1:
ipam:
config:
- subnet: 172.22.1.0/24
Note: make sure that we have already installed docker-compose
tool.
To start both services:
$ docker-compose up Creating network "docker-node-sample3_net1" with the default driver Creating my_mongo2 ... done Creating my_node2 ... done Attaching to my_mongo2, my_node2 my_mongo2 | 2018-08-26T16:32:02.730+0000 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none' my_mongo2 | 2018-08-26T16:32:02.767+0000 I CONTROL [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=4242c7edd4e6 ... my_node2 | my_node2 | > [email protected] start /usr/src/app my_node2 | > node index.js my_node2 | Server running... my_node2 | MongoDB Connected
Now we should be able to access the Node.js app with the URL: http://localhost:8080
If we list all containers, we will see:
# docker container ls -a CONTAINER ID IMAGE CREATED STATUS PORTS NAMES 5d0c81116e8e docker-node-mongo_node 7 minutes ago Up 7 minutes 0.0.0.0:8080->3000/tcp my_node2 4242c7edd4e6 mongo:4.0.1-xenial 7 minutes ago Up 7 minutes 0.0.0.0:27017->27017/tcp my_mongo2
And if we list all networks, we will see:
# docker network ls NETWORK ID NAME DRIVER SCOPE b8cc9ce346a0 bridge bridge local 5064cba0e45d docker-node-sample3_net1 bridge local 5452c8e97607 host host local d6a1ef964756 none null local
However, if we want to start both services in background, then run the command with '-d' option:
$ docker-compose up -d
$ docker-compose down Removing my_node2 ... done Removing my_mongo2 ... done Removing network docker-node-mongo_net1