All About Docker In — 1 Page .

Khemnath chauhan
11 min readMay 16, 2020

--

INTRODUCTION:

Docker is probably the most talk about and most widely used container technology. My love for this technology started a year back. While i was still exploring to get more insight. Finally, i started exploring by creating my own virtual labs in my desktop.

To run any software we need Operating System, and to run any operating system we need hardware. To run hardware we need — Storage, RAM, CPU..etc. This is software’s dependency chain.

There are different ways to run Operating System- on Bare Metal , Virtualization and New technology that we will be discussing- Container.

Bare Metal: These are old form of creating an application environment in physical data center for running an application/software. However, they take lots of turnaround time to Spin and get up and running.

VMs:- This technology solved some of the problem faced while creating an old Bare metal environment. The turn around time is reduced and overall environment resource utilization is increased.

Compare- VMs & Container.
Docker-Architecture

Here Docker is also known as Docker Engine or Docker service or Docker program.

LET’S DIVE INTO DOCKER AND EXPLORE IT’S KEY FEATURES:-

Docker follows Client-Server architecture, which includes the three main components that are Docker Client, Docker Host and Docker Registry.

The main advantages of Docker are:

  • Resource Efficiency: Process level isolation and usage of the container host’s kernel is more efficient when compared to virtualizing an entire hardware server.
  • Portability: All the dependencies for an application are bundled in the container. This means they can be easily moved between development, test, and production environments.
Docker Architecture.

The Docker Engine

Docker Engine allows you to develop, assemble, ship, and run applications using the following components:

  • Docker Daemon: A persistent background process that manages Docker images, containers, networks, and storage volumes. The Docker daemon constantly listens for Docker API requests and processes them.
  • Docker Engine REST API: An API used by applications to interact with the Docker daemon; it can be accessed by an HTTP client.
  • Docker CLI: A command line interface client for interacting with the Docker daemon. It greatly simplifies how you manage container instances and is one of the key reasons why developers love using Docker.

Docker Client

The Docker client enables users to interact with Docker. The Docker client provides a command line interface (CLI) that allows you to issue build, run, and stop application commands to a Docker daemon.

docker build
docker pull
docker run

Docker Host:

The Docker host provides a complete environment to execute and run applications. It comprises of the Docker daemon, Images, Containers, Networks, and Storage. The daemon is responsible for all container-related actions and receives commands via the CLI or the REST API. The Docker daemon pulls and builds container images as requested by the client.

Docker Registries:

Docker Registry manages and stores the Docker images. There are two types of registries in the Docker
- Pubic Registry — Public Registry is also called as Docker hub.
- Private Registry — It is used to share images within the enterprise.
Docker Hub is the default place of docker images, its stores’ public registry.
When you execute docker pull or docker run commands, the required docker image is pulled from the configured registry. When you execute docker push command, the docker image is stored on the configured registry.

By default, the Docker engine interacts with DockerHub , Docker’s public registry instance. However, it is possible to run on-premise the open-source Docker registry/distribution, as well as a commercially supported version called Docker Trusted Registry . There are other public registries available online.

To pull an image from an on-premises registry, you could run a command similar to:

docker pull test-registry:9090/foo/bar:4.1

where you pull the version of foo/bar image with tag 4.1 from our on-premise registry located at test-registry domain, port 9090 .

If you used DockerHub instead, and 4.1 was also the latest version, you could run this command to pull the same image locally:

docker pull foo/bar

IMPORTANT CONCEPTS:

#Dockerfile:

  • Developers use Dockerfiles to build container images, which then become the basis of running containers.
  • A Dockerfile is a simple text file that contains instructions on how to build a docker image.
  • A Dockerfile specifies the operating system that will underlie the container, along with the languages, environmental variables, file locations, network ports, and other components it needs — and what the container will do once we run it.
  • When we build a docker file it should name as Dockerfile(case-sensitive)
  • With a Dockerfile, the Docker daemon can automatically build a container image.

Sample Docker file:

# Use a base image
FROM ubuntu:latest

# Set the working directory inside the container.
# Docker handles the creation of directories specified in the WORKDIR instruction if they don't already exist
WORKDIR /app

# Copy the application files from the host into the container
COPY . .

# Run commands to install dependencies and configure the application
RUN apt-get update \
&& apt-get install -y python3 \
&& apt-get clean

# Set the command to run when the container starts
CMD ["python3", "app.py"]
  1. FROM ubuntu:latest: Specifies the base image for the Docker image. In this case, we're using the latest version of the Ubuntu image as the starting point for our container.
  2. WORKDIR /app: Sets the working directory inside the container where subsequent commands will be executed. In this example, it sets the working directory to /app.
  3. COPY . .: Copies the files and directories from the host machine's current directory (where the Dockerfile is located) into the container's working directory. The first dot (.) refers to the source directory on the host, and the second dot refers to the destination directory in the container.
  4. RUN apt-get update && apt-get install -y python3 && apt-get clean: Runs commands inside the container. In this case, it updates the package lists, installs Python 3, and then cleans up any temporary files created during the installation process.
  5. CMD ["python3", "app.py"]: Specifies the command that should be executed when the container starts. In this example, it runs the app.py file using the Python 3 interpreter.

#Docker Images:

  • A Docker image is containing everything needed to run an application as a container.
  • Docker images contain executable application source code, libraries, and other dependencies that the application code needs to run as a container.
  • When you run the Docker image, it becomes one instance (or multiple instances) of the container.

