Skip to main content

Command Palette

Search for a command to run...

Day 18: Docker for DevOps Engineers

Published
3 min read
A

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:

  1. 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: {}
  1. Defining Services:

    • Each service under services: represents a container.

    • In the example above, web is the service name for the Nginx container, and db is for PostgreSQL.

  2. Networking Between Services:

    • Docker Compose automatically creates an isolated network, so the containers (web and db) can communicate using their service names as hostnames. Here, web can reach db at db:5432 (PostgreSQL’s default port).
  3. Using Environment Variables:

    • Define environment variables under environment:. You can hard-code values or use variables from your .env file by referencing them like ${VAR_NAME}.
  4. Bringing Up and Down the Application:

    • Use docker-compose up -d to bring up all services in detached mode.

    • Use docker-compose down to stop and remove all containers and networks created.

  5. 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.

  1. Running Docker Without Sudo:

    • By default, Docker commands need sudo. To avoid this, add your user to the Docker group.
    sudo usermod -aG docker $USER
  • After running this command, reboot your machine to apply the change:
    sudo reboot
  1. 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
  1. Inspecting the Container:

    • Use docker inspect to check details like the container’s IP, ports, and configuration:
    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).
  1. Viewing Logs:

    • Use docker logs to check the output logs of the container, which is helpful for troubleshooting:
    docker logs my-nginx
  • To stream logs continuously, use docker logs -f my-nginx.
  1. Stopping and Starting the Container:

    • To stop the container, use:
    docker stop my-nginx
  • To start it again, use:
    docker start my-nginx
  1. 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! 🎉

More from this blog

DevOps Projects and Blog

69 posts