Table of contents
- Introduction
- TASK 1: What is Docker Compose?
- TASK 2: What is YAML?
- TASK 3: Learn how to use the docker-compose.yml file, to set up the environment, configure the services and links between different containers, and also to use environment variables in the docker-compose.yml file.
- TASK 4: Working with Docker Compose
- 1. Pull a pre-existing Docker image from a public repository (e.g. Docker Hub) and run it on your local machine. Run the container as a non-root user (Hint- Use usermod command to give user permission to docker). Make sure you reboot instance after giving permission to user.
- 2. Inspect the container's running processes and exposed ports using the docker inspect command.
- 3. Use the docker logs command to view the container's log output.
- 4. Use the docker stop and docker start commands to stop and start the container.
- 5. Use the docker rm command to remove the container when you're done.Once you're done with your application, you can remove the containers with:
- Conclusion
Introduction
Welcome to Day 18 of our journey into the world of Docker! In previous tasks, we've explored the fundamentals of Docker, containerization, and how to create custom images. Today, we'll delve into Docker Compose, an invaluable tool for managing multi-container applications.
TASK 1: What is Docker Compose?
Docker Compose is a powerful and user-friendly tool that allows you to define and manage multi-container Docker applications. It simplifies the process of configuring, orchestrating, and running multiple containers together, making it an ideal choice for complex applications with interdependent services.
๐ฆ With Docker Compose, you can define your application's services, networks, and volumes in a single file, docker-compose.yml
, and then use a single command to start your entire application stack.
TASK 2: What is YAML?
Before we dive deeper into Docker Compose, let's briefly discuss YAML. YAML (short for "YAML Ain't Markup Language") is a human-readable data serialization format that's often used for configuration files. In the context of Docker Compose, the docker-compose.yml
file is written in YAML. It uses indentation to represent data hierarchies and is known for its simplicity and clarity.
YAML is like a structured language for defining settings and options, and it's the basis for configuring your Docker Compose services.
TASK 3: Learn how to use the docker-compose.yml file, to set up the environment, configure the services and links between different containers, and also to use environment variables in the docker-compose.yml file.
The docker-compose.yml
file serves as the blueprint for your Docker Compose application. You can define various aspects of your application in this file, including:
Services: Containers that make up your application.
Networks: Defines how the containers communicate with each other.
Volumes: Shared data storage.
Environment variables: Configurable values for your containers.
Here's a sample
docker-compose.yml
file:#specify the version of the docker compose file format version: '3' #define services as containers for the application services: #configuration for the web server (nginx) web: #use the small nginx image from the docker hub image: nginx:alpine #map the ports 80 on the host and 80 in the container ports: - "80:80" #configuration for the (2nd service as mysql) database db: #use the mysql image from the docker hub image: mysql:5.7 #set the env variables for the MYSQL container environment: #set the root password for the MYSQL database MYSQL_ROOT_PASSWORD: example
In this example, we have two services - a web server using the Nginx image and a MySQL database. The
docker-compose.yml
file specifies the images to use, port mapping for the web service, and the root password for the database service.
TASK 4: Working with Docker Compose
1. Pull a pre-existing Docker image from a public repository (e.g. Docker Hub) and run it on your local machine. Run the container as a non-root user (Hint- Use usermod command to give user permission to docker). Make sure you reboot instance after giving permission to user.
To get started, you can pull a pre-existing Docker image from a public repository like Docker Hub. For example:
docker pull nginx
run the images, The -d
flag runs the containers in the background.
container is running now.
2. Inspect the container's running processes and exposed ports using the docker inspect command.
You can inspect a rmking container's processes and exposed ports using the docker inspect
command:
docker inspect <container_id>
This command provides detailed information about the container, including its configuration and network settings.
3. Use the docker logs command to view the container's log output.
To view the log output of a container, use the docker logs
command:
docker logs <container_id>
This is useful for troubleshooting and debugging your application.
4. Use the docker stop and docker start commands to stop and start the container.
You can gracefully stop and start containers using the following commands:
docker-compose stop
: Stops the containers.docker-compose start
: Starts the containers again.
5. Use the docker rm command to remove the container when you're done.Once you're done with your application, you can remove the containers with:
# to remove the container
docker rm <container-id>
# to kill the container
docker kill <container-id>
This cleans up your environment, making it ready for the next use.
Conclusion
Docker Compose simplifies container orchestration and management, allowing you to create and manage complex multi-container applications with ease. With the power of YAML and the simplicity of Docker Compose commands, you can efficiently develop, test, and deploy your applications. Keep exploring Docker, and don't forget to have fun along the way! ๐๐ณ