Docker Continued Part-II

PATIL PRASAD
9 min readAug 6, 2023

--

checkout part-I first ……

In this, we will see docker architecture in deep

  • Docker is a client-server architecture.
  • The entire docker architecture comprises four major components:
  • Docker Client
  • Docker Registry
  • Docker Host
  • Docker Objects

Docker Client:

  • The Docker Client component is the element that enables the developers or users to interact with the Docker platform.
  • The Docker Client can be present over the same host as that of the Docker daemon. If not that, the client can also connect to the daemon that is available upon a remote host.
  • A docker client intends to communicate with one or more daemon.

Docker Registries

  • Docker Registries are more like the location where all the Docker Images are stored.
  • There is no barrier to the registries being either public or private docker registries.
  • The developers can create their own private registries with ideal measures.
  • Docker Hub is one default registry of the platform that has a stock of docker images. It is public and is accessible to all.

Docker Host

  • Docker Host intends to offer an environment that is ready for running and executing the applications.
  • Docker Host comprises docker images, daemon, Networks, Storage, and Containers. — Host networking has a different set of advantages for Docker Host and the platform as a whole.

Docker Objects

Images

  • An image is a read-only template with instructions for creating a Docker container.
  • A docker image is described in a text file called a Dockerfile, which has a simple, well-defined syntax.
  • An image does not have states and never changes.
  • Docker Engine provides the core Docker technology that enables images and containers.
  • Docker Images proposes a collaboration between developers and the docker containers.
  • Docker images can be called or pulled from a public or private registry.
  • You can create a docker file that contains a variety of instructions and use it to create docker images.
  • The base layer of a docker image is read-only, while the top-most layer is open to be written by developers. Remember, every time you edit one dockerfile, rebuild and remodify it; only the part that is modified will be rebuilt over the top layer of docker image.

Dockerfile

  • An image is a read-only template with instructions for creating a Docker container.
  • This file can be used to create Docker Image.
  • These images can be pulled to create containers in any environment.
  • When you run a docker image, you get docker containers.
.dockerignore file : This helps to avoid unnecessarily sending large or sensitive files and directories to the daemon and potentially adding them to images using ADD or COPY


# Format
INSTRUCTION arguments

FROM : The FROM instruction specifies the Parent Image from which you are building.
COPY : The COPY instruction copies new files or directories from <src> and adds them to the filesystem of the container at the path <dest>.
RUN : The RUN instruction will execute any commands in a new layer on top of the current image and commit the results

CMD : The main purpose of a CMD is to provide defaults for an executing container. These defaults can include an executable, or they can omit the executable, in which case you must specify an ENTRYPOINT instruction as well.

ENV : The ENV instruction sets the environment variable <key> to the value <value>.
If an environment variable is only needed during build, and not in the final image, consider setting a value for a single command instead using RUN/ARG

ARG :
SHELL : The SHELL instruction allows the default shell used for the shell form of commands to be overridden
ENTRYPOINT : An ENTRYPOINT allows you to configure a container that will run as an executable.
You can override the ENTRYPOINT instruction using the docker run --entrypoint flag.

VOLUME : The VOLUME instruction creates a mount point with the specified name and marks it as holding externally mounted volumes from native host or other containers

USER : The USER instruction sets the user name (or UID) and optionally the user group (or GID) to use as the default user and group

WORKDIR : The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile.

STOPSIGNAL : The STOPSIGNAL instruction sets the system call signal that will be sent to the container to exit

HEALTHCHECK : The HEALTHCHECK instruction tells Docker how to test a container to check that it is still working. This can detect cases such as a web server that is stuck in an infinite loop and unable to handle new connections, even though the server process is still running.

ONBUILD
ARG
LABEL : The LABEL instruction adds metadata to an image
you are building.
COPY : The COPY instruction copies new files or directories from <src> and adds them to the filesystem of the container at the path <dest>.
RUN : The RUN instruction will execute any commands in a new layer on top of the current image and commit the results
EXPOSE : The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime. You can specify whether the port listens on TCP or UDP, and the default is TCP if the protocol is not specified.
The EXPOSE instruction does not actually publish the port. It functions as a type of documentation between the person who builds the image and the person who runs the container, about which ports are intended to be published. To actually publish the port when running the container, use the -p flag on docker run to publish and map one or more ports, or the -P flag to publish all exposed ports and map them to high-order port

ADD : The ADD instruction copies new files, directories or remote file URLs from <src> and adds them to the filesystem of the image at the path <dest>.ckerignore file : This helps to avoid unnecessarily sending large or sensitive files and directories to the daemon and potentially adding them to images using ADD or COPY


