Docker Compose – Part 9

Docker compose is similar to vagrant. It is automation for containers. Let’s say if we have to create multiple containers, as of now we have to issue multiple commands. Or let’s say we want to provide -v, -e and many others, if would be difficult to handle this long command. At that point docker compose comes in handy where we create a .yml file and executes it. And that’s all.

The real power of docker compose comes with docker swarm.

We can create multiple containers using docker compose and those containers are linked together.

Let’s install docker compose now

sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

sudo chmod +x /usr/local/bin/docker-compose

ubuntu@ip-172-31-5-45:~$ docker-compose --help

Let’s create a docker compose file

docker-compose.yml

version: '3'
services:
  xxxnginx:
    image: xxx/xx
    ports:
      - "80:80"
  xxxapp:
    image: xx/xx
    ports:
      - "8080:8080"
  xxxdb:
    image: xx/xx
    ports:
      - "3306:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=xxx

To run this yaml file give the command. It will pull the images and will execute the instructions.

docker-compose up => to run it in foreground
docker-compose up -d => to run it in background

docker-compose will create its own network

NETWORK ID          NAME                DRIVER              SCOPE
a884949f360a        bridge              bridge              local
c0f546cebf26        compose_default     bridge              local
d63bc051aa8d        host                host                local
9a2b1d66736f        my-net              bridge              local

Application would be up and running, all the request would be going to IP:80(nginx) and it would be routing the request to the tomcat and all the containers(nginx,tomcat,db) would be linked to each other.

To see the running containers

ubuntu@ip-172-31-5-45:~/compose$ docker-compose ps
       Name                     Command             State                 Ports
---------------------------------------------------------------------------------------------
compose_xxxapp_1     catalina.sh run               Up      0.0.0.0:8080->8080/tcp
compose_xxxdb_1      docker-entrypoint.sh mysqld   Up      0.0.0.0:3306->3306/tcp, 33060/tcp
compose_xxxnginx_1   nginx -g daemon off;          Up      0.0.0.0:80->80/tcp

Similarly, you can execute many commands using docker-compose(mentioned above using –help)

About the author

Deepak Sood

Deepak Sood is Lead Consultant in an IT firm holding expertise in Devops and QA Architecture with 8 years of experience.

His expertise is in building highly scalable frameworks. His skills include Java, Configuration Management, Containers, and Kubernetes.

Reach out to him using contact form.

View all posts