What is Docker?

Docker is defined as a software platform that allows you to build, test, manage and deploy applications. Thus, Docker is also known as an open-source containerized platform; it allows developers to package applications into containers. These containers are standardized units containing everything an application needs to run including source code, libraries, and dependencies required to run code. Thus, with Docker, you can confidently deploy, scale, and run applications in any environment while being assured of reliability and lower costs.

Use Cases of Docker

  • Application Isolation: Docker makes it possible to build applications with infrastructure isolated from the computing environment. Thus, you can run and build multiple applications without interrupting app versions or other factors in the development process.
  • Code Pipeline Management: Docker always provides an environment for an application from the development stage to production.
  • Simplifies Configuration: This is the primary use case of Docker. It allows you to put your environment and configurations into a code and you can deploy it freely. Keep in mind that the same configs that docker provides can be used on a couple of environments.
  • Multi-tenancy: This is a key use case for Docker since it helps in isolating containers for different users and tenants. Also, you can create isolated environments that will run several instances of application tiers for every tenant.

Docker Compose

One application can consist of a couple of containers that run different services. Thus, Docker Compose is a tool used to run or orchestrate multi-containers as a single service making it easy to start and manage containers. Compose allows you to configure an application’s services by the use of a YAML file. From your configurations, you can use one command to create and start all services. Also, if you have an application that requires both MySQL and NGNIX, it is possible to create a single file that will start both containers as a service instead of separately. With Docker Compose, you can start, stop and rebuild services, see the log output of running services and view the status of services.

Benefits of Docker Compose

  • Fast and easy configuration
  • Provides single-host deployment: It is possible to run everything on a single piece of hardware.
  • Increased productivity: Less time is taken to perform tasks.
  • Secure internal communication: Services cannot be accessed externally.

Installation of Docker CE on Solus

Docker Community Edition(CE) is a community-based version of Docker. It is free of cost and provides an outstanding experience with container-based applications. Docker CE is favorable for anyone including those who want to get started with Docker. To install Docker CE on Solus, start by upgrading your Solus system repositories using the below command on your terminal.

sudo eopkg upgrade

Install Docker CE using this command.

sudo eopkg install docker 

After installing Docker CE, start and enable the Docker Daemon.

sudo systemctl enable --now docker

Check the version of the Docker Community Edition package to confirm it was successfully installed on Solus.

$ docker version
Docker Engine - Community
 Version:           20.10.17
 API version:       1.41
 Go version:        go1.18.2
 Git commit:        a89b84221c8560e7a3dee2a653353429e7628424
 Built:             Wed Jun  8 13:51:39 2022
 OS/Arch:           linux/amd64
 Context:           default

Configuring Docker for Non-Root User

To enable your Solus user account to access and manage docker without administrative privileges, we will add the account to the Docker group.

sudo usermod -aG docker $USER
newgrp docker

Verify whether your Solus account is in the Docker Group using this command:

id -nG

For instance, the name for my Solus user account is technixleo. After checking the Docker group, I got these results.

$ id -nG
technixleo dialout audio video cdrom lpadmin sudo scanner plugdev users fuse docker

I can now run the Docker commands without Sudo.

$ docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

Install Docker Compose on Solus

To install Docker Compose, we will need curl and wget software. Install them on the Solus system using the below command.

sudo eopkg install curl wget

After the curl and wget have been installed, download the latest release of Docker Compose.

curl -s https://api.github.com/repos/docker/compose/releases/latest | grep browser_download_url  | grep docker-compose-linux-x86_64 | cut -d '"' -f 4 | wget -qi -

Make the Docker Compose file binary executable.

chmod +x docker-compose-linux-x86_64

Then move the file to a PATH .For instance /usr/bin/docker-compose.

sudo mv docker-compose-linux-x86_64 /usr/bin/docker-compose

Verify the Docker Compose version.

$ docker-compose version
Docker Compose version v2.6.1

Creating a Dockerfile on Solus

A Dockerfile is a text file containing commands or instructions used to build a Docker image. To keep everything organized, it is recommended that we should create a docker file directory. Hence, we will start by creating a new directory.

mkdir solus_docker

Change the directory to create the Dockerfile inside the new directory.

cd solus_docker

Create Dockerfile inside the directory.

nano Dockerfile

Inside the Docker file, copy-paste this text. The below text defines the base image which is ubuntu:18.04. Save the file using ctrl +x followed by Y then Enter.

FROM ubuntu:22.04

RUN apt-get update && \
    apt-get install -y redis-server && \
    apt-get clean

EXPOSE 6379

CMD ["redis-server", "--protected-mode no"]

Run Single Container with Docker Run

Now that we have created a docker file above, we can continue and build the image. Then after building an image we can run a container. Use the docker build command to build the ubuntu:18.04 image.

 docker build . 

Let’s build another image with any name such as image0 using the docker build command.

docker build -t image0 .

Output on building image0:

 $ docker build -t image0 .
