Introduction: Learning Objectives
In this section, you will be able to:
- Identify the components of the Docker architecture,
- Explain the features of the Docker architecture components, and
- Describe the process of containerization using Docker.
The Docker client-server architecture provides a complete application environment.
Docker components include the client, the host, and the registry.
Docker Process
Let’s take a high-level view of how Docker works.
- First, you’ll use either the Docker command line interface or REST APIs via the Docker client to send instructions to the Docker host server, commonly called the host.
- The Docker host contains the daemon, known as dockerd.
- The daemon listens for Docker API requests or commands such as “docker run” and processes those commands.
- The daemon does the heavy lifting to build, run, and distribute Docker containers.
- Then, Docker stores the container images in a registry.
Docker Host
The Docker host also includes and manages:
- Images
- Containers
- Namespaces
- Networks
- Storage
- Plugins and add-ons.
Docker Communications
- You can use the Docker client to communicate with local and remote Docker hosts.
- You can run the Docker client and daemon on the same system or connect your Docker client to a remote Docker daemon.
- And Docker daemons can also communicate with other daemons to manage Docker services.
Docker Registry
- Docker stores and distributes images in a registry.
- Registry access is either public, such as Docker Hub, which is accessible by everyone, or private.
- Enterprises usually opt to use a private registry for security reasons.
- And registry locations are hosted using a third-party provider, such as IBM Cloud Container Registry, or self-hosted in private data centers or on the cloud.
Registry Access
Let’s learn about moving images into the registry.
- First, developers build and push the images using automation or a build pipeline into a registry, where
- Docker stores these images.
- Then, local machines, cloud systems, and on-premises systems can pull those images.
Docker Architecture
Let’s examine the process in more detail. Here is a visual representation of the Docker architecture, which consists of the client; the Docker host, including the Docker daemon; and the registry with its existing stored images.
Let’s check out the containerization process.
Here’s how you use Docker to create a container image:
- First, use an existing base image or a Dockerfile.
- Then, issue the build command that creates a container image with a name.
- Next, issue the push command to store the image to the registry.
- The host first checks locally if the image is already available
- And then issues the run command with the image name to create the container.
- If the image is unavailable within the host, the Docker client connects to the registry and pulls the image to the host.
- The daemon then creates a running container using the image.
Hands-On:
How to verify that docker CLI is installed?
docker --version
Pull an image from Docker Hub and run it as a container?
Use the docker CLI to list your images.
docker images
Pull your first image (Hello-World) from Docker Hub.
docker pull hello-world
List images again and you should see the Hello-World image in the list.
Run the hello-world image as a container.
docker run hello-world
You should see a ‘Hello from Docker!’ message.
List the containers to see the container ID, image name and status that indicates that the container exited successfully.
docker ps -a
Remove the exited containers using the below command.
docker container rm <container-name>
How to build an image using a Dockerfile?
Create a docker file using the below code-snippet.
FROM node:9.4.0-alpine COPY app.js . COPY package.json . RUN npm install &&\ apk update &&\ apk upgrade EXPOSE 8080 CMD node app.js
Run the following command to build the image:
docker build . -t myimage:v1
The command creates a new layer for each instruction in the Dockerfile.
List images to see your image tagged myimage:v1 in the table.
docker images
Now that your image is built, let’s run it as a container with the following command:
docker run -dp 8080:8080 myimage:v1
Run the curl command to ping and verify the application as given below.
curl localhost:8080
Now to stop the container we use docker stop followed by the container id. The following command uses docker ps -q to pass in the list of all running containers:
docker stop $(docker ps -q)
Check if the container has stopped by running the following command.
docker ps OR docker ps -a
Summary
In this section, you learned that:
- Docker architecture consists of a Docker client, a Docker host, and a registry.
- The client interacts with the host using commands and REST APIs.
- The Docker host includes the daemon, commonly called dockerd.
- The Docker host also manages images, containers, namespaces, networks, storage, plugins, and add-ons
- Containerization is the process used to build, push, and run an image to create a running container.
- And commands to pull, build and run contianers.