在正式学习docker build命令前,我们先准备一个简单的Dockerfile文件,如下:
# 继承基础镜像centos:latest
FROM centos
# 设置作者信息
MAINTAINER myName<myName@163.com>
# 定义环境变量
ENV mypath /tmp
WORKDIR $mypath
# 安装 vim 和 ifconfig
RUN yum -y install vim
RUN yum -y install net-tools
# 暴露80端口
EXPOSE 80
CMD echo $mypath
CMD echo "success-------------------------ok"
CMD /bin/bashdocker build 命令根据 Dockerfile 构建一个镜像。可以使用“docker build --help”查看命令的帮助信息,如下:
[root@localhost ~]# docker build --help
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 [])实例1:下面使用“docker build -t myimage:v1.0 .”命令构建一个镜像,如下:
[root@localhost ~]# docker build -t myimage:v1.0 .
Sending build context to Docker daemon 204.8MB
Step 1/10 : FROM centos
latest: Pulling from library/centos
8ba884070f61: Already exists
Digest: sha256:a799dd8a2ded4a83484bbae769d97655392b3f86533ceb7dd96bbac929809f3c
Status: Downloaded newer image for centos:latest
---> 9f38484d220f
......
Step 9/10 : CMD echo "success-------------------------ok"
---> Running in 68159fa364b9
Removing intermediate container 68159fa364b9
---> c9fc0eb65daa
Step 10/10 : CMD /bin/bash
---> Running in 7aceb6325f5b
Removing intermediate container 7aceb6325f5b
---> 12298d1c5307
Successfully built 12298d1c5307
Successfully tagged myimage:v1.0其中:
上面命令最后有个点,代表使用当前路径的 Dockerfile 进行构建
-t myimage:v1.0 给新构建的镜像取名为 myimage,并设定版本为 v1.0
实例2:使用“-f”参数指定Dockerfile的绝对路径地址,如下:
[root@localhost ~]# docker build -f /root/Dockerfile -t myimage:v1.0 .
Sending build context to Docker daemon 204.8MB
Step 1/10 : FROM centos
---> 9f38484d220f
Step 2/10 : MAINTAINER myName<myName@163.com>
---> Running in 070c207cef23
... more ...
Step 9/10 : CMD echo "success-------------------------ok"
---> Running in 646ff518ed0c
Removing intermediate container 646ff518ed0c
---> 61ff096c4209
Step 10/10 : CMD /bin/bash
---> Running in e947787cc98e
Removing intermediate container e947787cc98e
---> 0821278552a7
Successfully built 0821278552a7
Successfully tagged myimage:v1.0
[root@localhost ~]#实例3:下面将docker build命令的最后一个选项由“.”改为一个目录绝对地址(指向Dockerfile存在的目录)。此时,不使用 -f 选项指定Dockerfile的位置,构建Docker镜像。如下:
[root@localhost ~]# docker build -t myimage:v1.0 /root/
Sending build context to Docker daemon 204.8MB
Step 1/10 : FROM centos
---> 9f38484d220f
Step 2/10 : MAINTAINER myName<myName@163.com>
---> Running in 52bc79f06ac5
Removing intermediate container 52bc79f06ac5
...more...
Step 9/10 : CMD echo "success-------------------------ok"
---> Running in cabefa916c4e
Removing intermediate container cabefa916c4e
---> 05a9ff2b63ce
Step 10/10 : CMD /bin/bash
---> Running in fef263c32a80
Removing intermediate container fef263c32a80
---> 280a2c1a770d
Successfully built 280a2c1a770d
Successfully tagged myimage:v1.0实例4:使用“-q”选项开启静默构建镜像,即在构建镜像时不输出过程信息。在镜像构建完成后,输出镜像的ID。如下:
[root@localhost ~]# docker build -q -f ./Dockerfile -t myimage:v1.0 .
sha256:729e175d50dbf51c08b4b23c34679520f13d0e2feea440a6991626547ad8046b
[root@localhost ~]# docker images myimage
REPOSITORY TAG IMAGE ID CREATED SIZE
myimage v1.0 729e175d50db 12 seconds ago 468MB