Getting Started with Docker

Hiten Pratap Singh
TechCret Software
Published in
4 min readSep 17, 2019

Just recently I was working on micro-service based project in spring boot. So to deploy these services, I looked for the best solution to make these independent deployable without much manual effort. There I came across Docker and Kubernetes (K8S) to make the whole deployment process smooth and automated. So in this article we will look into Docker and will post about Kubernetes (K8S) in a later post.

Docker Logo

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. The best part I like about Docker is that it removes the bridge between development and production environment. It doesn’t matter which OS you are using as Docker provides the uniformity among various OS. So it makes super easy to run any application on any machine with any libraries being used in it without the overhead of using machines with different OS.

In a way, Docker is a bit like a virtual machine. But unlike a virtual machine, rather than creating a whole virtual operating system, Docker allows applications to use the same Linux kernel as the system that they’re running on and only requires applications be shipped with things not already running on the host computer. This gives a significant performance boost and reduces the size of the application.

The Docker Daemon - The Docker daemon (dockerd) listens for Docker API requests and manages Docker objects such as images, containers, networks, and volumes.

The Docker Client - The Docker client (docker) is the primary way that many Docker users interact with Docker. When you use commands such as docker run, the client sends these commands to dockerd, which carries them out.

Docker Registries - A Docker registry stores Docker images. Docker Hub is a public registry that anyone can use, and Docker is configured to look for images on Docker Hub by default.

Docker Images - An image is a read-only template with instructions for creating a Docker container. Often, an image is based on another image, with some additional customization.

Docker Containers - A container is a runnable instance of an image. You can create, start, stop, move, or delete a container using the Docker API or CLI. You can connect a container to one or more networks, attach storage to it, or even create a new image based on its current state.

How to run nginx image using Docker

  1. Make sure that Docker is installed on your machine. You can install it as per your machine OS by going to https://www.docker.com/get-started. To verify the installation, just run docker -v or docker info command.
  2. Now it’s time to create Dockerfile so that an image can be built from it.

3. Now create index.html file in html folder so that we can replace the default nginx index.html file with it.

4. Now create default.conf nginx config file inside the same html folder so that we can remove the nginx’s default config file and add our custom config file to it as mentioned in the above Dockerfile.

5. Now to build image from Dockerfile just execute the following command (Just remember that you must be inside the same folder as in your Dockerfile):

docker build -t <dockerhub_username>/<name_of_image>:<tag> .

Here docker hub username would be your username you would get after registering at https://hub.docker.com so that you can push this image there. name_of_image can be anything you like and tag can be anything you like otherwise docker considers default if tag field left blank.

when you do this much work then you can confirm that you image has been built by executing following command:

docker images

Upon executing this command, you will get something like below output:

6. The last step is to spin up the container from this image. To do that we just have to execute below command:

docker run -it --name <name_container> -p 8081:80 -d <name_of_image>

Here name_container can be anything you like, but if you decide not to provide any name, then docker will choose some random name for you. -p is used to map port and here we are mapping 8081 with 80 port of the container and port can be anything in-place of 8081 of your choice. You must be careful to give a correct docker image name in the place of name_of_image. In Docker is can also be used in place of a name.

7. Now if you browse to the address http://localhost:8081 then you will see output something like below:

So this was the basic introduction to Docker and a simple demo of how a simple program like nginx can be run using Docker with custom configuration.

For more posts about Docker and Kubernetes (K8S), follow the blog.

--

--