Solve the problem that some configuration files under and etc are reset after docker container is restarted

  • 2021-10-16 05:36:17
  • OfStack

1.

The three files in the/etc/hosts,/etc/resolv. conf and/etc/hostname containers do not exist in the mirror, but in/var/lib/docker/containers/. When the container is started, these files are mounted inside the container in the form of mount.

Therefore, if you modify these files in the container, the modified parts will not exist in the container's top layer, but will be written directly to these three physical files.

2. Why doesn't the modification exist after restarting?

The reason: Every time Docker starts the container, by rebuilding a new/etc/hosts file, why?

The reason is: When the container restarts, the IP address is changed, and the original IP address in the hosts file is invalid, so the hosts file should be modified, otherwise dirty data will be generated.

3. Is there any good solution?

You can add a mapping between host and ip to the container through the add-host parameter of the docker run command.

Additional: Resolving Restart Configuration Not Effective in Docker Container (source/etc/profile)

Problem description:

During use of docker, some configuration files in the container instance are sometimes customized, such as:/etc/profile, but after modification, it is cumbersome to manually use source once every startup to make it take effect again.

Solution:

Add source/etc/profile at the end of the ~/. bashrc configuration file in the container instance, save and exit. In this way, the configuration file can be automatically refreshed after restart without manual operation.


Related articles: