Application scenarios
In some cases, we may need to modify the official docker image, but first we need to understand the difference between image: image and container:
- Mirror image: Similar to classes in java
- Container container: similar to objects in java
Therefore, we often download the official image from the docker hub, then deploy the image to the server, and then, when we really use the service, we often generate a container based on the image and then run the container.
It is very similar to the concepts of classes and objects in Java. Mirror is a class. After generating objects based on classes, the real use of objects is the object, and the corresponding docker is the container.
Modify the official docker image
First of all, if we understand the previous concepts about mirroring and containers, we will understand that what we need to modify is the mirror. If we just make changes while the container is running, it will not affect the mirror.
Let me explain it in detail with one of my examples:
My requirement is to update the Apache http server version in the matomo image
1. Download the mirror
First, download the image, you can download it from the docker hub or from your own server.
I was originally thinking that since the update is the Apache http server version in matomo, wouldn’t it be better if I just update matomo to the latest?
Unfortunately, the result is not as I thought. In the latest matomo, Apache version is still too low, so I can only modify the official image.
2. Start the container
Find all images:
docker images
Find the image you need to modify, save the image id
Then, enter the image you want to modify (actually the container that runs the image):
docker run -it ‘Mirrorid' /bin/sh
At this time, you will enter the shell command line inside the container. It is actually a mini Linux system, and then you can use Linux commands to operate the container. I have upgraded the Apache version here:
You can do the changes you need here
apt-get update //Get update informationapt-get upgrade //Updated version
If the following error occurs
$ apt-get update
Reading package lists... Done
E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)
E: Unable to lock directory /var/lib/apt/lists/
Explain the permission issue. It is possible that you don’t know the password, so, at this time, keep the container in the current window running, do not move, and then open a terminal.
In the new terminal, enter
docker ps -a
According to the image id, find the container you are running in another terminal
Notice:
Why do you need to hang another terminal? It is because if some containers exit, the container id will not be found. The following command requires the container id
docker exec -u root -it <container_id> /bin/bash
At this time, log in to the container through the administrator account, and then execute the apt-get update and apt-get upgrade mentioned above in turn.
3. Generate a new image
- After modification, first execute exit to exit the container
- Then look for the container information that just ran:
docker ps -a
Use the docker commit command to package it into a mirror from the container:
Among them, the notes and the author are actually not important. Write them casually. The container id is the container id you obtained when searching for container information in the previous step. Find the container you just modified. The mirror repository is the repository of the new image you packaged and generated.
docker commit -m “Remark” -a “author” containerid Mirrorrepository
Finally, docker images again and you can see the new image you just generated
Finally, add a small function, how to modify the mirrored tag:
docker tag IMAGEID(Mirrorid) REPOSITORY:TAG(storehouse:Label)
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.