Sneak view into Docker for devs| Part-2

aditya goel
12 min readDec 26, 2021

Question:- What is Docker ?

Answer:- Docker is a software development platform to deploy the apps. The Apps are packaged into containers that can be run on any OS and they shall be running in the exact same way independent on which platform are they running. This way, apps are easier to maintain and deploy. They can work with any language, any OS, any technology. Multiple apps running in different containers, don’t impact each other at all. In case one application is stressed, other apps would not be impacted. For example → On any EC2-instance OR On any bare-bone-metal, our Java based web-app (deployed in container-1) can interact with mysql-app (deployed in container-2).

Question:- What is the version of Docker we would be working against ?

Question:- Show some example with Docker powering Python?

Answer:- Following command runs ans launches an application :-

docker run -p 5000:5000 in28min/hello-world-python:0.0.1.RELEASE

And here is the output of the same (in our browser) :-

Question:- Whats’s the usual process for before Docker, for OPS team ?

Answer:-

  • We used to get a document from my development team describing the hardware, the specific version of the operating system, the specific version of the software, and the specific version of the application to deploy.
  • We used to manually follow the instructions — create a server, install an operating system on top of it, and install the right software, install Tomcat or whatever you’d need to run your application, and install the right language — Java or Python.
  • And after that is when we would download the application and deploy it, and very manually setting up the environment. We used to make a lot of mistakes.

Question:- Whats’s changed with Docker ?

Answer:-

  • With Docker, we don’t really need any details about the application that we would want to run.
  • We don’t really need to worry about what language the application is built with. Is it a Java application, or a python application or a Javascript application. We don’t really need to worry about which version of Java or which version of Python.
  • What were the frameworks which were used to build the application with. What is the software that is needed to run the application.

Question:- What does Developer needs to do ?

Answer:-

  • All that the developer needs to do, is to create something called Docker image, and you can run the image the same way, wherever Docker runtime is installed.
  • It does not matter if the image contains a java application, a python application, or a NodeJS application — you still run it the same way.

Question:- Show some example with Docker powering Java based application?

Answer:- Following command runs ans launches an application :-

docker run -p 5000:5000 in28min/hello-world-java:0.0.1.RELEASE

And here is the output of the same (in our browser) :-

Question:- What’s a Docker-Image ?

Answer:- A Docker image is a read-only template that contains a set of instructions for creating a container that can run on the Docker platform. It provides a convenient way to package up apps and preconfigured server environments, which we can use for our own private use or share publicly with other Docker users. Docker images are also the starting point for anyone using Docker for the first time.

Question:- Whats the Docker Image, we used in afore-shown example ?

Answer:- in28min/hello-world-java-0.0.1.RELEASE that we used above is nothing but a path to a Docker image, which is stored on a docker registry called Docker Hub.

Question:- Where are Docker-Images stored usually ?

Answer:- Docker-Images are stored usually in Docker-Registries.

Question:- What are the types of Docker-Registeries ?

Answer:- Following types of Registeries exists :-

  • Public docker Registry : https://hub.docker.com .From here, we can find docker-base-image for any of the repository. For ex → Image for Ubuntu, Mysql, NodeJS, Java, etc.
  • Private docker Registry : Amazon ECR. Here, we can store our own docker image files at AWS. Usually used in Enterprise eco-systems.

Question:- How do we usually use the Docker-Registry ?

Answer:- Docker registry contains a lot of repositories — different versions of different applications. Since Docker Hub is a public registry, anybody can access it. Typically, when we work in an enterprise, we use private repositories so that our application images are only accessed by people who have the right access to it.

Question:- Can we see the details of the Docker-Image, on the Docker-Registry, that we used above ?

Answer:- Yes, Let’s go to to DockerHub (i.e. our own public Docker-Registry) and see this docker-image.

  • The first part over here is called a repository. So what we are seeing in here is a repository called hello-world-java.
  • Inside this repository, we would want a specific release 0.0.2.RELEASE. And if you scroll down and go to tags, you’d see that there is one tag which is present in here for 0.0.1-RELEASE.

Question:- What’s so special about any Docker-Image ?

Answer:- Note that :-

  • A specific image contains everything that you would need to run your specific application. It contains the right software you’d need to run the application — for example the right version of Java.
  • It contains all the libraries that your application needs, and in addition to that, it might also contain any dependencies that your application might need to be able to run.
  • So the image is stored on Docker Hub (which is a public docker registry) and this specific repository for this particular image is : in28min/hello-world-java. And this specific tag of this specific image is 0.0.1.RELEASE.

Question:- What’s so special about Tags?

Answer:- We can have multiple tags for the same repository. For example → 0.0.1.RELEASE to 0.0.2.RELEASE, for different versions of the application. So the same repository can hold multiple versions of your application.

Question:- What’s happening behind the screen, when we executed this command ?

docker run -p 5000:5000 in28min/hello-world-java:0.0.1.RELEASE

Answer:-

  • You can see above screenshot that, when we executed this command, it saying I am unable to find this image locally. So what is happening is, Docker is checking locally if this image is present — it is unable to find that image locally.
  • So what it’s doing — it’s actually connecting to Docker Hub. So it’s going to Docker Hub and pulling the image from this repository. So it pulls down the entire image from the Docker Hub to your local machine.
  • And once the images downloaded, Docker is running that specific image.

Question:- What’s a Docker-Container ?

Answer:-

  • An image is something static on the repository. A Docker-Image is nothing but a set of bytes. This image is about 33 MB. When the image is downloaded onto your machine, even then, it’s just a set of bytes which are downloaded to your machine.
  • The running version of the image is called a container. When the image is running, It’s called a container.

Question:- Can we launch multiple containers with a single Docker-Image ?

