Docker 101

2020-08-08

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!) [sourcecode language=“plain” light=“true”] $ 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 [/sourcecode] 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? [sourcecode language=“plain” light=“true”] $ sudo groupadd docker $ sudo usermod -aG docker angelos $ sudo chmod 666 /var/run/docker.sock [/sourcecode] Yes, angelos is my username. Let’s play with the basic commandsEvery that respect themselves should start from the hello world. Let’s do it. [sourcecode language=“plain” light=“true”] docker run hello-world [/sourcecode] and then check that is there? [sourcecode language=“plain” light=“true”] $ docker images [/sourcecode] 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? [sourcecode language=“plain” light=“true”] $ docker pull python:3.7-alpine [/sourcecode] Let’s see it is there. [sourcecode language=“plain” light=“true”] $ docker images [/sourcecode] and after the image we create the container: [sourcecode language=“plain” light=“true”] $ docker run -it <image_id> /bin/ash [/sourcecode] 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: [sourcecode language=“plain” light=“true”] $ docker exec -it <container_name> /bin/bash [/sourcecode] Now we need to check the running containers in our system [sourcecode language=“plain” light=“true”] $ docker ps [/sourcecode] and the containers in general [sourcecode language=“plain” light=“true”] $ docker ps -a [/sourcecode] easy? In case you want to stop the container, you need to execute the following command [sourcecode language=“plain” light=“true”] $ docker stop <container_id> [/sourcecode] and start again [sourcecode language=“plain” light=“true”] $ docker start <container_id> [/sourcecode] and attach a different terminal to that? [sourcecode language=“plain” light=“true”] $ docker attach <container_id> [/sourcecode] 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: [sourcecode language=“plain” light=“true”] # apk update # apk upgrade # apk add nano [/sourcecode] 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? [sourcecode language=“plain” light=“true”] $ docker cp main.py <container_id>:/home [/sourcecode] 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: [sourcecode language=“plain” light=“true”] $ docker commit –change=‘CMD [“python”,“/home/main.py”]’ 162dfda7b417 my-new-image [/sourcecode] and after that, as usual, we could see that the image is there. [sourcecode language=“plain” light=“true”] $ docker images [/sourcecode] 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. [sourcecode language=“plain” light=“true”] $ docker rm <container_id> [/sourcecode] and the image like that: [sourcecode language=“plain” light=“true”] $ docker rmi <image_name> [/sourcecode] or to delete all exited images: [sourcecode language=“plain” light=“true”] $ docker rm $(docker ps -a -f status=exited -q) [/sourcecode] Hope that you enjoy it!