Docker In A Nutshell

At a Glance

Docker In A Nutshell is a quick guide to the most commonly used Docker commands in everyday use.

Using git you can share your source code. Using Docker you can share your infrastructure. For this reason, Docker is a must-have tool for every development team.

Docker is written in the Go (golang) programming language.

Docker looks like but it is not a virtualization solution. It is a containerization solution. Virtualization solutions provide hardware Virtualization. Docker provides Operation System Virtualization.

With a virtualization solution (eg Virtualbox), you use an image to create a virtual machine. With Docker, you use an image to create a container. Consider an image as a tar file, even though it is not exactly. A container is a live instance of an image.

On the contrary to a classic virtualization solution, Docker uses your computer resources (kernel, memory, etc) much smarter to run a container. This is the reason why Docker is extremely fast and lightweight.

Docker can run inside a Virtual machine.

You can easily create and share your images with your team using an online repository. The most used is the official Docker Hub. Docker Hub is for Docker what Github (or Bitbucket to Gitlab) is for git.

You can also Play with Docker even without install it, using an interactive Docker playground.

Docker is the ideal way to create and share development environments. However, in many cases it can be used to deploy production apps.

Examples of use

1. Different software versions

Let’s say you have an old project with Apache and PHP5 and another project with Nginx and PHP7. There are some ways to have multiple PHP versions, but it is not so easy and the most important it may harm your workstation.

With Docker, it’s a “piece of cake” to create a container with Apache and PHP5 and another with Nginx and PHP7. Moreover, it is possible to find ready images for every configuration (usually in Docker Hub)!

2. Software packages isolation

Some applications are difficult to set up or they may add tones of packages in your workstation. pgadmin4 (the famous Python management tool for Postgresql) is an example.

Instead of setup pgAdmin in your workstation, you can use Docker. There are many ready to use images for pgAdmin.

In this way the whole application is isolated from your workstation!

3. Easily mount your workstation

It is very easy to mount a folder with source code on your workstation computer from a running Docker container.

For example, Apache document root /var/www/html inside the container can be mounted to /work/my-projects on your workstation. See below “Create container from image”.

So, you can still use your favorite IDE and all your favorite development tools from the comfort of your computer.

Docker Is Awesome! – Why?

  • Extremely Fast and Lightweight
  • Cross-platform (Linux basically and others)
  • It makes your “infrastructure” shareable with your team
  • Great community and a lot of resources
  • Free and Open Source (even though an Enterprise version exists)

Setup

Detailed instructions for every Operation System are available here.

To setup on Debian/Ubuntu:

Add GPG key:

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

Add Docker repo:

sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

Install Docker Engine:

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

Add your user to docker group

Avoid running Docker as root user (using sudo). It is strongly recommended to add your user to docker group to run docker as common user:

sudo usermod -aG docker your-username

Log out and log in again for the changes to take effect.

Finally, install Docker Compose – instructions here.

Where are Docker files stored in my computer?

The Docker Root Dir on Linux is

/var/lib/docker
pontikis@athena:~$ sudo ls -la /var/lib/docker/
total 96
drwx--x--x  14 root root  4096 Οκτ  10 08:21 .
drwxr-xr-x  74 root root  4096 Αυγ  26 15:20 ..
drwx------   2 root root  4096 Αυγ  26 15:20 builder
drwx--x--x   4 root root  4096 Αυγ  26 15:20 buildkit
drwx------   9 root root  4096 Οκτ   9 22:30 containers
drwx------   3 root root  4096 Αυγ  26 15:20 image
drwxr-x---   3 root root  4096 Αυγ  26 15:20 network
drwx------ 308 root root 40960 Οκτ  10 08:21 overlay2
drwx------   4 root root  4096 Αυγ  26 15:20 plugins
drwx------   2 root root  4096 Οκτ  10 08:21 runtimes
drwx------   2 root root  4096 Αυγ  26 15:20 swarm
drwx------   2 root root  4096 Οκτ  10 08:21 tmp
drwx------   2 root root  4096 Αυγ  26 15:20 trust
drwx------  11 root root  4096 Αυγ  26 21:42 volumes

