Before make a container, need to the container will be hosting the hosts file sharing, online search to this article: www.cnblogs.com/runnerjack/… It doesn’t seem very useful to me, since I need to create and start the container using the API.

There are only two steps:

  1. Mount /etc to a directory in the container, for example: /root/container/ (containers do not have this directory can be created in Dockerfile)
  2. Add a link to the start container script to copy the host’s hosts to the container’s hosts (filter out lines containing 127.0.0.1).
Grep -v "127.0.0.1" /root/docker-hive/hosts >> /etc/hostsCopy the code

My Dockerfile

FROM centos:8 ARG app_name=docker-hive ENV TERM linux COPY docker-hive.jar /usr/docker-hive.jar COPY start.sh /usr/start.sh RUN mkdir -p /root/${app_name} RUN chmod +x /usr/start.sh ENTRYPOINT ["/bin/bash","/usr/start.sh"] CMD [" 1 "]Copy the code

Start. Sh content is

#! /bin/bash source /etc/profile export HADOOP_CONF_DIR=/etc/hadoop grep -v "127.0.0.1" /root/docker-hive/hosts >> /etc/hosts java -jar /usr/docker-hive/docker-hive.jar $1 $2Copy the code

Start the command

Docker run -v /data:/data -v /etc:/etc:ro -v /etc:/root/docker-hive:ro -v/TMP :/ TMP --name hive-test docker-hive:1.0  /data/12345.txt /data/log/12345.logCopy the code

The above startup commands can be created using The Java-API of Docker.

Beyond the topic:

1. Set the network mode to host when creating the container in the same way that you want to share the hosts file on the host. 2. Currently, the DNS configuration file /etc/resolv.conf is used to solve host access problems