Docker is the most popular containerization tool in the DevOps world. But, What is Docker Compose?
Docker Compose is used to run applications which have multiple containers using a YAML file.
There can be several cases where the docker application must run multiple containers for different technology stack. Now building, running, connecting separate dockerfiles for each container can be a difficult task; this is where docker-compose helps you out.
Using a single and straightforward docker-compose.yml file, you can build, connect, and launch all the containers by running a single command. This is very useful for enterprise applications in production, where several applications run inside containers. It saves a lot of time by running 100s of application inside docker containers with ease.
Installing Docker Compose
Docker should already be installed on your system before installing compose.
Run the command below to install docker-compose.
geekflare@geekflare:/home/geekflare$ sudo curl -L "https://github.com/docker/compose/releases/download/1.23.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
[sudo] password for geekflare:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 617 0 617 0 0 1209 0 --:--:-- --:--:-- --:--:-- 1209
100 11.1M 100 11.1M 0 0 348k 0 0:00:32 0:00:32 --:--:-- 476k
Check if it got installed correctly, it should return the docker-compose version.
geekflare@geekflare:/home/geekflare$ docker-compose --version
docker-compose version 1.23.1, build b02f1306
Below is the list of commands you can use with docker-compose.
Define and run multi-container applications with Docker.
docker-compose [-f <arg>…] [options] [COMMAND] [ARGS…]
-f, --file FILE Specify an alternate compose file
-p, --project-name NAME Specify an alternate project name
(default: directory name)
--verbose Show more output
--log-level LEVEL Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
--no-ansi Do not print ANSI control characters
-v, --version Print version and exit
-H, --host HOST Daemon socket to connect to
--tls Use TLS; implied by –tlsverify
--tlscacert CA_PATH Trust certs signed only by this CA
--tlscert CLIENT_CERT_PATH Path to TLS certificate file
--tlskey TLS_KEY_PATH Path to TLS key file
--tlsverify Use TLS and verify the remote
--skip-hostname-check Don’t check the daemon’s hostname against the
name specified in the client certificate
--project-directory PATH Specify an alternate working directory
(default: the path of the Compose file)
--compatibility If set, Compose will attempt to convert deploy
keys in v3 files to their non-Swarm equivalent
build Build or rebuild services
bundle Generate a Docker bundle from the Compose file
config Validate and view the Compose file
create Create services
down Stop and remove containers, networks, images, and volumes
events Receive real time events from containers
exec Execute a command in a running container
help Get help on a command
images List images
kill Kill containers
logs View output from containers
pause Pause services
port Print the public port for a port binding
ps List containers
pull Pull service images
push Push service images
restart Restart services
rm Remove stopped containers
run Run a one-off command
scale Set number of containers for a service
start Start services
stop Stop services
top Display the running processes
unpause Unpause services
up Create and start containers
version Show the Docker-Compose version information
Docker Compose File
Here is a sample docker-compose file which does all the magic.
This docker file is running two services/applications, web and Redis. The web service is built through a dockerfile and runs at default flask webserver port – 5000. The Redis service runs by pulling the Redis image from docker hub registry.
To execute the docker-compose.yml file, you need to run a very simple command: docker-compose up
Docker Compose Workflow
Below are the three steps to use docker-compose.
Create dockerfile of each service
Create a docker-compose.yml file to connect all the dockerfiles
Run docker-compose up command to start the system
Let me take an example of the sample docker-compose file we saw earlier and show you how the project structure will be created.
my-app is my main project directory. This directory has web and Redis service directories and the docker-compose YAML file. Dockerfile of Web service is present in a web directory. Since Redis service is being pulled from docker hub directly, no dockerfile is required in Redis directory. This is how docker-compose workflow looks like.
Containerizing A MEAN Stack Application using Docker Compose
Now that you have understood fundamental concepts. Let me show a demo on how to containerize a MEAN stack application using docker-compose.
MEAN stands for MongoDB, Express, Angular and Node.js. Applications using these services together is also called a MEAN/Full-stack application.
For this demo, we will run three docker containers:
Also, you can run docker images command to see which all images are present in docker.
geekflare@geekflare:~/mean$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mean_express latest 35dcb3df9806 14 hours ago 923MB
mean_angular latest 9f8d61db600c 14 hours ago 1.29GB
node 8 8e45c884a32e 9 days ago 895MB
mongo latest 785c65f61380 2 weeks ago 412MB
Run the command below to see the containers running inside docker.
geekflare@geekflare:~/mean$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
681c9c34bee2 mean_express "docker-entrypoint.s…" 14 hours ago Up 14 hours 0.0.0.0:3000->3000/tcp mean_express_1_574f07b045fc
f74b56905249 mongo "docker-entrypoint.s…" 14 hours ago Up 14 hours 0.0.0.0:27017->27017/tcp mean_database_1_ccc5c677e00b
260ef1e52dab mean_angular "docker-entrypoint.s…" 14 hours ago Up 14 hours 0.0.0.0:4200->4200/tcp mean_angular_1_f257e2233ef1
So now, all three docker containers are up and running.
Docker-compose took care of running all the containers with ease. This was a simple example for running three containers; you can imagine how useful it will be if the applications have to be launched on 100s of containers. Go ahead and try out to see how it works.