Docker for Developers. Have you ever encountered a situation…
By: Aadesh Jain
Tags: Windows 10, Docker Image, Dockerfiles, Docker, DevOps

Have you ever encountered a situation when a piece of code is working on your local machine but not working on Server??? If you have faced this situation then Docker is Made For You!!!!

In this blog, we will be discussing what is Docker, Differences between Containers vs VM’s, Docker Commands, Docker Images, and Docker Files.

What is Docker?

Docker is a platform for Developers and any other software engineers to develop, deploy, and run applications in containers. This process is called as Containerization.

Why Docker?

There are various advantages of using Docker containers which are given below:

Portability: Docker Containers are portable that means we can create containers in our local machine and deploy it anywhere.

Docker Containers are portable that means we can create containers in our local machine and deploy it anywhere. Lightweight: Docker Containers are lightweight as they use host OS kernels but they are capable of doing any type of complex task….

Docker Containers are lightweight as they use host OS kernels but they are capable of doing any type of complex task…. And many more…

Container’s vs. VM’s

I have seen many developers getting confused between Containers and VM’s. We will see the differences between the container’s and VM’s to clear all the doubts.

Left: Containers — Right: Virtual Machines

The Docker service is always running on top of the host operating system. Containers are containing the application itself. Containers do not contain a guest operating system which ensures that containers are lightweight.

In contrast, virtual machines are running on a hypervisor (responsible for running virtual machines) and include it’s own guest operating system. This increased the size of the virtual machines significantly, makes setting up virtual machines more complex and requires more resources to run each virtual machine.

Docker terminologies

While working with docker the words Images, containers, and Volumes are used.

Docker Image contains everything required to run an application in the container. This includes Code, Binaries, Dependencies, etc…

Docker container is a running instance of a docker image.

Docker Volumes are the preferred mechanism for persisting data generated by and used by Docker containers.

Docker commands

Since we now know the basics of docker, Let’s install docker on windows and check out some basic commands.

Download and install Docker for desktop on Windows 10 to be able to use docker commands with Linux containers.

Link: https://hub.docker.com/editions/community/docker-ce-desktop-windows

Finally, you should be sure that the Docker for desktop is started before we can run the below commands:

docker search: This command is used to search for public images on the Docker Hub. Alternatively, we can go to hub.docker.com to search for docker images.

docker search <Name of Image to be searched for>

docker search

2. docker pull — Pulls an image or a repository from a registry.

docker pull <name of image to pull>

docker pull

3. docker run- This command is used to run a new container.

docker run -d -p 80:80 nginx

By running the above command we can run the freshly downloaded Nginx image in a container. In this command, we have used various options like:

-d: Run container in background and print container ID.

-p: by using this option, we are mapping internal port 80 of the container with the external port 80.

In -p 80:80, the left port is the external port and the right port is internal port of the container.

Once the id of the container is printed on the shell, we can navigate to localhost:80 and see the Nginx homepage.

Nginx Homepage

4. docker container ls or docker ps — This command is used to show all running containers.

Also, this command can be used with -a option to show all the containers whether it is running or not.

docker container ls

5. docker stop/docker start: These commands are used to start/stop the containers.

docker start and docker stop

6. docker exec — The docker exec command runs a new command in a running container. This can be used to create an interactive bash session in the container.

docker exec

Now we know all the basic docker commands which are used everywhere so we can dive deep in docker and see how to host out custom index.html in our Nginx container using docker Volumes.

When we are using Docker for Desktop, we need to enable the Shared Drives from Docker for Desktop settings.

Once the shared drives are enabled, we can mount a folder on our drive to the container.

I have created an index.html file in e:/dockerdata/nginx and I will attach this path to /usr/share/nginx/html.

docker run -d -p 80:80 -v e:/dockerdata/nginx:/usr/share/nginx/html nginx

Now you can open any browser and navigate to localhost:80 and see that the volume has been attached.

Build our Images

Till now we have seen how to run any other image present on the docker hub but what if we want to create our image and upload it to the docker hub.

We will need to see two more commands docker build and docker push. To create our images, we need to write a Dockerfile with instructions in it.

In this tutorial, we will create our Dockerfile to create a custom image embed with our index.html running on Nginx.

I am creating a file named Dockerfile(without any extension) at the location where our index.html was created. (e:/dockerdata/nginx) with the below content.

FROM nginx

COPY index.html /usr/share/nginx/html/

So in the dockerfile, we used just two lines described below:

FROM nginx: Using this command we are specifying that the base image to be used is nginx.

COPY index.html /usr/share/nginx/html/: Using this command we are copying the index.html file present in our host to the /usr/share/nginx/html inside the container.

Now to create a docker image using the above docker file, we will use the docker build command.

docker build -t mynginximage .

The docker image will be created and we can see that docker image by using docker images command.

docker image

We will be able to see the myjenkinsimage generated a minute ago.

Currently, this image is only in our local system. Before pushing this image to the docker hub we will try to run this docker image and check if everything is working or not.

docker run -d -p 80:80 mynginximage

Now we can go to localhost:80 in any of the browsers to check if the image was created successfully or not.

This page shows that our custom docker image was created successfully. This was just basic on how to create our image. For detailed instructions on writing Dockerfiles please visit https://docs.docker.com/develop/develop-images/dockerfile_best-practices/ to know the best practices on writing dockerfiles.

Now, We know that our docker image is correct. So we can push this image to the docker hub. But for that, we will need to create an account on the Docker hub.

Go to https://hub.docker.com/signup to create a Docker hub account.

Once our docker hub account is created we will need to tag our image with our docker hub username.

docker tag mynginximage 27aadesh/mynginximage

When the docker image is tagged with our username, we can log in to the docker hub by using the docker login command.

docker login --username=yourhubusername --email=youremail@company.com

After login, execute the docker push to push the image successfully to the docker hub.

docker push 27aadesh/mynginximage

The image will be successfully pushed to the docker hub. We can go to Dockerhub and check that if the image is shown or not.

Voila!! Our Nginx image is shown on the docker hub which can be used by anyone in the world. Using this way we can ship any application using docker images.