Hands-on with Docker- A step by step tutorial to getting started with Docker on any machine

10 min readSep 17, 2019

Hello there, this blog post is inspired by multiple incidents which recently made me uncomfortable and helped me to learn something new that is docker. A few months back I asked one of my colleagues to help me understand docker, he replied: “When you use it, you will know it”. Which is very true. But I did not need to use it until the next few months. After 4 to 5 months another incident happened, I installed multiple libraries using the superuser command “sudo” on my machine and that made my root environment unstable and a few of my virtual environments too. I was unaware of not using “Sudo” to install anything on the machine.

Now you can understand what pain and rework it would take for a person whose virtual environment becomes unstable and that was the time when someone suggested I use docker, which provides isolation, multi-cloud platform usage, compatibility and maintainability.

You may find a lot of definitions to docker, docker image and containers, online, but believe me, if you are looking into it for the first time these terms might confuse you. I am going to explain everything you need to know about docker to get it working on your machine in the most simple and layman language. I Hope, it helps you.

Why use a docker?

  1. Every coder manages multiple projects and creates virtual environments for each project. Creating virtual environments is a good practice if you are working with a single machine but when you have to run your project, code on another machine whose configurations (OS, library version, python version) are different from yours, creating a virtual environment or a Virtual machine is challenging. So Docker saves you from saying “The code ran on my machine, but I am not sure why is it doesn’t runs on your machine”. Docker enables you to run your project, application, code just the way you ran it on your machine.
  2. If you have used GitHub, you know how easy it is to push your code changes and pull the project from anywhere in the world. In the same way, you can commit your project environment using Docker and pull it anywhere in the world. You can maintain the versions too.
  3. Using Docker is much easier than installing all the libraries in an environment using requirement.txt or a virtual machine. Just a single line of code and your project is ready to run. It creates a highly optimized VM (virtual machine)inside your localhost and is far more efficient than a VM as it uses OS kernel efficiently and is just tens of MBs in size, unlike a traditional VM machine. Each VM includes a full copy of an operating system, the application, necessary binaries and libraries — taking up tens of GBs. VMs can also be slow to boot. Docker also saves you and other users to intervene in each other's projects by providing complete isolation.

What is a docker?

Docker is the name of a company. Yes, it is the name of a company that launched its open-source Docker Engine in the year 2013.

Now you must be wondering if Docker is the name of a company then how do people use a Docker?

Now let me introduce you to a few very important terms, docker image, container, running a container. Let us see all of them one by one.

How to install docker on your machine?

Docker website has excellent step by step assistance to install docker on your machine. You can install docker on mac, ubuntu, windows, Debian, centos, fedora.

What is a docker image?

A docker image is an environment in the cloud, which you can download on your machine from the website of docker. Create your account and login. I will use Github as an analogy for you to understand docker.

In the above image. If you see on the top left, I searched for ubuntu and I get a result for the search which says ubuntu Dockers official Images. In the bottom right corner of the above image, you can find the command -docker pull ubuntu. This is very similar to Git. You can use this code in your terminal and pull the ubuntu image anywhere on your machine.

What will this ubuntu image have?. Is it an image with the logo of Ubuntu on it?.

Do not be confused by the term “Image”. When you run the command docker pull ubuntu, you get a copy of ubuntu which is a Debian-based Linux operating system on your machine.

Using “docker pull ubuntu” you get an image with the name ubuntu, which has the basic ubuntu installed in it. If you see the second line in the above image you can see that the tag given to this image is the latest, you can specify any other tag of your choice just to differentiate among multiple ubuntu images. Now you locally have an ubuntu machine.

$docker pull [image name] $docker pull ubuntu

When you run the command ”docker images” you can find all the docker images present on your system. Currently, you have only one image called ubuntu with the tag latest and an image ID and the time when this image was created on the docker website and also the size of this image. You can not view this as an image on any of the image viewers on your machine :)

$docker images

Now, you have a docker image but what do you do after that?

Now you run the docker image. I know it sounds a bit wired but this is the truth. I want you to say these lines with me “I pulled a docker image called ubuntu from the docker website and now I am going to run my docker image, which is quite similar to activating your virtual environment.”

To run a docker image we use the command docker run. Here I have used “docker run -t -d — name latest ubuntu”. This will generate an ID, the one that you see in the second line.

$docker run -t -d --name [image tag] [image name]

$docker run -t -d --name latest ubuntu

When you run a docker image, you call it a container. Yes! :)

What is a container?

When you run a docker image using the docker run command the process is called containerisation and you have a copy of your docker image which is activated.

In the above image, you see a 12 digit container ID which is similar to the output of the image above this. It is your container ID. The name of the docker image used to run the container is ubuntu. The name of the container is the latest.

docker ps -a

There you go. Just type the above command. “docker exec -it latest bash” and you have a machine with the name root@67a5052f3154. All of the Linux commands work here.

docker exec -it latest bash

In the above image, I used the command “ls” to list what was there in my container root@67a5052f3154. You can see that we have everything just like what we have in any fresh ubuntu system.
The above image shows how my container works just like yet another system in my local machine.

How do you stop and delete a container?

docker ps -a will list all the running containers. Here you can see our running container.

Let us stop this container.

After checking the running containers, you can choose to stop the one that you want. The command for stopping a container is docker stop [CONTAINER ID]. When you run this command container ID of the stopped container gets printed on the cmd window.

docker stop [container ID]

docker stop 67a5052f3154

In the above image under the header STATUS, you see exited (0) 5 minutes ago. Because we stopped the container. Stop the container does not delete the container. To remove the container we must delete it.
The command to delete a container is docker rm [CONTAINER ID]. Now your container is deleted.

