Dockerfile

Dockfile

步骤

  1. 手写符合规范的dockerfile文件
  2. docker build 创建一个自定义的镜像
  3. docker run 执行

dockerfile规范

保留字大写,后跟参数
#注释
每条指令都会形成一个新的镜像

例如:dockerhub上的centos、redis的例子

FROM scratch
MAINTAINER https://github.com/CentOS/sig-cloud-instance-images
ADD centos-6-docker.tar.xz /

LABEL org.label-schema.schema-version="1.0" \
    org.label-schema.name="CentOS Base Image" \
    org.label-schema.vendor="CentOS" \
    org.label-schema.license="GPLv2" \
    org.label-schema.build-date="20181006"

CMD ["/bin/bash"]
FROM debian:buster-slim

# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
RUN groupadd -r -g 999 redis && useradd -r -g redis -u 999 redis

# grab gosu for easy step-down from root
# https://github.com/tianon/gosu/releases
ENV GOSU_VERSION 1.12
RUN set -eux; \
	savedAptMark="$(apt-mark showmanual)"; \
	apt-get update; \
	apt-get install -y --no-install-recommends ca-certificates dirmngr gnupg wget; \
	rm -rf /var/lib/apt/lists/*; \
	...

ENV REDIS_VERSION 6.0.9
ENV REDIS_DOWNLOAD_URL http://download.redis.io/releases/redis-6.0.9.tar.gz
ENV REDIS_DOWNLOAD_SHA dc2bdcf81c620e9f09cfd12e85d3bc631c897b2db7a55218fd8a65eaa37f86dd

RUN set -eux; \
	\
	savedAptMark="$(apt-mark showmanual)"; \
	apt-get update; \
	apt-get install -y --no-install-recommends \
	; \
	rm -rf /var/lib/apt/lists/*; \
	\
	wget -O redis.tar.gz "$REDIS_DOWNLOAD_URL"; \
	echo "$REDIS_DOWNLOAD_SHA *redis.tar.gz" | sha256sum -c -; \
	mkdir -p /usr/src/redis; \
	tar -xzf redis.tar.gz -C /usr/src/redis --strip-components=1; \
	rm redis.tar.gz; \
	\
           .....      
	redis-cli --version; \
	redis-server --version

RUN mkdir /data && chown redis:redis /data
VOLUME /data
WORKDIR /data

COPY docker-entrypoint.sh /usr/local/bin/
ENTRYPOINT ["docker-entrypoint.sh"]

EXPOSE 6379
CMD ["redis-server"]
  • FROM 指定基础镜像(镜像源)

  • MAINTAINER 指定作者、邮箱

  • RUN 镜像构建时执行的命令

  • CMD 容器启动时运行的命令,仅最后一个生效,会被docker run的其他参数覆盖

  • ENTRYPOINT 追加容器运行时的命令

  • EXPOSE 当前容器暴露的端口

  • WORKDIR 创建容器后,终端默认登陆进来的工作目录

  • ENV 指定环境变量

  • ADD 将宿主机目录(源路径:构建上下文目录)下的文件拷贝进镜像中(目标路径),并且解压缩和处理相应的url

  • COPY 拷贝

  • LABEL 标签

  • VOLUMN 容器数据卷,用于持久化保存数据

posted @ 2020-11-23 10:02  jklixin  阅读(176)  评论(0编辑  收藏  举报