若是学习Docker,那么制做镜像这一步确定不能少的,别人给你的是环境,而你本身作的才是你最终须要的东西,接下来就记录一下如何制做一个知足本身的镜像,咱们使用docker通常就是部署咱们的应用,那么我就制做一个镜像来发布咱们的应用,制做方式咱们就选择Dockerfile的方式java
Dockerfile:python
docker其实就像是一个脚本文件,或者你能够直接把他当作一个脚本或者就是当作一条条命令的集合,在Dockerfile文件中咱们通常分为四个部分:基础镜像、建立者信息(可省略)、制做镜像过程(操做镜像指令)、启动容器的命令spring
接下来咱们先了解一下这四部分分别是什么?docker
准备:获取基础镜像app
咱们能够从镜像仓库中获取适合咱们的基础镜像使用docker search <镜像名> 查询镜像,使用docker pull <镜像名> 获取,如:我要获取一个jdk8的镜像那么咱们 ide
#查询镜像仓库中jdk8相关的镜像 docker search java8 #获取名字是corvis/java8的镜像 docker pull corvis/java8
以下图:这个镜像我已经下载过了因此显示的已经存在学习
所有下载完成以后咱们能够查一下咱们的本地会有一个名字是corvis/java8的镜像ui
#查询本地镜像 docker images
到这里咱们的基础镜像已经获取到了,咱们把建立的Dockerfile文件放到一个单独的目录下,并把咱们的项目打好的包也放在该目录下,接下里咱们开始写咱们的Dockerfile文件code
制做的步骤orm
指定基础镜像
#指定基础镜像(咱们全部的操做都是在这个镜像的基础上作的) 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经常使用指令,语法
一、FROM <镜像名image>:<版本号tag> 通常是Dockerfile的第一行,指定基础镜像 二、MAINTAINET <建立者信息> 指明该镜像的建立者信息 三、RUN <命令> 容器中须要执行的命令,如:在容器建立以后须要在根目录建立一个logs目录 RUN mkdir /logs 四、COPY <本地文件> <容器路径> 复制本地文件到镜像中,本地路径是以Dockkerfile所在目录为根目录 如:把test.jar复制到/usr/local目录 COPY test.jar /usr/local 五、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 六、ENV <key> <value> 设置环境变量,能够直接用的环境变量有以下 $HOME 用户主目录 $HOMENAME 容器主机名 $PATH 容器环境变量(这个会常常用到) 如:咱们要把/usr/bin添加到环境变量中 ENV PATH $PATH:/usr/bin 七、EXPOSE [<port>...] Docker服务端暴露端口,如:咱们镜像中有8080和8081两个端口须要暴露出去 EXPOSE 8080 8081 八、CMD ["",""] 括号中的是须要执行的命令 指定启动容器时执行的命令,每一个Dockerfile只能有一条CMD指令,若是指定了多条指令,则最后一条执行(若是启动的时候指定了命令会被启动时指定的命令覆盖) 九、ENTRYPOINT ["",""] 和CMD指令同样,惟一不一样的是即便启动的时候指定了命令,该命令也不会被覆盖 十、VOLUME ["/data"] 做用是建立在本地主机或其余容器能够挂载的数据卷,用来存放数据。 十一、USER username 指定容器运行时的用户名或UID,后续的RUN也会使用指定的用户。要临时使用管理员权限可使用sudo。在USER命令 以前可使用RUN命令建立须要的用户。 十二、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