docker rm [Container ID]

when you run the command “docker ps -a“ again. You do not see any container.

How to delete a docker image?

There is no process called as stopping the image before deleting it, unlike container. To delete an image the command is “docker rmi [Image name]”

Run the command “docker images” you will get the list of all the docker images on your machine. We have only one image which is ubuntu. Let us delete it.
The ubuntu image is deleted.

docker rmi [image name]

How to create your first Docker image and dockerize a Python Application

Dockerfiles enable you to create your own images. A Dockerfile describes the software that makes up an image. Dockerfiles contain a set of instructions that specify what environment to use and which commands to run.

Create a Dockerfile

Execute all the commands step by step to create a Dockerfile. The command “touch” is used on mac to create a file through cmd. I am using a mac, so I have used touch. You can create a file with name “Dockerfile” without any extension in the folder first_python_docker.
Use vim or any other editor to edit the Dockerfile.
This is how your Dockerfile would look where your base image will be a python3 image and we will install the PIL library.

Build an image from the Dockerfile

The image is built. Now let us run the image to create a container.

docker build -t [docker image]

Run your image

Your container is UP. You can install anything here and run any code.

docker run -t -d --name [tag] [image name]

You can enter inside the container with the command “docker exec -it python-pil bash”

docker exec -it [tag] bash

Copy any file from your localhost to docker container

docker cp ./file1.png [container ID]:/file1.png

docker cp [localhost folder path] [container ID]:/[container path]

You can find image.png in the docker container

Install vim editor on the docker container to view and edit files on a running container

apt-get update

apt-get install vim

vim [filename]

How to use Jupyter lab to view files on the docker container

To view, the folders and files present inside your container you must map TCP port in the container to port on the Docker host.

Pull this image from the docker hub docker pull jjanzic/docker-python3-opencv

When you are about to start running a container you must do the mapping of ports.

Run the command docker ps

In the above image, you can find the column port. “>8080/tcp” this means docker host port 7010 is mapped to 8080 TCP port of your container with ID 151b0c9558aa.
In the above image, you can see the mapping of the different ports of the docker host port to the different container ports.

This command maps the docker host port to the container port and starts (run) your container

docker run -t -d -p [dockerhost port]:[container port] --name [image tag] [image name]

docker run -t -d -p 7010:8080 --name opencv_python jjanzic/docker-pytho3-opencv

Run the command docker ps to check if the port got configured.

Now enter inside the container and install jupyter lab using.

pip install jupyter -U && pip install jupyterlab

Once installed use the below command to launch jupyter lab

jupyter lab --ip= --port=8080 --allow-root

Now, open your browser and type localhost:7010

This is how your browser would look like. Now you can get started to create, edit any file with a GUI.

Commit your container and push the image to your docker hub account:

We pulled an image from the docker hub and created a container out of that to run on our machine. Then we installed a few python packages for our code to run. After the updates into the container, we would definitely want to share the updated container with our team or keep it for future references. so what must we do? The answer is simple “commit it to your docker hub account”.

The below command will list all the containers running on your machine.

docker ps
docker commit c7a64fde03 pallawids/deeplabv3plus_tensorflow:version2
docker images
The container is converted and saved as an image on the local machine. The next step is to push the image to the docker hub.

Before this, we must create an account on the Docker hub and secure a userID and password. This user ID and password will help us to log in to our docker hub account through our command line window and thus allow us to push our new image to the Docker hub.

Use the below command to log in to the docker hub account through the command window.

docker login

If you are logging in for the first time things will be smooth and you can enter the credentials and log in successfully. But if in case you are not allowed to log in due to multiple unsuccessful attempts, the reasons could be typing mistakes. Then you can try the below command, which will delete the previous docker login caches and allow you to log in fresh. You may want to first view what is there in the config.json file using vim and then delete it using the “rm” command for a fresh start.

rm ~/.docker/config.json
docker login

After the successful login using the command line window.

Let us push our image to the docker hub. Use the below command to push your image.

docker image push userID/imagename:tag
docker image push pallawids/deeplabv3plus_tensorflow:version2
This is how the command line window looks when the image is getting pushed to your docker hub.
This is how the command line window looks once the image is successfully pushed to your docker hub.
Now you can find the pushed image on your docker hub.
It can be pulled by anyone, anywhere in the world.

Find the container which has exited — When the container is not running after a few hours of work

Sometimes it might happen that the containers exit and you may not be able to start the container with the bash command, then you can first find the list of the containers using the below command.

docker ps --filter "status=exited"

Exit does not mean dead or your data is lost. You can use the below command and start the container again.

docker start 5c2f4b9516c7
docker start containername

Then you can run your container using the below command

docker exec -it containername bash

How to save your Docker images into a tar file and push them on an AWS s3 bucket

docker save imagename:tag > imagename.tar

Use the above command to save your Docker image in a .tar file and share it with your team.

docker load --input imagename.tar

Docker load command helps you to unzip the image tar and use the docker image.

You can run :

docker images

To make sure your image was loaded correctly.


Teaching yourself and understanding a topic on your own requires an investment of time, focus, purpose, persistence. I have learnt all of the above from multiple sources. I find it useful and thought I must share. Two reasons for that, I did not find a single source to get a simple understanding of all of the above points so that anyone and everyone can benefit from it who googles What is a docker? The next reason is that now I will never forget the basics after writing it here. Thanks to medium. Hope I was able to help you to understand what a Docker is and get started in minutes.





Computer Vision contributor. Lead Data Scientist @https://www.here.com/ Love Data Science.