allows the default shell used for the shell form of commands to be overridden
ENTRYPOINT : An ENTRYPOINT allows you to configure a container that will run as an executable.
You can override the ENTRYPOINT instruction using the docker run --entrypoint flag.

VOLUME : The VOLUME instruction creates a mount point with the specified name and marks it as holding externally mounted volumes from native host or other containers

USER : The USER instruction sets the user name (or UID) and optionally the user group (or GID) to use as the default user and group

WORKDIR : The WORKDIR instruction sets the working directory for any RUN, CMD, ENTRYPOINT, COPY and ADD instructions that follow it in the Dockerfile.

STOPSIGNAL : The STOPSIGNAL instruction sets the system call signal that will be sent to the container to exit

HEALTHCHECK : The HEALTHCHECK instruction tells Docker how to test a container to check that it is still working. This can detect cases such as a web server that is stuck in an infinite loop and unable to handle new connections, even though the server process is still running.

ONBUILD
ARG
LABEL : The LABEL instruction adds metadata to an image
you are building.
COPY : The COPY instruction copies new files or directories from <src> and adds them to the filesystem of the container at the path <dest>.
RUN : The RUN instruction will execute any commands in a new layer on top of the current image and commit the results
EXPOSE : The EXPOSE instruction informs Docker that the container listens on the specified network ports at runtime. You can specify whether the port listens on TCP or UDP, and the default is TCP if the protocol is not specified.from <src> and adds them to the filesystem of the image at the path <dest>.

Example of Dockerfile:

Steps:

  • Create a file named ‘Dockerfile’
  • By default on building, docker searches for ‘Dockerfile’ $ docker build -t myimage:1.0 .
  • During building of the image, the commands in RUN section of Dockerfile will get executed. $ docker run ImageID
  • The commands in CMD section of Dockerfile will get executed when you create a container out of the image.

Containers

  • A runnable instance of an image is known as a container.
  • Your application is now running here. Container management is possible with the Docker API or CLI.
  • You can attach storage to a container, connect it to one or more networks, or even construct a new image depending on the state of the existing one.

Networking

  • Docker Networking allows you to create a Network of Docker containers managed by a master node called manager.
  • Container inside the docker can talk to each other by sharing packets of information. They can talk to each other using just the container name (without localhost, port no. , etc.) because they are in the same network.
  • The applications that run outside of Docker are going to connect to them from outside from the host using local host and the port No.

Storage

It explains your ability to store the data within the layer of the container that is writable. But for that, you will need a storage driver. The storage is non-persistent, for which the data will perish anytime the container is not under the run. In terms of persistent storage, Docker intends to offer few options that include:

  • Data Volumes
  • Data Volume Container
  • Directory Mounts
  • Storage Plugins

Docker Engine

It is responsible for the overall functioning of Docker platform. It is a client-server based application with three components:

(i) Server — which runs the daemon

(ii) Rest API — deals with the interaction of applications with their server

(iii) Client — which is nothing but the command line interface (CLI)

Docker Daemon

It is the heart of the Docker architecture, which does the crucial work of building, running, and distributing the containers. It also manages the Docker images and the containers.

Getting started with Docker commands

  • docker version – Echoes Client’s and Server’s Version of Docker
  • docker images – List all Docker images
  • docker build <image> – Builds an image from a Docker file
  • docker save <path> <image> – Saves Docker image to .tar file specified by path
  • docker run – Runs a command in a new container.
  • docker start – Starts one or more stopped containers
  • docker stop <container_id> – Stops container
  • docker rmi <image> – Removes Docker image
  • docker rm <container_id> – Removes Container
  • docker pull – Pulls an image or a repository from a registry
  • docker push – Pushes an image or a repository to a registry
  • docker export – Exports a container’s filesystem as a tar archive
  • docker exec – Runs a command in a run-time container
  • docker ps – Show running containers
  • docker ps -a – Show all containers
  • docker ps -l – Show latest created container
  • docker search – Searches the Docker Hub for images
  • docker attach – Attaches to a running container
  • docker commit – Creates a new image from a container’s changes
  • docker network ls
  • docker network create *network_name*

Examples:

  • docker build java-application - builds a java-application image.
  • docker pull redis - pulls the redis image (the latest version).
  • docker run redis - runs the redis image as a container.
  • docker run redis:4.0 - runs the redis image (version 4.0) as a container.
  • docker run -d redis runs the redis image as a container in a detached mode.

In next part we will see How to build aDocker Image and Push to Docker Hub

--

--