What is Docker?

Docker is an open source software platform that is used for building, deploying and managing applications quickly. The applications are stored as packages in containers that have their own software, libraries and configuration files. The containers operate independently but can communicate with each other via well-defined channels.

Docker has different tools including

  • Dockerfile – Text file containing instructions on how to create the docker image
  • Docker Image – Contain the source code for the executable application
  • Docker Containers – live running instances of docker images
  • Docker Daemon – software to create and managing docker images for use of the clients commands.
  • Docker Registry – open source storage/repository for the docker images

The advantage of Docker is that one can reuse container, there is an open registry for shared libraries and it is lightweight. Docker makes it easy to build and run distributed microservices architecture like Amazon or Uber.

What is Docker Compose?

Docker Compose is a tool for running an application in multiple containers meaning it can connect different containers to function as a single service. It creates a YAML file that specifies services included in the application and deploy and run the start-up process of all containers with a single command.

Compose features include;

  • Multiple isolated environments on a single host
  • Define System volumes
  • Configure service dependencies
  • Quick and Easy configuration by use of YAML files

In this guide I’ll show you how to;

  • Install Docker from a repository on KDE Neon|Kubuntu
  • Install Docker Compose on KDE Neon|Kubuntu
  • Configure Non-Root user access
  • Create a Dockerfile
  • Run an application on Docker Compose

Install Docker on KDE Neon/Kubuntu

First update the package using apt command.

sudo apt update

Screenshot containing the output:

This updates the package index. It will also give you packages that might need an upgrade. You can use pkcon update to upgrade.

Setting up Repository on KDE Neon/Kubuntu

Install packages to allow apt to use a repository over HTTPS.

sudo apt-get install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

Before installing Docker on a new machine. You need to set up the repository so as to install docket from the repository.

Import the Docker official GPG Key

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

Use the following command to setup the stable version

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

This will automatically choose a version for you

Install Docker Engine on KDE Neon/Kubuntu

Use the apt to update the package index.

sudo apt update

Install the Dcker Engine with the following command.

sudo apt-get install docker-ce docker-ce-cli containerd.io

It will show you the size of the disk that will be used and ask for a confirmation, Press Y to continue

To verify Docker engine is running use,

sudo docker run hello-world

This will pull the file from the Docker Hub as it is not available locally. You should see a message as below and your docker is installed and running correctly.

Installing Docker Compose on KDE Neon/Kubuntu

To install Docker compose, You should install Docker Engine first then continue to install compose.

Use the apt to update the package index

sudo apt update

Then use the following command to install Docker Compose

VER=$(curl -s https://api.github.com/repos/docker/compose/releases/latest|grep tag_name | cut -d '"' -f 4)
sudo curl -L "https://github.com/docker/compose/releases/download/${VER}/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

This will download the latest stable release of docker compose

Add execute permissions to the binary downloaded

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

This applies the executable permissions to the binary. Test the installation

$ docker-compose --version
Docker Compose version v2.2.3

The version will be shown as below

Configuring Docker Compose for Non-root User access

To run docker commands you must append the sudo at the beginning of each command.

Lets say I want to run docker run hello-world without appending sudo. I will get an error permission denied like shown,

To avoid this, You can add your name to the docker group.

sudo usermod -aG docker $USER
newgrp docker

The $USER contains the name of the current user. To confirm you are in the group use,

id -nG

It will output your name which confirms you’re a member of a group.

So if I am to run a docker command without sudo it will not deny me access as shown below.

Getting started with Docker Compose

The first thing is to first build an application running on docker compose. we will use python which is already provided by docker images hence no need to install it.

Create a project directory using

mkdir dctest

Then change to that directory using cd dctest

Create a file app.py by using nano app.py and paste this in and save your file by using ctrl+x then y then press enter

import time

import redis
from flask import Flask

app = Flask(__name__)
cache = redis.Redis(host='redis', port=6379)

def get_hit_count():
    retries = 5
    while True:
        try:
            return cache.incr('hits')
        except redis.exceptions.ConnectionError as exc:
            if retries == 0:
                raise exc
            retries -= 1
            time.sleep(0.5)

@app.route('/')
def hello():
    count = get_hit_count()
    return 'Hello World! I have been seen {} times.\n'.format(count)

This will create a python application that will use the flask module

Create another file in the project directory named requirements.txt by typing nano requirements.txt and paste this in then save your file by using ctrl+x then y then press enter

flask
redis

This lets you know which modules are required to run the docker application

Create a Dockerfile

A Dockerfile is a text file that contains commands used to build an image.

Create a file in project directory called Dockerfile and paste this in and save your work.

# syntax=docker/dockerfile:1
FROM python:3.7-alpine
WORKDIR /code
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0
RUN apk add --no-cache gcc musl-dev linux-headers
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
EXPOSE 5000
COPY . .
CMD ["flask", "run"]

This lets you know its running the alpine image it also specifies the environment variables and shows that it is copying from the requirements.txt file and install the Python dependencies. EXPOSE is used to specify the port on which the container listens at runtime.

Create another file named docker-compose.yml in your project directory and paste this in and save it by using ctrl+x then y then press enter

version: "3.9"
services:
  web:
    build: .
    ports:
      - "5000:5000"
  redis:
    image: "redis:alpine"

This file defines two services,

web which uses the image specified in the Dockerfile redis which pulls the image directly from the Docker Hub Registry.

Use ls -l to list the files you have created

Running the application using Docker Compose

Build and run the app using docker-compose up.

Go to port where the server is listening to in your browser and you will see the application run

Refresh the page and the number will increment.

Switch to another terminal, In my case I will just open a new tab in the Konsole.

Use docker image ls to list the images running.

Docker Compose Commands

Change to the project directory again by typing cd dctest in the switched terminal.

You can use docker-compose ps which lists the containers, their states and the port they are listening to

Using docker-compose stop exits the containers

And if you check from the previous terminal, the containers have stopped running.

Use docker-compose-start to start the containers

Use docker-compose down -v to bring everything down. This removes the containers.

If we do docker-compose ps the containers will not be listed.

Conclusion

This guide has shown you how to install Docker and Docker compose. It has also shown you the basics of how compose works like having non-root user access while using docker and docker compose commands. Also you have been able to run multiple containers using the same Dockerfile and one command.

Related articles:

LEAVE A REPLY

Please enter your comment!
Please enter your name here