Docker is a powerful tool that allows developers to easily manage and deploy applications using containers. Docker-compose is a tool that helps in orchestrating multiple containers and managing their dependencies. In this article, we will learn how to orchestrate three containers using Docker-compose.
Prerequisites
Before we begin, make sure you have the following prerequisites:
- Docker installed on your machine.
- Basic knowledge of Docker and containers.
Step 1: Create a Dockerfile for each container
The first step is to create a Dockerfile for each container. A Dockerfile is a text file that contains instructions on how to build a Docker image. In our case, we will have three Dockerfiles, one for each container.
Let's create a Dockerfile for the first container:
FROM ubuntu:latest
RUN apt-get update && apt-get install -y apache2
CMD ["apache2ctl", "-D", "FOREGROUND"]
This Dockerfile starts with the base Ubuntu image, installs Apache2, and sets the command to start the Apache server.
Similarly, create Dockerfiles for the other two containers. For example, the second container could be a MySQL database, and the third container could be a Redis cache.
Step 2: Create a docker-compose.yml file
The next step is to create a docker-compose.yml file. This file will define the configuration for all the containers and their dependencies.
Create a new file named docker-compose.yml and add the following content:
version: '3'
services:
web:
build: ./web
ports:
- "80:80"
depends_on:
- db
- cache
db:
build: ./db
environment:
- MYSQL_ROOT_PASSWORD=secret
cache:
build: ./cache
This docker-compose.yml file defines three services: web, db, and cache. The web service depends on the db and cache services. It also maps port 80 of the host machine to port 80 of the web container.
Make sure to update the paths in the build section to match the directories where you have created the Dockerfiles for each container.
Step 3: Build and run the containers
Now that we have the Dockerfiles and the docker-compose.yml file ready, we can build and run the containers using Docker-compose.
Open a terminal or command prompt and navigate to the directory where the docker-compose.yml file is located.
Run the following command to build and run the containers:
docker-compose up -d
This command will build the Docker images for each container and start the containers in the background.
You can use the following command to check the status of the containers:
docker-compose ps
This will show you the status of each container.
Step 4: Test the containers
Once the containers are up and running, you can test them to make sure everything is working as expected.
Open a web browser and navigate to http://localhost. You should see the default Apache2 page, indicating that the web container is working.
You can also connect to the MySQL database using a MySQL client and verify that it is accessible.
Similarly, you can test the Redis cache by connecting to it using a Redis client.
Conclusion
In this article, we learned how to orchestrate three containers using Docker-compose. We created Dockerfiles for each container, defined the configuration in a docker-compose.yml file, built and ran the containers using Docker-compose, and tested the containers to ensure they are working correctly.
References
| Link | Description |
|---|---|
| https://docs.docker.com/ | Docker documentation |
| https://docs.docker.com/compose/ | Docker-compose documentation |