docker镜像的生成

docker镜像的生成

一.镜像的生成途径:

  • Dockerfile
  • 基于容器制作
  • Docker Hub automated builds

二. 容器制作镜像

1. 根据容器的更改创建一个新镜像
docker commit [options] container[repository[:tag]]
选项默认说明
–author, -aAuthor (e.g., “John Hannibal Smith hannibal@a-team.com”) 作者(名字,邮箱)
-c, --change list对创建的镜像应用Dockerfile指令
-m, --message string提交信息
-p, --pausetrue提交期间暂停容器
2. 拉取镜像
[root@SYL4 ~]# docker images
REPOSITORY              TAG       IMAGE ID       CREATED        SIZE
seancheng1002/busybox   042601    841ec8ec6c37   5 hours ago    1.24MB
httpd                   2.4.53    c30a46771695   5 days ago     144MB
busybox                 latest    beae173ccac6   3 months ago   1.24MB
[root@SYL4 ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a2abf6c4d29d: Pull complete 
a9edb18cadd1: Pull complete 
589b7251471a: Pull complete 
186b1aaa4aa6: Pull complete 
b4df32aa5a72: Pull complete 
a0bcbecc962e: Pull complete 
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@SYL4 ~]# 
3. 运行容器
[root@SYL4 ~]# docker images
REPOSITORY              TAG       IMAGE ID       CREATED        SIZE
seancheng1002/busybox   042601    841ec8ec6c37   5 hours ago    1.24MB
httpd                   2.4.53    c30a46771695   5 days ago     144MB
busybox                 latest    beae173ccac6   3 months ago   1.24MB
nginx                   latest    605c77e624dd   3 months ago   141MB
[root@SYL4 ~]# docker run -it --name b1 busybox
/ # mkdir data
/ # ls
bin   data  dev   etc   home  proc  root  sys   tmp   usr   var
/ # echo 'dou luo da lu' > /data/index.html
/ # cat /data/index.html 
dou luo da lu
/ # 

4. 在创建镜像时,我们不能关闭容器,必须使其处于运行状态,所以我们必须要另起一个终端,然后执行
-p :在commit时,将容器暂停。
[root@SYL4 ~]# docker commit -p b1
sha256:aa5b7f970c0cd0a621967b5d6a111a41f220b6b5530049b8c416650d4b9a7854
[root@SYL4 ~]# 
[root@SYL4 ~]# docker commit -p b1
sha256:aa5b7f970c0cd0a621967b5d6a111a41f220b6b5530049b8c416650d4b9a7854
4.1 修改名字
[root@SYL4 ~]# docker images
REPOSITORY              TAG       IMAGE ID       CREATED              SIZE
<none>                  <none>    aa5b7f970c0c   About a minute ago   1.24MB
seancheng1002/busybox   042601    841ec8ec6c37   5 hours ago          1.24MB
httpd                   2.4.53    c30a46771695   5 days ago           144MB
busybox                 latest    beae173ccac6   3 months ago         1.24MB
nginx                   latest    605c77e624dd   3 months ago         141MB
httpd                   latest    dabbfbe0c57b   4 months ago         144MB
[root@SYL4 ~]# docker tag aa5b7f970c0c busybox:2060.1
[root@SYL4 ~]# docker images
REPOSITORY              TAG       IMAGE ID       CREATED         SIZE
busybox                 2060.1    aa5b7f970c0c   2 minutes ago   1.24MB
seancheng1002/busybox   042601    841ec8ec6c37   5 hours ago     1.24MB
httpd                   2.4.53    c30a46771695   5 days ago      144MB
busybox                 latest    beae173ccac6   3 months ago    1.24MB
nginx                   latest    605c77e624dd   3 months ago    141MB
httpd                   latest    dabbfbe0c57b   4 months ago    144MB
[root@SYL4 ~]# 
4.2 修改成标准名字
[root@SYL4 ~]# docker tag busybox:2060.1 xiaoyinguhong/busybox:2060.1
[root@SYL4 ~]# docker images
REPOSITORY              TAG       IMAGE ID       CREATED          SIZE
xiaoyinguhong/busybox   2060.1    aa5b7f970c0c   22 minutes ago   1.24MB
busybox                 2060.1    aa5b7f970c0c   22 minutes ago   1.24MB
seancheng1002/busybox   042601    841ec8ec6c37   6 hours ago      1.24MB
httpd                   2.4.53    c30a46771695   5 days ago       144MB
busybox                 latest    beae173ccac6   3 months ago     1.24MB
nginx                   latest    605c77e624dd   3 months ago     141MB
httpd                   latest    dabbfbe0c57b   4 months ago     144MB
[root@SYL4 ~]# 
4.3 运行所创建的镜像,退出后自动停止
busybox没有后台运行的程序
[root@SYL4 ~]# docker run -it --name b2 busybox:2060.1
/ # ls
bin   data  dev   etc   home  proc  root  sys   tmp   usr   var
/ # cat /data/index.html 
dou luo da lu
/ # exit
[root@SYL4 ~]# 
[root@SYL4 ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED          STATUS          PORTS     NAMES
f4fd248bf55e   busybox   "sh"      18 minutes ago   Up 18 minutes             b1
[root@SYL4 ~]# 
4.4 运行所创建的镜像,退出后还在启动
docker run -itd --name b3 busybox:2060.1
-d :分离模式: 在后台运行 -it 交互模式
[root@SYL4 ~]# docker ps -a
CONTAINER ID   IMAGE            COMMAND   CREATED          STATUS                     PORTS     NAMES
2537aa487c61   busybox:2060.1   "sh"      3 minutes ago    Exited (0) 2 minutes ago             b2
f4fd248bf55e   busybox          "sh"      21 minutes ago   Up 21 minutes                        b1
[root@SYL4 ~]# docker run -itd --name b3 busybox:2060.1
f2adf5ea7821fc0742ab019f7d22757d71a82737cf9ae7085bf3e557a4747583
[root@SYL4 ~]# docker ps
CONTAINER ID   IMAGE            COMMAND   CREATED          STATUS          PORTS     NAMES
f2adf5ea7821   busybox:2060.1   "sh"      5 seconds ago    Up 3 seconds              b3
f4fd248bf55e   busybox          "sh"      21 minutes ago   Up 21 minutes             b1
[root@SYL4 ~]# 
4.5 在运行的容器中运行命令 docker exec
[root@SYL4 ~]# docker exec -it b3 /bin/sh
/ # ls
bin   data  dev   etc   home  proc  root  sys   tmp   usr   var
/ # cat /data/index.html 
dou luo da lu
/ # exit
[root@SYL4 ~]# docker ps
CONTAINER ID   IMAGE            COMMAND   CREATED              STATUS              PORTS     NAMES
f2adf5ea7821   busybox:2060.1   "sh"      About a minute ago   Up About a minute             b3
f4fd248bf55e   busybox          "sh"      23 minutes ago       Up 23 minutes                 b1
[root@SYL4 ~]#
4.6 在新容器中运行命令 docker run -d ,-d在后台运行
[root@SYL4 ~]# docker run -d httpd
1dfb49be924cd3b8e99454f04c32215bd2be4e75542e3fc4c644d973ebd7dabb
[root@SYL4 ~]# docker ps
CONTAINER ID   IMAGE            COMMAND              CREATED          STATUS          PORTS     NAMES
1dfb49be924c   httpd            "httpd-foreground"   7 seconds ago    Up 5 seconds    80/tcp    adoring_proskuriakova
f2adf5ea7821   busybox:2060.1   "sh"                 5 minutes ago    Up 5 minutes              b3
f4fd248bf55e   busybox          "sh"                 27 minutes ago   Up 27 minutes             b1
[root@SYL4 ~]#   
5. 登录账号将创建好的镜像传到仓库,docker push
登录账号
[root@SYL4 ~]# docker login 
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: xiaoyinguhong
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[root@SYL4 ~]# 

上传镜像(docker push ),把镜像修改成标准名字
[root@SYL4 ~]# docker tag busybox:2060.1 xiaoyinguhong/busybox:2060.1
    
[root@SYL4 ~]# docker push xiaoyinguhong/busybox:2060.1
The push refers to repository [docker.io/xiaoyinguhong/busybox]
3b243ff3cb8b: Pushed 
01fd6df81c8e: Mounted from library/busybox 
2060.1: digest: sha256:863d27ce72c12a9146688f0241be0c4546083ac60caf6655436d4e9f09a2a34c size: 734
[root@SYL4 ~]# 
6. 查看上传的镜像,另一台主机拉取的镜像
[root@SYL2 ~]# docker pull xiaoyinguhong/busybox:2060.1
2060.1: Pulling from xiaoyinguhong/busybox
5cc84ad355aa: Pull complete 
3a13c6807a9d: Pull complete 
Digest: sha256:863d27ce72c12a9146688f0241be0c4546083ac60caf6655436d4e9f09a2a34c
Status: Downloaded newer image for xiaoyinguhong/busybox:2060.1
docker.io/xiaoyinguhong/busybox:2060.1
[root@SYL2 ~]# docker search xiaoyinguhong
NAME      DESCRIPTION   STARS     OFFICIAL   AUTOMATED
[root@SYL2 ~]# 

[root@SYL2 ~]# docker run -itd --name bpq xiaoyinguhong/busybox:2060.1
cf80d7e7155c20e307fcebc0e7e85eff549855cab99d4c1d6589bbaf2ec03c59
[root@SYL2 ~]# docker ps 
CONTAINER ID   IMAGE                          COMMAND   CREATED          STATUS         PORTS     NAMES
cf80d7e7155c   xiaoyinguhong/busybox:2060.1   "sh"      10 seconds ago   Up 8 seconds             bpq
[root@SYL2 ~]# docker exec -it bpq1 /bin/sh
Error: No such container: bpq1
[root@SYL2 ~]# docker exec -it bpq /bin/sh //-it后面接你所启动的容器
/ # ls
bin   data  dev   etc   home  proc  root  sys   tmp   usr   var
/ # cat /data/index.html 
dou luo da lu
/ # exit
[root@SYL2 ~]# 

在这里插入图片描述

在这里插入图片描述

7. 删除容器
-q 安静模式,成功后只输出镜像ID
[root@SYL4 ~]# docker run -d httpd
0e9ca3312e4a6f66bca2aa197ed84250b2f95cda88f89faec98ddd691296db67
[root@SYL4 ~]# docker run -d httpd
b0919096e9d41c62febc84dc847536af0562823f1e51633617d1fee14e8fa8dd
^[[A[root@SYL4 ~]# docker run -d httpd
77d0064ffb6ced86bfcd0d56a73c57d6d4004bdc816737ff16c50acc3c96aa1d
[root@SYL4 ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS          PORTS     NAMES
77d0064ffb6c   httpd     "httpd-foreground"   11 seconds ago   Up 10 seconds   80/tcp    admiring_blackwell
b0919096e9d4   httpd     "httpd-foreground"   12 seconds ago   Up 10 seconds   80/tcp    practical_fermat
0e9ca3312e4a   httpd     "httpd-foreground"   13 seconds ago   Up 12 seconds   80/tcp    adoring_cartwright
5c71dfb0bd2f   httpd     "httpd-foreground"   28 seconds ago   Up 26 seconds   80/tcp    dreamy_murdock
f4fd248bf55e   busybox   "sh"                 4 minutes ago    Up 4 minutes              b1
[root@SYL4 ~]# docker ps -q//-q 安静模式,成功后只输出镜像ID
77d0064ffb6c
b0919096e9d4
0e9ca3312e4a
5c71dfb0bd2f
f4fd248bf55e
[root@SYL4 ~]# docker rm -f $(docker ps -q|head -4)
77d0064ffb6c
b0919096e9d4
0e9ca3312e4a
5c71dfb0bd2f
[root@SYL4 ~]# docker ps -q
f4fd248bf55e
[root@SYL4 ~]# 
[root@SYL4 ~]# docker ps -aq
1dfb49be924c
f2adf5ea7821
2537aa487c61
f4fd248bf55e
[root@SYL4 ~]# docker rm -f $(docker ps -aq|head -3)
1dfb49be924c
f2adf5ea7821
2537aa487c61
[root@SYL4 ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED       STATUS       PORTS     NAMES
f4fd248bf55e   busybox   "sh"      2 hours ago   Up 2 hours             b1
[root@SYL4 ~]#         
8. 临时容器,一会要删除
[root@SYL4 ~]# docker run -it --rm busybox
/ # ln -s /bin/busybox ls  //做个软链接
/ # ls
bin   dev   etc   home  ls    proc  root  sys   tmp   usr   var
/ # ls -l
total 16
drwxr-xr-x    2 root     root         12288 Dec 29 21:12 bin
drwxr-xr-x    5 root     root           360 Apr 26 07:39 dev
drwxr-xr-x    1 root     root            66 Apr 26 07:39 etc
drwxr-xr-x    2 nobody   nobody           6 Dec 29 21:12 home
lrwxrwxrwx    1 root     root            12 Apr 26 07:42 ls -> /bin/busybox
/ # ./ls
bin   dev   etc   home  ls    proc  root  sys   tmp   usr   var
/ # 

8.1 -f 在前台运行,-h指定网站目录
[root@SYL4 ~]# docker run -it --rm busybox
/ # ls /bin/httpd 
/bin/httpd
/ # mkdir web
/ # echo 'dou po' > web/index.html
/ # httpd -f -h /web/

[root@SYL4 ~]# docker ps
CONTAINER ID   IMAGE            COMMAND              CREATED             STATUS             PORTS     NAMES
1ad63ac65f8a   busybox          "sh"                 6 minutes ago       Up 5 minutes                 vigorous_almeida
1dfb49be924c   httpd            "httpd-foreground"   About an hour ago   Up About an hour   80/tcp    adoring_proskuriakova
f2adf5ea7821   busybox:2060.1   "sh"                 About an hour ago   Up About an hour             b3
f4fd248bf55e   busybox          "sh"                 2 hours ago         Up 2 hours                   b1
[root@SYL4 ~]# docker inspect 1ad63ac65f8a

"Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.5",
[root@SYL4 ~]# curl 172.17.0.5
dou po
[root@SYL4 ~]# 

三. 镜像的生成,导入与导出

1. 重新生成镜像,上传,访问
创建镜像 -a指定作者 -c指定命令 -f在前台运行 -h指定网站的位置 -p暂停
登录账号 docker login
[root@SYL4 ~]# docker commit -a 'xiaoyinguhong <2807689558@qq.com>' -c 'CMD ["/bin/httpd","-f","-h","/data"]' -p b1  xiaoyinguhong/httpd:2060.1
sha256:06493b8526ebe95ebe0eb31b31490f9af851439081df188788963f4728740cbd
[root@SYL4 ~]# docker images
REPOSITORY              TAG       IMAGE ID       CREATED          SIZE
xiaoyinguhong/httpd     2060.1    06493b8526eb   21 seconds ago   1.24MB
busybox                 2060.1    aa5b7f970c0c   2 hours ago      1.24MB
xiaoyinguhong/busybox   2060.1    aa5b7f970c0c   2 hours ago      1.24MB
seancheng1002/busybox   042601    841ec8ec6c37   7 hours ago      1.24MB
httpd                   2.4.53    c30a46771695   5 days ago       144MB
busybox                 latest    beae173ccac6   3 months ago     1.24MB
nginx                   latest    605c77e624dd   3 months ago     141MB
httpd                   latest    dabbfbe0c57b   4 months ago     144MB
[root@SYL4 ~]# 

上传
[root@SYL4 ~]# docker push xiaoyinguhong/httpd:2060.1
The push refers to repository [docker.io/xiaoyinguhong/httpd]
3b243ff3cb8b: Mounted from xiaoyinguhong/busybox 
01fd6df81c8e: Mounted from xiaoyinguhong/busybox 
2060.1: digest: sha256:13e01b61ba1c804597b6bc2c374f2816ef77098d80e97dc5b68d4d32115270bd size: 734
[root@SYL4 ~]# 

访问
[root@SYL4 ~]# docker run -d --name web xiaoyinguhong/httpd:2060.1
4f23928e653ee3b73f4e08781239a8e9c0a92a988e1ba9193bff1ccb75c7e851
[root@SYL4 ~]# 
[root@SYL4 ~]# curl 172.17.0.3
dou luo da lu
[root@SYL4 ~]# 
2. docker +面向对象+操作
[root@SYL4 ~]# docker container ls
CONTAINER ID   IMAGE                        COMMAND                  CREATED         STATUS         PORTS     NAMES
4f23928e653e   xiaoyinguhong/httpd:2060.1   "/bin/httpd -f -h /d…"   8 minutes ago   Up 8 minutes             web
f4fd248bf55e   busybox                      "sh"                     2 hours ago     Up 2 hours               b1
[root@SYL4 ~]# docker image ls
REPOSITORY              TAG       IMAGE ID       CREATED          SIZE
xiaoyinguhong/httpd     2060.1    06493b8526eb   12 minutes ago   1.24MB
busybox                 2060.1    aa5b7f970c0c   2 hours ago      1.24MB
xiaoyinguhong/busybox   2060.1    aa5b7f970c0c   2 hours ago      1.24MB
seancheng1002/busybox   042601    841ec8ec6c37   7 hours ago      1.24MB
httpd                   2.4.53    c30a46771695   5 days ago       144MB
busybox                 latest    beae173ccac6   3 months ago     1.24MB
nginx                   latest    605c77e624dd   3 months ago     141MB
httpd                   latest    dabbfbe0c57b   4 months ago     144MB
[root@SYL4 ~]# 

2.1复制命令,复制到容器里,反过来也可以
[root@SYL4 ~]# docker cp anaconda-ks.cfg b1:/
[root@SYL4 ~]# 
[root@SYL4 ~]# docker run -it --name b1 busybox
/ # mkdir data
/ # ls
bin   data  dev   etc   home  proc  root  sys   tmp   usr   var
/ # echo 'dou luo da lu' > /data/index.html
/ # cat /data/index.html 
dou luo da lu
/ # ls
anaconda-ks.cfg  dev              proc             tmp
bin              etc              root             usr
data             home             sys              var
/ # 

[root@SYL4 ~]# docker cp -a b1:/data .
[root@SYL4 ~]# ls
anaconda-ks.cfg   apr-util-1.6.1         data          httpd-2.4.53.tar.gz
apr-1.7.0         apr-util-1.6.1.tar.gz  findfiles
apr-1.7.0.tar.gz  backup.tar.bz2         httpd-2.4.53
[root@SYL4 ~]# cat data/index.html 
dou luo da lu
[root@SYL4 ~]# 
2.2 看历史
[root@SYL4 ~]# docker history httpd
IMAGE          CREATED        CREATED BY                                      SIZE      COMMENT
dabbfbe0c57b   4 months ago   /bin/sh -c #(nop)  CMD ["httpd-foreground"]     0B        
<missing>      4 months ago   /bin/sh -c #(nop)  EXPOSE 80                    0B        
2.3 查看端口,并访问
[root@SYL4 ~]# docker run -d --name b2 -p 8080:80 xiaoyinguhong/httpd:2060.1
e95ba3a6335e7c8c0cd6a550535eb651b93d4ed173bdfd1cea1d0dcf5bd41e7a
[root@SYL4 ~]# docker port b2
80/tcp -> 0.0.0.0:8080
80/tcp -> :::8080
[root@SYL4 ~]# 

在这里插入图片描述

2.4 看状态
[root@SYL4 ~]# docker stats web
显示运行的进程
[root@SYL4 ~]# docker top b1
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                19285               19265               0                   14:19               pts/0               00:00:00            sh
[root@SYL4 ~]# 

docker rmi 删除一个或多个镜像
3. 镜像的导入 docker save -o
[root@SYL4 ~]# ls
anaconda-ks.cfg  apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz  data          httpd-2.4.53.tar.gz
apr-1.7.0        apr-util-1.6.1    backup.tar.bz2         httpd-2.4.53
[root@SYL4 ~]# docker save -o httpd.xz xiaoyinguhong/httpd:2060.1
[root@SYL4 ~]# ls
anaconda-ks.cfg  apr-1.7.0.tar.gz  apr-util-1.6.1.tar.gz  data          httpd-2.4.53.tar.gz
apr-1.7.0        apr-util-1.6.1    backup.tar.bz2         httpd-2.4.53  httpd.xz
[root@SYL4 ~]# 
[root@SYL4 ~]# scp httpd.tar root@192.168.232.129:.
The authenticity of host '192.168.232.129 (192.168.232.129)' can't be established.
ECDSA key fingerprint is SHA256:WlI+c2MQDTEJhLAvW//ahd5T4DlwkGIfuB3+u8cWJZY.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.232.129' (ECDSA) to the list of known hosts.
root@192.168.232.129's password: 
Permission denied, please try again.
root@192.168.232.129's password: 
httpd.tar                     100% 1442KB  19.3MB/s   00:00    
[root@SYL4 ~]# 
4. 镜像的导出 docker load -i
[root@SYL2 ~]# ls
mysql
[root@SYL2 ~]# ls
httpd.tar  mysql
[root@SYL2 ~]# docker load -i httpd.tar 
Loaded image: xiaoyinguhong/httpd:2060.1
[root@SYL2 ~]# docker images
REPOSITORY              TAG       IMAGE ID       CREATED             SIZE
xiaoyinguhong/httpd     2060.1    06493b8526eb   About an hour ago   1.24MB
xiaoyinguhong/busybox   2060.1    aa5b7f970c0c   3 hours ago         1.24MB
[root@SYL2 ~]# 
[root@SYL2 ~]# docker run -itd --name b1 xiaoyinguhong/httpd:2060.1
16199c3f2656c7d33fed4abee64fcfcb60306f7657214829ac23e9e96112c649
[root@SYL2 ~]# docker ps
CONTAINER ID   IMAGE                        COMMAND                  CREATED          STATUS         PORTS     NAMES
16199c3f2656   xiaoyinguhong/httpd:2060.1   "/bin/httpd -f -h /d…"   11 seconds ago   Up 7 seconds             b1
[root@SYL2 ~]# 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值