Day 18 Task: Docker for DevOps Engineers

Day 18 Task: Docker for DevOps Engineers

Β·

5 min read

Till now we have created a Docker file and pushed it to the Repository. Let's move forward and dig more into other Docker concepts. Let's do some study today on Docker Compose πŸ˜ƒ

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 another best tool for docker to set up multi-container environments. Using this create a single compose file with defining all the containers with their environments. You can easily use a single command to build images and run all the containers.

    There is a three-step process to work with Docker Compose.

    1. Define the application environment with Dockerfile for all services.

    2. Create a docker-compose.yml file defining all services under the application.

    3. Run docker-compose up to run all services under applications.

What is YAML?

  • YAML is a data serialization language that is often used for writing configuration files. Depending on whom you ask, YAML stands for yet another markup language or YAML ain’t markup language (a recursive acronym), which emphasizes that YAML is for data, not documents.

  • YAML is a popular programming language because it is human-readable and easy to understand.

  • YAML files use a .yml or .yaml extension.

YAML syntax:

  • Indentation: Think of it like building blocks. When things are related, just indent them.

  • Key & Value: It's like saying "Here's what this is" and "This is what it means."

  • Lists: When you want to list things, use a dash -. Easy peasy.

  • Words & Numbers: For regular words, just write them. Numbers are like they are, no extra fuss.

  • Quotes for Special: If you're talking about something special, like spaces in the text, put it in quotes.

  • Notes for You: Use # to write notes only for humans. Computers ignore them.

# Here's a simple YAML example
name: sriparthu
age: 25
isStudent: true
hobbies:
  - Reading
  - Bloger
  - Writer
address:
  city: Hyderbad , Telangana
  zip: "500030"
  • Task🎈

  • Setting Up Environment, Services, Links, and Variables πŸš€πŸ“

    Here's a step-by-step guide with the given example syntax explained using bullet points and emojis:

    1. Environment Setup: Create a file named docker-compose.yml to orchestrate your containers.

    2. Version Definition: At the beginning, define the version you're using. In this case, version: "3.3" tells Docker Compose what features you're using.

    3. Services Section:

      • Under services:, define each container you want to run as a "service."
    4. Service Configuration:

      • For each service (container), set a name, like web and db.

      • Define their images, like nginx:latest and mysql.

    5. Port Mapping:

      • Use ports: to map your computer's ports to container ports.

      • For example, "80:80" means connect your computer's port 80 to the container's port 80.

    6. Container Links (Optional):

      • Containers can talk by using their service names.

      • No need to worry about IP addresses.

    7. Environment Variables:

      • Use environment: to set environment variables.

      • For instance, "MYSQL_ROOT_PASSWORD=test@123" sets the MySQL root password.

Example Syntax and Explanation:

    version: "3.3"
    services:
      web:
        image: nginx:latest
        ports:
          - "80:80"
      db:
        image: mysql
        ports:
          - "3306:3306"
        environment:
          - "MYSQL_ROOT_PASSWORD=test@123"
  • The docker-compose.yml version is set to 3.3.

  • Two services are defined: web with Nginx and db with MySQL.

  • Ports are mapped so that your computer's port 80 connects to Nginx's port 80, and port 3306 connects to MySQL's port 3306.

  • The MySQL container's root password is set to test@123.

Docker Compose takes this YAML script and creates a symphony of containers, setting up the environment, linking them together, and following your configuration. It's like instructing a team of chefs to cook up a feast together! 🍽️🎢

  • Task🎈🎈

  • 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 the user permission to docker). Make sure you reboot the instance after giving permission to the user

    1. Pull a Docker Image from Hub πŸ›’οΈπŸŒ

      • Open your terminal and type:

  • This fetches the latest version of the "nginx" image from Docker Hub.

πŸ›’οΈ Pulling the image... πŸš€

  1. Run Container as Non-Root User πŸ³πŸ‘€

    • To give your user Docker powers, add them to the docker group:

        sudo usermod -aG docker $USER
        sudo reboot
      
    • Replace $USER with your real username.

    • Afterwards, reboot your machine for changes to take effect.

πŸ³πŸ‘€ Adding user to Docker group... πŸ”„

  1. Run the Docker Container πŸš’πŸƒ

    • To run the pulled image as a non-root user, use:

  • -u 1001 assigns UID 1001 to the container's user.

  • -d runs it in detached mode, and -p maps ports.

πŸš’πŸƒ Running the container... 🌐

  1. Access Your Containerized App 🌐πŸ–₯️

    • Open your browser and navigate to http://localhost:8080.

    • You'll see the Nginx default page, indicating success.

🌐 Accessing your app... πŸ’»

  1. Cleanup: Stopping and Removing πŸ§ΉπŸ—‘οΈ

    • To stop the container, use:

  • Replace container_id with the actual container ID.

  • To remove it afterwards:

🧹 Stopping and removing... πŸ—‘οΈ

Docker brings images to life and running them is like hosting a mini-container party on your machine! πŸŽ‰πŸ°

How to run Docker commands without sudo?

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

  • sudo usermod -a -G docker $USER

  • Reboot the machine. ( or )

  • sudo chown $USER /var/run/docker.sock


Happy Learning

Thanks For Reading! :)

-SriparthuπŸ’

Β