Hands-on with Docker- A step by step tutorial to getting started with Docker on any machine
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?
- 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.
- 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.
- 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?
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.
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.
$docker pull [image name]
$docker pull ubuntu
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.”
$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.
docker ps -a
docker exec -it latest bash
How do you stop and delete a container?
Let us stop this container.
docker stop [container ID]
docker stop 67a5052f3154
docker rm [Container ID]
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]”
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
Build an image from the Dockerfile
docker build -t [docker image]
Run your image
docker run -t -d --name [tag] [image name]
docker exec -it [tag] bash
docker cp ./file1.png [container ID]:/file1.png
docker cp [localhost folder path] [container ID]:/[container path]
Install vim editor on the docker container to view and edit files on a running container
apt-get install vim
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
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=0.0.0.0 --port=8080 --allow-root
Now, open your browser and type
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 commit c7a64fde03 pallawids/deeplabv3plus_tensorflow:version2
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.
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.
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
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 <CONTAINER_ID> or <CONTAINER_NAMES>
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 :
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.
How do you attach and detach from Docker's process?
Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question. Provide details and share…
Dockerize your Python Application
Dockerfiles enable you to create your own images. A Dockerfile describes the software that makes up an image…