RUN 可以运行任何被基础镜像支持的命令。如基础镜像选择了centos,那么软件管理部分只能使用 centos 的命令。该指令有两种格式:
# the command is run in a shell - `/bin/sh -c` RUN <command> # exec form RUN ["executable", "param1", "param2" ... ]
实例:下面是 Redis 的部分 Dockerfile文件内容。
FROM debian:stretch-slim # add our user and group first to make sure their IDs get assigned consistently, # regardless of whatever dependencies get added RUN groupadd -r redis && useradd -r -g redis redis
上面中使用 RUN 命令运行 groupadd 和 useradd 命令添加组和用户信息。
也可以使用exec格式RUN ["executable", "param1", "param2"]的命令,如:
RUN ["apt-get","install","-y","nginx"]
实例2:下面是tomcat/7/jdk8/openjdk/的dockerfile文件。注意:只是部分内容
FROM openjdk:8-jdk ENV CATALINA_HOME /usr/local/tomcat ENV PATH $CATALINA_HOME/bin:$PATH # 这里使用 RUN 指令运行 mkdir去创建目录 RUN mkdir -p "$CATALINA_HOME" WORKDIR $CATALINA_HOME # ...more... # 下面是一个非常复杂的RUN指令,其实就是一段SHELL脚本 # verify Tomcat Native is working properly RUN set -e \ && nativeLines="$(catalina.sh configtest 2>&1)" \ && nativeLines="$(echo "$nativeLines" | grep 'Apache Tomcat Native')" \ && nativeLines="$(echo "$nativeLines" | sort -u)" \ && if ! echo "$nativeLines" | grep 'INFO: Loaded APR based Apache Tomcat Native library' >&2; then \ echo >&2 "$nativeLines"; \ exit 1; \ fi EXPOSE 8080 CMD ["catalina.sh", "run"]