Day 18 Docker Compose Task: Step-by-Step Instructions

ยท

6 min read

Hey there, tech explorers! ๐Ÿ‘‹ Ready to dive deep into the world of containers? If youโ€™ve been playing around with Docker and are looking to take things to the next level, youโ€™re in the right place! In todayโ€™s blog, weโ€™ll be exploring Docker Compose and YAML ๐Ÿ“โ€”two game-changing tools that can turn your container management from meh to magnificent! ๐Ÿ’ฅ

So, fasten your seatbelt, grab a cup of coffee โ˜•, and letโ€™s dive into it.


๐Ÿ“‹ Prerequisites ๐Ÿ“‹

Before we dive in, make sure you have the following in place:

  • ๐Ÿง Linux-based system (Ubuntu, CentOS, etc.) or Windows/Mac with Docker Desktop installed.

  • ๐Ÿณ Docker and Docker Compose are installed and running on your system.

    Install Docker:

      sudo apt-get update
      sudo apt-get install docker.io -y
    

    Install Docker Compose:

      sudo apt-get install docker-compose -y
    

    Docker Compose Installed: Check with

      docker-compose --version
    
  • ๐Ÿ‘จโ€๐Ÿ’ป Basic understanding of terminal commands.

  • ๐ŸŒ Internet connection to pull images from Docker Hub.


What is Docker Compose? ๐Ÿณ๐Ÿค”

Imagine you want to run a web app that needs three things:

  1. A frontend to display stuff,

  2. A backend to handle logic, and

  3. A database to store data.

Now, setting up each service manually and linking them can be a pain, right? Thatโ€™s where Docker Compose comes in to save the day! ๐ŸŽ‰

โœจ One file, one command, and BAM! ๐Ÿ’ฅ Your entire environment is up and running in seconds! ๐Ÿš€

In simple terms:

Docker Compose is a tool that allows you to define and manage multiple containers in one single file called docker-compose.yml.
It automates everything, from building images to linking containers and managing their networks. ๐ŸŽฉโœจ


What is YAML? ๐Ÿ“„๐Ÿค”

YAML is like the secret language ๐Ÿคซ Docker Compose speaks!

YAML (Yet Another Markup Language or "YAML Ain't Markup Language") is a human-friendly data format that's easy to read and write. No more confusing syntax or complicated codeโ€”just simple, clean configurations. ๐Ÿงผ

YAML Syntax Basics:

  • Indentation Matters: Use spaces, not tabs.

  • Key-Value Pairs: Define data with simple key: value pairs.

  • Lists: Use hyphens (-) for lists.

Why YAML?

  • ๐Ÿ“ Readable: Easy to read and write.

  • ๐ŸŽฏ Structured: Organizes data hierarchically.

  • โœ… Widely Used: Popular in DevOps for defining configurations like Docker Compose, Kubernetes manifests, and Ansible playbooks.

Think of YAML as a way to describe what you want Docker to do:

  • Which containers to start ๐Ÿ› ๏ธ

  • How they should communicate ๐Ÿ“ก

  • What environment variables to use ๐ŸŒ

Hereโ€™s what YAML looks like in action:

version: '3'
services:
  web:
    image: nginx
    ports:
      - "80:80"
  db:
    image: mysql
    environment:
      MYSQL_ROOT_PASSWORD: example

In this example:

  • web and db are services.

  • We specify images (like Nginx and MySQL).

  • We expose ports and set environment variables.

Easy, right? ๐Ÿ˜Ž


Letโ€™s Get Hands-On! ๐Ÿ’ช

Task 1: Mastering Docker Compose

Now that you know what Docker Compose and YAML are, letโ€™s get hands-on! ๐Ÿ’ช Hereโ€™s how to set up your environment using a docker-compose.yml file.

๐Ÿง‘โ€๐Ÿณ Step 1: Create a docker-compose.yml File

  1. Open your favorite code editor (VS Code, Nano, or even Notepad).

  2. Create a file named docker-compose.yml.

  3. Add the following content to define two services: a web server and a database.

