[Dockerfile] 在Docker build阶段更改hosts


怎么在docker build阶段修改hosts啊?试了3种方法:

1.直接在dockerfile里修改/etc/hosts: 不行

2.docker build的时候用--add-host 设置:可行

但是,host太多的时候,写成一长串很不方便

3. 让容器不去找/etc/hosts,而是去找我们自定义的hosts文件: 可行

 

# Change hosts finding path
RUN cp /etc/hosts /tmp/hosts
RUN mkdir -p -- /lib-override-hosts && cp /lib/x86_64-linux-gnu/libnss_files.so.2 /lib-override-hosts
RUN sed -i 's:/etc/hosts:/tmp/hosts:g' /lib-override-hosts/libnss_files.so.2
ENV LD_LIBRARY_PATH /lib-override-hosts

# Add hosts by environment
RUN if [ "$TEST_ENV" = "PROD1Set" ]; then \
	echo "Need to add hosts for "${TEST_ENV}; \
	cat myHosts >> /tmp/hosts; \
else \
	echo "No need to add hosts for "${TEST_ENV}; \
fi

# Check hosts
RUN if [ "$REGION" = "US" ]; then \
	host test1.company.com; \
	getent ahosts test1.company.com; \
else \
	host test2.company.com; \
	getent ahosts test2.company.com; \
fi

 

  

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM