Docker学习
Docker学习:
从标准输入接收Dockerfile的方法:docker build -
分步骤构建镜像–Use multi-stage builds---------
FROM alpine/latest
WORKDIR /app/
…
FROM golang:1.16
…
COPY --from 0 /app/preDockerApp ./
或者用别名
FROM alpine/latest as firstBuild
WORKDIR /app/
…
FROM golang:1.16
…
COPY --from firstBuild /app/preDockerApp ./
Stop at a specific build stage,分步骤构建镜像时,指定截止构建位置用–target 加FROM 后的别名
docker build --target builder -t alexellis2/href-counter:latest .
------------Use an external image as a “stage”------------
COPY --from=nginx:latest /etc/nginx/nginx.conf /nginx.conf
-----------------Use a previous stage as a new stage----------------
syntax=docker/dockerfile:1
FROM alpine:latest AS builder
RUN apk --no-cache add build-base
FROM builder AS build1
COPY source1.cpp source.cpp
RUN g++ -o /binary source.cpp
FROM builder AS build2
COPY source2.cpp source.cpp
RUN g++ -o /binary source.cpp
---------------enable BuildKit builds-----------------
在/etc/docker/daemon.json中添加如下配置
{ “features”: { “buildkit”: true } }
github上docker官方镜像:hello-world,在linux上用git clone下来后,需要进入该目录运行update.sh(大概20分钟),
然后linux运行docker images会发现多了2个image(hello-world:test-hello-world和hello-world:latest)
自己进入clone下来的hello-world/arm64v8运行docker build --tag myhello . 可以构建,但是运行会报错:standard_init_linux.go:228: exec user process caused: exec format error
构建自己的镜像:FROM sratch
Dockerfile第一行语法注释:# syntax=docker/dockerfile:1
echo -e “OK! \n” # -e 开启转义
echo -e “OK! \c” # -e 开启转义 \c 不换行
dockerfile 管道命令链接多个命令时,可能前面命令已经异常,但是后面命令正常,这样看到的结果仍然是正常
为了防止这种情况,需要在RUN 后+ set -o pipefail
If you want the command to fail due to an error at any stage in the pipe, prepend set -o pipefail && to ensure that an unexpected error prevents the build from inadvertently succeeding. For example:
RUN set -o pipefail && wget -O - https://some.site | wc -l > /number
但是the dash shell on Debian-based images不支持这个set -o pipefail命令,所以最好用exec 格式的RUN命令
Not all shells support the -o pipefail option.
In cases such as the dash shell on Debian-based images, consider using the exec form of RUN to explicitly choose a shell that does support the pipefail option. For example:
RUN [“/bin/bash”, “-c”, “set -o pipefail && wget -O - https://some.site | wc -l > /number”]
Dockerfile在From之前只能有 sytax,escape,#,args
ENV 变量语法
${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.
ENV abc=hello
ENV abc=bye def=
a
b
c
E
N
V
g
h
i
=
abc ENV ghi=
abcENVghi=abc
will result in def having a value of hello, not bye. However, ghi will have a value of bye because it is not part of the same instruction that set abc to bye.
.dockerignore
comment Ignored.
/temp Exclude files and directories whose names start with temp in any immediate subdirectory of the root. For example, the plain file /somedir/temporary.txt is excluded, as is the directory /somedir/temp.
//temp* Exclude files and directories starting with temp from any subdirectory that is two levels below the root. For example, /somedir/subdir/temporary.txt is excluded.
temp? Exclude files and directories in the root directory whose names are a one-character extension of temp. For example, /tempa and /tempb are excluded.
**/*.go will exclude all files that end with .go that are found in all directories, including the root of the build context.
RUN (shell form, the command is run in a shell, which by default is /bin/sh -c on Linux or cmd /S /C on Windows)
RUN [“executable”, “param1”, “param2”] (exec form)
The CMD instruction has three forms:
CMD [“executable”,“param1”,“param2”] (exec form, this is the preferred form)
CMD [“param1”,“param2”] (as default parameters to ENTRYPOINT)
CMD command param1 param2 (shell form)
For example, to copy a file named arr[0].txt, use the following;
COPY arr[[]0].txt /mydir/
COPY hom* /mydir/
COPY hom?.txt /mydir/
entrypoint:------------------------------
You can specify a plain string for the ENTRYPOINT and it will execute in /bin/sh -c. This form will use shell processing to substitute shell environment variables, and will ignore any CMD or docker run command line arguments.
To ensure that docker stop will signal any long running ENTRYPOINT executable correctly, you need to remember to start it with exec:
FROM ubuntu
ENTRYPOINT exec top -b
Command line arguments to docker run will be appended after all elements in an exec form ENTRYPOINT, and will override all elements specified using CMD. This allows arguments to be passed to the entry point, i.e., docker run -d will pass the -d argument to the entry point. You can override the ENTRYPOINT instruction using the docker run --entrypoint flag.
The shell form prevents any CMD or run command line arguments from being used, but has the disadvantage that your ENTRYPOINT will be started as a subcommand of /bin/sh -c, which does not pass signals. This means that the executable will not be the container’s PID 1 - and will not receive Unix signals - so your executable will not receive a SIGTERM from docker stop .
Only the last ENTRYPOINT instruction in the Dockerfile will have an effect.
You can specify a plain string for the ENTRYPOINT and it will execute in /bin/sh -c. This form will use shell processing to substitute shell environment variables, and will ignore any CMD or docker run command line arguments. To ensure that docker stop will signal any long running ENTRYPOINT executable correctly, you need to remember to start it with exec:
FROM ubuntu
ENTRYPOINT exec top -b