Important Docker Tutorial for Beginners | 2021

Docker Tutorial For Beginners Part 1 1

In this blog, you will get hands-on experience with the Getting started with Docker tutorial. I have shared Lab link, You can use that link to Practise useful commands.

Why Do We Need Docker?

Let me start by sharing how I got introduced to Docker. In one of my previous projects, I had this requirement to set up an end to end application stack, including various different technologies, like a web server using node js, and a database such as MongoDB, and a messaging system like Redis, and an orchestration tool like Ansible, we had a lot of issues developing this application stack with all these different components. First of all, their compatibility with the underlying OS was an issue, we had to ensure that all these different services were compatible with the version of OS that we were planning to use.

pasted image 0

There have been times when certain versions of the services were not compatible with the OS. And we’ve had to go back and look at different OS that was compatible with all of these different services. Secondly, we had to check the compatibility between the services and the libraries and dependencies on the OS. We’ve had issues that one service requires one version of a dependent library, whereas another service requires another version, the architecture of our application changed over time, we’ve had to upgrade to newer versions of these components, or change the database, etc. And every time something changed, we had to go through the same process of checking compatibility between these various components, and the underlying infrastructure. So I needed something that could help us with the compatibility issue. And something that will allow us to modify or change these components without affecting the other components and even modify the underlying operating systems as required. And that search landed me on Docker.

download 4

With Docker,  I was able to run each component in a separate container with its own dependencies,  and its own libraries, all on the same VM and the OS, but within separate environments,  or containers. We just had to build the Docker configuration once and all our developers could now get started with a simple Docker run command. Irrespective of what the underlying operating system they’re on. All they needed to do was to make sure they had Docker installed on their systems.

I have shared all the commands below and tried to explain using the docker command. This docker tutorial contains all the basic commands that are present in docker.

What are containers?

Containers are completely isolated environments. As in they can have their own processes or services, their own network interfaces, their own mounts, except they all share the same OS kernel.

What are Images?

An image is a package or a template, just like a VM template that you might have worked within the virtualization world, it is used to create one or more containers.

Difference Between Container and Images?

Containers are running instances of images that are isolated and have their own environments and set of processes.

Docker Edition

Docker has two editions,

  1. Community Edition
  2. Enterprise Edition

The Community Edition is the set of free Docker products. The Enterprise Edition is the certified and supported container platform that comes with enterprise add ons like image management image security, universal control plane for managing and orchestrating container runtimes. But of course, these come with a price.

In this blog, we will basically use Community Edition. If you are on Mac or Windows,  you have two options, either install a Linux VM using VirtualBox or some kind of virtualization platform.

Let’s Begin with the docker tutorial command

Install Docker

First check OS requirement in Community Edition Page(Ubuntu, CentOS, Debian, Fedora). In my case, I am going to use Ubuntu.

If you have any older versions then remove them using the below command :

sudo apt-get remove docker docker-engine containerd runc

After removing the older version you can install it with the command

curl -fsSL -o
sudo sh

If everything goes well then you can confirm by checking

sudo docker version

For finding a list of popular Docker images – Docker Images

If you wish to set up Docker then you can refer above process or else you can use Hands on Lab.

Docker Commands

Run – Start a Container
docker run nginx

The Docker run command is used to run a container from an image. Running the Docker run Nginx command will run an instance of the Nginx application from the Docker host if it already exists. If the image is not present on the host, he will go out to Docker Hub and pull the image down.

docker tutorial
ps – list all containers
docker ps -a

The docker ps command lists all running containers and some basic information about them. Such as the container ID, the name of the image we use to run the containers, the current status and the name of the container. Each container automatically gets a random ID and Name created for it by Docker, which in this case is the awesome_lamarr. To see all containers running or not use the -a option. This output is all running as well as previously stopped or exited containers.

docker tutorial
stop – Stop a Container
docker stop <name>

It will stop the container but it will still be displayed on docker ps -a. If you still want to remove from docker ps -a then follow the below command.

rm – Remove a Container
docker rm <name>

It will remove the container permanently. It will not display in docker ps -a

images – List Docker Images
docker images

