How to get started with docker

Hi, I’ve been wanting to do this article on how to get started with docker for a while.

We appreciate you sharing this article on your social networks and/or leaving a comment.

Let’s get started!

1- What is docker?

There are people who consider docker to be a virtualization system like KVM, Vmware, etc. This is false. Docker is a container system like LXC but more advanced. It has better management of the raised instances and has a template management system.

That is to say, Docker is a system to abstract the application and make its ecosystem independent of the operating system. Making everything configured inside a container to run correctly in any operating system with docker installed.

2- What advantages does it have?

The advantages are several depending on what we compare it with.

If we compare it with running applications on a system directly, the main advantage is compatibility, since what has been tested in the container, in the same container of another system will work the same.

If we refer to advantage over using a full virtualization, the main advantage is that each container uses practically the memory of the application while an instance of KVM, Vmware, etc. requires also memory consumption for the kernel of the virtual system, etc.

Another advantage is that you can run applications as if you had a different system. That is to say, in the host system we have a CentOS system for example and in the container we can have, Free BSD or Debian, or Arch or any other. So if in a system it works better than in the host we can use this advantage.

3- How can I use it?

Well it can be used to create development environments and then upload the container to production (keep in mind that before putting anything in production with docker you must secure the host and the container to avoid problems). This way we can avoid problems in code deployments.

The first thing to do is to install docker. In CentOS it is done in the following way:  #yum install docker -y

Instalacion docker CentOS

In debian and derivatives: #apt install docker.io

After installing it, if it is a modern system, based on systemd, we start the service: #systemctl start docker

If you do not have systemd, run: #service docker start

4- Useful commands

Start containers (even if the images have not been downloaded):

docker run -i -t -d   IMAGEN:VERSION

Where the image and version can be specified as follows:

docker run -i -t -d   centos:latest

We can define a name for the container with –name MY_NAME

[root@localhost ~]# docker run -i -t -d --name testing debian:9
Unable to find image 'debian:9' locally
Trying to pull repository docker.io/library/debian ... 
9: Pulling from docker.io/library/debian
54f7e8ac135a: Pull complete 
Digest: sha256:df6ebd5e9c87d0d7381360209f3a05c62981b5c2a3ec94228da4082ba07c4f05
Status: Downloaded newer image for docker.io/debian:9
b6f568b78470b417b8a141f4b7c5686420df7cc1050402239ab1847310a67116
[root@localhost ~]# 

You can specify a port that will be forwarded from the host machine to the container:

docker run -i -t -d -p PUERTO_EXTERIOR:INTERIOR IMAGEN

Or it can also be done by specifying the host ip:

docker run -i -t -d -p IP_ANFITRION:PUERTO_EXTERIOR:INTERIOR IMAGEN

Adding “–restart always” to the equation, it is achieved that whenever the container is stopped, it is restarted. Even on host restart:

docker run -i -t -d -p PUERTO_EXTERIOR:INTERIOR --restart always IMAGEN

To create a container and not to lift it immediately:

docker create -i -t centos:6 /bin/bash

Download images and manage them

To download an image and have it available at the time of raising a new container without having to wait for it to be downloaded from docker-hub, we only have to do the following:

docker pull IMAGEN:VERSION

Or also:

docker pull IMAGEN

For example:

#Eligiendo version
docker pull centos:7
#o seleccionando la ultima disponible
docker pull centos:latest

We can also see the images available locally by running:

docker images

For example:

ger@portatil$ sudo docker images
[sudo] contraseña para ger: 
REPOSITORY                                  TAG                 IMAGE ID            CREATED             SIZE
debian                                      9                   4879790bd60d        9 days ago          101MB
ubuntu                                      18.04               ea4c82dcd15a        5 weeks ago         85.8MB
centos                                      6                   0cbf37812bff        6 weeks ago         194MB
centos                                      7                   75835a67d134        6 weeks ago         200MB
ubuntu                                      17.04               fe1cc5b91830        11 months ago       95.6MB

We can search for available images in docker hub:

ger@portatil$ sudo docker search ubuntu
NAME                                                   DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
ubuntu                                                 Ubuntu is a Debian-based Linux operating sys…   8793                [OK]                
dorowu/ubuntu-desktop-lxde-vnc                         Ubuntu with openssh-server and NoVNC            244                                     [OK]
rastasheep/ubuntu-sshd                                 Dockerized SSH service, built on top of offi…   184                                     [OK]
consol/ubuntu-xfce-vnc                                 Ubuntu container with "headless" VNC session…   136                                     [OK]
ansible/ubuntu14.04-ansible                            Ubuntu 14.04 LTS with ansible                   95                                      [OK]
ubuntu-upstart                                         Upstart is an event-based replacement for th…   92                  [OK]                
neurodebian                                            NeuroDebian provides neuroscience research s…   55                  [OK]                
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5   ubuntu-16-nginx-php-phpmyadmin-mysql-5          47                                      [OK]
ubuntu-debootstrap                                     debootstrap --variant=minbase --components=m…   40                  [OK]                
nuagebec/ubuntu                                        Simple always updated Ubuntu docker images w…   23                                      [OK]
tutum/ubuntu                                           Simple Ubuntu docker images with SSH access     18                                      
i386/ubuntu                                            Ubuntu is a Debian-based Linux operating sys…   15                                      

 

We can import a custom image:docker import /path/to/exampleimage.tgz:

[root@localhost ~]# docker import /tmp/debian-personalizado.tgz
[root@localhost ~]#

View the status of containers and their data

To see the created containers and their current status we only have to execute:

docker ps -a

For example:

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
b6f568b78470        debian:9            "bash"              About a minute ago   Up About a minute                       testing
[root@localhost ~]# 

To raise a machine we only have to execute docker start CONTAINER_ID the ID can be found by runningdocker ps -a :

[root@localhost ~]# docker start b6f568b78470
b6f568b78470
[root@localhost ~]#

 

Container management

To stop a docker container docker stop CONTAINER_ID:

[root@localhost ~]# docker stop b6f568b78470
b6f568b78470
[root@localhost ~]#

And to delete a container docker stop CONTAINER_ID:

[root@localhost ~]# docker rm b6f568b78470
b6f568b78470
[root@localhost ~]#

We can make a copy of a container by executing:docker export -o filesystem.tar CONTAINER_ID:

[root@localhost ~]# docker export -o filesystem.tar b6f568b78470
[root@localhost ~]#

 

We can access a container console: docker exec -it “id of running container” bash:

[root@localhost ~]# docker exec -it b6f568b78470 bash
[root@b6f568b78470 ~]#

 

 

If you are interested in learning Docker you can purchase our book here.

Docker para novatosDocker para novatos

 

And this has been all about how to get started with docker, in a next post, we will discuss how to manage docker with python.

Remember that if you liked this post we appreciate if you share in your social networks and/or comment on the publication.

See you soon!

Leave a Reply