Docker构建镜像有两种方式:
docker commit 通过容器构建
docker build 通过dockerfile文件构建
1.使用docker commit 构建
[root@localhost docker]# 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 value Apply Dockerfile instruction to the created image (default [])
--help Print usage
-m, --message string Commit message
-p, --pause Pause container during commit (default true)
[root@localhost docker]#
可以看到格式为:docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
- -a 作者信息
- -m 提交信息
- -p 提交时暂停容器运行,默认为true
查看我们之前创建的容器( Docker容器中部署静态网页):
[root@localhost docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
25fcbf6e953d hub.c.163.com/library/ubuntu "/bin/bash" 45 hours ago Up 45 hours 0.0.0.0:32773->80/tcp static_test
然后,使用以下命令构建镜像:
[root@localhost docker]# docker commit -a 'xingguo' -m 'commit_test2' static_test xingguo/commit_test2
sha256:cbff3a77973d63a38421e8a02e403409169c7e0d266aa2457e8c8ea5001b9f91
[root@localhost docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
xingguo/commit_test2 latest cbff3a77973d 39 seconds ago 278.9 MB
[root@localhost docker]#
然后,使用这个镜像运行一个容器(这里同时启动容器中nginx进程):
[root@localhost docker]# docker run -d --name commit_con_test1 -p 80 xingguo/commit_test2 nginx -g 'daemon off;'
b1bd5b34b42ffd58a76eea6692e4975a8d62d6f2d5945262d2d2fc457a411837
[root@localhost docker]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b1bd5b34b42f xingguo/commit_test2 "nginx -g 'daemon off" 10 minutes ago Up 9 minutes 0.0.0.0:32774->80/tcp commit_con_test1
[root@localhost docker]#
然后进行访问里面的静态网页index.html
[root@localhost docker]# curl http://127.0.0.1:32774/index.html
<html>
<body>
<h1>
this is the first docker static file
</h1>
</body>
</html>
[root@localhost docker]#
到此,就把原来的容器构建成一个新的镜像。
这里是把之前已经构建好的容器构建成镜像,提供同样的服务。但是之前创建容器的过程步骤比较多的。下面使用dockerfile进行构建。步骤就简单很多。
2.使用docker build通过dockerfile文件构建镜像
dockerfile文件就是包含了一些命令的文本文件,里面的命令包含我们需要需要运行的镜像或者安装的软件等。
示例:
From hub.c.163.com/library/ubuntu
MAINTAINER diaoxingguo “diaoxingguo@163.com”
RUN apt-get update
RUN apt-get install -y nginx
EXPOSE 80
其中:
FROM 需要用到的镜像
MAINTAINER 描述创建人,联系方式
RUN 是容器内需要执行的一些命令
EXPOSE 是暴露的端口号
下面先准备存放Dockerfile文件的目录和内容。我这里放在了
/root/dockerfile/df_test下,可以自行创建,然后创建Dockerfile文件。
我这里创建好了:
[root@localhost df_test]# pwd
/root/dockerfile/df_test
[root@localhost df_test]# ll
总用量 4
-rw-r--r--. 1 root root 164 3月 11 11:14 Dockerfile
[root@localhost df_test]# cat Dockerfile
#first dockerfile for test
From hub.c.163.com/library/ubuntu
MAINTAINER diaoxingguo "diaoxingguo@163.com"
RUN apt-get update
RUN apt-get install -y nginx
EXPOSE 80
[root@localhost df_test]#
下面是docker build的格式:
docker build [OPTIONS] PATH | URL | -
使用docker build --help
可以查看相关参数,其中-t是指定镜像的名字。
下面构建镜像:
[root@localhost df_test]# docker build -t='df_test2' .
由于在当前目录下,所以直接使用“.”
创建完成后使用docker images
查看。
Successfully built 133e65cea131
[root@localhost df_test]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
xingguo/commit_test2 latest cbff3a77973d About an hour ago 278.9 MB
df_test2 latest 133e65cea131 2 minutes ago 226.3 MB
然后运行这个镜像:
[root@localhost df_test]# docker run -d --name build_con_test1 -p 80 df_test2 nginx -g 'daemon off;'
a3984cdcab6050167c4bc690949e7123607188a4f8d55d438caf4007f2db3267
[root@localhost df_test]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a3984cdcab60 df_test2 "nginx -g 'daemon off" 14 seconds ago Up 8 seconds 0.0.0.0:32775->80/tcp build_con_test1
[root@localhost df_test]#
可以看到镜像运行成功。