#Docker Container:

  • A Docker container is a runtime instance of an image.
  • Using single docker image we can create multiple containers.

Running Images as Containers:

Images and containers are not the same — a container is a running instance of an image. A single image can be used to start any number of containers. Images are read-only, while containers can be modified, Also, changes to a container will be lost once it gets removed, unless changes are committed into a new image.

Follow these steps to run an image as container:

  • First, note that you can run containers specifying either the image name or image ID (reference).
  • Run the docker images command to view the images you have pulled locally or, alternatively, explore the Docker Hub repositories for the image you want to run the container from.

Once you know the name or ID of the image, you can start a docker container with the docker run command. For example, to download the Ubuntu 16.04 image (if not available locally yet), start a container and run a bash shell:

# This is create container with ubuntu and provide interactive terminal
docker run -i -t ubuntu:16.04 /bin/bash

DOCKER COMMANDS:

# Docker login:
Before pulling/pushing any docker image from/to docker hub repository, one needs to login to the docker hub repository. Hence this command is used to login the docker hub repository.

# Docker pull
This command is used to pull images from the docker repository. By default, this command always downloads the latest version of image, though we can specify the particular image by using the tag name. (<image_name>:<tag_name>.

pull image

# Docker build
This command is used to build the docker image from a specified Docker file. To name the image ‘-t’ flag is used.

# Docker run
This command is used to create a container from the specified image.

Create container from image
run image with name

If a port is exposed in the dockerfile, we need to map it in the container by using ‘-p’ flag.

# Docker tag
Docker tags convey useful information about a specific image version/variant. They are aliases to the ID of your image which often look like this: e477ec11d12. It’s just a way of referring to your image.
Before pushing the docker image to docker hub repository, it is necessary to tag the image as <username/image_name> or <your_private_repo_hostname /image_name>.

The two most common cases where tags come into play are:

  1. When building an image, we use the following command:
docker build -t username/image_name:tag_name .

In above command, we tell the Docker daemon to fetch the Docker file present in the current directory (that’s what the . at the end does). Next, we tell the Docker daemon to build the image and give it the specified tag. If you run docker images, you should see an image whose repository is username/image_name and tag is tag_name.

2. Explicitly tagging an image through the tag command.

docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

This command just creates an alias (a reference) by the name of the TARGET_IMAGE that refers to the SOURCE_IMAGE. That’s all it does. It’s like assigning an existing image another name to refer to it. Notice how the tag is specified as optional here as well, by the [:TAG] .

What happens when you don’t specify a tag?

When you don’t specify a tag while tagging an image. This is where the latest tag comes into the picture. Whenever an image is tagged without an explicit tag, it’s given the latest tag by default. It’s an unfortunate naming choice that causes a lot of confusion. But I like to think of it as the default tag that’s given to images when you don’t specify one.

A lot of confusion around latest is caused due to the expectation that it’s the latest version of the image, especially in Dockerfiles. Let’s consider the various scenarios with an example:

# Docker push
This command is used to push the image to the docker hub repository.

# Docker ps
This command is used to list all the running containers on the system.

To display the list of all the running and the stopped containers on the system use ‘-a’ flag.

Running Container

Docker exec
This command is used to access the docker container and run commands inside the container.

# Docker stop
This command is used to stop a running container.

Stop Running container

To stop all running container.

# Docker kill
This command is used to kill a running container immediately.

The difference between ‘docker kill’ and ‘docker stop’ is that ‘docker stop’ gives the container time to shutdown gracefully, while ‘docker kill’ command kills the running container abruptly.

Docker pause
This command is used to pause a running container.

When you do not need a container momentarily to perform administrative functions, it is convenient to pause other than stop containers.

Docker unpause
This command is used to resume a paused container.

#Docker rm
This command is used to delete (remove) a stopped container.

remove stopped dockers containers

# Docker images
This command is used to list the docker images stored on the system.

Images on hosts

#Docker rmi
This command is used to delete (remove) a docker image.

# Docker info
This command is used to view the system wide information like Docker root Directory, OS version, Kernel Version, Docker Version, RAM, CPU and Docker Registry.

# Docker search
This command is used to search the docker image with the keyword from the docker hub repository.

# Docker attach
This command to attach your terminal’s standard input, output, and error (or any combination of the three) to a running container using the container’s ID or name. attach the running container with the following command. you need to press enter once execute this command, docker is waiting for your input.

once attached, you can do all the cli commands like you can check the node version, run the bundle file, exit etc.

But the problem here once you exit out of this, your container is no logger in the up status. your container is also in exit status.

what if you want to do all this stuff without exiting the running container. that’s where docker exec command comes into picture.

Docker copy
This command is used to copy the files from local machine to inside the container and vice versa.

From docker container to host machine

Syntax

From host machine to inside the docker container

Syntax

Docker commit
This command is used to create a new image of an edited container on the local system.

docker restart
This command is used to restart the docker container with container id mentioned in the command.

Run the command below and check the STATUS parameter to verify if the container started recently.

docker network
This command is used to lists the details of all the network in the cluster.

There are several other docker network commands.

docker history
This command is used to show the history of a docker image with the image name mentioned in the command.

docker logs
This command is used to show the logs of the docker container with contained id mentioned in the command.

docker logout
This command is used to logging out from docker hub.

--

--

Khemnath chauhan
Khemnath chauhan

No responses yet