Docker compose is a tool for defining and running multi-container docker applications. Docker compose can configure your application services inside a yaml file called the docker-compose.yml You can start all services with a single command and stop all your application services with a single command too. A Docker container is a way to package applications with all the necessary dependencies and configurations. The container package is portable and easily shared between your development and engineering teams.

Docker compose lets you manage multiple containers and run all your multi-container applications. With the yaml file, you can configure application services, build your containers, and start your containers.

Prerequisites

In this tutorial, we’ll learn how to configure and run containers using Docker Compose.

Build and Run Applications with Docker Compose

In a docker-compose file, you can map your whole commands with their configuration into a file for your container workflow. In a docker-compose.yml file, we have the version of your docker-compose and your services list.

After specifying your services name, you need to write the image where the app will be built, with your ports and environment variables. In this tutorial, we’ll use MongoDB and MongoExpress images to build our containers.

version: '3'
services:
 mongodb:
   image: mongo
   ports:
     - 27017:27017
   environment:
     - MONGO_INITDB_ROOT_USERNAME=admin
     - MONGO_INITDB_ROOT_PASSWORD=password
   volumes:
   # path of mongodb container
     - mongo-data:/data/db

  • Next, in your mongo-express configuration, you must configure your yaml file and enter the image name, port number, and environment variables. Docker compose will take care of creating a common network for your containers.
mongo-express:
   image: mongo-express
   ports:
     - 8080:8081
   environment:
     - ME_CONFIG_MONGODB_ADMINUSERNAME=admin
     - ME_CONFIG_MONGODB_ADMINPASSWORD=password
     - ME_CONFIG_MONGODB_SERVER=mongodb

  • Next, go to your command line and start your containers using the docker-compose file. Run the docker-compose -f <file.yaml> up command, this will start all the containers in your yaml file.

  • You can see that the command is pulling and downloading your mongo image with the latest tag.
  • We started both containers simultaneously; you can see the logs of both containers in your CLI. The MongoDB container has to start immediately so that the Mongo Express container can establish a connection with your MongoDB database container.
  • Wait for some minutes; you can see your request is now successful.
  • Back in your terminal, you can see your Docker containers with the ports.
  • Next, access your mongo-express server running on port 8080
  • Create your database, and give it a name.
  • You can see your database Stats and Collections.
  • When you create a new collection, it creates the Tools, Collection Stats, and Indexes for your Mongo Express workflow App.
  • When you’re done with the configuration, you can tear down your containers using the docker-compose -f docker-compose.yaml down command.


Conclusion

Docker-compose is a tool to help define and share multi-container applications. It’s an important tool for any application that needs multiple microservices, as it allows each service to easily be in a separately managed container. With the docker-compose file, you can run multiple services and build multi-service container workflows effectively.