Talking about how to customize host file in Docker

  • 2021-11-02 03:48:40
  • OfStack

Directory 1. Commands
2. docker-compose.yml
3. dockerfile
Step 4 Modify directly
Step 5 Modify the image
Summarize

1. Q: When we really develop, the database is deployed in the intranet, and when our program connects to the database, we need to specify the address of the intranet. But sometimes we need to migrate the environment, so our back-end code has to be modified. Is there a good way not to let us modify the code?

Answer: To be sure, there is, that is, the domain name is not the IP address specified in the code. We only need to configure the domain name and IP address to establish a mapping relationship, and all the projects can achieve their goals without changing the code.

2. Q: Formal environment 1 is generally in the form of clusters, with at least 3 servers. Do we need to buy 3 domain names? Moreover, the domain name can only be bound to the public network IP. Can we disclose the database to the external network? For security, no one will expose the database to the external network. So what should I do?

A: That is to modify the host file, in host customize our domain name and database cluster intranet IP. On the physical machine linux, you can directly modify the/etc/host file.

If our service is deployed on linux as docker. So how to modify host in docker?

1. Command

1. The images of formal environment are compiled in advance. If the images compiled by docker-compose are not adopted, it is difficult to modify host, so we can only configure them through parameters at startup.


docker run --add-host=www.scalerwang.com:192.168.1.100 --add-host=blog.scalerwang.com:192.168.1.200 --name wangscaler -it mydocker

Specify with command arguments--add-host

2. docker-compose.yml

The host container specified in yml and compiled automatically configures the host file. The relevant contents of yaml are as follows


services:
  service-nginx:
    image: nginx
    extra_hosts:
    - "www.scalerwang.com:192.168.1.100"
    - "blog.scalerwang.com:192.168.1.200"

3. dockerfile

dockerfile has no direct parameters to modify host file. If you want to modify host file through dockerfile, you need to prepare host file in advance and put it in your code root directory


MAINTAINER WangScaler@163.com
ADD / /wangscaler
RUN cat /wangscaler/hosts >> /etc/hosts

This is also possible.

Step 4 Modify directly

If you don't bother, you can try. Of course, if your program backstage start, your environment is not right to start up, you can not go in to modify.
This method 1 is tiring (the more the number of clusters, the more tired), 2 is not applicable (docker can't run without this correct host, and it is possible that the domain name you set is someone else's, so make a request to someone else).

Step 5 Modify the image

If your project is deployed in the form of docker, it is often compiled into mirrors through dockerfile, docker-compose first. At this point, you can pull down the image, modify the host and push it back to cover the original image.

Summarize

The best way to use is the first two, saving time and effort, and getting it done once. If you are a mirror of dockerfile compilation, choose the first one; If you are compiled by docker-compose, you can directly modify it by docker-compose. yml. The third kind is also acceptable, but you should think you don't know the last two kinds, so don't try to do them.
For more ways to use Docker, such as specifying IP address when creating containers, and how dockerfile and docker-compose create containers mentioned above, please refer to the previous article Docker Building and Basic Commands.


Related articles: