012、構建映象(2018-12-29 週六)

三角形發表於2018-12-29
 
在使用Docker的時候,幾乎所有的作業系統和應用程式都有現成的映象,比如centos、debian、ubuntu、nginx、jenkins。直接docker pull 即可。
 
Docker Hub上的官方映象已經幫我們做好了應用程式的配置和優化工作,一般情況下直接使用即可。
 
如果Docker Hub上找不到我們需要的映象,比如自己開發的程式,那就需要自己構建映象了。
 
構建映象的方法有兩種:1、docker commit 命令構建;2、Dockerfile檔案構建
 
docker commit
 
這種方法構建映象比較簡單直觀
    1、執行容器
    2、修改容器
    3、將容器儲存為映象
 
下面是一個基於centos base映象構建vim映象的過程
 
 
root@docker-lab:~# docker run -it centos
Unable to find image 'centos:latest' locally
latest: Pulling from library/centos
a02a4930cb5d: Pull complete
Digest: sha256:38777a4106f0072649128ea4236241345a3ed21c55abbbd53bad5342549f6126
Status: Downloaded newer image for centos:latest
[root@02d010de5fe9 /]# wget
bash: wget: command not found
[root@02d010de5fe9 /]# yum install -y wget
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
* base: mirrors.neusoft.edu.cn
* extras: mirrors.huaweicloud.com
* updates: mirrors.huaweicloud.com
Resolving Dependencies
--> Running transaction check
---> Package wget.x86_64 0:1.14-18.el7 will be installed
--> Finished Dependency Resolution
 
Dependencies Resolved
 
=============================================================================================================================================================
Package                            Arch                                 Version                                    Repository                          Size
=============================================================================================================================================================
Installing:
wget                               x86_64                               1.14-18.el7                                base                               547 k
 
Transaction Summary
=============================================================================================================================================================
Install  1 Package
 
Total download size: 547 k
Installed size: 2.0 M
Downloading packages:
wget-1.14-18.el7.x86_64.rpm                                                                                                           | 547 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : wget-1.14-18.el7.x86_64                                                                                                                   1/1
install-info: No such file or directory for /usr/share/info/wget.info.gz
  Verifying  : wget-1.14-18.el7.x86_64                                                                                                                   1/1
 
Installed:
  wget.x86_64 0:1.14-18.el7                                                                                                                                  
 
Complete!
[root@02d010de5fe9 /]# wget
wget: missing URL
Usage: wget [OPTION]... [URL]...
 
Try `wget --help' for more options.
 
root@docker-lab:~# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
02d010de5fe9        centos              "/bin/bash"         5 minutes ago       Up 5 minutes                            vibrant_bhabha
root@docker-lab:~# docker commit
"docker commit" requires at least 1 and at most 2 arguments.
See 'docker commit --help'.
 
Usage:  docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]] [flags]
 
Create a new image from a container's changes
root@docker-lab:~# docker commit vibrant_bhabha centos-wget
sha256:7fe32c85b3a25ed750373af73dd808600c1e0112b96ddade85a01532cce0f2f3
root@docker-lab:~# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos-wget         latest              7fe32c85b3a2        13 seconds ago      328MB
centos              latest              1e1148e4cc2c        3 weeks ago         202MB
root@docker-lab:~# docker run -it centos-wget
[root@2e47049d7633 /]# wget     
wget: missing URL
Usage: wget [OPTION]... [URL]...
 
Try `wget --help' for more options.
[root@2e47049d7633 /]#
 
docker commit 構建映象,不是推薦的方法:1、手工操作,效率,容易出錯;2、使用者不清楚構建的過程,無法進行審計,存在安全隱患
 
建議使用Dockerfile 構建映象
 

相關文章