Skip to main content

Development

Docker Bootcamp – Using Docker Compose

Compose

Welcome back to Docker Bootcamp. In this post, we’ll look at Docker Compose to configure and run multiple dependent containers.

Looking back at my first post about linking containers, we used the Docker run command along with an environment variables text file to start two containers. It was important to start the database container before the CMS container. We’ll accomplish the same thing here using Docker Compose.

Don’t worry about the structure of the docker-compose file in the example. Most of it will look familiar if you’ve been following along, but we won’t focus on writing the compose file.

Commands

  • compose – Docker subcommand to manage multi-container applications
  • compose down – Stop and remove containers and networks
  • compose start – Start containers
  • compose stop – Stop containers
  • compose up – Create and start containers

Command Details

You can find a full list of commands and all available flags at https://docs.docker.com/engine/reference/commandline/docker/.

compose

docker compose [options] [command]

–file -f O Configuration file (defaults to ./docker-compose.yml)
–project-name -p O Project name (defaults to the name of the directory)

 

  • Prior to Docker Desktop v3.3.2 (2021-05-03) docker compose was a separate command docker-compose
  • The new compose subcommand is a drop-in replacement for the docker-compose command
  • The compose subcommand improves the docker-compose command by outputting helpful status information such as container state and execution time

compose down

docker compose [options] down

compose start

docker compose [options] start

compose stop

docker compose [options] stop

compose up

docker compose [options] up [up-options]

–detach -d O Run containers in the background

Examples

Setup

  • Create a file named “docker-compose.yml”
  • Copy and paste the following text
version: '2.4'

services:
  mysqldb:
    image: mysql   
    environment:
      MYSQL_ROOT_PASSWORD: SecretRootPassword0987
      MYSQL_DATABASE: WordPress
      MYSQL_USER: wpress
      MYSQL_PASSWORD: Wordpr3ss!

  wordpresscms:
    image: wordpress
    ports:
      - "8080:80"
    environment:
      WORDPRESS_DB_HOST: mysqldb
      WORDPRESS_DB_USER: wpress
      WORDPRESS_DB_PASSWORD: Wordpr3ss!
      WORDPRESS_DB_NAME: WordPress
    depends_on:
      - mysqldb

networks:
  default:
    name: wp

Use Docker Compose

  • Create, start and run multiple containers
    • docker compose -f .docker-compose.yml -p wp up -d
      • Notice it creates a new user-defined network named wp
      • Notice the containers names are prefixed with the project name and appended with a number
        • Make sure to use this name when interacting with the containers
        • The prefix helps group the containers
        • The suffix helps when scaling applications to multiple containers
  • View container processes
    • docker ps -a
      • Notice both containers are running
  • View wordpress site
    • Browse to localhost:8080
      • Notice the wordpress installer loads
      • Complete the installer
  • View wordpress site
    • Browse to localhost:8080
      • Notice the page loads
  • Stop running containers
    • docker compose -f .docker-compose.yml -p wp stop
  • View container processes
    • docker ps -a
      • Notice both containers are exited
  • View wordpress site
    • Browse to localhost:8080
      • Notice the page is not found
  • Start multiple existing compose containers
    • docker compose -f .docker-compose.yml -p wp start
  • View container processes
    • docker ps -a
      • Notice both containers are running
  • View wordpress site
    • Browse to localhost:8080
      • Notice the page loads
  • Stop and remove running containers
    • docker compose -f .docker-compose.yml -p wp down
  • View container processes
    • docker ps -a
      • Notice no containers are listed
  • Create, start and run multiple containers
    • docker compose -f .docker-compose.yml -p wp up -d
  • View container processes
    • docker ps -a
      • Notice both containers are running
  • View wordpress site
    • Browse to localhost:8080
      • Notice the wordpress installer loads
        • The new containers are recreated from the image and the previous data is lost

Docker Compose is an easy way to create, start, stop and remove multiple dependent containers. It helps prevent start-up errors from typing commands manually.  It ensures that everyone on your team is starting their containers in the same way.

You can also learn more at docs.docker.com.

Thoughts on “Docker Bootcamp – Using Docker Compose”

  1. Hi,
    Thanks for sharing the nice information.
    It is a very nice and useful article.
    Your information is very helpful thanks those who want to take online it certificate programs.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Eric Sanner, Solutions Architect

More from this Author

Categories
Follow Us
TwitterLinkedinFacebookYoutubeInstagram