Docker镜像创建

  • 一、基于现有容器(现有镜像)创建镜像服务
  • 1.1创建镜像命令
  • 1.2 制作过程
  • 二、基于模板创建镜像服务
  • 2.1 创建过程
  • 三、基于Dockerfile创建镜像
  • 3.1 Dockerfile操作指令
  • 3.2 创作镜像过程(例如,创建apache)


一、基于现有容器(现有镜像)创建镜像服务

1.1创建镜像命令

  • docker create -it centos /bin/bash
  • docker commit [选项] 容器id/名称 仓库名称:标签
  • docker images | grep daoke

1.2 制作过程

[root@localhost ~]# docker pull centos   ##下载一个镜像
Using default tag: latest
latest: Pulling from library/centos
3c72a8ed6814: Pull complete 
Digest: sha256:76d24f3ba3317fa945743bb3746fbaf3a0b752f10b10376960de01da70685fbd
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest


[root@localhost ~]# docker images    ##查看镜像
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              0d120b6ccaa8        3 months ago        215MB
[root@localhost ~]# docker create -it centos /bin/bash    ##对已有镜像创建容器
35b0118ddf88d177b802a52be14ca4c47c8b95608eca26bc8a1fcd213acb5b39


[root@localhost ~]# docker ps -a    ##查看创建的容器
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
35b0118ddf88        centos              "/bin/bash"         12 seconds ago      Created                                 xenodochial_heyrovsky


##创建镜像
[root@localhost ~]# docker commit -m "newest" -a "daoke" 35b0118ddf88 daoke:latest    
sha256:18d693dea91e87749b4a440b9b5208b4819d7925b85d0e1589ed8d7e85f1318c

[root@localhost ~]# docker images      ##查看镜像
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
daoke               latest              18d693dea91e        16 seconds ago      215MB
centos              latest              0d120b6ccaa8        3 months ago        215MB

[root@localhost ~]# docker images | grep daoke
daoke               latest              18d693dea91e        35 seconds ago      215MB

二、基于模板创建镜像服务

2.1 创建过程

##把debian-7.0-x86-minimal.tar.gz软件包导入到root目录下
[root@localhost ~]# ll
总用量 134068
-rw-------.  1 root root      1903 8月  10 11:14 anaconda-ks.cfg
-rw-r--r--.  1 root root  88436521 11月 26 03:24 debian-7.0-x86-minimal.tar.gz
-rw-r--r--.  1 root root      1951 8月  10 11:15 initial-setup-ks.cfg
drwxr-xr-x. 38 7161 31415     4096 10月 26 20:37 mysql-5.7.20
drwxr-xr-x.  2 root root         6 9月  10 08:02 公共
drwxr-xr-x.  2 root root         6 9月  10 08:02 模板
drwxr-xr-x.  2 root root         6 9月  10 08:02 视频
drwxr-xr-x.  2 root root         6 9月  10 08:02 图片
drwxr-xr-x.  2 root root         6 9月  10 08:02 文档
drwxr-xr-x.  2 root root         6 9月  10 08:02 下载
drwxr-xr-x.  2 root root         6 9月  10 08:02 音乐
drwxr-xr-x.  2 root root         6 9月  10 08:02 桌面

[root@localhost ~]# docker rmi 18d693dea91e  ##把刚刚创建的镜像删除
Untagged: daoke:latest
Deleted: sha256:18d693dea91e87749b4a440b9b5208b4819d7925b85d0e1589ed8d7e85f1318c
[root@localhost ~]# docker images    ##查看
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              0d120b6ccaa8        3 months ago        215MB

###导出新的镜像
[root@localhost ~]# cat debian-7.0-x86-minimal.tar.gz | docker import - daoke:latest
sha256:37906cde6825cc6f4b0d54256ccede074de61b720f1d0b55688401c9c4dd81a3

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
daoke               latest              37906cde6825        8 seconds ago       215MB
centos              latest              0d120b6ccaa8        3 months ago        215MB

三、基于Dockerfile创建镜像

定义:dockerfile是由一组指令组成的文件

3.1 Dockerfile操作指令

指令

含义

FROM镜像

指定新镜像所基于的镜像,第一条指令必须为FROM指令,每创建一个镜像就需要一条FROM指令

MAIINTAINER名字

说明新镜像的维护人信息

RUN 命令

在所基于的镜像上执行的命令,并提交到新的镜像中

CMD[ "要运行的程序”,“参数1,参数2”]

指令启动容器时要运行的命令或脚本,Dockerfile只能有一条CMD命令,如果指令多条则只能最后一条被执行

EXPOSE端口号

指定新镜像加载到Docker时要开启的端口

ENV环境变量 变量值

设置一个环境变量的值,会被后面的RUN使用

ADD 源文件/目录 目标文件/目录

将源文件复制到目标文件,源文件要与Dockerfile位于相同目录,或者一个URL

COPY 源文件/目录 目标文件/目录

将本地主机上的文件/目录复制到目标地点,源文件/目录要与Dockerfile在相同目录中

VOLUME [ “目录” ]

在容器中创建一个挂载点

USER 用户名/UID

指定运用容器时的用户

WORKDIR 路径

为后续的RUN,CMD,ENTRYPOINT指定工作目录

ONBUILD命令

指定所生成的镜像作为一个基础镜像时所要运行的命令

HEALTHCHECK

健康检查

3.2 创作镜像过程(例如,创建apache)

1,创建挂载点目录

[root@localhost ~]# mkdir apache
[root@localhost ~]# cd apache/

2,创建dockerfile文件

[root@localhost apache]# vim DOckerfile
FROM  centos:7                     ###基于centos:7镜像创建apache镜像,没有centos:7,会自动创建
MAINTAINER  This  is my  object    ###指定维护镜像的用户信息(自己定义的)
RUN yum -y update                  ##镜像操作指定apache软件
RUN yum -y install httpd
EXPOSE 80                          ##开启80端口
ADD index.html /var/www/html/index.html   ##复制网站首页文件
ADD run.sh /run.sh                  ##将执行的脚本复制到镜像中
CMD ["/run.sh"]                     ##启动容器时执行脚本

3, 编写执行脚本

[root@localhost apache]# vim run.sh
#!/bin/bash
rm -rf /run/httpd/*
exec /usr/sbin/apachectl -D FOREGROUND

4, 写apache页面

[root@localhost apache]# vi index.html
<h1>This is my web</h1>

5,生成新镜像(注意:每生成一个临时容器到下一步都会丢弃掉)

[root@localhost apache]# docker build -t httpd:centos .
Sending build context to Docker daemon  4.096kB
Step 1/9 : FROM centos:7
7: Pulling from library/centos
2d473b07cdd5: Pull complete 
Digest: sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e
Status: Downloaded newer image for centos:7
 ---> 8652b9f0cb4c
Step 2/9 : MAINTAINER This is my object
 ---> Running in 0dbd69a5f274
Removing intermediate container 0dbd69a5f274
 ---> 30258fd67529
Step 3/9 : RUN yum -y update
 ---> Running in 08a65a126c4d
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
 * base: mirrors.ustc.edu.cn
 * extras: mirrors.cn99.com
 * updates: mirrors.ustc.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package bind-license.noarch 32:9.11.4-26.P2.el7 will be updated
---> Package bind-license.noarch 32:9.11.4-26.P2.el7_9.2 will be an update
---> Package coreutils.x86_64 0:8.22-24.el7 will be updated
---> Package coreutils.x86_64 0:8.22-24.el7_9.2 will be an update
---> Package curl.x86_64 0:7.29.0-59.el7 will be updated
---> Package curl.x86_64 0:7.29.0-59.el7_9.1 will be an update
---> Package glib2.x86_64 0:2.56.1-7.el7 will be updated
---> Package glib2.x86_64 0:2.56.1-8.el7 will be an update
---> Package kpartx.x86_64 0:0.4.9-133.el7 will be updated
---> Package kpartx.x86_64 0:0.4.9-134.el7_9 will be an update
---> Package libcurl.x86_64 0:7.29.0-59.el7 will be updated
---> Package libcurl.x86_64 0:7.29.0-59.el7_9.1 will be an update
---> Package python.x86_64 0:2.7.5-89.el7 will be updated
---> Package python.x86_64 0:2.7.5-90.el7 will be an update
---> Package python-libs.x86_64 0:2.7.5-89.el7 will be updated
---> Package python-libs.x86_64 0:2.7.5-90.el7 will be an update
---> Package systemd.x86_64 0:219-78.el7 will be updated
---> Package systemd.x86_64 0:219-78.el7_9.2 will be an update
---> Package systemd-libs.x86_64 0:219-78.el7 will be updated
---> Package systemd-libs.x86_64 0:219-78.el7_9.2 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package           Arch        Version                       Repository    Size
================================================================================
Updating:
 bind-license      noarch      32:9.11.4-26.P2.el7_9.2       updates       90 k
 coreutils         x86_64      8.22-24.el7_9.2               updates      3.3 M
 curl              x86_64      7.29.0-59.el7_9.1             updates      271 k
 glib2             x86_64      2.56.1-8.el7                  updates      2.5 M
 kpartx            x86_64      0.4.9-134.el7_9               updates       81 k
 libcurl           x86_64      7.29.0-59.el7_9.1             updates      223 k
 python            x86_64      2.7.5-90.el7                  updates       96 k
 python-libs       x86_64      2.7.5-90.el7                  updates      5.6 M
 systemd           x86_64      219-78.el7_9.2                updates      5.1 M
 systemd-libs      x86_64      219-78.el7_9.2                updates      418 k

Transaction Summary
================================================================================
Upgrade  10 Packages

Total download size: 18 M
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
warning: /var/cache/yum/x86_64/7/updates/packages/bind-license-9.11.4-26.P2.el7_9.2.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for bind-license-9.11.4-26.P2.el7_9.2.noarch.rpm is not installed
--------------------------------------------------------------------------------
Total                                              6.8 MB/s |  18 MB  00:02     
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
 Userid     : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
 Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
 Package    : centos-release-7-9.2009.0.el7.centos.x86_64 (@CentOS)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Updating   : libcurl-7.29.0-59.el7_9.1.x86_64                            1/20 
  Updating   : coreutils-8.22-24.el7_9.2.x86_64                            2/20 
  Updating   : python-libs-2.7.5-90.el7.x86_64                             3/20 
  Updating   : systemd-libs-219-78.el7_9.2.x86_64                          4/20 
  Updating   : systemd-219-78.el7_9.2.x86_64                               5/20 
Failed to get D-Bus connection: Operation not permitted
  Updating   : python-2.7.5-90.el7.x86_64                                  6/20 
  Updating   : curl-7.29.0-59.el7_9.1.x86_64                               7/20 
  Updating   : glib2-2.56.1-8.el7.x86_64                                   8/20 
  Updating   : 32:bind-license-9.11.4-26.P2.el7_9.2.noarch                 9/20 
  Updating   : kpartx-0.4.9-134.el7_9.x86_64                              10/20 
  Cleanup    : systemd-219-78.el7.x86_64                                  11/20 
  Cleanup    : python-2.7.5-89.el7.x86_64                                 12/20 
  Cleanup    : python-libs-2.7.5-89.el7.x86_64                            13/20 
  Cleanup    : curl-7.29.0-59.el7.x86_64                                  14/20 
  Cleanup    : 32:bind-license-9.11.4-26.P2.el7.noarch                    15/20 
  Cleanup    : libcurl-7.29.0-59.el7.x86_64                               16/20 
  Cleanup    : coreutils-8.22-24.el7.x86_64                               17/20 
  Cleanup    : systemd-libs-219-78.el7.x86_64                             18/20 
  Cleanup    : glib2-2.56.1-7.el7.x86_64                                  19/20 
  Cleanup    : kpartx-0.4.9-133.el7.x86_64                                20/20 
  Verifying  : curl-7.29.0-59.el7_9.1.x86_64                               1/20 
  Verifying  : python-libs-2.7.5-90.el7.x86_64                             2/20 
  Verifying  : kpartx-0.4.9-134.el7_9.x86_64                               3/20 
  Verifying  : coreutils-8.22-24.el7_9.2.x86_64                            4/20 
  Verifying  : systemd-libs-219-78.el7_9.2.x86_64                          5/20 
  Verifying  : libcurl-7.29.0-59.el7_9.1.x86_64                            6/20 
  Verifying  : 32:bind-license-9.11.4-26.P2.el7_9.2.noarch                 7/20 
  Verifying  : systemd-219-78.el7_9.2.x86_64                               8/20 
  Verifying  : python-2.7.5-90.el7.x86_64                                  9/20 
  Verifying  : glib2-2.56.1-8.el7.x86_64                                  10/20 
  Verifying  : libcurl-7.29.0-59.el7.x86_64                               11/20 
  Verifying  : python-2.7.5-89.el7.x86_64                                 12/20 
  Verifying  : kpartx-0.4.9-133.el7.x86_64                                13/20 
  Verifying  : systemd-219-78.el7.x86_64                                  14/20 
  Verifying  : 32:bind-license-9.11.4-26.P2.el7.noarch                    15/20 
  Verifying  : curl-7.29.0-59.el7.x86_64                                  16/20 
  Verifying  : systemd-libs-219-78.el7.x86_64                             17/20 
  Verifying  : coreutils-8.22-24.el7.x86_64                               18/20 
  Verifying  : python-libs-2.7.5-89.el7.x86_64                            19/20 
  Verifying  : glib2-2.56.1-7.el7.x86_64                                  20/20 

Updated:
  bind-license.noarch 32:9.11.4-26.P2.el7_9.2                                   
  coreutils.x86_64 0:8.22-24.el7_9.2                                            
  curl.x86_64 0:7.29.0-59.el7_9.1                                               
  glib2.x86_64 0:2.56.1-8.el7                                                   
  kpartx.x86_64 0:0.4.9-134.el7_9                                               
  libcurl.x86_64 0:7.29.0-59.el7_9.1                                            
  python.x86_64 0:2.7.5-90.el7                                                  
  python-libs.x86_64 0:2.7.5-90.el7                                             
  systemd.x86_64 0:219-78.el7_9.2                                               
  systemd-libs.x86_64 0:219-78.el7_9.2                                          

Complete!
Removing intermediate container 08a65a126c4d
 ---> efa963f95abb
Step 4/9 : RUN yum -y install httpd
 ---> Running in ff02d3c4d646
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: mirrors.ustc.edu.cn
 * extras: mirrors.cn99.com
 * updates: mirrors.ustc.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-97.el7.centos will be installed
--> Processing Dependency: httpd-tools = 2.4.6-97.el7.centos for package: httpd-2.4.6-97.el7.centos.x86_64
--> Processing Dependency: system-logos >= 7.92.1-1 for package: httpd-2.4.6-97.el7.centos.x86_64
--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-97.el7.centos.x86_64
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-97.el7.centos.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-97.el7.centos.x86_64
--> Running transaction check
---> Package apr.x86_64 0:1.4.8-7.el7 will be installed
---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed
---> Package centos-logos.noarch 0:70.0.6-3.el7.centos will be installed
---> Package httpd-tools.x86_64 0:2.4.6-97.el7.centos will be installed
---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package            Arch         Version                    Repository     Size
================================================================================
Installing:
 httpd              x86_64       2.4.6-97.el7.centos        updates       2.7 M
Installing for dependencies:
 apr                x86_64       1.4.8-7.el7                base          104 k
 apr-util           x86_64       1.5.2-6.el7                base           92 k
 centos-logos       noarch       70.0.6-3.el7.centos        base           21 M
 httpd-tools        x86_64       2.4.6-97.el7.centos        updates        93 k
 mailcap            noarch       2.1.41-2.el7               base           31 k

Transaction Summary
================================================================================
Install  1 Package (+5 Dependent packages)

Total download size: 24 M
Installed size: 32 M
Downloading packages:
--------------------------------------------------------------------------------
Total                                              8.4 MB/s |  24 MB  00:02     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : apr-1.4.8-7.el7.x86_64                                       1/6 
  Installing : apr-util-1.5.2-6.el7.x86_64                                  2/6 
  Installing : httpd-tools-2.4.6-97.el7.centos.x86_64                       3/6 
  Installing : centos-logos-70.0.6-3.el7.centos.noarch                      4/6 
  Installing : mailcap-2.1.41-2.el7.noarch                                  5/6 
  Installing : httpd-2.4.6-97.el7.centos.x86_64                             6/6 
  Verifying  : mailcap-2.1.41-2.el7.noarch                                  1/6 
  Verifying  : apr-1.4.8-7.el7.x86_64                                       2/6 
  Verifying  : apr-util-1.5.2-6.el7.x86_64                                  3/6 
  Verifying  : httpd-2.4.6-97.el7.centos.x86_64                             4/6 
  Verifying  : httpd-tools-2.4.6-97.el7.centos.x86_64                       5/6 
  Verifying  : centos-logos-70.0.6-3.el7.centos.noarch                      6/6 

Installed:
  httpd.x86_64 0:2.4.6-97.el7.centos                                            

Dependency Installed:
  apr.x86_64 0:1.4.8-7.el7                                                      
  apr-util.x86_64 0:1.5.2-6.el7                                                 
  centos-logos.noarch 0:70.0.6-3.el7.centos                                     
  httpd-tools.x86_64 0:2.4.6-97.el7.centos                                      
  mailcap.noarch 0:2.1.41-2.el7                                                 

Complete!
Removing intermediate container ff02d3c4d646
 ---> 3db0bc2d8888
Step 5/9 : EXPOSE 80
 ---> Running in c5f1df1b2dcb
Removing intermediate container c5f1df1b2dcb
 ---> 167b2b41d6dd
Step 6/9 : ADD index.html /var/www/html/index.html
 ---> e06644772dab
Step 7/9 : ADD run.sh /run.sh
 ---> a69d4a700f35
Step 8/9 : RUN chmod 755 /run.sh
 ---> Running in 34dbe82ca2d6
Removing intermediate container 34dbe82ca2d6
 ---> d5f3e15fdc17
Step 9/9 : CMD ["/run.sh"]
 ---> Running in 34dc49192d48
Removing intermediate container 34dc49192d48
 ---> 2b43313c1d7e
Successfully built 2b43313c1d7e
Successfully tagged httpd:centos

6,指定新镜像运行的容器(这里的1000只要不和地址冲突就行)

[root@localhost apache]# docker run -d -p 1000:80 httpd:centos 
163af4d0fb0c7de929fb96bbab9af0e546778f54ec3ca11e379092be77d5bc3b

9 验证

docker u盘 镜像制作 怎样制作docker镜像_docker u盘 镜像制作