Docker Compose

Docker Compose

·

4 min read

What is Docker compose ❓

  • Docker Compose is a tool that was developed to help define and share multi-container applications.

  • With Compose, we can create a YAML file to define the services and with a single command, can spin everything up or tear it all down.

  • Docker Compose is a powerful tool for defining and managing multi-container applications through the use of a simple, human-readable YAML file. It allows developers to describe a whole application stack - including services, networks, and volumes - in a single file, making complex application setups easier to share and collaborate on.

What is YAML ⁉️

  • YAML is a data serialisation format that allows for script interaction. It is commonly used to create configuration files because it prioritises human readability over JSON.

  • YAML files use a .yml or .yaml extension, and follow specific syntax rules.

  • Example :

#Comment: This is a supermarket list using YAML
#Note that - character represents the list
---
food: 
  - vegetables: tomatoes #first list item
  - fruits: #second list item
      citrics: oranges 
      tropical: bananas
      nuts: peanuts
      sweets: raisins
  • You can read about YAML here

📜Docker compose file

  • Let's create a docker-compose file
# Specify the version of the Docker Compose file format
version: '3'

# Define services (containers) for the application
services:
  # Configuration for the frontend web server (Nginx)
  frontend:
    # Use the latest Nginx image from Docker Hub
    image: nginx:latest
    # Map port 80 on the host to port 80 in the container
    ports:
      - "80:80"

  # Configuration for the backend database (MySQL)
  backend:
    # Use the latest MySQL image from Docker Hub
    image: mysql:latest
    # Set environment variables for the MySQL container
    environment:
      # Set the root password for the MySQL database
      MYSQL_ROOT_PASSWORD: example_password
      # The backend service uses a custom backend image mysql:latest and is linked to the frontend service
      links:
        - frontend
  • We specify the Compose file version as '3'.

  • We define two services: frontend and backend

  • For the frontend service:

    • We use the latest Nginx image.

    • We map port 80 on the host to port 80 in the container.

  • For the backend service:

    • We use the latest MySQL image.

    • We set the MYSQL_ROOT_PASSWORD environment variable for MySQL.

    • The backend service uses a custom backend image mysql:latest and is linked to the frontend service .

  • So , the above was a simple docker compose file which is simple to understand but yeah you have to practice :D

📜Docker Commands :

  • Let's understand how to pull and image and run it as coontainer and then try to understand how to stop or kill the running container and remove the running containers.

  • Pull a pre-existing Docker image from a public repository (e.g. Docker Hub)

    • Docker Hub contains many pre-built images that you can pull and try without needing to define and configure your own.

    • To download a particular image, or set of images (i.e., a repository), use docker pull.

        #syntax 
        docker pull [OPTIONS] NAME[:TAG|@DIGEST]
      
        #example
        docker pull nginx
      

  • Run the container as a non-root user

      # syntax
      docker run -d --name my-container --user $(id -u):$(id -g) <image_name>
      # Example
    
    • -d: Run the container in detached mode.

    • --name my-container: Assign a name to the container (replace my-container with your preferred name).

    • --user $(id -u):$(id -g): Run the container as a non-root user (the id -u gets the current user's user ID, and id -g gets the group ID).

  • Use usermod command to give user permission to docker and reboot once

      sudo usermod -aG docker $USER
      # -aG - addes the user to docker group
      # usermod - modify user attributes
      sudo reboot
    
  • Inspect the container's running processes and exposed ports using docker inspect

      # syntax
      docker inspect <image name>
      #example
      docker inspect NGINX
    

  • use docker log command to view container's log output

       docker logs [OPTIONS] CONTAINER
    
  • Use docker stop command to stop the container

      docker stop <image name>
      #example
      docker stop NGINX
    

  • Use docker start container to start the container

      docker start <image name>
      #example
      docker start NGINX
    

How to run Docker commands without sudo?

  • Make sure docker is installed and system is updated (This is already been completed as a part of previous tasks):

  • sudo usermod -a -G docker $USER

  • Reboot the machine sudo reboot.

I hope now you can pull an image ,run it ,start and stop and atlast inspect the container as well ..
See you again with some more knowledge

Happy Learning :D