This is the fourth day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021
Use of Docker containers
The nature of mirror images
As mentioned in the previous article, a mirror is a read-only file system, and we can instantiate containers based on an image. This section takes a closer look at mirroring.
A mirror is essentially a stack of read-only file systems. At the bottom is a boot file system, similar to the Linux boot file system. The second layer is the root file system, which can be one or more operating systems (Debian or Ubuntu file systems), and this layer is the base image. The other images are built directly or indirectly on top of the underlying image, with each build adding a read-only file system to the file system stack of the image on which it is based. Docker loads multiple layers of file systems at once through the joint loading technology, but only provides a single file system that contains all the underlying files and directories.
Such a file system is called a mirror. In addition to the basic image, each image is created by superlaying another layer of file systems on top of its parent image. The multiple layers of file systems in the image are read-only. Therefore, the image itself is also read-only. When a new container is launched based on an image, Docker loads a read-write file system at the top of the image, where all the programs and operations we run in Docker are executed.
Another technique mentioned here is called Copy on Write. When the container is first started, its read/write layer is empty and changes only when the file system changes. For example, if we modify a file, the file will be copied from the read-only layer to the read-write layer, and then modified at the read-write layer. The read-only version before the modification still exists, but when we access the file, it is hidden by the modified file in the read/write layer.
That is, the mirror layer and the read/write layer above it, together with some configuration data, constitute the container.
Where is the image stored
We can run the docker images command on the command line to view the list of locally stored images. The result is as follows:
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest ba6acccedd29 2 weeks ago 72.8MB
Copy the code
In the previous article, we ran an Ubuntu image-based container, and Docker automatically pulled the image from the Docker Hub because there were no ubuntu images locally. In fact, if you’re running Linux, Docker keeps both the image and container files in /var/lib/docker.
Docker Hub is the official Registry maintained by Docker. The code of Docker Registry is open source, so you can also build your own Registry. Many enterprises have private Registry for internal use.
Registry contains the mirror repository, which contains the image, the layer, and metadata about the image. Each repository contains many images. For example, the Ubuntu repository contains many versions of Ubuntu, which are distinguished by tags. When we need to pull the image of a given tag, we can use the following command.
Docker pull Image: Tag Docker pull Ubuntu :12.04Copy the code
The repository name in Docker Hub has a form similar to Pseudocode/someApp in addition to the form similar to Ubuntu. These two name formats are used for images in the top-level repository and user repository, respectively, in the Docker Hub. The top-level warehouse is officially managed by Docker, and the user warehouse is created by Docker users.
In pseudocode/ someApp, pseudocode is the user name and someApp is the repository name.
We can use the repository name to search for images on the Docker Hub website, or use the Docker search command on the command line, for example:
docker search nginx
Copy the code
You get something like this:
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 15742 [OK]
...
Copy the code
How do I build/modify an image
In addition to using existing images, we can also build our own images and share them with others. Next, we will build an image and share it with Docker Hub. Therefore, we need to register an account on the Docker Hub website first.
Then login using the docker login command on the cli. If the login Succeeded message is displayed.
There are two ways to build an image: using the Docker commit command and using the Dockerfile file, as described below.
Use the commit command to build the image
As mentioned earlier, an image is usually built by adding a read-only file system layer to another image, so we can modify an existing image to create a new image.
Next, we will use the Ubuntu image as a base, install nginx in it and build a new image that contains nginx.
First, create a container based on an Ubuntu image and run bash.
docker run -i -t ubuntu /bin/bash
Copy the code
Then execute commands in the container to install Nginx.
apt-get update && apt-get install -y nginx
Copy the code
After the installation is complete, exit the container and execute the following command from the host’s command line:
docker commit 70e797fe9800 pseudocode/nginx
Copy the code
The docker commit command is followed by the container ID that you just ran, and finally by the image name of the target repository (note that you need to use your own docker Hub username for the username). After successful execution, you can use the Docker images command to see the image you just created.
Using Dockerfile is a better option than building images this way, which is more flexible and powerful.
Build the image using the Dockerfile file
Now the host creates a directory and creates a Dockerfile in it, for example:
➜ mkdir nginx_image
➜ cd nginx_image
➜ touch Dockerfile
Copy the code
This nginx_image directory is used by Docker as a build context. When building the image, Docker sends all of its contents to the Docker daemon so that the Docker daemon can directly access any data that the user wants to store in the image.
Next, you need to write the instructions in the Dockerfile. A Dockerfile is made up of a series of instructions and arguments. Here is an example:
FROM Ubuntu :14.04 RUN apt-get update && apt-get install -y nginx EXPOSE 80Copy the code
Instructions here are in uppercase letters, followed by arguments.
- FROM is the name and TAG of the parent mirror.
- The RUN command will RUN the specified command in the current image, where we have installed the Nginx server.
- EXPOSE specifies the port numbers that container applications will use.
After that, we execute the following command in the build context directory:
docker build -t="pseudocode/nginx_image" .
Copy the code
The -t parameter here specifies the name of the image (note that you will also need to use your own Docker Hub username), and finally. Dockerfile is found in the current directory.
When the execution is complete, an image named Pseudocode /nginx_image can be seen using the Docker image. Next, run a container with the image you just created:
docker run -d -p 80 pseudocode/nginx_image nginx -g "daemon off;"
Copy the code
Here the -p 80 directive tells Docker to open ports inside the container to the outside. We didn’t specify a port number to map to manually, so Docker randomly selects a free port number on the host. Nginx -g “daemon off;” Start the Nginx server in the previous way. After the container is run, use the docker ps command to view the result as follows:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0204000C73F9 Pseudocode /nginx_image "nginx -g 'daemon of..." 3 seconds ago Up 2 seconds 0.0.0.0:61190->80/ TCP wonderful_ARYabhataCopy the code
As you can see, port 80 in the container is mapped to port 61190 on the host.
Success.
There are a lot of instructions that can be used in Dockerfile files. We can use these instructions to perform various complex operations. We will write a separate article about the instructions in Dockerfile.
Share the image in the Docker Hub
If you want to share your image, you can use the Docker push command to push it to the Docker Hub.
docker push pseudocode/nginx
Copy the code
After successful execution, you can see your image on the Docker Hub website.
Remove the mirror
If an image is no longer in use, there is no need for it to occupy local storage space, and it can be removed using the docker RMI command.
docker rmi pseudocode/nginx
Copy the code
If you want to Delete a shared Repository on Docker Hub, go to the Delete Repository button in the Repository Settings and click Delete.