Docker images will list down all images with the heading- Repository, Tag, Image ID, Created, Size

Screenshot 2021 08 30 at 11.45.08 AM
rmi – Remove Docker images
docker rmi <repository>

Remember to stop and delete all dependent containers in order to remove images. If it is running then you will not be able to delete the image.

pull – download an image
docker pull nginx
Screenshot 2021 08 30 at 11.39.08 AM

Pull command will simply download the image and it will not run as we have seen in case if docker run the command.

Let's take some examples and understand more. Suppose if you want to run Ubuntu Image.

docker run ubuntu

This command will run an instance of Ubuntu Image and exists Immediately.If you list running container ,then you will not see the running containers.

Note A container only lives as long as the process inside it is alive. If the web service inside the container is stopped, or crash, then the container exits. This is why when you run a container from an Ubuntu image, it stops immediately. Because Ubuntu is just an image of an operating system that is used as the base image.

For other applications. There is no process or application running in it by default. If the image isn't running any service, as is the case with Ubuntu, you could instruct Docker to run a process with the Docker run command.  For example, a sleep command with a duration of five seconds. When the container starts,  it runs the sleep command and goes into sleep for five seconds post with the sleep command exit, and the container stops.

docker run ubuntu sleep 5
Exec – Execute a Command
docker exec awesome_lamarr cat /etc/os-release

If we would like to execute a command on your running container. Let’s say you would like to see the contents of a file inside this particular container. In this case, let’s print the os-release file of the running container.

docker tutorial
run – attach and detach

When you run a docker run command, it runs in the foreground or in attached mode. Meaning you will be attached to the console or the standard out of the Docker. It won’t respond to your input. You need to press ctrl+c to get back to prompt.

docker run kodekloud/simple-webapp

Another option is to run the docker container in detached mode. It will run in the background mode and you will be back to prompt immediately.

docker run -d kodekloud/simple-webapp

In order to view, you can run docker ps the command. If you want to attach again then you can run the docker attach command.

docker attach kodekloud/simple-webapp
run – tag

If you wish to run a specific version of the service. Let’s say if you want to run Redis of version 4. 0 then you can do using the command.

docker run redis:4.0

Here 4.0 is a tag. If you don’t use any tag by default it picks the latest tag.

run- stdin

If you wish to provide standard input then you can run with the below command.

docker run -it kodekloud/simple-webapp

-i : Interactive , -t- Pseudo Terminal

run – port mapping or port publishing

When we run a containerized web application it runs and we’re able to see that the server is running. But how does the user access my application? As you can see, my application is listening on port 5000. So I could access my application by using Port 5000.  But what IP do I use to access it from a web browser? There are two options available. One is to use the IP of the Docker container. Every Docker container gets an IP assigned by default,  in this case, it is Remember that this is an internal IP and is only accessible within the Docker host.

So if you open a browser from within the Docker host, you can go to to access the IP address. But since this is an internal IP user outside of the Docker host cannot access it using this IP.

docker tutorial

If you wish to access port 80 of the docker host then you can map using the below command.

docker run -p 80:5000 kodecloud/simple-webapp

So now users can access applications outside the docker host using

All Traffic on Port 80 will get routed to Port 5000. In this way, you can run multiple instances. You can map this to any number of ports you want.

docker tutorial
docker run -p 8000:5000 kodekloud/simple-webapp
docker run -p 8001:5000 kodekloud/simple-webapp
docker run -p 3306:3306 mysql
docker run -p 3307:3306 mysql

run – volume mapping

If you want to make data persistent such that data in docker is destroyed you can use volume mapping. This way you run the docker container it will implicitly mount the external directory to a folder inside the Docker container. This way all your data will now be stored in the external volume at /opt/datadir. It will be there even if you delete the docker container.

docker run -v /opt/datadir:/var/lib/mysql mysql

Container logs

docker logs <container id/name>
docker tutorial

Hope you like this docker tutorial. Please feel free to comment. If you need a tutorial on any other topic feels free to comment. That’s all in this blog. Don’t forget to subscribe. You can visit our Tech Category. There are a lot more you can learn.


Leave a comment

Your email address will not be published.