美文网首页
Dockerfile注意事项

Dockerfile注意事项

作者: 九月_1012 | 来源:发表于2020-05-27 13:50 被阅读0次

1、以当前Dockerfile所在目录构建images

mkdir myproject && cd myproject
echo "hello" > hello
echo -e "FROM busybox\nCOPY /hello /\nRUN cat /hello" > Dockerfile
docker build -t helloapp:v1 .   #注意后面有个.   代表当前目录

2、再次构建image,不依赖上次的构建缓存,-f 指定Dockerfile 目录,并且指定context目录

mkdir -p dockerfiles context
mv Dockerfile dockerfiles && mv hello context
docker build --no-cache -t helloapp:v2 -f dockerfiles/Dockerfile context

3、当build 镜像的时候,屏幕会出现以下,越大越耗费时间

Sending build context to Docker daemon  187.8MB

4、从标准输入(为dockerfile)构建image

echo -e 'FROM busybox\nRUN echo "hello world"' | docker build -
#或者如下
docker build -<<EOF
FROM busybox
RUN echo "hello world"
EOF
#再或者
docker build -t myimage:latest -<<EOF
FROM busybox
RUN echo "hello world"
EOF

5、如果入标准输入的Dockerfile build images的话,无需发送其他文件作为构建上下文,要-f- (-代表PATH的位置),并指示Docker从stdin而不是目录中读取构建context(仅包含Dockerfile):

# create a directory to work in
mkdir example
cd example

# create an example file
touch somefile.txt

# build an image using the current directory as context, and a Dockerfile passed through stdin
docker build -t myimage:latest -f- . <<EOF
FROM busybox
COPY somefile.txt .
RUN cat /somefile.txt
EOF

6、如果您要从不包含Dockerfile的存储库中构建映像,或者想要使用自定义Dockerfile进行构建而又不维护自己的存储库,则此语法很有用。
以下示例使用stdin中的Dockerfile构建映像,并从GitHub上的“ hello-world” Git存储库添加hello.c文件。(服务器默认安装git)

docker build -t myimage:latest -f- https://github.com/docker-library/hello-world.git <<EOF
FROM busybox
COPY hello.c .
EOF
#FROM构建一个层
FROM golang:1.11-alpine AS build

# Install tools required for project
# Run `docker build --no-cache .` to update dependencies
RUN apk add --no-cache git
RUN go get github.com/golang/dep/cmd/dep

# List project dependencies with Gopkg.toml and Gopkg.lock
# These layers are only re-built when Gopkg files are updated
COPY Gopkg.lock Gopkg.toml /go/src/project/
WORKDIR /go/src/project/
# Install library dependencies
RUN dep ensure -vendor-only

# Copy the entire project and build it
# This layer is rebuilt when a file changes in the project directory
COPY . /go/src/project/
RUN go build -o /bin/project

# This results in a single layer image
FROM scratch
COPY --from=build /bin/project /bin/project
ENTRYPOINT ["/bin/project"]
CMD ["--help"]

参考:https://docs.docker.com/develop/develop-images/dockerfile_best-practices/

相关文章

网友评论

      本文标题:Dockerfile注意事项

      本文链接:https://www.haomeiwen.com/subject/cpgvohtx.html