Monday, 12 October 2020

Create Microservices and Multiple containers with Docker- Compose(Deploy Wordpress with Docker-Compose)

 Docker Compose is a way to run multiple containers together using ONE file.

Without Compose:

docker run nginx docker run mysql docker run redis

With Compose:

docker compose up

๐Ÿ‘‰ One command.
๐Ÿ‘‰ One file.
๐Ÿ‘‰ Multiple containers.

Think of docker-compose as an automated multi-container workflow. Compose is an excellent tool for development, testing, CI workflows, and staging environments. According to the Docker documentation, the most popular features of Docker Compose are:

  • Multiple isolated environments on a single host
  • Preserve volume data when containers are created
  • Only recreate containers that have changed
  • Variables and moving a composition between environments
  • Orchestrate multiple containers that work together

Compose answers 3 questions:

  1. What containers do I need?

  2. How do they talk to each other?

  3. How do I start them together?

Docker Compose file structure

Now that we know how to download Docker Compose, we need to understand how Compose files work. It’s actually simpler than it seems. In short, Docker Compose files work by applying mutiple commands that are declared within a single docker-compose.yml configuration file. The basic structure of a Docker Compose YAML file looks like this:


  • version ‘3’: This denotes that we are using version 3 of Docker Compose, and Docker will provide the appropriate features. At the time of writing this article, version 3.7 is latest version of Compose.

  • services: This section defines all the different containers we will create. In our example, we have two services, web and database.

  • web: This is the name of our Flask app service. Docker Compose will create containers with the name we provide.

  • build: This specifies the location of our Dockerfile, and . represents the directory where the docker-compose.yml file is located.

  • ports: This is used to map the container’s ports to the host machine.

  • volumes: This is just like the -v option for mounting disks in Docker. In this example, we attach our code files directory to the containers’ ./code directory. This way, we won’t have to rebuild the images if changes are made.

  • links: This will link one service to another. For the bridge network, we must specify which container should be accessible to which container using links.

  • image: If we don’t have a Dockerfile and want to run a service using a pre-built image, we specify the image location using the image clause. Compose will fork a container from that image.

  • environment: The clause allows us to set up an environment variable in the container. This is the same as the -e argument in Docker when running a container.

๐Ÿงฑ The 6 things you MUST learn

Write this on the board / slide:

1️⃣ services

Every service => one container

services: web: db:

2️⃣ image OR build

Where does the container come from?

image: nginx:alpine

OR

build: .

3️⃣ ports

How we access the container

ports: - "8080:80"

Left = host
Right = container


4️⃣ environment

Configuration values

environment: NODE_ENV: production

5️⃣ volumes

Data persistence or live code

volumes: - data:/var/lib/mysql

6️⃣ depends_on

Startup order (not readiness)

depends_on: - db

If you understand these 6, you can write 80% of Compose files in the real world.”

First Hands-On (10 minutes)

Goal

Run Nginx using Compose.

Step 1: Create file

mkdir compose-lab && cd compose-lab
vi docker-compose.yml

Step 2: Paste this

services: web: image: nginx:alpine ports: - "8080:80"

Step 3: Run it

docker compose up

Open browser:

http://localhost:8080

Real-World Example (15 minutes)

Node + MySQL App (Classic Interview Scenario)

services: mysql: image: mysql:8.4 environment: MYSQL_ROOT_PASSWORD: rootpass MYSQL_DATABASE: appdb MYSQL_USER: app MYSQL_PASSWORD: apppass volumes: - mysqldata:/var/lib/mysql api: image: node:24-alpine ports: - "3000:3000" depends_on: - mysql volumes: mysqldata:



  • No localhost between containers
  • Volumes keep DB data safe
  • Containers talk using service names (mysql)


  • Final Lab Exercise: Create Docker Containers running WordPress; a MySQL database and an Apache PHP web server.
    Prerequisite: Docker installed, Docker-compose installed
    Step 1: Create yml file
    Make a directory  called wordpress
    $ mkdir wordpress
    Go into the directory
    $ cd wordpress
    Using vi Editor to create docker-compose file:
    $ vi docker-compose.yml

    Copy and paste the below code in the editor
    version: '3.3'
    
    services:
       wordpress:
         depends_on:
           - db
         image: wordpress:latest
         volumes:
           - wordpress_files:/var/www/html
         ports:
           - "80:80"
         restart: always
         environment:
           WORDPRESS_DB_HOST: db:3306
           WORDPRESS_DB_USER: wordpress
           WORDPRESS_DB_PASSWORD: my_wordpress_db_password
    
       db:
         image: mysql:5.7
         volumes:
           - db_data:/var/lib/mysql
         restart: always
         environment:
           MYSQL_ROOT_PASSWORD: my_db_root_password
           MYSQL_DATABASE: wordpress
           MYSQL_USER: wordpress
           MYSQL_PASSWORD: my_wordpress_db_password
    volumes:
        wordpress_files:
        db_data:




    Save the File
    Step 2: Run Docker-compose
     $ docker-compose up -d

    if you have permission issues run the below commands
    sudo groupadd docker
    
    • Add your user to the docker group.
    sudo usermod -aG docker ${USER}
    
    • You would need to loog out and log back in so that your group membership is re-evaluated or type the following command:
    su -s ${USER}
    
    • Verify that you can run docker commands without sudo.
    Log out and log back in 

    This will run docker-compose  and fetch the images from the docker hub and create your WordPress instance. You’ll be able to access WordPress from a browser on port 
    80
    Go to your browser: your_ip:80


    ๐Ÿงช Compose Commands Students MUST Memorize

    docker compose up docker compose up -d docker compose down docker compose down -v docker compose ps docker compose logs -f

    No comments:

    Post a Comment

    Understanding Software Testing Using a Simple JSP Web App

      ๐ŸŽฏ Lab Context (Very Important) This project is a basic Maven web application created for learning DevOps concepts. It displays a sim...