Day 18: Docker for DevOps Engineers
DevOps | Cloud Practitioner | AWS | GIT | Kubernetes | Terraform | ArgoCD | Gitlab
Learning Docker Compose and YAML
Docker Compose allows you to define multi-container applications. With a single YAML file, you can manage the configuration of your application’s services, networking, and dependencies, and then bring them up with a single command. Let’s go through the tasks one by one.
Task 1: Learn to Use docker-compose.yml
The docker-compose.yml file is the heart of Docker Compose, where you define all your services. Here’s how to use it to set up the environment and configure services:
Basic Structure of docker-compose.yml:
YAML files are structured with indentation, so pay close attention to spacing (2 spaces per level are typical).
The file usually starts with defining services under the
services:section.
Here’s a sample structure:
version: '3' # Specify version of Docker Compose
services:
web:
image: nginx:latest # Pulls the latest Nginx image
ports:
- "8080:80" # Maps host port 8080 to container port 80
environment:
- NGINX_HOST=localhost
- NGINX_PORT=80
db:
image: postgres:latest # Uses the latest PostgreSQL image
environment:
POSTGRES_USER: myuser
POSTGRES_PASSWORD: mypass
POSTGRES_DB: mydb
volumes:
- db_data:/var/lib/postgresql/data
volumes:
db_data: {}
Defining Services:
Each service under
services:represents a container.In the example above,
webis the service name for the Nginx container, anddbis for PostgreSQL.
Networking Between Services:
- Docker Compose automatically creates an isolated network, so the containers (
webanddb) can communicate using their service names as hostnames. Here,webcan reachdbatdb:5432(PostgreSQL’s default port).
- Docker Compose automatically creates an isolated network, so the containers (
Using Environment Variables:
- Define environment variables under
environment:. You can hard-code values or use variables from your.envfile by referencing them like${VAR_NAME}.
- Define environment variables under
Bringing Up and Down the Application:
Use
docker-compose up -dto bring up all services in detached mode.Use
docker-compose downto stop and remove all containers and networks created.
Common Commands:
docker-compose up -d: Starts all containers in the background.docker-compose down: Stops and removes containers and networks.docker-compose logs: Shows logs for all services.docker-compose ps: Lists running services and their status.
Task 2: Run Docker Commands Without Sudo and Manage a Container
This task involves running a Docker container as a non-root user, inspecting its details, managing logs, and stopping/starting the container. Let’s go step-by-step.
Running Docker Without Sudo:
- By default, Docker commands need
sudo. To avoid this, add your user to the Docker group.
- By default, Docker commands need
sudo usermod -aG docker $USER
- After running this command, reboot your machine to apply the change:
sudo reboot
Pulling and Running a Docker Image:
- Pull a pre-existing image from Docker Hub, for example, Nginx:
docker pull nginx
- Run it as a detached container (in the background) on your local machine:
docker run -d --name my-nginx nginx
Inspecting the Container:
- Use
docker inspectto check details like the container’s IP, ports, and configuration:
- Use
docker inspect my-nginx
- This command outputs a JSON document with detailed information. Look for sections like
"NetworkSettings"(to view port mappings) and"Config"(to see environment variables and other settings).
Viewing Logs:
- Use
docker logsto check the output logs of the container, which is helpful for troubleshooting:
- Use
docker logs my-nginx
- To stream logs continuously, use
docker logs -f my-nginx.
Stopping and Starting the Container:
- To stop the container, use:
docker stop my-nginx
- To start it again, use:
docker start my-nginx
Removing the Container:
- Once done, remove the container to free up resources:
docker rm my-nginx
By following these steps, you’ve now learned how to set up multi-container environments with Docker Compose, configure services using docker-compose.yml, and manage containers efficiently without needing root permissions. Great work! 🎉


