如果学习 Docker, 那么制作镜像这一步肯定不能少的, 别人给你的是环境, 而你自己做的才是你最终需要的东西, 接下来就记录一下如何制作一个满足自己的镜像, 我们使用 docker 一般就是部署我们的应用, 那么我就制作一个镜像来发布我们的应用, 制作方式我们就选择 Dockerfile 的方式
Dockerfile:
docker 其实就像是一个脚本文件, 或者你可以直接把他看成一个脚本或者就是看成一条条命令的集合, 在 Dockerfile 文件中我们一般分为四个部分: 基础镜像, 创建者信息 (可省略), 制作镜像过程 (操作镜像指令), 启动容器的命令
接下来我们先了解一下这四部分分别是什么?
准备: 获取基础镜像
我们可以从镜像仓库中获取适合我们的基础镜像使用 docker search < 镜像名 > 查询镜像, 使用 docker pull < 镜像名 > 获取, 如: 我要获取一个 jdk8 的镜像那么我们
- # 查询镜像仓库中 jdk8 相关的镜像
- docker search java8
- # 获取名字是 corvis/java8 的镜像
- docker pull corvis/java8
如下图: 这个镜像我已经下载过了所以显示的已经存在
全部下载完成之后我们可以查一下我们的本地会有一个名字是 corvis/java8 的镜像
- # 查询本地镜像
- docker images
到这里我们的基础镜像已经获取到了, 我们把创建的 Dockerfile 文件放到一个单独的目录下, 并把我们的项目打好的包也放在该目录下, 接下里我们开始写我们的 Dockerfile 文件
制作的步骤
指定基础镜像
- # 指定基础镜像 (我们所有的操作都是在这个镜像的基础上做的)
- FROM corvis/java8
写入创建者信息
MAINTAINER SunArmy
把我们的项目 copy 到镜像中, 并暴露端口, 这里我直接用 springBoot 写了一个空的项目打包 test.jar, 端口开的 80
- # 从本地 copy 到镜像的 / usr/local 目录下, 使用 ADD 和 COPY 都可以
- ADD test.jar /usr/local/
- # 暴露项目端口到宿主机
- EXPOSE 80
启动容器我们需要执行的命令 (启动项目的命令)
- # 使用 CMD 和 ENTRYPOINT 都可以
- CMD ["java","-jar","/usr/local/test.jar"]
- FROM corvis/java8
- MAINTAINER SunArmy
- COPY test.jar /usr/local/
- EXPOSE 80
- CMD ["java","-jar","/usr/local/test.jar"]
我们已经写好了 Dockerfile 文件那么我们直接在当前目录下执行命令来生成镜像
- # 根据 Dockerfile 文件制作镜像
- docker build -t test:1.0 .
- # 查看本地镜像
- docker images
至此, 我们的镜像已经做好了
Dockerfile 常用指令, 语法
1,FROM < 镜像名 image>:< 版本号 tag>
一般是 Dockerfile 的第一行, 指定基础镜像
2,MAINTAINET < 创建者信息 >
指明该镜像的创建者信息
3,RUN < 命令 >
容器中需要执行的命令, 如: 在容器创建之后需要在根目录创建一个 logs 目录
RUN mkdir /logs
4,COPY < 本地文件 > < 容器路径 >
复制本地文件到镜像中, 本地路径是以 Dockkerfile 所在目录为根目录 如: 把 test.jar 复制到 / usr/local 目录
COPY test.jar /usr/local
5,ADD < 本地文件 > < 容器路径 >
复制本地文件到镜像中, 本地路径是以 Dockkerfile 所在目录为根目录, 区别与 COPY 的可以从 URL 复制, 可以直接解压. tar.gz 并把解压之后的文件复制到镜像
如: 把 test.jar 复制到 / usr/local 目录
ADD test.jar /usr/local
从 URL 复制到 / usr/local
ADD URL /usr/local
解压 test.tar.gz 复制到镜像 / usr/local
- ADD test.tar.gz /usr/local
- 6,ENV <key> <value>
设置环境变量, 可以直接用的环境变量有如下
$HOME 用户主目录
$HOMENAME 容器主机名
$PATH 容器环境变量 (这个会经常用到)
如: 我们要把 / usr/bin 添加到环境变量中
- ENV PATH $PATH:/usr/bin
- 7,EXPOSE [<port>...]
Docker 服务端暴露端口, 如: 我们镜像中有 8080 和 8081 两个端口需要暴露出去
- EXPOSE 8080 8081
- 8,CMD ["",""]
括号中的是需要执行的命令
指定启动容器时执行的命令, 每个 Dockerfile 只能有一条 CMD 指令, 如果指定了多条指令, 则最后一条执行 (如果启动的时候指定了命令会被启动时指定的命令覆盖)
9,ENTRYPOINT ["",""]
和 CMD 指令一样, 唯一不同的是即使启动的时候指定了命令, 该命令也不会被覆盖
10,VOLUME ["/data"]
作用是创建在本地主机或其他容器可以挂载的数据卷, 用来存放数据.
11,USER username
指定容器运行时的用户名或 UID, 后续的 RUN 也会使用指定的用户. 要临时使用管理员权限可以使用 sudo. 在 USER 命令 之前可以使用 RUN 命令创建需要的用户.
12,WORKDIR /path
为后续的 RUN CMD ENTRYPOINT 指定配置工作目录, 可以使用多个 WORKDIR 指令, 若后续指令用得是相对路径, 则会基 于之前的命令指定路径.
13.ONBUILD [INSTRUCTION]
该配置指定当所创建的镜像作为其他新建镜像的基础镜像时所执行的指令.
以上就是写 Dockerfile 文件所需要的基本指指令
写好之后我们切入 Dockerfile 目录下执行 docker build 即可制成我们自己的镜像, 如: 使用当前目录的 Dockerfile 文件创建镜像并设置标签,-t 参数设置标签
- docker build -t test:1.0 .
- Usage: docker build [OPTIONS] PATH | URL | -
- Build an image from a Dockerfile
- Options:
- --add-host list Add a custom host-to-IP mapping (host:ip)
- --build-arg list Set build-time variables
- --cache-from strings Images to consider as cache sources
- --cgroup-parent string Optional parent cgroup for the container
- --compress Compress the build context using gzip
- --CPU-period int Limit the CPU CFS (Completely Fair
- Scheduler) period
- --CPU-quota int Limit the CPU CFS (Completely Fair
- Scheduler) quota
- -c, --CPU-shares int CPU shares (relative weight)
- --cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
- --cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
- --disable-content-trust Skip image verification (default true)
- -f, --file string Name of the Dockerfile (Default is
- 'PATH/Dockerfile')
- --force-rm Always remove intermediate containers
- --iidfile string Write the image ID to the file
- --isolation string Container isolation technology
- --label list Set metadata for an image
- -m, --memory bytes Memory limit
- --memory-swap bytes Swap limit equal to memory plus swap:
- '-1' to enable unlimited swap
- --network string Set the networking mode for the RUN
- instructions during build (default "default")
- --no-cache Do not use cache when building the image
- --pull Always attempt to pull a newer version of
- the image
- -q, --quiet Suppress the build output and print image
- ID on success
- --rm Remove intermediate containers after a
- successful build (default true)
- --security-opt strings Security options
- --shm-size bytes Size of /dev/shm
- -t, --tag list Name and optionally a tag in the
- 'name:tag' format
- --target string Set the target build stage to build.
- --ulimit ulimit Ulimit options (default [])
把镜像保存到本地
docker save < 保存到本地的文件 > < 镜像 >
如: 把 test:1.0 这个镜像保存到本地
- docker save -o test_1.0.tar test:1.0
- Usage: docker build [OPTIONS] PATH | URL | -
- Build an image from a Dockerfile
- Options:
- --add-host list Add a custom host-to-IP mapping (host:ip)
- --build-arg list Set build-time variables
- --cache-from strings Images to consider as cache sources
- --cgroup-parent string Optional parent cgroup for the container
- --compress Compress the build context using gzip
- --CPU-period int Limit the CPU CFS (Completely Fair
- Scheduler) period
- --CPU-quota int Limit the CPU CFS (Completely Fair
- Scheduler) quota
- -c, --CPU-shares int CPU shares (relative weight)
- --cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
- --cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
- --disable-content-trust Skip image verification (default true)
- -f, --file string Name of the Dockerfile (Default is
- 'PATH/Dockerfile')
- --force-rm Always remove intermediate containers
- --iidfile string Write the image ID to the file
- --isolation string Container isolation technology
- --label list Set metadata for an image
- -m, --memory bytes Memory limit
- --memory-swap bytes Swap limit equal to memory plus swap:
- '-1' to enable unlimited swap
- --network string Set the networking mode for the RUN
- instructions during build (default "default")
- --no-cache Do not use cache when building the image
- --pull Always attempt to pull a newer version of
- the image
- -q, --quiet Suppress the build output and print image
- ID on success
- --rm Remove intermediate containers after a
- successful build (default true)
- --security-opt strings Security options
- --shm-size bytes Size of /dev/shm
- -t, --tag list Name and optionally a tag in the
- 'name:tag' format
- --target string Set the target build stage to build.
- --ulimit ulimit Ulimit options (default [])
- C:\Users\SunArmy\Desktop\demo>docker save --help
- Usage: docker save [OPTIONS] IMAGE [IMAGE...]
- Save one or more images to a tar archive (streamed to STDOUT by default)
- Options:
- -o, --output string Write to a file, instead of STDOUT
来源: https://www.cnblogs.com/SunArmy/p/11061003.html