docker 使用wget后文件消失

2023-10-23 14:25:58 浏览数 (3)

发现一个挺有意思的问题,之前一直没发现docker的这个问题,今天在改 Dockerfile 的时候发现,构建后运行容器,进行容器内部发现一个配置文件丢失了。 检查命令没有问题,查看日志也没有问题。

build日志如下:

代码语言:javascript复制
--2022-03-15 10:02:31--  https://raw.githubusercontent.com/protocol/deployment/master/main_net_config.conf
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.111.133, 185.199.110.133, 185.199.109.133, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.111.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 17180 (17K) [text/plain]
Saving to: '/java/config/main_net_config.conf'

     0K .......... ......                                     100% 28.5M=0.001s

2022-03-15 10:02:31 (28.5 MB/s) - '/java/config/main_net_config.conf' saved [17180/17180]

各种验证后,可以确定不是 Dockerfile 的问题,日志也是提示 saved,大概率是docker的问题,各种找解决方法

有问题的写法

代码语言:javascript复制
FROM centos7/centos7

ENV TMP_DIR="/build"
ENV JDK_TAR="jdk-8u202-linux-x64.tar.gz"
ENV JDK_DIR="jdk1.8.0_202"
ENV JDK_MD5="0029351f7a946f6c05b582100c7d45b7"
ENV BASE_DIR="/java"


RUN set -o errexit -o nounset 
    && yum -y install git wget 
    && wget -P /usr/local https://github.com/frekele/oracle-java/releases/download/8u202-b08/$JDK_TAR 
    && echo "$JDK_MD5 /usr/local/$JDK_TAR" | md5sum -c 
    && tar -zxf /usr/local/$JDK_TAR -C /usr/local
    && rm /usr/local/$JDK_TAR 
    && export JAVA_HOME=/usr/local/$JDK_DIR 
    && export CLASSPATH=$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar 
    && export PATH=$PATH:$JAVA_HOME/bin 
    && echo "git clone" 
    && mkdir -p $TMP_DIR 
    && cd $TMP_DIR 
    && git clone https://github.com/protocol/java.git 
    && cd java 
    && git checkout master 
    && ./gradlew build -x test 
    && cd build/distributions 
    && unzip -o java-1.0.0.zip 
    && mv java-1.0.0 $BASE_DIR 
    && wget -P $BASE_DIR/config https://raw.githubusercontent.com/protocol/deployment/master/main_net_config.conf
    && rm -rf $TMP_DIR 
    && rm -rf ~/.gradle 
    && mv $JAVA_HOME/jre /usr/local 
    && rm -rf $JAVA_HOME 
    && yum clean all

解决后的写法

将wget拆出来,单独写一个 RUN 执行。

代码语言:javascript复制
FROM centos7/centos7

ENV TMP_DIR="/build"
ENV JDK_TAR="jdk-8u202-linux-x64.tar.gz"
ENV JDK_DIR="jdk1.8.0_202"
ENV JDK_MD5="0029351f7a946f6c05b582100c7d45b7"
ENV BASE_DIR="/java"


RUN set -o errexit -o nounset 
    && yum -y install git wget 
    && wget -P /usr/local https://github.com/frekele/oracle-java/releases/download/8u202-b08/$JDK_TAR 
    && echo "$JDK_MD5 /usr/local/$JDK_TAR" | md5sum -c 
    && tar -zxf /usr/local/$JDK_TAR -C /usr/local
    && rm /usr/local/$JDK_TAR 
    && export JAVA_HOME=/usr/local/$JDK_DIR 
    && export CLASSPATH=$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar 
    && export PATH=$PATH:$JAVA_HOME/bin 
    && echo "git clone" 
    && mkdir -p $TMP_DIR 
    && cd $TMP_DIR 
    && git clone https://github.com/protocol/java.git 
    && cd java 
    && git checkout master 
    && ./gradlew build -x test 
    && cd build/distributions 
    && unzip -o java-1.0.0.zip 
    && mv java-1.0.0 $BASE_DIR 
    && rm -rf $TMP_DIR 
    && rm -rf ~/.gradle 
    && mv $JAVA_HOME/jre /usr/local 
    && rm -rf $JAVA_HOME 
    && yum clean all

RUN wget -P $BASE_DIR/config https://raw.githubusercontent.com/protocol/deployment/master/main_net_config.conf

官方文档 https://docs.docker.com/develop/develop-images/dockerfile_best-practices/

0 人点赞