A brief discussion on how to customize the host file in Docker

A brief discussion on how to customize the host file in Docker

1. Question: When we actually develop, the database is deployed on the intranet, and when our program connects to the database, we need to specify the intranet address. But sometimes we need to migrate the environment, so our backend code has to be modified accordingly. Is there a good way to prevent us from modifying the code?

Answer: It is certain that there is, and that is, what is specified in the code is not the IP address, but the domain name. We only need to configure the domain name and IP address to establish a mapping relationship, and all projects can achieve the goal without changing the code.

2. Question: The official environment is usually in the form of a cluster, with at least three servers. Do we need to buy three domain names? Moreover, the domain name can only be bound to the public IP. Can we make the database public to the external network? For security reasons, no one will open the database to the external network. What should we do then?

Answer: That is to modify the host file and customize our domain name and the intranet IP of the database cluster in the host. On a physical Linux machine, simply modify the /etc/host file.

If our service is deployed on Linux in the form of docker. So how do you modify the host in docker?

1. Command

Generally, the images of the formal environment are compiled in advance. If the image is not compiled by docker-compose, it is difficult to modify the host, so we can only configure it 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

Use command parameters to specify --add-host

2. docker-compose.yml

Specify in yml that the compiled docker container automatically configures the host file. The yaml related content is 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 does not have direct parameters to modify the host file directly. If you want to modify the host file through Dockerfile, you need to prepare the host file in advance and put it in the root directory of your code.

MAINTAINER [email protected]
ADD //wangscaler
RUN cat /wangscaler/hosts >> /etc/hosts

This is also possible.

4. Direct modification

If you don't mind the effort, you can try it. Of course, if your program is started in the background and cannot be started due to the wrong environment, you cannot go in and modify it.
This method is tiring (the more clusters there are, the more tiring it is), and it is not applicable (docker cannot run without the correct host, and it is also possible that the domain name you set is someone else's, in which case you will initiate a request to that person).

5. Modify the image

If your project is deployed in the form of docker, it is often compiled into an image through dockerfile and docker-compose first. At this time, you can pull down the image, modify the host, and then push it up again to overwrite the original image.

Summarize

The best methods are the first two, which save time and effort and can be done in one go. If you are compiling an image with dockerfile, choose the first option; if you are compiling with docker-compose, just modify docker-compose.yml once and for all. The third one is also acceptable, but for the last two, just pretend you don’t know and don’t try to do them.
For more information about how to use Docker, such as specifying an IP address when creating a container, and how to create a container using Dockerfile and Docker-compose, please refer to the previous article Docker Setup and Basic Commands.

This is the end of this article about how to customize the host file in Docker. For more information about customizing the host file in Docker, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Solution to the problem of not being able to obtain the hostname of the host in the docker container
  • Each time Docker starts a container, the IP and hosts specified operations
  • Docker container custom hosts network access operation
  • Docker network single host network and use cases
  • Docker Cannot connect to the Docker daemon. Is the docker daemon running on this host error solution

<<:  Detailed explanation of the idea of ​​achieving the point-earning effect with CSS animation

>>:  In-depth analysis of the role of HTML <!--...--> comment tags

Recommend

Solution to the gap between divs

When you use HTML div blocks and the middle of th...

Use Visual Studio Code to connect to the MySql database and query

Visual Studio Code is a powerful text editor prod...

Detailed tutorial on installing Hbase 2.3.5 on Vmware + Ubuntu18.04

Preface The previous article installed Hadoop, an...

react-beautiful-dnd implements component drag and drop function

Table of contents 1. Installation 2.APi 3. react-...

MySQL 8.0.22 winx64 installation and configuration graphic tutorial

mysql 8.0.22 winx64 installation and configuratio...

Summary of MySQL lock related knowledge

Locks in MySQL Locks are a means to resolve resou...

Fixed table width table-layout: fixed

In order to make the table fill the screen (the re...

Steps to install MySQL using Docker under Linux

As a tester, you may often need to install some s...

Detailed explanation of how to use the calendar plugin implemented in Vue.js

The function to be implemented today is the follo...

mysql replace part of the field content and mysql replace function replace()

[mysql] replace usage (replace part of the conten...

Detailed explanation of several methods of JS array dimensionality reduction

Dimensionality reduction of two-dimensional array...

Simple Mysql backup BAT script sharing under Windows

Preface This article introduces a simple BAT scri...

Learn about JavaScript closure functions in one article

Table of contents Variable Scope The concept of c...

The correct way to use Homebrew in Linux

Many people use Linux Homebrew. Here are three ti...