Dockerfile Synax

Env

Format:$variable_name、${variable_name}

  • ${variable:-word} indicates that if variable is set then the result will be that value. If variable is not set then word will be the result.
  • ${variable:+word} indicates that if variable is set then word will be the result, otherwise the result is the empty string.

Usage

1
2
3
ENV abc=hello
ENV abc=bye def=$abc
ENV ghi=$abc
1
2
3
4
5
FROM busybox
ENV foo /bar
WORKDIR ${foo} # WORKDIR /bar
ADD . $foo # ADD . /bar
COPY \$foo /quux # COPY $foo /quux
1
CMD ["sh", "-c", "django-admin startproject $PROJECTNAME"]

Example

  1. Java Use Environment Parameter

需要注意点:

  • CMD[“java”, “$environment”, “-jar”, “jar_path”],其中的环境变量是无法解析的。
  • CMD [“sh”, “-c”, “echo $environment”],可以解析环境变量,是因为调用sh进行执行。
1
2
3
4
# 基础镜像
ENTRYPOINT ["/entrypoint.sh"]

CMD ["/bin/sh", "/execute.sh"]
1
2
3
4
5
6
7
8
9
10
11
12
# entrypoint.sh
FROM anapsix/alpine-java:8_jdk

ADD ./entrypoint.sh /entrypoint.sh
ADD ./execute.sh /execute.sh

RUN chmod +x /entrypoint.sh
RUN chmod +x /execute.sh

ENTRYPOINT ["/entrypoint.sh"]

CMD ["/bin/sh", "/execute.sh"]
1
2
3
# execute.sh
echo "Java JVM_OPS=$JVM_OPS, Jar Path=$JAR_PATH"
java $JVM_OPS -jar $JAR_PATH

ARG

The ARG instruction defines a variable that users can pass at build-time to the builder with the docker build command using the –build-arg = flag.

If a user specifies a build argument that was not defined in the Dockerfile, the build outputs a warning.

Usage

1
docker build --build-arg user=what_user .

Add/Copy

[The Difference between COPY and ADD in a Dockerfile] https://nickjanetakis.com/blog/docker-tip-2-the-difference-between-copy-and-add-in-a-dockerile