You can find Docker Root Dir (in any O/S) using docker info (see below).

Pull image

Use docker pull to get an image from Docker Hub.

Additionally, you can update an existing image to its latest version with the same command.

For example, to download the latest Debian stable image from the official Debian Docker Hub repository, use

docker pull debian

Read details at https://hub.docker.com/_/debian.

Actually, you will find Docker images in Docker Hub for almost any Operating System, Server application, Database, etc.

Create container from image

Use docker run to start a container from an existing image

docker run debian

The container will get a random name. To give it the name you want (recommended), use:

docker run debian --name "my-debian"

Start a container and mount Apache document root to a local folder in your workstation:

    docker run \
    --name "my-debian" \
    -it \
    --mount type=bind,source=/work/my-projects,target=/var/www/html \
    debian

Start/Stop container

Use container name (recommended) or container ID to start or stop a container.

Use docker start

docker start my-debian

Use docker stop

docker stop my-debian

Connect container

Use docker exec to get access to the command line of the running container:

docker exec -it my-debian /bin/bash

Now, you have root access to the running container. It looks like an “SSH connection” (of course it is not).

List running containers

Use docker ps to get a list of running containers

docker ps

result (in my case)

pontikis@athena:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

List all containers

Use docker ps to get a list of all containers (running or not)

docker ps -a

result (in my case)

pontikis@athena:~$ docker ps -a
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS                     PORTS               NAMES
134e70726d60        portainer/portainer   "/portainer"             14 hours ago        Exited (2) 13 hours ago                        portainer
0fc628dc70cc        dpage/pgadmin4        "/entrypoint.sh"         14 hours ago        Exited (0) 14 hours ago                        pgadmin4
2f8048acc456        pontikis/ansible      "/bin/bash"              3 weeks ago         Exited (0) 17 hours ago                        ansible
4bb7f96275c9        medisign/medisign     "/bin/sh -c '/usr/lo…"   6 weeks ago         Exited (137) 2 days ago                        medisign
d80004852285        opexsa/opex           "/bin/sh -c '/usr/lo…"   6 weeks ago         Exited (137) 6 weeks ago                       opex
3e890168b7cd        pontikis/content      "/bin/sh -c '/usr/lo…"   6 weeks ago         Exited (0) 6 weeks ago                         content
254a777d9cd9        pontikis/mariadb      "/bin/sh -c '/usr/lo…"   6 weeks ago         Exited (137) 6 weeks ago                       mariadb

List images

Use docker image to get a list of all images in your docker setup

docker image ls

In my case

pontikis@athena:~$ docker image ls
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
pontikis/ansible      latest              5c760d2ba4e2        3 weeks ago         302MB
dpage/pgadmin4        latest              d316f8b2a079        3 weeks ago         221MB
rclone/rclone         latest              58fdc00acdcc        5 weeks ago         45.3MB
pontikis/mariadb      latest              af4cf67988fc        7 weeks ago         2.4GB
pontikis/content      latest              32496aa97e8b        7 weeks ago         834MB
ubuntu                focal               4e2eef94cd6b        7 weeks ago         73.9MB
dpage/pgadmin4        <none>              ae36b8785e03        7 weeks ago         220MB
medisign/medisign     latest              ee0f61de3382        2 months ago        1.86GB
opexsa/opex           latest              5317398e33a4        2 months ago        1.11GB
portainer/portainer   latest              62771b0b9b09        2 months ago        79.1MB
d2dyno/rclone-gui     latest              083f75b8ae9d        6 months ago        41.9MB

Delete container

Use docker rm to delete a container

docker rm my-debian

Delete image

Use docker rmi to delete an image

docker rmi debian

Save container as an image