Sending build context to Docker daemon  2.048kB
Step 1/4 : FROM ubuntu:22.04
 ---> ad080923604a
Step 2/4 : RUN apt-get update &&  apt-get install -y redis-server &&     apt-get clean
 ---> Using cache
 ---> b118dc70b889
Step 3/4 : EXPOSE 6379
 ---> Using cache
 ---> 6aab88c6791c
Step 4/4 : CMD ["redis-server", "--protected-mode no"]
 ---> Using cache
 ---> 1bf1a261dc22
Successfully built 1bf1a261dc22
Successfully tagged image0:latest

We can now verify the images we have built using the below command.

 docker images

Here is the list of available images.

~/solus_docker $ docker images
REPOSITORY    TAG       IMAGE ID       CREATED              SIZE
image0        latest    1bf1a261dc22   About a minute ago   108MB
ubuntu        18.04     ad080923604a   3 weeks ago          63.1MB
hello-world   latest    feb5d9fea6a5   9 months ago         13.3kB

To run a container, use the Docker Run command. Note that containers are created from images with the use of the Docker Run command. In the command below, -d tells Docker to use the detached mode, followed by the host machine port number. While –name redis will specify the container name.Image0 is the name of the image used to run the container.

docker run -d -p 6379:6379 --name redis image0

The container will run as illustrated.

$ docker run -d -p 6379:6379 --name redis image0
3eea239d3b002b856039e47e0bcd4bb1f0bc4251443c685b3e30a89f6f54be63

Use this command to view a list of the running containers.

docker container ls -a

Results on the running containers.

CONTAINER ID   IMAGE         COMMAND                  CREATED          STATUS                     PORTS                                       NAMES
8cc14a6d8f07   image0        "redis-server '--pro…"   23 seconds ago   Up 22 seconds              0.0.0.0:6379->6379/tcp, :::6379->6379/tcp   redis
ac0d0f9685b0   hello-world   "/hello"                 7 minutes ago    Exited (0) 7 minutes ago                                               unruffled_heisenberg

Running Multiple Containers with Docker Compose

We will start by creating a docker-compose.yml file. Ensure you are inside the directory we created for our Docker files.

cd solus_docker

Then create a docker-compose.yml file.

nano docker-compose.yml

Inside the file, copy-paste this text that has container services.

version: '3'  
services:
  web:
    image: nginx:latest
    ports:
     - "8080:80"
    links:
     - php
  php:
    image: php:7-fpm

Now run multiple containers in the Docker Compose file, using the below command

docker-compose up -d

Here are the results after running web and PHP container services from the Docker Compose file.

~/solus_docker $ docker-compose up -d
[+] Running 17/17
 ⠿ web Pulled                                                                                                                             8.5s
   ⠿ f4407ba1f103 Pull complete                                                                                                           6.0s
   ⠿ 4a7307612456 Pull complete                                                                                                           6.1s
   ⠿ 935cecace2a0 Pull complete                                                                                                           6.2s
   ⠿ 8f46223e4234 Pull complete                                                                                                           6.3s
   ⠿ fe0ef4c895f5 Pull complete                                                                                                           6.4s
 ⠿ php Pulled                                                                                                                            13.5s
   ⠿ b85a868b505f Pull complete                                                                                                           3.7s
   ⠿ 78fdfd2598e0 Pull complete                                                                                                           3.8s
   ⠿ 26769c8659f4 Pull complete                                                                                                           9.4s
   ⠿ 0bd105fadbe3 Pull complete                                                                                                           9.5s
   ⠿ 2fadb39daff2 Pull complete                                                                                                           9.7s
   ⠿ acf0bff51100 Pull complete                                                                                                           9.8s
   ⠿ 402939ef1cc5 Pull complete                                                                                                          11.2s
   ⠿ 6886d213a86f Pull complete                                                                                                          11.2s
   ⠿ 89cb460f70a1 Pull complete                                                                                                          11.3s
   ⠿ 4fdf0d4fa324 Pull complete                                                                                                          11.4s
[+] Running 3/3
 ⠿ Network solus_docker_default  Created                                                                                                  0.1s
 ⠿ Container solus_docker-php-1  Started                                                                                                  3.5s
 ⠿ Container solus_docker-web-1  Started   

List the Containers Deployed from a Docker Compose File using the below command.

docker-compose ps

Output:

~/solus_docker $ docker-compose ps
NAME                 COMMAND                  SERVICE             STATUS              PORTS
solus_docker-php-1   "docker-php-entrypoi…"   php                 running             9000/tcp
solus_docker-web-1   "/docker-entrypoint.…"   web                 running             0.0.0.0:8080->80/tcp, :::8080->80/tcp

Docker Compose Commands Usage

In this section let’s cover some of the Docker Compose commands.

a) Create and Start containers

Run and start the container services using this command. When you are ready to run services, cd to the directory you created to access the docker-compose.yml file. Then run the docker-compose up command.