sudversion: '3'
services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"  # Expose port 8080 on your machine to access the web service
    environment:
      ENV: production
    volumes:
      - .:/app
  db:
    image: mysql:latest
    environment:
      MYSQL_ROOT_PASSWORD: mypassword123  # Set environment variables

โš™๏ธ Step 2: Run the Services

Open your terminal and navigate to the folder containing docker-compose.yml. Run:

docker-compose up -d

This command spins up both the web and database services. ๐ŸŽ‰

๐Ÿ”Step 3: Check the running containers

docker ps

๐ŸŒ Step 4: Check Everything is Running

Visit http://localhost:8080 in your browser. You should see the Nginx welcome page. If everything works, congrats! ๐ŸŽŠ You just ran multiple containers with one command!

โน๏ธ Step 5: Stop the services

docker-compose down

Using Environment Variables ๐ŸŒฑ

You can manage environment variables in Docker Compose like this:

  • Option 1: Directly in the docker-compose.yml file (as shown above).

  • Option 2: Use a .env file for better manageability:

      ENV=production
      MYSQL_ROOT_PASSWORD=mypassword123
    

    Example:

    Store sensitive data in a .env file:

      NODE_ENV=production
      PORT=3000
    

    Update your docker-compose.yml to use it:

      environment:
        - NODE_ENV=${NODE_ENV}
        - PORT=${PORT}
    

Task 2: Running Docker Containers ๐Ÿ‹

Letโ€™s take it a step further and work with individual Docker commands.

๐Ÿ‹ Step 1: Pull a Docker Image

Grab a pre-built image from Docker Hub:

sudo docker pull nginx:latest

๐Ÿ‘จโ€๐Ÿ’ป Step 2: Run the Container as a Non-Root User

To run Docker commands without using sudo (admin rights), follow these steps:

  1. Add your user to the docker group:

     sudo usermod -aG docker $USER
    
  2. Reboot your machine for changes to take effect:

     sudo reboot
    
  3. After the reboot, check if you can run Docker without sudo:

     docker ps
    

    If you can run the command without sudo, youโ€™re all set! ๐ŸŽ‰

๐Ÿ” Step 3: Inspect the Container

Run a container:

docker run -d -p 8080:80 --name mynginx nginx:latest

Now inspect its details:

docker inspect mynginx

This will display all the juicy details like exposed ports, IP address, and running processes. ๐Ÿ•ต๏ธโ€โ™‚๏ธ

๐Ÿ“œ Step 4: View Logs

Want to see whatโ€™s happening inside the container? Run:

docker logs mynginx

๐Ÿ›‘ Step 5: Stop and Start the Container

Stop the container:

docker stop mynginx

Start it again:

docker start mynginx

๐Ÿ—‘๏ธ Step 6: Remove the Container

Once you're done, clean up by removing the container:

docker rm mynginx

Wrapping It Up with a Fun Twist! ๐ŸŽ‰

And thatโ€™s a wrap, folks! ๐ŸŽฌ You've just unlocked another level in your Docker journey. ๐Ÿš€ Letโ€™s take a quick recap of what we covered:

  1. Docker Compose helps you manage multi-container apps with ease.

  2. YAML is the language you use to configure services in Docker Compose.

  3. You can pull, inspect, log, stop, start, and remove Docker containers like a pro. ๐Ÿš€

  4. Bonus: Running Docker commands without sudo makes life much easier. ๐Ÿ˜‰

With Docker Compose and YAML under your belt, youโ€™re now ready to compose containerized symphonies! ๐ŸŽต Whether itโ€™s running a simple web app ๐Ÿ•ธ๏ธ or building complex microservices architecture ๐Ÿ—๏ธ, you're in control. Keep experimenting, breaking things ๐Ÿ”ง, and building them back stronger! ๐Ÿ’ฅ

๐Ÿš€ โ€œThe best way to predict the future is to create it.โ€ โ€“ Abraham Lincoln (and probably every DevOps engineer ever ๐Ÿ˜‰)

Happy Docking! โš“ See you in the next adventure! ๐Ÿšข

ย