Docker and Kubernetes [CKA/ CKS/ CKAD] Q/A (Docker Compose, Docker Swarm, Docker Service): Day 6 Live Session Review

Kubernetes

Share Post Now :

HOW TO GET HIGH PAYING JOBS IN AWS CLOUD

Even as a beginner with NO Experience Coding Language

Explore Free course Now

Table of Contents

Loading

This blog post covers a brief overview of the topics covered and some common questions asked on Day 6 Live Interactive training on Docker and Kubernetes Certification i.e. CKACKAD  and CKS.

This will help you to learn Docker & Kubernetes and prepare you for these certifications and get a better-paid job in the field of Microservices, Containers and Kubernetes.

In the Day 5 CKA Live session we covered an overview of Multi-Stage Dockerfile, Docker Volume, Docker Networking. And in this week, Day 6, we covered Docker Compose, Docker Swarm, Docker service. We also performed labs.

Docker Compose:

Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a Compose file to configure your application’s services. Then, using a single command, you create and start all the services from your configuration. Using Compose is basically a three-step process.

  1. Define your app’s environment with a Dockerfile so it can be reproduced anywhere.
  2. Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment.
  3. Lastly, run docker-compose up and Compose will start and run your entire app.

You must have Docker Engine installed on your system. If you don’t have already installed, Visit our Docker installation section of this tutorial.

Q/A’s asked in sessions are:

Q) How do we install Docker Compose?
Ans: First of all we need to grab the software. We can do this on Linux using Curl.

sudo curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

This downloaded the docker-compose tool to /usr/local/bin. Once downloaded, you must make the file executable. This can be done with:

sudo chmod +x /usr/local/bin/docker-compose

Finally, we can test that everything is working as it should by running:

$ docker-compose --version
docker-compose version 1.23.2, build 1110ad01

Looks good! Now we can move on to some examples of how docker compose can be used.

Q) How to create docker-compose.yml, and explain the steps?
Ans: Let’s create our docker-compose.yml

version: "3.9" # optional since v1.27.0
services:
  db:
    container_name: postgres-comose
    image: postgres
    ports:
      - 5432:5432
    networks:
      - compose-network
    environment:
      - POSTGRES_DB=docker-demo
      - POSTGRES_PASSWORD=mysecretpassword
  cache:
    container_name: redis-compose
    image: redis
    ports:
      - 6379:6379
    networks:
      - compose-network
  app:
    container_name: golang-compose
    # image: app-golang:1.0
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - 8080:8080
    restart: unless-stopped
    networks:
      - compose-network
    depends_on:
      - db
      - cache
    environment:
      - REDIS_HOST=redis-compose
      - DB_HOST=postgres-compose
      - MYNAME=REPOERNA
networks:
  compose-network:
    name: new-example-network

*Note: in the app service environment, we use REDIS_HOST and DB_HOST using Redis and Postgres container name

Okay, I will explain what we write. At the root of the file, we have:

  • version define Compose release. Each release has different features and parameters that we can use. You can read the compatibility matrix of each version here. If you omit the versionby default, it will use version 1.
  • services defined all services in our system. In our example, before we use Postgres, Redis, and golang app, those 3 services will be defined here.
  • network define network will be used, if you haven’t created the network, it will be automatically created, and you can create more than one network here.

Inside services, there is the service name; we have DB, cache, and app. On every service, we will define a parameter that will be used on each service. these parameters are:

  • container_name: defines name services container.
  • image: define an image that will be used for creating the container.
  • port: define port binding to exposed the port inside a container with the environment outside.
  • networks: define a network that will be used by services.
  • depends_on: define other services that needed by app services.
  • environment: define environment variable inside the container.

In app service, instead of using an image, we will use build to build the image, below build there is context that define the location of Dockerfile that will be used to build the image and Dockerfile to define Dockerfile filename.

Docker Compose CLI Reference

Below mentioned docker-compose command will provide you way to manage Docker containers with docker-compose
build – Build images for services for which build is defined.

$ docker-compose build ## Build all services 
$ docker-compose build web ## Build single service

up – Create docker containers with available services in docker-compose.yml file of current directory. Use -d switch to launch containers in daemon mode.

$ docker-compose up -d ## Create all containers 
$ docker-compose up -d web ## Create single container

down –  Stops and deletes all containers, network and associated images for the services defined in a config file

$ docker-compose down ## Restart all containers 
$ docker-compose down web ## Restart single container

ps – Lists all containers created for the services defined in a config file with there status, port bindings and command.

$ docker-compose ps

exec – Executes a command to the running container. For example list files in container associated with web service.

$ docker-compose exec web ls -l

start –  Starts stopped containers of the services defined in config file

$ docker-compose start ## Start all containers 
$ docker-compose start web ## Start single container

stop – Stop running containers for the services defined in config file

$ docker-compose stop ## Stop all containers 
$ docker-compose stop web ## Stop single container

restart – Restart containers of the services defined in config file

$ docker-compose restart ## Restart all containers 
$ docker-compose restart web ## Restart single container

pause – Pause running containers for the services defined in config file.

$ docker-compose pause ## Pause all containers 
$ docker-compose pause web ## Pause single container

unpause – Start paused containers for the services defined in config file.

$ docker-compose pause ## Start all paused containers 
$ docker-compose pause web ## Start single paused container

rm – This will remove stopped containers for the services defined in config file.

$ docker-compose rm ## Start all paused containers 
$ docker-compose pause web ## Start single paused container
Picture of mike

mike

I started my IT career in 2000 as an Oracle DBA/Apps DBA. The first few years were tough (<$100/month), with very little growth. In 2004, I moved to the UK. After working really hard, I landed a job that paid me £2700 per month. In February 2005, I saw a job that was £450 per day, which was nearly 4 times of my then salary.