The easiest way to save a container as an image after you make changes is to use docker commit and docker save.

For example to save your container my-container as a new image in your docker setup and apply the tag bkp1, use

docker commit my-container username/my-image:bkp1

and

docker save username/my-image:bkp1 > my-image.tar

Load an image from backup

Use docker load to restore an image from backup file

docker load < my-image.tar

Push image

You can push an image to Docker Hub (or any other repository). Then your team can pull this image. So, all team members are working with the same infrastructure. This is the meaning of the Docker!

You need an account on Docker Hub. Find pricing plans here. There is also a Free Plan with unlimited public repositories and one private.

Use docker login, docker push and docker logout

Full example below:

pontikis@athena:~$docker run -it busybox
make a change - eg mkdir TEST_DIR and exit
 
 
pontikis@athena:~$ docker ps -a
CONTAINER ID        IMAGE                 COMMAND             CREATED             STATUS                     PORTS                    NAMES
d8ffaa393e92        busybox               "sh"                2 minutes ago       Exited (0) 4 seconds ago                            elated_haibt
47bd22924237        busybox               "/bin/bash"         2 minutes ago       Created                                             flamboyant_taussig
a2e69289f8ae        pontikis/mosaas       "/bin/bash"         39 minutes ago      Exited (0) 2 minutes ago                            ecstatic_varahamihira
4402de60a228        portainer/portainer   "/portainer"        43 minutes ago      Up 43 minutes              0.0.0.0:9000->9000/tcp   eloquent_wiles
 
pontikis@athena:~$ docker commit d8ffaa393e92 pontikis/busybox
sha256:27934e6fdd24a1cf34c67d76aa71feb01087dbb071464a3d51450e5a0dd98cf8
 
pontikis@athena:~$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you dont have a Docker ID, head over to https://hub.docker.com to create one.
Username: pontikis
Password: 
WARNING! Your password will be stored unencrypted in /home/pontikis/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
 
Login Succeeded
 
pontikis@athena:~$ docker push pontikis/busybox
The push refers to repository [docker.io/pontikis/busybox]
2df8244b5b04: Pushed 
8a788232037e: Mounted from library/busybox 
latest: digest: sha256:6c48a209531e8f7089e245f205ecce445742979239954fd373ba095b915262e4 size: 734
 
pontikis@athena:~$ docker logout
Removing login credentials for https://index.docker.io/v1/

Build an image

You can create custom images using docker build. For example, you can start from a base image (eg Debian) and the install custom software according to your needs (php, Apache, MySQL or whatever).

docker build

In future posts I will provide examples.

Where I can find Dockerfiles?

Transfer files

Use docker cp

From local file system to running container:

docker cp /store/test.conf my-container:/etc/test.conf

From running container to local file system:

docker cp my-container:/etc/test.conf /store/test.conf

Docker info

Use docker info to get information about your docker environment

docker info

Docker inspect

Use docker inspect to get information for a specific docker object (eg image, container, network, etc)

docker inspect my-container

Docker GUI

Portainer

Portainer is a great cross-platform web-based Docker front-end, running as a Docker container! Find installation instructions here.

At a glance (on Linux with Docker installed):

docker volume create portainer_data
docker run -d -p 8000:8000 -p 9000:9000 --name=portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce

Then access Portainer at http://localhost:9000

Portainer
Portainer

Official Docker Desktop for Mac

See https://docs.docker.com/docker-for-mac/

Official Docker Desktop for Windows

See https://docs.docker.com/docker-for-windows/

DockStation

Desktop UI (Linux/Mac/Windows)

See https://dockstation.io/

LazyDocker

A simple terminal UI for both docker and docker-compose, written in Go (Linux/Mac/Windows)

See https://github.com/jesseduffield/lazydocker

docui

Terminal UI Client for Docker Written in Go (Mac/Linux)

See https://github.com/skanehira/docui

Related posts

You may also be interested in

Docker In A Nutshell – Great tutorials