21 Days of Docker-Day 16 -Introduction to Docker Compose

Welcome to Day 16 of 21 Days of Docker. The topic for today is a docker compose

Docker compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.

Install Compose

  • Run this command to download the current stable release of Docker Compose:
$ 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
  • Set the execute permission
sudo chmod +x /usr/local/bin/docker-compose
  • To verify it
$ docker-compose --version
docker-compose version 1.24.1, build 4667896b
  • Define the services that make up your app in docker-compose.yml
version: '3'
    image: nginx
      - "80:80"
    image: "redis:alpine"
  • This Compose file defines two services: web and redis.

Web service

  • The web service uses an nginx image. It then binds the container and the host machine to the exposed port, 8080. This example service uses the default port for the nginx web server, 80.

Redis service

  • The redis service uses a public Redis image pulled from the Docker Hub registry.
  • To validate your configuration
$ docker-compose config
    image: redis:alpine
    image: nginx
    - 8080:80/tcp
version: '3.0'
  • Start up your application by running docker-compose up
$ docker-compose up -d

Creating network "cloud_user_default" with the default driver
Creating cloud_user_redis_1 ... done
Creating cloud_user_web_1   ... done
  • To bring down the service
$ docker-compose down
Removing cloud_user_web_1   ... done
Removing cloud_user_redis_1 ... done
Removing network cloud_user_default

Famous WordPress example, I will leave up to you guys to test it


version: '3.3'

     image: mysql:5.7
       - db_data:/var/lib/mysql
     restart: always
       MYSQL_ROOT_PASSWORD: somewordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress

       - db
     image: wordpress:latest
       - "8000:80"
     restart: always
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress
       WORDPRESS_DB_NAME: wordpress
    db_data: {}

Deploy a stack to a swarm

When running Docker Engine in swarm mode, you can use docker stack deploy to deploy a complete application stack to the swarm. The deploy command accepts a stack description in the form of a Compose file.

$ docker stack deploy -c docker-compose.yml mystack
Creating network mystack_default
Creating service mystack_web
Creating service mystack_redis
  • List stacks
$ docker stack ls
NAME                SERVICES            ORCHESTRATOR
mystack             2                   Swarm
  • List the tasks in the stack
$ docker stack ps  mystack
ID                  NAME                IMAGE               NODE                          DESIRED STATE       CURRENT STATE            ERROR               PORTS
gd34he1ej70p        mystack_redis.1     redis:alpine        plakhera14c.example.com   Running             Running 14 seconds ago                       
r3d8fmrigxwx        mystack_web.1       nginx:latest        plakhera12c.example.com   Running             Running 18 seconds ago                       
  • As you can see redis is deployed on plakhera14c.example.com and web on plakhera12c.example.com
  • List the services in the stack
$ docker stack services mystack
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
3mcj15hyaq6a        mystack_web         replicated          1/1                 nginx:latest        *:8080->80/tcp
j1h8c1ygn796        mystack_redis       replicated          1/1                 redis:alpine        
  • Remove one or more stacks
$ docker stack rm  mystack
Removing service mystack_redis
Removing service mystack_web
Removing network mystack_default

Please follow me with my Journey

This time to make learning more interactive, I am adding

  • Slack
  • Meetup

Please feel free to join this group.



Meetup Group

If you are in the bay area, please join this meetup group https://www.meetup.com/100daysofdevops/

One Reply to “21 Days of Docker-Day 16 -Introduction to Docker Compose”

  1. When I initially commented I clicked the -Notify me when new comments are added- checkbox and now each time a remark is added I get 4 emails with the identical comment. Is there any manner you possibly can remove me from that service? Thanks!

Comments are closed.