Docker Compose is a way to run multiple containers together using ONE file.
Without Compose:
With Compose:
๐ One command.
๐ One file.
๐ Multiple containers.
Think of docker-compose as an automated multi-container workflow. Compose is an excellent tool for development, testing, CI workflows, and staging environments. According to the Docker documentation, the most popular features of Docker Compose are:
- Multiple isolated environments on a single host
- Preserve volume data when containers are created
- Only recreate containers that have changed
- Variables and moving a composition between environments
- Orchestrate multiple containers that work together
Compose answers 3 questions:
-
What containers do I need?
-
How do they talk to each other?
-
How do I start them together?
Docker Compose file structure
Now that we know how to download Docker Compose, we need to understand how Compose files work. It’s actually simpler than it seems. In short, Docker Compose files work by applying mutiple commands that are declared within a single docker-compose.yml configuration file. The basic structure of a Docker Compose YAML file looks like this:
Now that we know how to download Docker Compose, we need to understand how Compose files work. It’s actually simpler than it seems. In short, Docker Compose files work by applying mutiple commands that are declared within a single docker-compose.yml configuration file. The basic structure of a Docker Compose YAML file looks like this:
version ‘3’: This denotes that we are using version 3 of Docker Compose, and Docker will provide the appropriate features. At the time of writing this article, version 3.7 is latest version of Compose.services: This section defines all the different containers we will create. In our example, we have two services, web and database.web: This is the name of our Flask app service. Docker Compose will create containers with the name we provide.build: This specifies the location of our Dockerfile, and.represents the directory where thedocker-compose.ymlfile is located.ports: This is used to map the container’s ports to the host machine.volumes: This is just like the-voption for mounting disks in Docker. In this example, we attach our code files directory to the containers’./codedirectory. This way, we won’t have to rebuild the images if changes are made.links: This will link one service to another. For the bridge network, we must specify which container should be accessible to which container using links.image: If we don’t have a Dockerfile and want to run a service using a pre-built image, we specify the image location using theimageclause. Compose will fork a container from that image.environment: The clause allows us to set up an environment variable in the container. This is the same as the-eargument in Docker when running a container.
๐งฑ The 6 things you MUST learn
Write this on the board / slide:
1️⃣ services
Every service => one container
2️⃣ image OR build
Where does the container come from?
OR
3️⃣ ports
How we access the container
Left = host
Right = container
4️⃣ environment
Configuration values
5️⃣ volumes
Data persistence or live code
6️⃣ depends_on
Startup order (not readiness)
If you understand these 6, you can write 80% of Compose files in the real world.”
First Hands-On (10 minutes)
Goal
Run Nginx using Compose.
Step 1: Create file
Step 2: Paste this
Step 3: Run it
Open browser:
version: '3.3' services: wordpress: depends_on: - db image: wordpress:latest volumes: - wordpress_files:/var/www/html ports: - "80:80" restart: always environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_USER: wordpress WORDPRESS_DB_PASSWORD: my_wordpress_db_password db: image: mysql:5.7 volumes: - db_data:/var/lib/mysql restart: always environment: MYSQL_ROOT_PASSWORD: my_db_root_password MYSQL_DATABASE: wordpress MYSQL_USER: wordpress MYSQL_PASSWORD: my_wordpress_db_password volumes: wordpress_files: db_data:
sudo groupadd docker
- Add your user to the docker group.
sudo usermod -aG docker ${USER}
- You would need to loog out and log back in so that your group membership is re-evaluated or type the following command:
su -s ${USER}
- Verify that you can run docker commands without sudo.


No comments:
Post a Comment