Dockerrrrr!!!!!

Hey! Hope you all are doing good.
I am Muhammad Mahad but you can call me sid.
I'll be sharing my Devops Journey through writing blogs and today I learned one of the most essential topic DOCKER! and I want to share it with you.

What is docker?

Docker is an open platform for developing, shipping, and running applications.
Docker enables you to separate your applications from your infrastructure so you can deliver software quickly.

What is a container?

A way to package application with all the necessary dependencies and configuration.
Portable artifact, easily shared and moved around.

Where do container live?

  1. Container Repository
  2. Private Repository
  3. Public Repository (Docker Hub)

Docker architecture

Docker uses a client-server architecture. The Docker client talks to the Docker daemon, which does the heavy lifting of building, running, and distributing your Docker containers. The Docker client and daemon can run on the same system, or you can connect a Docker client to a remote Docker daemon. The Docker client and daemon communicate using a REST API, over UNIX sockets or a network interface. Another Docker client is Docker Compose, that lets you work with applications consisting of a set of containers.

Docker Architecture Diagram

image.png

The Docker daemon

The Docker daemon (dockerd) listens for Docker API requests and manages Docker objects such as images, containers, networks, and volumes. A daemon can also communicate with other daemons to manage Docker services.

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. The docker command uses the Docker API. The Docker client can communicate with more than one daemon.

Docker Desktop

Docker Desktop is an easy-to-install application for your Mac, Windows or Linux environment that enables you to build and share containerized applications and microservices. Docker Desktop includes the Docker daemon (dockerd), the Docker client (docker), Docker Compose, Docker Content Trust, Kubernetes, and Credential Helper. For more information, see Docker Desktop.

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. You can even run your own private registry.

When you use the docker pull or docker run commands, the required images are pulled from your configured registry. When you use the docker push command, your image is pushed to your configured registry.

Docker objects

When you use Docker, you are creating and using images, containers, networks, volumes, plugins, and other objects. This section is a brief overview of some of those objects.

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. For example, you may build an image which is based on the ubuntu image, but installs the Apache web server and your application, as well as the configuration details needed to make your application run.

You might create your own images or you might only use those created by others and published in a registry. To build your own image, you create a Dockerfile with a simple syntax for defining the steps needed to create the image and run it. Each instruction in a Dockerfile creates a layer in the image. When you change the Dockerfile and rebuild the image, only those layers which have changed are rebuilt. This is part of what makes images so lightweight, small, and fast, when compared to other virtualization technologies.

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.

By default, a container is relatively well isolated from other containers and its host machine. You can control how isolated a container’s network, storage, or other underlying subsystems are from other containers or from the host machine.

A container is defined by its image as well as any configuration options you provide to it when you create or start it. When a container is removed, any changes to its state that are not stored in persistent storage disappear.

Basic Commands

Here are the top 10 essential docker commands

  1. docker - version
  2. docker pull
  3. docker run
  4. docker ps
  5. docker exec
  6. docker stop
  7. docker restart
  8. docker kill
  9. docker commit
  10. docker push

Let's understand the commands along with their usage. The following are the docker basic commands for beginners

  • docker –version
    This command is used to get the current version of the docker

Syntax: docker - -version [OPTIONS]

By default, this will render all version information in an easy-to-read layout.

  • docker pull
    Pull an image or a repository from a registry

Syntax: docker pull

To download an image or set of images (i.e. A Repository) , Once can use docker pull command

Example:
$ docker pull dockerimage

  • docker run
    This command is used to create a container from an image

Syntax : docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

The docker run command creates a writeable container layer over the specified image and then starts it using the specified command.

The docker run command can be used with many variations, One can refer to the following documentation docker run.

  • docker ps
    This command is used to list all the containers

Syntax: docker ps [OPTIONS]

The above command can be used with other options like - all or –a

docker ps -all: Lists all containers

Example:

$ docker ps

$ docker ps -a

  • docker exec
    This command is used to run a command in a running container

Syntax: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

Docker exec command runs a new command in a running container.

Refer to the following article for more detail regarding the usage of the docker exec command docker exec.

  • docker stop
    This command is used to stop one or more running containers.

Syntax: docker stop [OPTIONS] CONTAINER [CONTAINER...]

The main process inside the container will receive SIGTERM, and after a grace period, SIGKILL.
The first signal can be changed with the STOPSIGNAL instruction in the container’s Dockerfile, or the --stop-signal option to docker run.

Example:

$ docker stop my_container

  • docker restart
    This command is used to restart one or more containers.

Syntax: docker restart [OPTIONS] CONTAINER [CONTAINER...]

Example:

$ docker restart my_container

  • docker kill
    This command is used to kill one or more containers.

Syntax: docker kill [OPTIONS] CONTAINER [CONTAINER...]

Example:

$ docker kill my_container

  • docker commit
    This command is used to create a new image from the container image.

Syntax: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

Docker commit command allows users to take an existing running container and save its current state as an image

There are certain steps to be followed before running the command

First , Pull the image from docker hub
Deploy the container using the image id from first step
Modify the container (Any changes ,if needed)
Commit the changes

Example:

$ docker commit c3f279d17e0a dev/testimage:version3.

  • docker push
    This command is used to push an image or repository to a registry.

Syntax: docker push [OPTIONS] NAME[: TAG]

Use docker image push to share your images to the Docker Hub registry or to a self-hosted one.

Example:

$ docker image push registry-host:5000/myadmin/rhel-httpd:lates

Best Resources

Since Docker is a vast and very interesting topic, there are some best video tutorials from we can learn.
YouTube:
TechWorld with Nana
Kunal Kushwaha

Also the Docs himself:
docker docs

Thank-You for reading, meet you with another blog in my next step in learning journey. Till then take care!!!