[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
浙公网安备 33010602011771号