Background - Sometimes it is necessary to temporarily modify it in the container
Docker brings great convenience to on-site deployment. We can block both code and dependencies into a Docker image, and then load it into a container through Docker for easy deployment.
In addition, we can weave multiple containers, such as Nginx containers, application containers, and database containers through Docker-compose to form a complete set of services.
However, sometimes, when we arrive on the scene, we need to make some small adjustments and modifications to the temporary code.
After adjustment, it is often necessary to restart the container or service to make the modification take effect. At this time, people often use:
- docker restart container name, or
- docker-compose up -d
Restart the container. But what is the difference between the two?
docker-compose up -d
If we have modified it, for example, added or modified:
- Exposed ports
- Mapping files
- Environment variables
At this time, using docker-compose up -d can make these modifications take effect.
But be aware that docker-compose up -d will:
- Stop the original container
- Delete the original container
- Use new parameters, combined with the original image file, to create a new container to provide services
At this time, if you make modifications in the original container, if you docker-commit is not used for persistence, it will be washed away and will not be left behind.
docker restart container name
If you use the docker restart container name, restart the container. Then the temporary modifications were made in the docker container before and will not be washed away. docker will provide services using modified code.
However, this method has no way to modify the port number exposed in docker-compose, and there is no way to perform volume mapping and other operations.
Use docker commit to preserve temporary modifications
If you feel that these temporary modifications are not problematic after on-site testing, and want to be retained, you can use docker commit container name on the host. Image name: tag to commit the temporary modifications into the image.
In this way, even if docker-compose up -d is used later, the previous modifications can be preserved as long as the image name: tag after commit is used in the file.
Give an example
Temporarily modify the code in the container
If we are on the scene, we found that a piece of python code in the docker container needs to be modified, sodocker exec -it container name /bin/sh
Enter the container and find the corresponding code to modify it.
After modification, it can be used on the host machinedocker restart container name
Restart. The modified code will take effect.
Adjustment configuration
If you are on site, you find that you want to modify the port of the container, or add or modify the environment variable configuration. It needs to be modified. Then use docker-compose up -d to make the modified configuration take effect.
However, be aware:docker-compose up -d
The original image will be stopped and deleted, and the original image in the system will be used, combined with the new configuration in it to generate a new image and provide services.
At this time, if there are temporary modifications in the original container, such as modifying a certain piece of python code in the original container, these modifications will be flushed out.
Therefore, be careful when using it.
Use docker commit to save the container's modification to the image
For example, we modified some code in the my_container container on site, and we hope to preserve these modifications.
Assuming that the corresponding image name of this container is my_image:1.0.1, we can use:
docker commit my_container my_image:1.0.1
Save the modification to the original image. Of course, you can also save the modification to my_image:1.0.2.
But remember to change the corresponding image name to my_image:1.0.2 in the file.
The above described method is more suitable for scenes where there is no Internet connection on site.
This is the end of this article about the difference between Docker compose up -d and Docker restart. For more related contents of Docker compose up -d and Docker restart, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!