How to get started using Docker correctly

December 3, 2019

Lang: cs en de es

Docker has proliferated. It's good to learn it (for a Linux guy it will be a piece of cake). But it is also necessary to understand it and use it correctly. It will also be good to know some advanced features if you are building custom Docker containers or solving Docker performance on a Mac or Windows.

Docker has proliferated. It's good to learn it (for a Linux guy it will be a piece of cake). But it is also necessary to understand it and use it correctly. It will also be good to know some advanced features, if you're building your own Docker containers or solving Docker performance on a Mac or Windows.

This article is a guide or tutorial on how to get started with Docker. It describes how Docker works and how to use it. Tutorials on specific uses will follow later.
Anyone who needs to get an application up and running in Docker and is not interested in training can arrange for individual collaboration.

Docker is one of the visualization technologies, specifically container virtualization. It is a virtualization that takes advantage of the capabilities of the Linux kernel, which is why Docker is primarily designed for Linux. But in various ways it can also be made to run on Windows and Mac OS X, and often is.

An application in Docker runs as a system process that shares memory and the system kernel. Because the kernel is shared, the application only has to use the features that the kernel has. But for most common applications, no special or new features are needed.

Specifically, docker uses the following Linux kernel features: cgroups and namespaces.

What is Docker good for?

You can run and develop applications without Docker. So Docker is not necessary, but there are a few typical uses for which Docker deployment is useful.

  • Testing
  • Command-line applications - the result is a complete working application. It can also be an application that runs using wine.
  • Stateless applications.
  • State applications - need to store data separately using volumes.
  • Development - Vagrant is also used for this, but Docker has been the most popular. (Vagrant is a simple wrapper around Virtualbox's CLI interface)
  • Open operation - the environment as you need it.
  • HA, scaling - solution with orchestration tool e.g. Kubernetes.
  • Testing, fast migration to new software versions. For example, upgrading PHP.
  • Isolving the application.
  • Migration.
  • Quick start.

As I write, Docker has made a lot of inroads, especially with web applications. But for some uses it is better suited to, for example, Virtualbox. Also, for Mac OS X and Windows use, VirtualBox would be better, as non-native Docker use has performance issues.
For the experienced Linux admin will have no problem preparing a reproducible environment in VirtualBox using, for example Ansible

The environment in Docker runs in isolation, so the application running in it cannot interfere with other applications. It is also possible to create a unique environment for each application. It is possible to change the environment quickly, for example, to upgrade to new versions or to test the application on different versions of libraries.

Virtualization

Learn about the types of virtualization and the specific technologies that implement virtualization in this video:

Docker installation

First you need to install Docker if you have a Linux distribution of Mageia version 6 or 7, Docker is in its repositories. And so it downloads the following command to install Docker:

urpmi docker
In order to manage docker as a normal user you need to join the docker group.

Docker on Debian

Docker is not included in the Debian Linux distribution. Therefore, we need to add the official Docker repository and then we can install Docker.

First, we add the certificates of the repository:

curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -
 

Create a repository configuration file:
echo "deb https://download.docker.com/linux/debian $(lsb_release -cs) stable">> /etc/apt/sources.list.d/docker.list

Install Docker:
apt-get update && apt-get install docker-ce

This command will tell you if Docker is running:
systemctl status docker

Source - instructions in English: how to install and use docker on debian 9 . I've already done this on Debian 10.s

Impressions

In order to get to grips with a new technology you need to explain the basic concepts.

Image

An image is what represents a prepared environment without local modifications. From a programmer's point of view it could be called a class.

Container

A container can be compared to an instance of an Object. It is a running environment created from an image and already contains the data or local changes it created.

In virtualization, the relationship between image and virtual machine is typically 1:1. In Docker, however, you can run as many containers as you want from a single image.

Docker compose

Docer-compose is a tool for defining how multiple Docker containers run, how they should be set up, and how they should be linked. The docker-compose configuration is written in a file that is in YAML format. It contains information about which containers are to be launched from which images, which volumes are to be connected, and how they are to be connected by a virtual network.

Kubernetes

Kubernetes is an orchestration tool for docker and other similar technologies. This tool controls and manages containers so that they run as required. It automatically turns them on and off etc..

Docker swarm

Docker swarm is orchestration tool from Docker, currently Kubernetes is more widely used.

Docker usage commands

This command will download the data and build a sample docker image for you to try out.

docker run hello-world

Listing of running containers:

docker ps
You can also see other information about the containers, such as which ports are mapped from the local machine to which in the container.

Listing of existing panels:

docker images

In the Dockerfile you can define your own configuration for creating the image.
For example

FROM php:7.2.18-cli-alpine3.9
RUN php -v
CMD php -v

Build the image according to your Dockerfile and name it alpine-php72:
docker build -t alpine-php72 .alpine-php72

This command will launch the image.
 docker run alpine-php72
You may notice that when the image is built, the PHP version is printed and then when the image is run.

You can search for prebuilt containers using the web interface https://hub.docker.com/ or from the console using the command:

docker search NAME

This is how you start the console, that is, the bash interpreter in the container with the ID e286a85fdcac:

docker exec -it e286a85fdcac /bin/bash

This will start the container from image web and map local port 8080 to port 80 of the container:

docker run -ditp 8080:80 web
So what is running in the container on port 80 is available at localhost:8080.

Running the container from the docketette-web-alpine image, which we also name www and if a container with that name already exists, it is deleted and a clean one is created again.

docker run -it --rm --name www -p 80:80 -p 9001:9001 dockette-web-alpine

Importance of switches

-t Allocate and pseudo-tty
-i Keep STDIN open even if not attached
-d will detach our terminal
-P will publish all exposed ports to random ports and finally --name corresponds to a name we want to give. Now we can see the ports by running the docker port [CONTAINER] command

Copy, Add

When you give the COPY command in a Dockerfile, it copies the local file to the image. This is good for uploading configuration or other files that are needed in the container.

Copy the virtualhost configuration for the apache web server:

COPY site.conf /etc/apache2/sites-available/000-default.conf

The ADD command works almost the same way.

Volumes

Volumes is a way to get local data into a container so you can work on it at the same time. So it's ideal for development where you edit files locally and the container has access to them so you can run them right away.

An example where you can try using volumes and verify in the container that what you create will be visible in both places:

docker run -v /root/container_dir:/root/host -i -t debian /bin/bash

Manually create a volume. The location is on the disk according to the docker configuration.

docker volume create my-vol

List of available volumes: docker volume ls

Display the details of a given volume: docker volume inspect my-vol

Starting a docker container with a Mageia 7 distribution and attaching the created volume to /srv and running the shell in the container: docker run --mount source=my-vol,destination=/srv -it mageia7 /bin/bash

If you're solving a performance problem volumes on Mac or Windows is an option to have most things right in the docker. Or have everything in the docker and access its data. If you have a bigger performance problem there is an option to choose another type of virtualization such as Virtualbox.
But things around development and devops are already for personal consultation.

More on docker volumes.

Hints

Only one thing belongs in a container.

Docker video first use

This video explains the basic concepts and commands and shows you how to use docker for the first time.

Advanced Using Docker

You can define environment variables

 ENV PHP_CLI_DIR=/etc/php/7.3/cli

Copy the file to the running container:

docker cp foo.txt mycontainer:/foo.txt

Linking containers. Start a container with a database and then a container with a project that will connect to the database. docker run --name somemysql -e MYSQL_ROOT_PASSWORD=pass1234 -d mysql docker run --name someproject --link somemysql:mysql -d project - MYSQL_DATABASE=dbname

If you need to load some data into DB once you can use one of these commands:

cat temp.sql | pv | docker exec -i mysql.master /bin/mysql --init-command="SET autocommit=0;"
cat dump.sql | docker-compose exec -T  mysql -u  -p 

When it comes to database initialization, the normal MariaDB container expects an initialization SQL file in /docker-entrypoint-initdb.d/dump.sql So, for example, with the v parameter (for volume) you can post-fix the connection of the initialization SQL file to the docker container.

-v./dump.sql:/docker-entrypoint-initdb.d/dump.sql

The logs for each container are here: /var/lib/docker/containers/

If the Docker container is running in the background (typical for a server) this is how you connect to STDOU:

 docker logs -f f6010bf5a375

Videa docker

Developing a PHP/Nette3 web application in Docker

Here is a video-live stream showing the use of Docker in web application development:

Docker compose

In my Linux distribution (Mageia 6), some collision made me unable to install docker-compose (I guess due to Docker over-development), So I downloaded docker-compose manually and integrated it into the system.
Command to download, save and set permissions: curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" \ -o /usr/local/bin/docker-compose chmod +x /usr/local/bin/docker-compose ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
Learn more about installing docker-compose.

Configuring docker compose

The configuration for docker-compose is written to the docker-compose.yml file. There you can specify which images to start which containers from. How volumes and ports should be mapped and what other configuration values should be passed to them.

This configuration specifies that the Dockerfile is in the same directory. It specifies the name of the source container and what the image will be called. The port mapping is specified. The variable NETTE_DEBUG=1 is an environment variable that we can use to load a different project configuration in PHP.

version: '3.6'
  
services:

  web:
    build: ./
    image: linuxserveradmin
    container_name: linuxserveradmin
    environment:
     - NETTE_DEBUG=1
    volumes:
      - .:/srv

    ports:
      - 8081:80

Run what you have configured in the docker-compose file in one go with the command:

docker-compose up

Delete

List all docker containers:

docker inspect -f '{{ .Mounts }}' containerid
Delete image:
docker rmi hello-world
To delete all docker images, use the following command:
docker rmi $(docker images -a -q) -f
This command deletes all containers, networks, images, and built caches: docker system prune -a

Cache

Docker caches data and then uses that cached data. For example, the image has cached information about packages in the repository. But when an update comes in, the old packages disappear, and when your layer builds on that cached data, it tries to download what doesn't exist anymore. For this, it's a good idea to have an apt-get install before every apt-get update command that updates the repository information.
Then you write it to the Dockerfile as follows:

RUN apt-get -qq update && apt-get install -y ........
Those switches say not to use interactive mode, because docker runs it automatically.

Rebuild image without using cache:

docker-compose build --no-cache

Automatic container startup

Disable automatic container startup after a reboot:

docker update --restart=no 491dfa4b274a

Setting the container to start automatically when the computer boots:
docker update --restart=yes 491dfa4b274a

Or you need to run the container with the parameter
 --restart always

For the same functionality in the docker-compose YAML file, specify:
    restart:
      always

Docker and BTRFS

I use the BTRFS filesystem, which Docker takes advantage of. Again, this is an example of why to use Linux and why to use the BTRFS filesystem and other good modern technologies that make work more efficient.
Listing the layers that are in BTRFS i.e. subvolume, show the command:

 btrfs subvolume list /
And lo and behold you'll see a bunch of subvolumes that Docker has created.

GUI applications in Docker

You can also run GUI applications in Docker. Here I will show you the basic way to run a GUI application eyes.

Create the following Dockerfile:

FROM centos
RUN yum install -y xeyes
CMD ["/usr/bin/xeyes"]

Build the image:
docker build -t gui-app .
Start the image container and the program in it:
docker run --net=host --env="DISPLAY" --volume="$HOME/.Xauthority:/root/.Xauthority:rw" gui-app

Running Adobe Acrobad Reader on Linux without installation - i.e. using docker:

docker run --net=host --env="DISPLAY" --volume="$HOME/.Xauthority:/root/.Xauthority:rw" -v $HOME:/srv mgor/acroread

Docker - Web Application Development

If you are developing web applications, it is a good idea to have some default docker image. I have prepared such an image for you. For PHP8 I have prepared docker image with PHP8. There are also other images prepared by me, with older versions of PHP e.g. version 5.6, 7.4 etc.

If you program web applications in Nette or Symfony PHP frameworks, I have prepared a docker image with configuration for these frameworks. So you just need to start the docker container and attach the application source. Here is docker image for Nette and here is docker image for Symfony.

Then all you need to do is create a docker-compose.yml file to start the development environment. with the following content/configuration:
version: '3.6'

services:

  web:
    image: debian-apache-php8.0-nette
    container_name: linuxserveradmin_web
    environment:
     - NETTE_DEBUG=1
    working_dir: /var/www/html

    volumes:
      - .:/var/www/html
      - ./docker/development/web/msmtprc:/etc/msmtprc
    ports:
      - 8081:80
      - 8082:443
The msmtprc file contains the configuration for sending mail through an external SMTP server. And the docker image is configured so that the mail() function in PHP sends mail automatically over the configured external mail server. See the article for details how to configure sending mail in PHP in docker.

Training

For companies that want to streamline their operations, I can recommend Docker. And through Docker training and subsequent collaboration together we will bring Docker into the company.

Docker Hub

Video on how to create a docker image and upload it to Docker Hub:

For developing projects in PHP in the Nette framework. or Symfony you can use docker image created by me which contain everything you need: apache, composer, PHP with modules. Here is docker image for Symfony with PHP7.4 and docker image for Nette with PHP7.4. I also use these docker images on my web application development training courses.

Windows and Docker

You can run Docker on Windows too, but installation is more complicated and performance is weaker. There are more ways to get Docker to work on Windows:

Docker Tutorials

Links and resources

docker
docker hub
docs docker

Other Resources

https://dckr.cz/
docker-800-days-pote
vojta.kalcik.cz navody:docker
docker mariadb
start-containers-automatically/
proc-powered-docker/

Nette in docker

docker nette
nette is php 7.3 - November 2019 myself, I am already running Nette on PHP 7.4

Articles on a similar topic

VMware vs Proxmox: performance comparison
GitLab CI/CD: test automation and application deployment
Migrating VPS from VMware to Proxmox
VMware licensing change
Running Microsoft SQL Server on Linux
Backup: the Proxmox Backup Server
Linux as a router and firewall
How to upload a docker image to the Docker Registry
Linux: logical volume management
Linux Software RAID
Running a web application behind a proxy
Mailbox migration
Docker multistage build
Backing up your data by turning on your computer
Podman
Importing Windows into Proxmox virtualization
Docker and PHP mail
Proxmox virtualization
Docker and Cron
Lenovo ThinkPad X1 Carbon: LTE modem EM7544 commissioning
Yocto Project: Build custom operating system for embedded devices
Preparing a Linux server to run a web application in Python
How to address poor file share performance in Docker
Installing Linux on a dedicated HPE ProLiant DL320e server
How to stress test a web application
Why use the JFS filesystem
How to boot from a 4TB drive with GTP using UEFI
Btrfs file system
Raspberry PI
WINE - running Windous programs under Linux
GNU/Linux operating system

Newsletter

If you are interested in receiving occasional news by email.
You can register by filling in your email news subscription.


+