Docker

we used to have an infrastructure where we used to run an application on one server and if we want to run another application we need to get another server which is not cool. to overcome this VMware introduced a virtual machine where we can run more than one application on the server. But every virtual machine needs its os and which makes the system slow and requires space in the hardware. The containers came into action where more than one application is run on a single os.

Virtual Machine Vs Containers

Docker vs Virtual Machines (VMs) : A Practical Guide to Docker Containers  and VMs

To containerize the applications we have tools like docker.

Docker Architecture

Docker is an open-source platform for developing, shipping and running applications. Docker uses a client-server architecture. The docker CLI talks with the Docker daemon about which action should perform on containers via REST API. And to work with a set of containers we have Docker Compose.

Docker Architecture Diagram

Docker daemon

Dockerd listens to the API calls from the Docker CLI and manages images, containers, networks, and volumes

Docker Client

This is the layer where a user gives commands like docker run and docker pull etc. to the docker daemon. docker command uses Docker API.

Docker Registries

Docker registry stores Docker images. Docker Hub is a public registry. when we pull the images docker by default pulls them from the Docker Hub.

Docker objects

Images, containers, networks, volumes, and plugins are some of the objects Docker uses.

What is a Container?

containers are a form of lightweight virtualization. The container is a sandboxed process on the machine that is isolated from all other processes on the host machine. the container is a runnable instance of images. the container can be run on local machines, virtual machines or deployed to the cloud. The containers are runnable instances of an image and we can stop, start, move, or delete a container with the help of Docker CLI and APIs

what is a Container Image?

As we know that containers run in an isolated file system. This custom file is provided by a container image. Image contains everything needed to run an application like all dependencies, configurations, scripts, binaries, etc.

Docker Commands

docker pull image_name

we can pull the images from the Docker Hub or private repository via

Docker Pull command

We can also mention the particular version of Ubuntu via

We can see that when it is pulling the image, it is pulling some layers with hash values. If that is already present, it will not be pulled; only layers that are required will be pulled.

Docker run

To run an image we use docker run command

To run a container in the detached mode we can use the command

docker ps

displays a list of running containers

Docker container restart

docker stop

To stop the container we have docker stop and then start the container again using docker start with the help of container id.

When we run multiple containers of the same image but with different versions, they both listen to the same port, as mentioned in the logs, and we can overcome this by specifying the port to the container.

Docker logs

we can view the logs of the container using docker logs with help of Container ID and also with Container Name. In the above image, we used the Redis image.

we can also name the container of our own

Here we stopped the docker container and run by naming this as redis-older. when we ran with default then it was named sharp_base and we changed it to redis-older.

Docker exec

To navigate into the directory of a container and check the configurations of a container we can exec into the container using the docker exec command.

Docker File Instructions

The docker file should be named Dockerfile and according to the application requirements we need to mention all the instructions in that file.

  • FROM [base image]

  • COPY [copy files from local to the container]

  • ARG [Pass arguments]

  • ENV [Environment variables]

  • RUN [Any arbitrary shell command]

  • EXPOSE [open port from container starts]

  • CMD [Commands to run when the container is started]

  • WORKDIR [Create a directory where all the files are copied and used]

Building a docker Image

  • First, we need to create Dockerfile and mention the instructions required to build an application. We consider a simple Golang application.
FROM golang:1.19
RUN mkdir /app
ADD . /app 
WORKDIR /app
RUN go build -o main .
CMD ["/app/main"]
  • To build a docker image we use the command

    docker build -t <ImageName>

Removing Docker Images

  • docker images -q Gives all the images IDs

  • docker rmi <imageID> removing one image

  • docker rmi $(docker images -q) -f

    Note: Only images that are not running are removed and cannot remove the images that are running

  • docker inspect <imageID> to inspect an image

Did you find this article valuable?

Support manda supraja by becoming a sponsor. Any amount is appreciated!