Docker for DevOps Engineers

EP 04: Docker for Beginners

Jigarkumar Rathod
7 min readJul 18, 2023

In this article, we will uncover the fundamentals of Docker. You, like me, may have heard of Docker before. You may have co-workers or developer friends who rave about it, who “dockerize” everything they can, and who are at an utter loss for words when it comes to explaining Docker, and all its glory, in terms that are understandable to the average person. Today, I will attempt to demystify Docker for you.

What is Docker?

Docker is a software platform that allows you to build, test, and deploy applications quickly. Docker packages software into standardized units called containers that have everything the software needs to run including libraries, system tools, code, and runtime. Using Docker, you can quickly deploy and scale applications into any environment and know your code will run.

Docker Architecture — Workflow with Examples:

Docker Architecture

Let’s understand Some Docker terminologies :

1)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 this command to the dockerd, which carries them out and processes them.

->Internally the docker command uses the Docker REST APIs.

2)The Docker Daemon:

->The Docker daemon (dockerd) listens for Docker API requests (docker build, docker run, docker push, docker pull, etc).

->Manages Docker objects such as images, containers, networks, and volumes. A daemon can also communicate with other daemons to manage Docker services.

3)Docker Host:

->Docker Host is used to provide an environment to execute and run containerized applications.

->It contains the docker daemon, images, containers, networks, volumes, and storage.

4)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.

Let’s take a look at Docker components

1)What is a Docker Container?

A Docker container is a running instance of a Docker image. Containers provide an isolated environment where applications run consistently across different systems and platforms. Each container has its own file system, network interfaces, and process space, ensuring that applications are isolated from one another.

Think of a Docker container as the above image. There are multiple applications running on the same machine. These applications are put into docker containers; any changes made on these containers do not affect the other container. Each container has different Os running on the same physical machine. Docker helps you to create, deploy and run applications using containers.

2)What is a Docker Engine?

At the core of Docker is the Docker Engine, also known as the Docker daemon. It is a lightweight and powerful runtime that runs and manages containers. The Docker Engine performs tasks such as building, running, and distributing containers, making it the heart of the Docker ecosystem.

3)What is a Docker Image?

A Docker image is a read-only template containing a set of instructions for creating a container that can run on the Docker platform. It provides a convenient way to package up applications and preconfigured server environments, which you can use for your 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.

4)What is a Docker Registry?

The Docker Registry is a centralized repository that stores Docker images. It serves as a distribution mechanism for sharing and pulling images across different environments. The default public registry is Docker Hub, which hosts a vast collection of pre-built images. Additionally, organizations can set up private registries to store proprietary or customized images.

5)What is a Docker Compose?

Docker Compose is a tool that allows for defining and managing multi-container applications. It uses a YAML file to specify the services, networks, and volumes required for a complex application. With Docker Compose, you can spin up multiple containers, link them together, and configure their interactions, making it easier to orchestrate and manage the application stack.

Why Docker is needed for DevOps?

Docker is essential for DevOps engineers because it offers:

1)Containerization and Portability

Docker packages applications and their dependencies into self-contained units, ensuring consistent execution across different environments.

2)Streamlined Deployment and Scalability

Docker simplifies deployment processes and enables efficient scaling with container orchestration tools like Docker Swarm or Kubernetes.

3)Faster and Reproducible Development Workflow

Docker creates reproducible development environments, reducing setup time and troubleshooting efforts.

4)Versioning

Docker allows you to version control your application, as well as the dependencies and configuration.

5)Microservices

Docker makes it easier to implement and manage a microservices architecture, allowing you to break down a monolithic application into smaller, loosely-coupled services.

The Dockerfile — where it all begins

->A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession.

->The first thing the Dockerfile needs is a base image. A base image tells the container what to install as its OS — Ubuntu, RHEL, SuSE, Node, Java, etc.

->Next, you’ll provide setup instructions. These are all the things the Docker container needs to know about: environment variables, dependencies to install, where files live, etc.

->Finally, you have to tell the container what to do. Typically it will be running specific installations and commands to the application specified in the setup instructions. I’ll give a quick overview of the most common Dockerfile commands next and then show some examples to help it make sense.

Dockerfile Commands

Below, are the commands that will be used 90% of the time when you’re writing Dockerfiles, and what they mean.

  1. FROM — this initializes a new build stage and sets the Base Image for subsequent instructions. As such, a valid Dockerfile must start with a FROM instruction.
  2. RUN — will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile.
  3. EXPOSE — 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. This makes it possible for the host and the outside world to access the isolated Docker Container.
  4. ENV — ENV in Dockerfile is used to set Environment Variables with key and value.
  5. ENTRYPOINT — ENTRYPOINT in Dockerfile is used to configure a container that you can run as an executable. ENTRYPOINT specifies a command that will execute when the Docker container starts.

Dockerfile Examples

Java Dockerfile Example

# creates a layer from the openjdk:11 Docker image
FROM openjdk:11

# copy the project JAR file to the container
COPY target/docker-java-jar-0.0.1-SNAPSHOT.jar app.jar

# execute that JAR in the entry point below
ENTRYPOINT ["java","-jar","/app.jar"]

Docker Commands

1)Docker run Command

The docker run command is used to create a new container.

To create a Container with a Container name:

docker run -it -d --name <container-name> <image-name>

To create a Container with a Port number:

docker run -it -d -p 8080:80 <container-name> <image-name>

2)Docker inspect Command

The docker inspect command is used to view detailed information about a container or image.

docker inspect <container-name>

3)Docker top Command

The docker top command is used to view the processes running inside a container.

docker top <container-name>

4)Docker save Command

The docker save command is used to save an image to a tar archive.

docker save [options] [image]

5)Docker load Command

The docker load command is used to load an image from a tar archive.

docker load [options] xyz.tar

6)Docker build Command

The docker build command is used to build an image from a specified docker file.

docker build <path to docker file>

7)Docker exec Command

The docker exec command is used to access the running container.

docker exec -it <container-id> bash

8)Docker ps Command

The docker ps command is used to list the running containers.

docker ps

The docker ps -a command is used to list the running and exited containers.

docker ps -a

9)Docker stop Command

The docker stop command is used to stop a running container.

docker stop <container id>

10)Docker kill Command

This command kills the container by stopping its execution immediately. The difference between ‘docker kill’ and ‘docker stop’ is that ‘docker stop’ gives the container time to shut down gracefully, in situations when it is taking too much time for getting the container to stop, one can opt to kill it.

docker kill <container id>

11)Docker images Command

The docker images command is used to list all the locally stored docker images.

docker images

12)Docker rm Command

The docker rm command is used to delete a stopped container.

docker rm <container id>

13)Docker rmi Command

The docker rmi command is used to delete an image from local storage.

docker rmi <image-id>

14)Docker push Command

The docker push command is used to push an image to the docker hub repository.

docker push <username/image name>

Thanks for Reading!

If you like my work and want to support me…

The BEST way is to follow me on Medium here.

--

--

Jigarkumar Rathod

I’m Jigar. Passionate about DevOps and cloud-native stuff ☁️, I like working with data a little more than I should😊 | Exploring Cloud Tech