docker-compose up

Expected results:

[+] Running 2/2
 ⠿ Container solus_docker-php-1  Recreated                                                                                                0.2s
 ⠿ Container solus_docker-web-1  Recreated                                                                                                0.2s
Attaching to solus_docker-php-1, solus_docker-web-1
solus_docker-php-1  | [01-Jul-2022 13:39:22] NOTICE: fpm is running, pid 1
solus_docker-php-1  | [01-Jul-2022 13:39:22] NOTICE: ready to handle connections
solus_docker-web-1  | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
solus_docker-web-1  | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
solus_docker-web-1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
solus_docker-web-1  | 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
solus_docker-web-1  | 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
solus_docker-web-1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
solus_docker-web-1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
solus_docker-web-1  | /docker-entrypoint.sh: Configuration complete; ready for start up
solus_docker-web-1  | 2022/07/01 13:39:22 [notice] 1#1: using the "epoll" event method
solus_docker-web-1  | 2022/07/01 13:39:22 [notice] 1#1: nginx/1.23.0
solus_docker-web-1  | 2022/07/01 13:39:22 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
solus_docker-web-1  | 2022/07/01 13:39:22 [notice] 1#1: OS: Linux 5.15.50-216.current
solus_docker-web-1  | 2022/07/01 13:39:22 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
solus_docker-web-1  | 2022/07/01 13:39:22 [notice] 1#1: start worker processes
solus_docker-web-1  | 2022/07/01 13:39:22 [notice] 1#1: start worker process 31
solus_docker-web-1  | 2022/07/01 13:39:22 [notice] 1#1: start worker process 32

b) View Logs from all the running Containers.

docker-compose logs

Some of the logs in the Docker containers we created are as illustrated.

~/solus_docker $ docker-compose logs
solus_docker-php-1  | [01-Jul-2022 13:39:22] NOTICE: fpm is running, pid 1
solus_docker-php-1  | [01-Jul-2022 13:39:22] NOTICE: ready to handle connections
solus_docker-php-1  | [01-Jul-2022 13:48:26] NOTICE: Finishing ...
solus_docker-php-1  | [01-Jul-2022 13:48:26] NOTICE: exiting, bye-bye!
solus_docker-web-1  | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
solus_docker-web-1  | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
solus_docker-web-1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
solus_docker-web-1  | 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
solus_docker-web-1  | 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
solus_docker-web-1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
solus_docker-web-1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
solus_docker-web-1  | /docker-entrypoint.sh: Configuration complete; ready for start up
solus_docker-web-1  | 2022/07/01 13:39:22 [notice] 1#1: using the "epoll" event method
solus_docker-web-1  | 2022/07/01 13:39:22 [notice] 1#1: nginx/1.23.0
solus_docker-web-1  | 2022/07/01 13:39:22 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
solus_docker-web-1  | 2022/07/01 13:39:22 [notice] 1#1: OS: Linux 5.15.50-216.current
solus_docker-web-1  | 2022/07/01 13:39:22 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
solus_docker-web-1  | 2022/07/01 13:39:22 [notice] 1#1: start worker processes
solus_docker-web-1  | 2022/07/01 13:39:22 [notice] 1#1: start worker process 31
solus_docker-web-1  | 2022/07/01 13:39:22 [notice] 1#1: start worker process 32

c) Pull Images in a Docker Compose File command.

docker-compose pull

Here are the images pulled after entering the above command.

~/solus_docker $ docker-compose pull
[+] Running 2/2
 ⠿ web Pulled                                                                                                                             1.2s
 ⠿ php Pulled    

d) Restart all containers in the docker-compose file.

docker-compose restart

Restarted containers:

~/solus_docker $ docker-compose restart
[+] Running 2/2
 ⠿ Container solus_docker-php-1  Started                                                                                                  0.4s
 ⠿ Container solus_docker-web-1  Started  

e) Destroying or stopping containers with Docker Compose.

docker-compose stop

Stopped Containers:

~/solus_docker $ docker-compose stop
[+] Running 2/2
 ⠿ Container solus_docker-web-1  Stopped                                                                                                  0.2s
 ⠿ Container solus_docker-php-1  Stopped    

f) The below command is used to delete containers

docker-compose rm -f

Here are the removed containers after applying the above command.

~/solus_docker $ docker-compose rm -f
Going to remove solus_docker-web-1, solus_docker-php-1
[+] Running 2/0
 ⠿ Container solus_docker-php-1  Removed                                                                                                  0.0s
 ⠿ Container solus_docker-web-1  Removed    

Conclusion

Improved technology has made many companies leverage Docker containers. Docker and its tools, enhance the operational capability of DevOps and other Information Technology operations. Thus, it is important to learn more about Docker and Docker Compose. From this guide, you will be in a position to install and use Docker and Docker Compose on your Solus system.

More Guides:

LEAVE A REPLY

Please enter your comment!
Please enter your name here