Management of multiple containers
Usage of docker compose
Introduction of docker compose#
Docker compose
helps us manage multiple containers with a file.
Define a network
Define containers
For each container, it defines their images, environments, ports and so on.
Docker network configuration#
Setting the name of docker network
I set the name to dev-net
.
IP range of devnet
was set to 192.168.100.0/24
.
Namely, all of containers in dev-net
network have 192.168.100.x
IP address.
The above expression is called by CIDR which will be dealt in network section.
Creating a docker network#
Generating a docker network with using network configuration described in Network information section.
It can be automatically generated by docker compose
, but I will describe how you can create the docker network in manual.
I recommend that you manually create the network rather than automatic creation for Mac OS
, I will describe why I recommend it in docker virtual private network(VPN) section.
docker network create --attachable --subnet 192.168.100.0/24 devnet
Docker container configuration#
In this phase, I describe container information I need to configure.
Image collection#
Choose docker images which you want to use.
Purpose
Image
Database
mongo:4
Application server
golang:1.15
Setting container names#
Setting names for each container.
Purpose
Container name
Database
mongodb
Development server
greeting-server
Setting environment variables#
Some containers require environment variables.
mongodb
container needs some environment variables.
Container name
Environment variables
Value
mongodb
MONGO_INITDB_ROOT_USERNAME
root
mongodb
MONGO_INITDB_ROOT_PASSWORD
root
Setting container ports#
We can bind containers' ports to host machine’s ports.
There are two ways to have access to guest containers from host machine.
Direct access to container’s IP.
Basically, it can’t be used on Mac OS
.
Binding container ports to host machine.
Access to bound ports to localhost
.
Container name
Host port
Container port
mongodb
27017
27017
greeting-server
3000
3000
Setting volume mounts#
Mounting Some path from host to container.
Assume that PROJECT_ROOT
is /home/hackartist/data/greeting-server
.
Container name
Host path
Container path
mongodb
$PROJECT_ROOT/.build/data
/data/db
greeting-server
$PROJECT_ROOT
/workdir
Docker compose configuration#
Writing docker-compose.yaml file#
Composing dockers with Docker container configuration .
In networks
section, I described the network name which has been manually created.
The containers in the network have a predefined subnet.
Describing containers' information in services
section.
services
is composed of mongodb
and greeting-server
.
The services' names of each container are used for containers' aliases in the composed network.
These aliases make each container able to access to other containers with the aliases as domain names.
restart
section describes restart policy which makes containers are automatically restarted.
Other configurations related to the containers should be described in ports
, environment
and volumes
sections.
working_dir
denotes the location which the container is started in.
command
describes starting command when the container is started.
greeting-server
runs go run main.go
command when it starts.
version: "3.8"
networks:
default:
external:
name: devnet
services:
mongodb:
image: mongo:4
container_name: mongodb
restart: always
ports:
- 27017:27017
environment:
- MONGO_INITDB_ROOT_USERNAME: root
- MONGO_INITDB_ROOT_PASSWORD: root
volumes:
- $PROJECT_ROOT/.build/data:/data/db
greeting-server:
image: golang:1.15
container_name: greeting-server
restart: always
ports:
- 3000:3000
volumes:
- $PROJECT_ROOT:/workdir
working_dir: /workdir
command: go run main.go
Running docker compose#
Using a configuration file written in Writing docker-compose.yaml file section.
The configuration file passes an environmental variable, PROJECT_ROOT
.
-f
flag describes a configuration filename of docker compose.
docker-compose.yaml
is a default configuration name, so it can be omitted.
-p
flag describes a name of a docker stack which the container belongs to. It could make us easy to manage the containers.
up
command composes dockers.
-d
flag indicates that it should be run as background without attaching I/O.
If the only mongodb
should be ran, ... up -d mongodb
could be executed.
PROJECT_ROOT = /home/hackartist/data/greeting-server \
docker-compose -f docker-compose.yaml -p greeting-server up -d
Shutting down the docker compose#
down
command removes all containers and network created by the docker compose.
If some containers would be orphans due to renaming or something else, --remove-orphans
flag makes them removed.
PROJECT_ROOT = /home/hackartist/data/greeting-server \
docker-compose -f docker-compose.yaml -p greeting-server down --remove-orphans