Answer:- So, for one image, you can have a lot of containers which are running, but note that, we would have to use the different host-ports for each different container.

  • So we containerised our Java application and our python application And you saw that we were able to launch them up very very easily. We did not need to worry about how to install Java, how to install Python, etc. All that we needed to do, was to create an image. Here, we were having the image pre-built.
  • Another thing to note here is that, the container that we spawned is tied with the terminal.

Question:- What’s the meaning of using -p 5000:5000, in above commands?

Answer:- Whenever we run a container, it is part of internal Docker network, called a bridge network. So by default, all containers run inside the bridge network. You will not be able to access the container, unless the port is exposed outside. So what we are doing in here, is we’re taking the container port to 5000, and mapping it to a host port. So I’m saying take the container port 5000, and map it to a host port — a port on the local machine — at 5000. And the option which is used to enable us to do this is -p. -p is a shortcut for something called — publish. So we’re publishing a container port on to a host port. The first-part of this pair represents HOST-PORT and the second-part of this pair represents CONTAINER-PORT.

Question:- Let’s conclude the story so far ?

Answer:-

  • The image is actually downloaded from a public Docker registry called Docker Hub.
  • A Docker-Registry contains a number of repositories, and one of the repositories which is present in there is : hello-world-java. So in28min/hello-world-java is the name of the repository, and 0.0.1.RELEASE is a version of this specific repository.
  • A repository typically maps to a specific deployable unit, and you might have multiple versions of your deployable units — 0.0.1.RELEASE to 0.0.3.RELEASE.
  • This 0.0.1.RELEASE in here, is a tag which identifies which version of the application, which version of the deployable unit.
  • A running version of this image is called a container. The last thing which we have learned is the fact that we map the container port to a host port, so that, we can access the application which is running inside a container.

Question:- How do we run the docker-container in detached mode?

Answer:- We can do so by using “-d” option.

Question:- Let’s access our application, launched through the aforesaid container ?

Question:- How do see the logs of this detached docker now ?

Answer:- We can do so by using the container-id, we got above.

Question:- How do we get to see, which all docker images we have so far ?

Question:- How do we get to see, which all docker containers we have so far ?

  • First thing to note here is that, a container name is also assigned to each containers running. So when we were running the command, we did not really assign any name for the container. So Docker assigned a default name to every container. So you can see that there was a name called admiring_banach, that was assigned to python based container and there is a name called pedantic_hamilton, that is assigned to the java based container.
  • The interesting thing is docker container ls, only shows the containers which are running at this particular point in time.

Question:- How do we see all the containers, which are even stopped now as well ?

Answer:- We can do a docker container ls -a, to see which all containers we have right now.

Question:- How do we stop the active/running containers ?

Question:- What’s the architecture of the Docker ?

  • The first & top-most component that we have is : Docker-Client. It just sends that command out to the docker daemon.
  • Docker daemon is the one which is really responsible for execution of that specific command.

Question: For what all things, Is the docker daemon responsible for ?

Answer:- Docker-Daemon is responsible for lot of things :-

  • It is responsible for managing your containers.
  • For managing your local images.
  • It is mainly responsible for pulling the image from the image repository.
  • Create a number of Docker images on our local machine. The Docker demon is also responsible for pushing those images out to the image repository.

Question: When we ran the “docker images” command, what’s happening under the hoods ?

Answer:-

  • Docker-Client first sends the command to Docker-Daemon.
  • Then docker-daemon looks at the local images which are present, and the results are sent back. And that’s the results which we are showing in here.

Question: When we ran the “docker container ls” command, what’s happening under the hoods ?

Answer:-

  • The command that we executed, first executes on the docker client, and the docker client sends it out to the docker daemon.
  • Daemon then sees that, which all containers are running at this specific point in time and returns the same.
  • If we do “docker container ls -a” command, then daemon sends-out command and we get the list of containers which are in various kinds of statuses, including stop and running.

A lot of communication also happens between the “docker client” and the “docker daemon”.

Question: When we ran the “docker run -p 5002:5000 <reposit-name>” command, what’s happening under the hoods ?

Answer:-

  • The “docker client” is sending the command out to the “docker daemon”.
  • The “docker daemon checks locally, if this image is available. So, it will do something like docker images, and it would check, if <reposit-name> is available locally ?
  • In case, seems the <reposit-name> is available locally, it does not go to the docker registry. So it will actually directly use the local image which is present.
  • However, if docker daemon identifies that the requested image is not present locally, then docker client would talk to the docker registry, which is the public Docker registry, and it would try and download that specific image, and it would try to run it.

Question: Can you show another Docker-Image example as well ?

Answer:- Let’s see the working example with this particular image now : “in28min/hello-world-nodejs”. This is yet another image, being published by some respectable soul on the Internet. We would be launching our brand new container in detached mode, with this Image preset over Docker-Repository :-

Recall that, the first-part of this pair represents HOST-PORT and the second-part of this pair represents CONTAINER-PORT @ which the application is running. Let’s see whether the port no. 5004 is accessible or not ?

Further to this, let’s see the logs of the newly launched container :-

Question: Let’s conclude the story so far ?

Answer:-

  • Whenever we install Docker desktop, it means, we have installed docker client and docker daemon.
  • Whenever we send a command from the docker client, it’s executed by the daemon.
  • Whenever we try to run a container, docker checks the local images whether Is that specific image present locally? If it’s not present, what it does is, daemon starts talking to the image registry & gets the image down into our local images, and then starts it as a container.

Thanks for reading through this so far and we shall see you in next article. If you liked reading, do clap 👏 for this story.

References :-

--

--

aditya goel

Software Engineer for Big Data distributed systems