构建镜像(两种方式)
作用
- 保存对容器的修改,并再次使用
- 自定义镜像的能力
- 以软件的形式打包并分发服务及运行环境
当我们从docker
镜像仓库中下载的镜像不能满足我们的需求时,我们可以通过以下两种方式对镜像进行更改。
- 1、从已经创建的容器中更新镜像,并且提交这个镜像
- 2、使用
Dockerfile
指令来创建一个新的镜像
构建方法一:通过容器构建镜像(docker commit
)
使用 commit
构建镜像
[yang@izwz9bge0ivbgoesxak65wz /]$ docker commit --help
Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Create a new image from a container's changes
Options:
-a, --author string Author (e.g., "John Hannibal Smith <hannibal@a-team.com>") # 作者
-c, --change list Apply Dockerfile instruction to the created image # 将Dockerfile指令应用于创建的镜像
-m, --message string Commit message # 提交信息
-p, --pause Pause container during commit (default true) # 在提交期间暂停容器(默认为true)
[yang@izwz9bge0ivbgoesxak65wz /]$
例子:
1、首先启动一个交互式容器,端口为80
,命名为 commit_test
,为 ubuntu
系统:
[yang@izwz9bge0ivbgoesxak65wz /]$ docker run -it -p 80 --name commit_test ubuntu /bin/bash
root@5e6adcebb4ca:/#
2、在运行的容器内使用 apt-get update
命令进行更新。
[yang@izwz9bge0ivbgoesxak65wz /]$ docker run -it -p 80 --name commit_test ubuntu /bin/bash
root@5e6adcebb4ca:/# apt-get update
Get:1 http://archive.ubuntu.com/ubuntu bionic InRelease [242 kB]
Get:2 http://security.ubuntu.com/ubuntu bionic-security InRelease [83.2 kB]
Get:3 http://security.ubuntu.com/ubuntu bionic-security/multiverse amd64 Packages [1365 B]
Get:4 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [135 kB]
Get:5 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
......
3、安装 nginx
。
root@5e6adcebb4ca:/# apt-get install -y nginx
4、在完成操作之后,输入 exit
命令来退出这个容器。
5、查看我们刚才建立好的容器
[yang@izwz9bge0ivbgoesxak65wz /]$ docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5e6adcebb4ca ubuntu "/bin/bash" 11 minutes ago Exited (0) 5 seconds ago commit_test
[yang@izwz9bge0ivbgoesxak65wz /]$
此时ID为 5e6adcebb4ca
的容器,是按我们的需求更改的容器。
6、将容器提交为镜像
我们可以通过命令 docker commit
来提交容器为镜像。
[yang@izwz9bge0ivbgoesxak65wz /]$ docker commit -m "add a nginx" -a "yang" 5e6adcebb4ca yang/ubuntu_commit_test
sha256:6cf1911fcffaced9df7b84399ce14f05f977e8d83d12ccdae77ad5f458f250bf
[yang@izwz9bge0ivbgoesxak65wz /]$
各个参数说明:
-m
: 提交镜像的描述信息-a
: 指定镜像的作者-
5e6adcebb4ca
: 容器ID或名字
yang/ubuntu_commit_test
: 给 要创建的目标镜像 命名
返回的 ID
为新生成的镜像的 ID
。
7、使用 docker images
命令来查看我们的新镜像。
[yang@izwz9bge0ivbgoesxak65wz /]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
yang/ubuntu_commit_test latest 6cf1911fcffa 2 minutes ago 171MB
hello-world latest fce289e99eb9 13 days ago 1.84kB
ubuntu latest 1d9c17228a9e 2 weeks ago 86.7MB
centos latest 1e1148e4cc2c 5 weeks ago 202MB
ubuntu 15.10 9b9cb95443b5 2 years ago 137MB
[yang@izwz9bge0ivbgoesxak65wz /]$
8、使用我们的新镜像 yang/ubuntu_commit_test
来启动一个容器,并运行nginx
命令
[yang@izwz9bge0ivbgoesxak65wz /]$ docker run -d --name nginx_web -p 80 yang/ubuntu_commit_test nginx -g "daemon off;"
85b8c68cf6daf564523b2402e7e43a0246a50d9f4418bebe7700280e19fb3e5e
[yang@izwz9bge0ivbgoesxak65wz /]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
85b8c68cf6da yang/ubuntu_commit_test "nginx -g 'daemon of…" 40 seconds ago Up 39 seconds 0.0.0.0:32770->80/tcp nginx_web
[yang@izwz9bge0ivbgoesxak65wz /]$
测试:
[yang@izwz9bge0ivbgoesxak65wz /]$ curl http://127.0.0.1:32770
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[yang@izwz9bge0ivbgoesxak65wz /]$
可以访问到 nginx
的服务了。
总结
由于我们将之前容器中的修改保存到镜像中,这样我们可以很容易的重复执行这个镜像,提供同样的服务。
构建方法二(重点):通过 Dockerfile
文件构建镜像(docker build
)
我们使用命令 docker build
, 从零开始来创建一个新的镜像。
步骤一:创建 Dockerfile
文件
什么是Dockerfile
文件?
是包含了一系列命令的文本文件。
提供了一系列构建按镜像的能力。
包含了:
- 镜像的基础
- 镜像的维护人
- 镜像中需要执行的命令
- 暴露的端口
例子:
# First Dockerfile for test
FROM ubuntu:14.04
MAINTAINER yangshuo "yangshuo@outlook.com"
RUN apt-get update
RUN apt-get install -y nginx
EXPOSE 80
创建步骤
1、创建一个存放 Dockerfile
文件的目录
[yang@izwz9bge0ivbgoesxak65wz ~]$ mkdir -p docker/df_1
[yang@izwz9bge0ivbgoesxak65wz ~]$
2、进入该目录,并创建一个 Dockerfile
文件
[yang@izwz9bge0ivbgoesxak65wz ~]$ cd docker/df_1/
[yang@izwz9bge0ivbgoesxak65wz df_1]$ vi Dockerfile
3、在Dockerfile
文件中添加命令
# First Dockerfile for test
FROM ubuntu:14.04
MAINTAINER yangshuo "yangshuo@outlook.com"
RUN apt-get update
RUN apt-get install -y nginx
EXPOSE 80
- 每一个指令都会在镜像上创建一个新的层,每一个指令的前缀都必须是大写的。
- 第一条
FROM
,指定使用哪个镜像源 RUN
指令告诉docker
在镜像内执行命令,安装了什么…
步骤二:使用 docker build
命令 来 构建镜像
[yang@izwz9bge0ivbgoesxak65wz df_1]$ 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 # 以“name:tag”格式命名和选择标记
--target string Set the target build stage to build. # 设置要构建的目标构建阶段。
--ulimit ulimit Ulimit options (default []) # Ulimit选项(默认[])
[yang@izwz9bge0ivbgoesxak65wz df_1]$
1、然后,我们使用 Dockerfile
文件,通过 docker build
命令来构建一个镜像。
[yang@izwz9bge0ivbgoesxak65wz df_1]$ docker build -t "yangshuo/df_1" .
Sending build context to Docker daemon 2.048kB
Step 1/5 : FROM ubuntu:14.04
14.04: Pulling from library/ubuntu
9b316e271c60: Pull complete
dea703e2e1f1: Pull complete
dd50fddc64ae: Pull complete
9d32d2e6dcde: Pull complete
Digest: sha256:2ceb8f8fef6dfa7433c992efdedc90c35b572747f34d9bac726e18a9e086e95e
Status: Downloaded newer image for ubuntu:14.04
---> 7e4b16ae8b23
Step 2/5 : MAINTAINER yangshuo "yangshuo@outlook.com"
---> Running in 7a1140db3436
Removing intermediate container 7a1140db3436
---> ba9c9213ec09
Step 3/5 : RUN apt-get update
---> Running in 99146ff211ff
Ign http://archive.ubuntu.com trusty InRelease
Get:1 http://security.ubuntu.com trusty-security InRelease [65.9 kB]
Get:2 http://archive.ubuntu.com trusty-updates InRelease [65.9 kB]
Get:3 http://security.ubuntu.com trusty-security/main amd64 Packages [988 kB]
Get:4 http://archive.ubuntu.com trusty-backports InRelease [65.9 kB]
Get:5 http://archive.ubuntu.com trusty Release.gpg [933 B]
Get:6 http://archive.ubuntu.com trusty-updates/main amd64 Packages [1413 kB]
Get:7 http://security.ubuntu.com trusty-security/restricted amd64 Packages [18.1 kB]
Get:8 http://security.ubuntu.com trusty-security/universe amd64 Packages [354 kB]
Get:9 http://security.ubuntu.com trusty-security/multiverse amd64 Packages [4723 B]
Get:10 http://archive.ubuntu.com trusty-updates/restricted amd64 Packages [21.4 kB]
Get:11 http://archive.ubuntu.com trusty-updates/universe amd64 Packages [652 kB]
Get:12 http://archive.ubuntu.com trusty-updates/multiverse amd64 Packages [16.0 kB]
Get:13 http://archive.ubuntu.com trusty-backports/main amd64 Packages [14.7 kB]
Get:14 http://archive.ubuntu.com trusty-backports/restricted amd64 Packages [40 B]
Get:15 http://archive.ubuntu.com trusty-backports/universe amd64 Packages [52.5 kB]
Get:16 http://archive.ubuntu.com trusty-backports/multiverse amd64 Packages [1392 B]
Get:17 http://archive.ubuntu.com trusty Release [58.5 kB]
Get:18 http://archive.ubuntu.com trusty/main amd64 Packages [1743 kB]
Get:19 http://archive.ubuntu.com trusty/restricted amd64 Packages [16.0 kB]
Get:20 http://archive.ubuntu.com trusty/universe amd64 Packages [7589 kB]
Get:21 http://archive.ubuntu.com trusty/multiverse amd64 Packages [169 kB]
Fetched 13.3 MB in 21s (630 kB/s)
Reading package lists...
Removing intermediate container 99146ff211ff
---> 842289e51a72
Step 4/5 : RUN apt-get install -y nginx
---> Running in 2ea3409a4c27
Reading package lists...
Building dependency tree...
Reading state information...
The following extra packages will be installed:
fontconfig-config fonts-dejavu-core geoip-database libfontconfig1
libfreetype6 libgd3 libgeoip1 libjbig0 libjpeg-turbo8 libjpeg8 libtiff5
libvpx1 libx11-6 libx11-data libxau6 libxcb1 libxdmcp6 libxml2 libxpm4
libxslt1.1 nginx-common nginx-core sgml-base xml-core
Suggested packages:
libgd-tools geoip-bin fcgiwrap nginx-doc sgml-base-doc debhelper
The following NEW packages will be installed:
fontconfig-config fonts-dejavu-core geoip-database libfontconfig1
libfreetype6 libgd3 libgeoip1 libjbig0 libjpeg-turbo8 libjpeg8 libtiff5
libvpx1 libx11-6 libx11-data libxau6 libxcb1 libxdmcp6 libxml2 libxpm4
libxslt1.1 nginx nginx-common nginx-core sgml-base xml-core
0 upgraded, 25 newly installed, 0 to remove and 2 not upgraded.
Need to get 5593 kB of archives.
After this operation, 19.8 MB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu/ trusty/main libgeoip1 amd64 1.6.0-1 [71.0 kB]
Get:2 http://archive.ubuntu.com/ubuntu/ trusty/main libxau6 amd64 1:1.0.8-1 [8376 B]
Get:3 http://archive.ubuntu.com/ubuntu/ trusty/main libxdmcp6 amd64 1:1.1.1-1 [12.8 kB]
Get:4 http://archive.ubuntu.com/ubuntu/ trusty/main libxcb1 amd64 1.10-2ubuntu1 [38.0 kB]
Get:5 http://archive.ubuntu.com/ubuntu/ trusty-updates/main libx11-data all 2:1.6.2-1ubuntu2.1 [111 kB]
Get:6 http://archive.ubuntu.com/ubuntu/ trusty-updates/main libx11-6 amd64 2:1.6.2-1ubuntu2.1 [561 kB]
Get:7 http://archive.ubuntu.com/ubuntu/ trusty-updates/main libxml2 amd64 2.9.1+dfsg1-3ubuntu4.13 [573 kB]
Get:8 http://archive.ubuntu.com/ubuntu/ trusty/main sgml-base all 1.26+nmu4ubuntu1 [12.5 kB]
Get:9 http://archive.ubuntu.com/ubuntu/ trusty/main fonts-dejavu-core all 2.34-1ubuntu1 [1024 kB]
Get:10 http://archive.ubuntu.com/ubuntu/ trusty-updates/main fontconfig-config all 2.11.0-0ubuntu4.2 [47.4 kB]
Get:11 http://archive.ubuntu.com/ubuntu/ trusty-updates/main libfreetype6 amd64 2.5.2-1ubuntu2.8 [304 kB]
Get:12 http://archive.ubuntu.com/ubuntu/ trusty-updates/main libfontconfig1 amd64 2.11.0-0ubuntu4.2 [123 kB]
Get:13 http://archive.ubuntu.com/ubuntu/ trusty-updates/main libjpeg-turbo8 amd64 1.3.0-0ubuntu2.1 [104 kB]
Get:14 http://archive.ubuntu.com/ubuntu/ trusty/main libjpeg8 amd64 8c-2ubuntu8 [2194 B]
Get:15 http://archive.ubuntu.com/ubuntu/ trusty-updates/main libjbig0 amd64 2.0-2ubuntu4.1 [26.1 kB]
Get:16 http://archive.ubuntu.com/ubuntu/ trusty-updates/main libtiff5 amd64 4.0.3-7ubuntu0.9 [147 kB]
Get:17 http://archive.ubuntu.com/ubuntu/ trusty/main libvpx1 amd64 1.3.0-2 [556 kB]
Get:18 http://archive.ubuntu.com/ubuntu/ trusty-updates/main libxpm4 amd64 1:3.5.10-1ubuntu0.1 [33.2 kB]
Get:19 http://archive.ubuntu.com/ubuntu/ trusty-updates/main libgd3 amd64 2.1.0-3ubuntu0.10 [123 kB]
Get:20 http://archive.ubuntu.com/ubuntu/ trusty-updates/main libxslt1.1 amd64 1.1.28-2ubuntu0.1 [145 kB]
Get:21 http://archive.ubuntu.com/ubuntu/ trusty/main geoip-database all 20140313-1 [1196 kB]
Get:22 http://archive.ubuntu.com/ubuntu/ trusty/main xml-core all 0.13+nmu2 [23.3 kB]
Get:23 http://archive.ubuntu.com/ubuntu/ trusty-updates/main nginx-common all 1.4.6-1ubuntu3.9 [18.9 kB]
Get:24 http://archive.ubuntu.com/ubuntu/ trusty-updates/main nginx-core amd64 1.4.6-1ubuntu3.9 [325 kB]
Get:25 http://archive.ubuntu.com/ubuntu/ trusty-updates/main nginx all 1.4.6-1ubuntu3.9 [5418 B]
debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (This frontend requires a controlling tty.)
debconf: falling back to frontend: Teletype
dpkg-preconfigure: unable to re-open stdin:
Fetched 5593 kB in 23s (238 kB/s)
Selecting previously unselected package libgeoip1:amd64.
(Reading database ... 11579 files and directories currently installed.)
Preparing to unpack .../libgeoip1_1.6.0-1_amd64.deb ...
Unpacking libgeoip1:amd64 (1.6.0-1) ...
Selecting previously unselected package libxau6:amd64.
Preparing to unpack .../libxau6_1%3a1.0.8-1_amd64.deb ...
Unpacking libxau6:amd64 (1:1.0.8-1) ...
Selecting previously unselected package libxdmcp6:amd64.
Preparing to unpack .../libxdmcp6_1%3a1.1.1-1_amd64.deb ...
Unpacking libxdmcp6:amd64 (1:1.1.1-1) ...
Selecting previously unselected package libxcb1:amd64.
Preparing to unpack .../libxcb1_1.10-2ubuntu1_amd64.deb ...
Unpacking libxcb1:amd64 (1.10-2ubuntu1) ...
Selecting previously unselected package libx11-data.
Preparing to unpack .../libx11-data_2%3a1.6.2-1ubuntu2.1_all.deb ...
Unpacking libx11-data (2:1.6.2-1ubuntu2.1) ...
Selecting previously unselected package libx11-6:amd64.
Preparing to unpack .../libx11-6_2%3a1.6.2-1ubuntu2.1_amd64.deb ...
Unpacking libx11-6:amd64 (2:1.6.2-1ubuntu2.1) ...
Selecting previously unselected package libxml2:amd64.
Preparing to unpack .../libxml2_2.9.1+dfsg1-3ubuntu4.13_amd64.deb ...
Unpacking libxml2:amd64 (2.9.1+dfsg1-3ubuntu4.13) ...
Selecting previously unselected package sgml-base.
Preparing to unpack .../sgml-base_1.26+nmu4ubuntu1_all.deb ...
Unpacking sgml-base (1.26+nmu4ubuntu1) ...
Selecting previously unselected package fonts-dejavu-core.
Preparing to unpack .../fonts-dejavu-core_2.34-1ubuntu1_all.deb ...
Unpacking fonts-dejavu-core (2.34-1ubuntu1) ...
Selecting previously unselected package fontconfig-config.
Preparing to unpack .../fontconfig-config_2.11.0-0ubuntu4.2_all.deb ...
Unpacking fontconfig-config (2.11.0-0ubuntu4.2) ...
Selecting previously unselected package libfreetype6:amd64.
Preparing to unpack .../libfreetype6_2.5.2-1ubuntu2.8_amd64.deb ...
Unpacking libfreetype6:amd64 (2.5.2-1ubuntu2.8) ...
Selecting previously unselected package libfontconfig1:amd64.
Preparing to unpack .../libfontconfig1_2.11.0-0ubuntu4.2_amd64.deb ...
Unpacking libfontconfig1:amd64 (2.11.0-0ubuntu4.2) ...
Selecting previously unselected package libjpeg-turbo8:amd64.
Preparing to unpack .../libjpeg-turbo8_1.3.0-0ubuntu2.1_amd64.deb ...
Unpacking libjpeg-turbo8:amd64 (1.3.0-0ubuntu2.1) ...
Selecting previously unselected package libjpeg8:amd64.
Preparing to unpack .../libjpeg8_8c-2ubuntu8_amd64.deb ...
Unpacking libjpeg8:amd64 (8c-2ubuntu8) ...
Selecting previously unselected package libjbig0:amd64.
Preparing to unpack .../libjbig0_2.0-2ubuntu4.1_amd64.deb ...
Unpacking libjbig0:amd64 (2.0-2ubuntu4.1) ...
Selecting previously unselected package libtiff5:amd64.
Preparing to unpack .../libtiff5_4.0.3-7ubuntu0.9_amd64.deb ...
Unpacking libtiff5:amd64 (4.0.3-7ubuntu0.9) ...
Selecting previously unselected package libvpx1:amd64.
Preparing to unpack .../libvpx1_1.3.0-2_amd64.deb ...
Unpacking libvpx1:amd64 (1.3.0-2) ...
Selecting previously unselected package libxpm4:amd64.
Preparing to unpack .../libxpm4_1%3a3.5.10-1ubuntu0.1_amd64.deb ...
Unpacking libxpm4:amd64 (1:3.5.10-1ubuntu0.1) ...
Selecting previously unselected package libgd3:amd64.
Preparing to unpack .../libgd3_2.1.0-3ubuntu0.10_amd64.deb ...
Unpacking libgd3:amd64 (2.1.0-3ubuntu0.10) ...
Selecting previously unselected package libxslt1.1:amd64.
Preparing to unpack .../libxslt1.1_1.1.28-2ubuntu0.1_amd64.deb ...
Unpacking libxslt1.1:amd64 (1.1.28-2ubuntu0.1) ...
Selecting previously unselected package geoip-database.
Preparing to unpack .../geoip-database_20140313-1_all.deb ...
Unpacking geoip-database (20140313-1) ...
Selecting previously unselected package xml-core.
Preparing to unpack .../xml-core_0.13+nmu2_all.deb ...
Unpacking xml-core (0.13+nmu2) ...
Selecting previously unselected package nginx-common.
Preparing to unpack .../nginx-common_1.4.6-1ubuntu3.9_all.deb ...
Unpacking nginx-common (1.4.6-1ubuntu3.9) ...
Selecting previously unselected package nginx-core.
Preparing to unpack .../nginx-core_1.4.6-1ubuntu3.9_amd64.deb ...
Unpacking nginx-core (1.4.6-1ubuntu3.9) ...
Selecting previously unselected package nginx.
Preparing to unpack .../nginx_1.4.6-1ubuntu3.9_all.deb ...
Unpacking nginx (1.4.6-1ubuntu3.9) ...
Processing triggers for ureadahead (0.100.0-16) ...
Setting up libgeoip1:amd64 (1.6.0-1) ...
Setting up libxau6:amd64 (1:1.0.8-1) ...
Setting up libxdmcp6:amd64 (1:1.1.1-1) ...
Setting up libxcb1:amd64 (1.10-2ubuntu1) ...
Setting up libx11-data (2:1.6.2-1ubuntu2.1) ...
Setting up libx11-6:amd64 (2:1.6.2-1ubuntu2.1) ...
Setting up libxml2:amd64 (2.9.1+dfsg1-3ubuntu4.13) ...
Setting up sgml-base (1.26+nmu4ubuntu1) ...
Setting up fonts-dejavu-core (2.34-1ubuntu1) ...
Setting up fontconfig-config (2.11.0-0ubuntu4.2) ...
Setting up libfreetype6:amd64 (2.5.2-1ubuntu2.8) ...
Setting up libfontconfig1:amd64 (2.11.0-0ubuntu4.2) ...
Setting up libjpeg-turbo8:amd64 (1.3.0-0ubuntu2.1) ...
Setting up libjpeg8:amd64 (8c-2ubuntu8) ...
Setting up libjbig0:amd64 (2.0-2ubuntu4.1) ...
Setting up libtiff5:amd64 (4.0.3-7ubuntu0.9) ...
Setting up libvpx1:amd64 (1.3.0-2) ...
Setting up libxpm4:amd64 (1:3.5.10-1ubuntu0.1) ...
Setting up libgd3:amd64 (2.1.0-3ubuntu0.10) ...
Setting up libxslt1.1:amd64 (1.1.28-2ubuntu0.1) ...
Setting up geoip-database (20140313-1) ...
Setting up xml-core (0.13+nmu2) ...
Setting up nginx-common (1.4.6-1ubuntu3.9) ...
debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (This frontend requires a controlling tty.)
debconf: falling back to frontend: Teletype
Processing triggers for ureadahead (0.100.0-16) ...
Setting up nginx-core (1.4.6-1ubuntu3.9) ...
invoke-rc.d: policy-rc.d denied execution of start.
Setting up nginx (1.4.6-1ubuntu3.9) ...
Processing triggers for libc-bin (2.19-0ubuntu6.14) ...
Processing triggers for sgml-base (1.26+nmu4ubuntu1) ...
Removing intermediate container 2ea3409a4c27
---> 769fa9518184
Step 5/5 : EXPOSE 80
---> Running in 2ea05e55193d
Removing intermediate container 2ea05e55193d
---> 3ed11623e037
Successfully built 3ed11623e037
Successfully tagged yangshuo/df_1:latest
[yang@izwz9bge0ivbgoesxak65wz df_1]$
参数说明:
-t
:指定要创建的目标镜像名.
:Dockerfile
文件所在目录,可以指定Dockerfile
的绝对路径
从上面可以看出,我们在 Dockerfile
文件中写入的命令,会在docker build
命令中分步的执行。
同时,每执行一步,会返回一个唯一 ID
,这个ID
,就是我们所说的构建过程中的中间层镜像。
如果我们使用了 -q
选项,就不会显示构建过程。
2、使用 docker images
查看创建的镜像已经在列表中存在,镜像ID为 be5e1d8bd085
[yang@izwz9bge0ivbgoesxak65wz df_1]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
yangshuo/df_1 latest 3ed11623e037 About a minute ago 222MB
yang/ubuntu_commit_test latest 6cf1911fcffa 35 minutes ago 171MB
hello-world latest fce289e99eb9 13 days ago 1.84kB
ubuntu 14.04 7e4b16ae8b23 2 weeks ago 188MB
ubuntu latest 1d9c17228a9e 2 weeks ago 86.7MB
centos latest 1e1148e4cc2c 5 weeks ago 202MB
ubuntu 15.10 9b9cb95443b5 2 years ago 137MB
[yang@izwz9bge0ivbgoesxak65wz df_1]$
3、使用新的镜像来创建容器
[yang@izwz9bge0ivbgoesxak65wz df_1]$ docker run -d --name nginx_web_df -p 80 yangshuo/df_1 nginx -g "daemon off;"
c3c025ce40904b6cce725e304ad8f45d0d21f61b984bb6211ffdf7fd471665c0
[yang@izwz9bge0ivbgoesxak65wz df_1]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c3c025ce4090 yangshuo/df_1 "nginx -g 'daemon of…" 41 seconds ago Up 40 seconds 0.0.0.0:32771->80/tcp nginx_web_df
85b8c68cf6da yang/ubuntu_commit_test "nginx -g 'daemon of…" 32 minutes ago Up 32 minutes 0.0.0.0:32770->80/tcp nginx_web
[yang@izwz9bge0ivbgoesxak65wz df_1]$
4、测试
[yang@izwz9bge0ivbgoesxak65wz df_1]$ curl http://127.0.0.1:32771
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[yang@izwz9bge0ivbgoesxak65wz df_1]$
设置镜像标签
我们可以使用 docker tag
命令,为镜像添加一个新的标签。
[root@docker ~]# docker tag be5e1d8bd085 runoob/centos:dev
docker tag
镜像ID,这里是 be5e1d8bd085
,用户名称、镜像源名(repository name)和新的标签名(tag)。
使用 docker images
命令可以看到,ID
为 be5e1d8bd085
的镜像多一个标签。
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
runoob/centos 6.7 be5e1d8bd085 3 minutes ago 191MB
runoob/centos dev be5e1d8bd085 3 minutes ago 191MB
runoob/ubuntu v2 b14eca5fdbcb 17 minutes ago 137MB
ubuntu 14.04 f17b6a61de28 8 days ago 188MB
httpd latest 2a51bb06dc8b 12 days ago 132MB
centos 6.7 192ad0341c8b 7 weeks ago 191MB
centos latest 75835a67d134 7 weeks ago 200MB
hello-world latest 4ab4c602aa5e 2 months ago 1.84kB
ubuntu 15.10 9b9cb95443b5 2 years ago 137MB
training/webapp latest 6fae60ef3446 3 years ago 349MB
[root@docker ~]#
来源:CSDN
作者:不知所起 一往而深
链接:https://blog.csdn.net/weixin_42112635/article/details/84679532