Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to modify hosts in Docker

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to modify hosts in Docker. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Docker modified hosts? It's not easy, just open vim and knock on it! However, things went against our wishes, and things were not as simple as we thought. In many scenarios, for example, we need to build a cluster. In this case, the container needs to add host parsing to identify the nodes in the cluster. At this point, you need to modify the hosts file of the container, and we will see that it is not so easy to automate the modification of hosts in Docker.

The origin of the problem

In fact, hosts files are not stored in the Docker image. / etc/hosts, / etc/resolv.conf and / etc/hostname are stored in the / var/lib/docker/containers/ (docker_id) directory on the host. When the container is started, these files are mounted to the container through mount. So if you modify these files in the container, the modifications will not exist in the container's top layer, but will be written directly to the three files. The reason why the modified content does not exist after container restart is that every time Docker creates a new container, it will re-establish the hosts file based on the IP information of all nodes under the current docker0. In other words, your changes will be automatically overwritten by Docker.

Solution.

It seems easy to modify hosts at first glance. According to the above analysis, it is not so simple. If a distributed system is on dozens of nodes, it is troublesome to modify hosts every time it is restarted. At present, there are the following ways to solve this problem.

Add parameters when opening

Add the parameter-add-host machine:ip when opening the container to modify the hosts, and the machine host can be identified in the container. The downside is that commands with many nodes can be long and a little uncomfortable (of course, you can write a script).

Enter the container to modify

After entering the container, you can edit and modify the hosts file with a variety of editors, which is the simplest and most rude way, but the disadvantage is that you have to modify it every time you start the container.

Dnsmasq

Enable dnsmasq to resolve the hostname of the container node in the LAN in each container, but this method does not seem to be very convenient when crossing physical machines (the specific situation has not been practiced yet, and the digging hole will be filled in later). You can refer to kiwenlau's work

Modify the container hosts lookup directory

We can find a way to get the container to open instead of looking for the / etc/hosts file, but to find our own defined hosts file. Here is an example of Dockerfile.

1234567FROM ubuntu:14.04RUN cp / etc/hosts / tmp/hosts # it is best to keep the same path length RUN mkdir-p-/ lib-override & & cp / lib/x86_64-linux-gnu/libnss_files.so.2 / lib-overrideRUN sed-I-I tmp/hosts To modify / tmp/hosts at will.

After this modification, the container will not look for / etc/hosts every time it starts, but the / tmp/hosts defined by ourselves, which can be modified at will.

Summary

The above problems can be used to deploy distributed Docker clusters across hosts. Combined with the previous interconnection of Docker networks across physical machines, distributed clusters can be deployed quickly and efficiently.

Original text link:

Https://wongxingjun.github.io

-more articles-

The first experience of Spring Cloud GateWay

Kubernetes Foundation and Architecture

Monitoring No.118 of business system

Kubernetes object model

Please! In the interview, please don't ask me about the basic principles of Spring Cloud.

[the way of performance optimization] Spring Cloud parameter optimization under tens of thousands of concurrency per second

Core Technology and implementation principle of Docker

Don't talk to me about interests, No.88.

The correct use posture of conditional note @ ConditionalOnBean

The implementation principle of distributed transaction

The correct use posture of conditional note @ ConditionalOnBean

The above is how to modify hosts in Docker. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report