Docker installation and sample application deployment – a simple & practical guide

Introduction to Docker

Docker is a containerization platform.  Docker provides us OS-level virtualization, we can use it to package software and run them as container. To deploy an application as a docker container we need a docker image of that application, the docker image can be built from a Dockerfile. A Dockerfile is nothing but a text document which contains all the commands which we need to run to build the docker image. If we don’t use the Dockerfile, then we need to run all the command manually to build that docker image. So, it is better to put all those commands in a text file (Dockerfile) and use it as many times you want to build the image.

To know more about Docker, you can have a look at the wiki page of Docker.

If you would like to know about Hypervisor & Container Technology, then please head over to below article.

Hypervisor vs Container technology – Let’s discuss

Docker Installations Steps

To install Docker on Ubuntu, run the bellow commands.

  • apt update
  • apt install docker.io

If you are using a Red Hat Linux machine, then use ‘yum’ instead of ‘apt’.

To check the version of installed docker, run the below command.

docker --version

docker version

By running the below command, we can check all the running container in the Docker engine. Since we have just installed docker, and we have not deployed any container, so output of the below command should show no container.

docker ps

(here ps means “process status”, this command will show all the running docker containers which are running)

Output:

docker ps

We can use the below command to check all the containers (stopped & running).

docker ps -all

Deploying Sample application in Docker

As we have installed the docker (docker engine), let’s play with it. Let’s deploy a sample application in docker (docker engine) which will run as a docker container.

  • To deploy an application as a docker container, we need the docker image, which is nothing but a packaging of that application.
  • We need to build the docker image from a Dockerfile.

For this purpose, we will use the below sample python application.

https://github.com/sk617/my-hello-world-python.git

we need to clone the repo in our local machine by running the below command.

git clone https://github.com/sk617/my-hello-world-python.git

Now go inside the “my-hello-world-python” directory.

cd my-hello-world-python

Run the below command to build the docker image.

docker build -t <build context> <Path of Dockerfile>

Here, -t is for tagging, it is nothing but the name of the image we want to give. It is also called build context.

For details about ‘docker build’ command, please follow official documentation of docker.

docker build -t my-hello-world-python .

docker build

Now we can check the built docker images. For this, run the below command.

docker images

docker image

Now we will use this docker image to deploy this sample application as docker container. For this, run the below command.

docker run -d -p 5000:5000 my-hello-world-python

(In the above command my-hello-world-python is the image name, if we don’t specify container name with the above command, our docker container will have same name as the docker image. But if we want to have our docker container a different name then we need the below command.

docker run -d –name hello-world-container -p 5000:5000 my-hello-world-python

here, our container will have ‘hello-world-container’ name.)

docker run

Note:

-d: To run the container in background (in detach mode). Instead of ‘-d’, if we use ‘-t’ it will run in terminal mode (not in the background),

-p: To mention the port.

There is two port,

  • One is the port used by the container internally, i.e. on 5000 port our python flask application run inside the container.
  • But If we don’t map this port to any external port or if we don’t expose this port to public, we won’t be able to access it outside the docker host. For this we need

Note: 5000 port is the default port in python flask application.

docker port

To check the running container, we need to run the below command.

docker ps

docker ps

Now we can access that application by running below CURL command.

curl localhost:5000

docker app

 

This is how we can deploy any application as docker container.

 

Thank You.