Docker 101

Who can live without docker? Hmm, I don’t know someone. The concept is known and I am not going to explain it in this space. What makes me think that worths is to gather a few essential commands to hit the ground running, don’t you think?


How to install it Ubuntu 18.04?

There are a few alternative ways to install it. The most popular is to set up Docker’s repositories and install from there. In this case, we install some pre-requirements, add Docker’s official GPG key, set up the stable repository and press the button (install docker I mean!)

$ sudo apt-get update
$ sudo apt-get install \
     apt-transport-https \
     ca-certificates \
     curl \   
     gnupg-agent \   
     software-properties-common
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
     $(lsb_release -cs) \
     stable"
$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io

Then in case, you work in Ubuntu Linux (in my case 18.04), you will start getting the annoying message “How to fix docker: Got permission denied while trying to connect to the Docker daemon socket” when trying to run without sudo. The solution?

$ sudo groupadd docker
$ sudo usermod -aG docker angelos
$ sudo chmod 666 /var/run/docker.sock

Yes, angelos is my username.

Let’s play with the basic commands

Every that respect themselves should start from the hello world. Let’s do it.

docker run hello-world

and then check that is there?

$ docker images

ok, it’s there. What if you want to experiment with something more complicated, like running a simple python3 script. Okay, here you are. We download a minimal image like Alpine Linux, and python 3, how?

$ docker pull python:3.7-alpine

Let’s see it is there.

$ docker images

and after the image we create the container:

$ docker run -it <image_id> /bin/ash

The -it switch is used to run the command /bin/ash interactively
Similarly, if we want to attach to an image running bash, we could type:

$ docker exec -it <container_name> /bin/bash

Now we need to check the running containers in our system

$ docker ps

and the containers in general

$ docker ps -a

easy?

In case you want to stop the container, you need to execute the following command

$ docker stop <container_id>

and start again

$ docker start <container_id>

and attach a different terminal to that?

$ docker attach <container_id>

Yes, as you will quickly realise, Alpine is not similar to Ubuntu. If you want to install nano, for editing files, you should use apk in a similar fashion. So, it will look like the following:

# apk update
# apk upgrade
# apk add nano

As we mentioned earlier, the container supports Python3. It is much more convenient to type the script using our luxury editor in the host computer and copy it in the container. How?

$ docker cp main.py <container_id>:/home

If execute ls in the home folder of the container, you will see it there.

Finally, we copied the script and we are satisfied with the functionality. There is an easy way to convert the container into an image and freeze the development. in addition, we could make the script run automatically as we run the container, and this is the way:

$ docker commit --change='CMD ["python","/home/main.py"]' 162dfda7b417 my-new-image

and after that, as usual, we could see that the image is there.

$ docker images

But we like housekeeping and we want to clean the place after our experiments, don’t we?

After we have stopped the container, we use the following command to delete it.

$ docker rm <container_id>

and the image like that:

$ docker rmi <image_name>

or to delete all exited images:

$ docker rm $(docker ps -a -f status=exited -q)

Hope that you enjoy it!