Part 2 – Before Docker Compose
We can see that when we did not use Docker Compose, it was a more lengthy process where all the images needed to be pulled manually and containers needed to run manually, we will now see how to use Docker Compose to simplify this process:
Step 1. Stop and Delete the previous containers
Let us first stop the previous containers that were running. For that you need to enter the following command:
docker stop [CONTAINER_ID] [CONTAINER_ID]
And to remove the containers, enter the following command:
docker rm [CONTAINER_ID] [CONTAINER_ID]
Step 2: Creating the Docker Compose file
Now let’s create a Docker Compose file. This file pulls two images – mongo and mongo-express, the file includes all the environment variables required and all the images you need so that we don’t have to manually run all the containers. Create a file named docker-compose.yaml and enter the following code:
version: '3'
services:
mongodb:
image: mongo
ports:
- 27017:27017
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=gfg-secret
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=admin
- ME_CONFIG_MONGODB_ADMINPASSWORD=gfg-secret
- ME_CONFIG_MONGODB_SERVER=mongodb
depends_on:
- "mongodb"
Step 3: Running the Docker Compose file
We can simply run our docker-compose.yaml by entering the following command:
docker-compose -f docker-compose.yaml up
This command alone builds, creates and starts the defined containers. You will get a similar result:
Step 4: Running the application on browser
And now if we open our browser and go to “http://0.0.0.0:8081” will we get the same sign in window:
And upon signing in, we will have our MongDB application running:
You will clearly notice that with Docker Compose, we can bring all the manual commands under a single file. This is how Docker compose makes it easier for us to build and run containers. Pat yourself on the back, you have completed the tutorial on how to execute multiple commands using Docker Compose.
Docker Compose – How To Execute Multiple Commands?
Docker Compose is an orchestration tool that comes with Docker and enables users to run and manage multi-container applications using a YAML format file. Docker Compose comes pre-installed with Docker and does not require any additional installation or activation. In this article, we will explore what Docker Compose is. and how we can execute multiple commands using Docker Compose.