查看所有容器

  1. [root@alice ~]# docker ps -a
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. f5895a16fb3d hello-world "/hello" 34 seconds ago Exited (0) 33 seconds ago hopeful_edison
  4. [root@alice ~]#

启动容器(运行镜像)

docker run是日常用的最频繁用的命令之一,同样也是较为复杂的命令之一 命令格式: docker run [OPTIONS] IMAGE [COMMAND] [ARG…] OPTIONS :选项 -i:表示启动-一个可交互的容器, 并持续打开标准输入 -t:表示使用终端关联到容器的标准输入输出上 -d :表示将容器放置后台运行 --rm:退出后即删除容器 --name :表示定义容器唯一名称 IMAGE :表示要运行的镜像 COMMAND :表示启动容器时要运行的命令* ARG:参数

交互式启动容器

[root@alice ~]# docker run -it mmdghh/alpine:latest
/ # 
/ # ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
47: eth0@if48: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP 
    link/ether 02:42:ac:18:26:02 brd ff:ff:ff:ff:ff:ff
    inet 172.24.38.2/24 brd 172.24.38.255 scope global eth0 # 之前/etc/docker/daemon.json写了网段地址
       valid_lft forever preferred_lft forever
/ # [root@alice ~]# 
[root@alice ~]# docker ps -a
CONTAINER ID        IMAGE                  COMMAND             CREATED             STATUS                       PORTS               NAMES
facbbda54346        mmdghh/alpine:latest   "/bin/sh"           2 minutes ago       Exited (130) 6 seconds ago                       nostalgic_bartik
f5895a16fb3d        hello-world            "/hello"            18 minutes ago      Exited (0) 18 minutes ago                        hopeful_edison
[root@alice ~]# 
这里退出之后容器就挂了 因为init为1的进程没有夯住 执行完就退出了

非交互式启动容器

[root@alice ~]# docker run -d --name alpine_sleep  docker.io/mmdghh/alpine:latest /bin/sleep 300
ac75ed049d2a2a3020310a3bb24496d4c72aae76a4a71449cb3c37c589df9395
[root@alice ~]# docker ps -a
CONTAINER ID        IMAGE                  COMMAND             CREATED             STATUS                       PORTS               NAMES
ac75ed049d2a        mmdghh/alpine:latest   "/bin/sleep 300"    21 seconds ago      Up 20 seconds                                    alpine_sleep
facbbda54346        mmdghh/alpine:latest   "/bin/sh"           12 minutes ago      Exited (130) 9 minutes ago                       nostalgic_bartik
f5895a16fb3d        hello-world            "/hello"            27 minutes ago      Exited (0) 27 minutes ago                        hopeful_edison
[root@alice ~]# 
这里有进程夯住之后 容器就不会挂掉了

在宿主机查看进程

[root@alice ~]# ps aux |grep sleep|grep -v grep
root     20957  0.0  0.0   1540   248 ?        Ss   16:30   0:00 /bin/sleep 300
[root@alice ~]# 
docker用了宿主机的内核 所以虽然是隔离的 但是在宿主机仍然可以查看到docker的进程 而且有自己的pid

进入容器

[root@alice ~]# docker ps -a
CONTAINER ID        IMAGE                  COMMAND             CREATED             STATUS                        PORTS               NAMES
06fbbee401aa        mmdghh/alpine:latest   "/bin/sleep 300"    33 seconds ago      Up 32 seconds                                     alpine_sleep
facbbda54346        mmdghh/alpine:latest   "/bin/sh"           18 minutes ago      Exited (130) 16 minutes ago                       nostalgic_bartik
f5895a16fb3d        hello-world            "/hello"            33 minutes ago      Exited (0) 33 minutes ago                         hopeful_edison
[root@alice ~]# docker exec -it 06fbbee401aa /bin/sh
/ # ps aux
PID   USER     TIME  COMMAND
    1 root      0:00 /bin/sleep 300
    6 root      0:00 /bin/sh
   11 root      0:00 ps aux
/ # 
[root@alice ~]# docker exec -it alpine_sleep  /bin/sh # 也可以使用容器的名称进入
/ #

容器的启动/停止/重启

[root@alice ~]# docker ps -a
CONTAINER ID        IMAGE                  COMMAND             CREATED             STATUS                        PORTS               NAMES
06fbbee401aa        mmdghh/alpine:latest   "/bin/sleep 300"    2 minutes ago       Up 2 minutes                                      alpine_sleep
facbbda54346        mmdghh/alpine:latest   "/bin/sh"           20 minutes ago      Exited (130) 17 minutes ago                       nostalgic_bartik
f5895a16fb3d        hello-world            "/hello"            35 minutes ago      Exited (0) 35 minutes ago                         hopeful_edison
[root@alice ~]# docker stop 06fbbee401aa
06fbbee401aa
[root@alice ~]# docker ps -a
CONTAINER ID        IMAGE                  COMMAND             CREATED             STATUS                        PORTS               NAMES
06fbbee401aa        mmdghh/alpine:latest   "/bin/sleep 300"    2 minutes ago       Exited (137) 4 seconds ago                        alpine_sleep
facbbda54346        mmdghh/alpine:latest   "/bin/sh"           20 minutes ago      Exited (130) 18 minutes ago                       nostalgic_bartik
f5895a16fb3d        hello-world            "/hello"            36 minutes ago      Exited (0) 36 minutes ago                         hopeful_edison
[root@alice ~]# docker start 06fbbee401aa
06fbbee401aa
[root@alice ~]# docker ps -a
CONTAINER ID        IMAGE                  COMMAND             CREATED             STATUS                        PORTS               NAMES
06fbbee401aa        mmdghh/alpine:latest   "/bin/sleep 300"    3 minutes ago       Up 1 second                                       alpine_sleep
facbbda54346        mmdghh/alpine:latest   "/bin/sh"           21 minutes ago      Exited (130) 18 minutes ago                       nostalgic_bartik
f5895a16fb3d        hello-world            "/hello"            36 minutes ago      Exited (0) 36 minutes ago                         hopeful_edison
[root@alice ~]# docker restart 06fbbee401aa
06fbbee401aa
[root@alice ~]# docker ps -a
CONTAINER ID        IMAGE                  COMMAND             CREATED             STATUS                        PORTS               NAMES
06fbbee401aa        mmdghh/alpine:latest   "/bin/sleep 300"    3 minutes ago       Up 3 seconds                                      alpine_sleep
facbbda54346        mmdghh/alpine:latest   "/bin/sh"           21 minutes ago      Exited (130) 19 minutes ago                       nostalgic_bartik
f5895a16fb3d        hello-world            "/hello"            36 minutes ago      Exited (0) 36 minutes ago                         hopeful_edison
[root@alice ~]# docker restart alpine_sleep # 可以用名字也可以用ID
alpine_sleep
[root@alice ~]# docker ps -a
CONTAINER ID        IMAGE                  COMMAND             CREATED             STATUS                        PORTS               NAMES
06fbbee401aa        mmdghh/alpine:latest   "/bin/sleep 300"    4 minutes ago       Up 3 seconds                                      alpine_sleep
facbbda54346        mmdghh/alpine:latest   "/bin/sh"           22 minutes ago      Exited (130) 19 minutes ago                       nostalgic_bartik
f5895a16fb3d        hello-world            "/hello"            37 minutes ago      Exited (0) 37 minutes ago                         hopeful_edison
[root@alice ~]#

在宿主机和容器之间传输文件

docker cp container_id:/tmp/xxx.txt  .

删除容器

[root@alice ~]# docker ps -a
CONTAINER ID        IMAGE                  COMMAND             CREATED             STATUS                        PORTS               NAMES
06fbbee401aa        mmdghh/alpine:latest   "/bin/sleep 300"    5 minutes ago       Up About a minute                                 alpine_sleep
facbbda54346        mmdghh/alpine:latest   "/bin/sh"           23 minutes ago      Exited (130) 21 minutes ago                       nostalgic_bartik
f5895a16fb3d        hello-world            "/hello"            39 minutes ago      Exited (0) 39 minutes ago                         hopeful_edison
[root@alice ~]# docker rm facbbda54346
facbbda54346
[root@alice ~]# docker ps -a
CONTAINER ID        IMAGE                  COMMAND             CREATED             STATUS                      PORTS               NAMES
06fbbee401aa        mmdghh/alpine:latest   "/bin/sleep 300"    5 minutes ago       Up About a minute                               alpine_sleep
f5895a16fb3d        hello-world            "/hello"            39 minutes ago      Exited (0) 39 minutes ago                       hopeful_edison
[root@alice ~]# docker rm 06fbbee401aa # 正在运行的容器需要用-f 来强制删除
Error response from daemon: You cannot remove a running container 06fbbee401aaad02da272f920dcb264d539187121f962c6bb3dea5acb90321dd. Stop the container before attempting removal or force remove
[root@alice ~]# docker rm -f 06fbbee401aa
06fbbee401aa
[root@alice ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
f5895a16fb3d        hello-world         "/hello"            39 minutes ago      Exited (0) 39 minutes ago                       hopeful_edison
[root@alice ~]#

删除所有未在运行的容器
docker rmdocker ps -a -q``

[root@alice ~]# docker ps -a
CONTAINER ID        IMAGE                  COMMAND             CREATED              STATUS                      PORTS               NAMES
9072af8ca86b        mmdghh/alpine:latest   "/bin/sh"           55 seconds ago       Exited (0) 54 seconds ago                       nifty_dirac
a21628344d61        mmdghh/alpine:latest   "/bin/sleep 30"     About a minute ago   Exited (0) 31 seconds ago                       blissful_varahamihira
9c494e9667b9        mmdghh/alpine:latest   "/bin/sleep 300"    About a minute ago   Up About a minute                               alpine_sleep
f5895a16fb3d        hello-world            "/hello"            42 minutes ago       Exited (0) 42 minutes ago                       hopeful_edison
[root@alice ~]# docker ps -a -q
9072af8ca86b
a21628344d61
9c494e9667b9
f5895a16fb3d
[root@alice ~]# docker rm `docker ps -a -q`
9072af8ca86b
a21628344d61
f5895a16fb3d
Error response from daemon: You cannot remove a running container 9c494e9667b9b560563d64fbbe245b881985c910ccb721dba5df906688d5280f. Stop the container before attempting removal or force remove
[root@alice ~]# docker ps -a
CONTAINER ID        IMAGE                  COMMAND             CREATED              STATUS              PORTS               NAMES
9c494e9667b9        mmdghh/alpine:latest   "/bin/sleep 300"    About a minute ago   Up About a minute                       alpine_sleep
[root@alice ~]# 
如果需要删除所有容器 包括正在进行的容器,加上-f即可(慎重)

保存镜像

[root@alice ~]# docker run -d --name alpine_sleep docker.io/mmdghh/alpine:latest /bin/sleep 300s
c3d1aa7a1bc2df47f47621f16e420883b334ef8e242349f1f734ad9cb4533968
[root@alice ~]# docker ps -a
CONTAINER ID        IMAGE                  COMMAND             CREATED             STATUS              PORTS               NAMES
c3d1aa7a1bc2        mmdghh/alpine:latest   "/bin/sleep 300s"   5 seconds ago       Up 4 seconds                            alpine_sleep
[root@alice ~]# docker exec -it c3d1aa7a1bc2 /bin/sh
/ # ls
bin    dev    etc    home   lib    media  mnt    opt    proc   root   run    sbin   srv    sys    tmp    usr    var
/ # echo "hello world" >1.txt
/ # ls
1.txt  bin    dev    etc    home   lib    media  mnt    opt    proc   root   run    sbin   srv    sys    tmp    usr    var
/ # [root@alice ~]# docker ps -a
CONTAINER ID        IMAGE                  COMMAND             CREATED             STATUS              PORTS               NAMES
c3d1aa7a1bc2        mmdghh/alpine:latest   "/bin/sleep 300s"   45 seconds ago      Up 44 seconds                           alpine_sleep
[root@alice ~]# docker run -it docker.io/mmdghh/alpine:latest /bin/sh
/ # ls
bin    dev    etc    home   lib    media  mnt    opt    proc   root   run    sbin   srv    sys    tmp    usr    var
/ # 
写入到容器的文件并不会保存在镜像里

-p 保存到执行命令这一时刻的内容 之后更新的不会报错

[root@alice ~]# docker ps -a
CONTAINER ID        IMAGE                  COMMAND             CREATED             STATUS              PORTS               NAMES
c3d1aa7a1bc2        mmdghh/alpine:latest   "/bin/sleep 300s"   5 minutes ago       Up 4 minutes                            alpine_sleep
[root@alice ~]# docker commit -p alpine_sleep docker.io/mmdghh/alpine:v_1.txt 
sha256:11be5214792460b1e258a6d6e7dbca5dccfddce0d438c0ad9b04d78b147006e0
[root@alice ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mmdghh/alpine       v_1.txt             11be52147924        8 seconds ago       5.55MB
alpine              latest              389fef711851        3 weeks ago         5.58MB
hello-world         latest              bf756fb1ae65        12 months ago       13.3kB
mmdghh/alpine       latest              965ea09ff2eb        14 months ago       5.55MB
[root@alice ~]# docker run -it docker.io/mmdghh/alpine:v_1.txt /bin/sh
/ # ls
1.txt  bin    dev    etc    home   lib    media  mnt    opt    proc   root   run    sbin   srv    sys    tmp    usr    var
/ # cat 1.txt 
hello world
/ # [root@alice ~]#

导入导出镜像

导出: docker save image_name/image_id > xxx.tar
导入: docker load -i xxx.tardocker load < xxx.tar
tip: 如果你导出的时候名称用了:记得导入的时候用\转义

[root@alice tmp]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mmdghh/alpine       v_1.txt             11be52147924        10 minutes ago      5.55MB
alpine              latest              389fef711851        3 weeks ago         5.58MB
hello-world         latest              bf756fb1ae65        12 months ago       13.3kB
mmdghh/alpine       latest              965ea09ff2eb        14 months ago       5.55MB
[root@alice tmp]# docker save 11be52147924 > mmdghh_alpine_v_1.txt.tar
[root@alice tmp]# ll mmdghh_alpine_v_1.txt.tar 
-rw-r--r-- 1 root root 5829632 Jan 10 17:18 mmdghh_alpine_v_1.txt.tar
[root@alice tmp]# docker rmi mmdghh/alpine:v_1.txt -f
Untagged: mmdghh/alpine:v_1.txt
Deleted: sha256:11be5214792460b1e258a6d6e7dbca5dccfddce0d438c0ad9b04d78b147006e0
[root@alice tmp]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              latest              389fef711851        3 weeks ago         5.58MB
hello-world         latest              bf756fb1ae65        12 months ago       13.3kB
mmdghh/alpine       latest              965ea09ff2eb        14 months ago       5.55MB
[root@alice tmp]# docker load < mmdghh_alpine_v_1.txt.tar 
Loaded image ID: sha256:11be5214792460b1e258a6d6e7dbca5dccfddce0d438c0ad9b04d78b147006e0
[root@alice tmp]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              11be52147924        12 minutes ago      5.55MB
alpine              latest              389fef711851        3 weeks ago         5.58MB
hello-world         latest              bf756fb1ae65        12 months ago       13.3kB
mmdghh/alpine       latest              965ea09ff2eb        14 months ago       5.55MB
[root@alice tmp]# docker tag 11be52147924 docker.io/mmdghh/alpine:v_1.txt
[root@alice tmp]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mmdghh/alpine       v_1.txt             11be52147924        12 minutes ago      5.55MB
alpine              latest              389fef711851        3 weeks ago         5.58MB
hello-world         latest              bf756fb1ae65        12 months ago       13.3kB
mmdghh/alpine       latest              965ea09ff2eb        14 months ago       5.55MB
[root@alice tmp]# docker run -it docker.io/mmdghh/alpine:v_1.txt /bin/sh
/ # ls
1.txt  bin    dev    etc    home   lib    media  mnt    opt    proc   root   run    sbin   srv    sys    tmp    usr    var
/ # cat 1.txt 
hello world
/ #

查看容器日志

docker logs container_id/``container_name [-f]

[root@alice ~]# docker ps -a
CONTAINER ID        IMAGE                   COMMAND             CREATED             STATUS                        PORTS               NAMES
ee20554ab3e9        mmdghh/alpine:v_1.txt   "/bin/sh"           5 minutes ago       Exited (130) 12 seconds ago                       sharp_hopper
8735b44becfe        mmdghh/alpine:v_1.txt   "/bin/sh"           16 minutes ago      Exited (0) 16 minutes ago                         pensive_mclean
f2123463239b        mmdghh/alpine:v_1.txt   "/bin/sh"           18 minutes ago      Exited (0) 18 minutes ago                         happy_bartik
c3d1aa7a1bc2        mmdghh/alpine:latest    "/bin/sleep 300s"   25 minutes ago      Exited (0) 19 minutes ago                         alpine_sleep
[root@alice ~]# docker logs ee20554ab3e9

查看容器的详细信息

docker inspect container_name/container_di
下面有例子

args

端口映射

-p host_port:container_port

[root@alice ~]# docker pull nginx:1.12.2
1.12.2: Pulling from library/nginx
f2aa67a397c4: Pull complete 
e3eaf3d87fe0: Pull complete 
38cb13c1e4c9: Pull complete 
Digest: sha256:72daaf46f11cc753c4eab981cbf869919bd1fee3d2170a2adeac12400f494728
Status: Downloaded newer image for nginx:1.12.2
[root@alice ~]# docker images |grep nginx
nginx               1.12.2              4037a5562b03        2 years ago         108MB
[root@alice ~]# docker run -d --name nginx -p 83:80 nginx:1.12.2
6ce9e4bb303b754a576d3bf587e0aaec7e3749a3d20f1e40f43c734b28196c67
[root@alice ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
6ce9e4bb303b        nginx:1.12.2        "nginx -g 'daemon of…"   4 seconds ago       Up 3 seconds        0.0.0.0:83->80/tcp   nginx
[root@alice ~]#

image.png

挂载目录

-v host_path:container_path

[root@alice ~]# docker pull nginx:1.12.2
1.12.2: Pulling from library/nginx
f2aa67a397c4: Pull complete 
e3eaf3d87fe0: Pull complete 
38cb13c1e4c9: Pull complete 
Digest: sha256:72daaf46f11cc753c4eab981cbf869919bd1fee3d2170a2adeac12400f494728
Status: Downloaded newer image for nginx:1.12.2
[root@alice ~]# docker images |grep nginx
nginx               1.12.2              4037a5562b03        2 years ago         108MB
[root@alice ~]# docker run -d --name nginx -p 83:80 nginx:1.12.2
6ce9e4bb303b754a576d3bf587e0aaec7e3749a3d20f1e40f43c734b28196c67
[root@alice ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
6ce9e4bb303b        nginx:1.12.2        "nginx -g 'daemon of…"   4 seconds ago       Up 3 seconds        0.0.0.0:83->80/tcp   nginx
[root@alice ~]# 
[root@alice ~]# mkdir html
[root@alice ~]# cd html/
[root@alice html]# wget  www.baidu.com -O index.html
--2021-01-10 17:54:31--  http://www.baidu.com/
Resolving www.baidu.com (www.baidu.com)... 220.181.38.149, 220.181.38.150
Connecting to www.baidu.com (www.baidu.com)|220.181.38.149|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2381 (2.3K) [text/html]
Saving to: ‘index.html’

100%[===================================================================================================================>] 2,381       --.-K/s   in 0s      

2021-01-10 17:54:31 (264 MB/s) - ‘index.html’ saved [2381/2381]

[root@alice html]# cat index.html 
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
[root@alice html]# docker run -d --name nginx_with_baidu -p 84:80 -v /root/html:/usr/share/nginx/html nginx:1.12.2
acf79798ce19fdb6e584723d0ab1cc057508082466f6b9be92acc19eca737699
[root@alice html]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
acf79798ce19        nginx:1.12.2        "nginx -g 'daemon of…"   7 seconds ago       Up 5 seconds        0.0.0.0:84->80/tcp   nginx_with_baidu
6ce9e4bb303b        nginx:1.12.2        "nginx -g 'daemon of…"   6 minutes ago       Up 6 minutes        0.0.0.0:83->80/tcp   nginx 
[root@alice html]# docker exec -it acf79798ce19 /bin/bash
root@acf79798ce19:/# ls /usr/share/nginx/html/
index.html

image.png
查看挂载的详细信息

[root@alice html]# docker inspect nginx_with_baidu |grep -A 9 'Mounts'
        "Mounts": [
            {
                "Type": "bind",
                "Source": "/root/html",
                "Destination": "/usr/share/nginx/html",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }
        ],
[root@alice html]#

传递环境变量

-e ``variate_name=``variate_value

[root@alice ~]# docker run --rm -e E_OPTS=qwert docker.io/mmdghh/alpine:latest printenv
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=62db172fe9da
E_OPTS=qwert
HOME=/root 
[root@alice ~]# docker run --rm -e E_OPTS=qwert -e C_OPTS=12345 docker.io/mmdghh/alpine:latest printenv  #传递多个变量
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=3ac265a1cf85
E_OPTS=qwert
C_OPTS=12345
HOME=/root
[root@alice ~]#

容器内下载软件

红帽系 yum

debian系 apt-get

alpine apt

进入容器并且下载

[root@alice ~]# docker exec -it nginx_with_baidu /bin/bash
root@acf79798ce19:/# curl
bash: curl: command not found
root@acf79798ce19:/# exit
[root@alice ~]# docker exec -it nginx_with_baidu /bin/bash
root@acf79798ce19:/# tee /etc/apt/sources.list << EOF
> deb http://mirrors.163.com/debian/ jessie main non-free contrib
> deb http://mirrors.163.com/debian/ jessie-updates main non-free contrib
> EOF
deb http://mirrors.163.com/debian/ jessie main non-free contrib
deb http://mirrors.163.com/debian/ jessie-updates main non-free contrib
root@acf79798ce19:/# cat /etc/apt/sources.list
deb http://mirrors.163.com/debian/ jessie main non-free contrib
deb http://mirrors.163.com/debian/ jessie-updates main non-free contrib
root@acf79798ce19:/# apt-get update && apt-get install curl -y 
Ign:1 http://mirrors.163.com/debian jessie InRelease
Get:2 http://mirrors.163.com/debian jessie-updates InRelease [16.3 kB]
Get:3 http://mirrors.163.com/debian jessie Release [77.3 kB]
Get:4 http://mirrors.163.com/debian jessie Release.gpg [1652 B]
Get:5 http://mirrors.163.com/debian jessie/main amd64 Packages [9098 kB]
Get:6 http://mirrors.163.com/debian jessie/non-free amd64 Packages [101 kB]
Get:7 http://mirrors.163.com/debian jessie/contrib amd64 Packages [59.2 kB]
Fetched 9353 kB in 1s (4951 kB/s)                          
Reading package lists... Done
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  ca-certificates krb5-locales libcurl3 libffi6 libgmp10 libgnutls-deb0-28 libgssapi-krb5-2 libhogweed2 libidn11 libk5crypto3 libkeyutils1 libkrb5-3
  libkrb5support0 libldap-2.4-2 libnettle4 libp11-kit0 librtmp1 libsasl2-2 libsasl2-modules libsasl2-modules-db libssh2-1 libssl1.0.0 libtasn1-6 openssl
Suggested packages:
  gnutls-bin krb5-doc krb5-user libsasl2-modules-otp libsasl2-modules-ldap libsasl2-modules-sql libsasl2-modules-gssapi-mit
  | libsasl2-modules-gssapi-heimdal
The following NEW packages will be installed:
  ca-certificates curl krb5-locales libcurl3 libffi6 libgmp10 libgnutls-deb0-28 libgssapi-krb5-2 libhogweed2 libidn11 libk5crypto3 libkeyutils1 libkrb5-3
  libkrb5support0 libldap-2.4-2 libnettle4 libp11-kit0 librtmp1 libsasl2-2 libsasl2-modules libsasl2-modules-db libssh2-1 libssl1.0.0 libtasn1-6 openssl
0 upgraded, 25 newly installed, 0 to remove and 1 not upgraded.
Need to get 7883 kB of archives.
After this operation, 15.2 MB of additional disk space will be used.
Get:1 http://mirrors.163.com/debian jessie/main amd64 libssl1.0.0 amd64 1.0.1t-1+deb8u8 [1044 kB]
Get:2 http://mirrors.163.com/debian jessie/main amd64 libgmp10 amd64 2:6.0.0+dfsg-6 [253 kB]
Get:3 http://mirrors.163.com/debian jessie/main amd64 libnettle4 amd64 2.7.1-5+deb8u2 [176 kB]
Get:4 http://mirrors.163.com/debian jessie/main amd64 libhogweed2 amd64 2.7.1-5+deb8u2 [125 kB]
Get:5 http://mirrors.163.com/debian jessie/main amd64 libffi6 amd64 3.1-2+deb8u1 [20.2 kB]
Get:6 http://mirrors.163.com/debian jessie/main amd64 libp11-kit0 amd64 0.20.7-1 [81.2 kB]
Get:7 http://mirrors.163.com/debian jessie/main amd64 libtasn1-6 amd64 4.2-3+deb8u3 [49.2 kB]
Get:8 http://mirrors.163.com/debian jessie/main amd64 libgnutls-deb0-28 amd64 3.3.8-6+deb8u7 [696 kB]
Get:9 http://mirrors.163.com/debian jessie/main amd64 libkeyutils1 amd64 1.5.9-5+b1 [12.0 kB]
Get:10 http://mirrors.163.com/debian jessie/main amd64 libkrb5support0 amd64 1.12.1+dfsg-19+deb8u4 [59.4 kB]
Get:11 http://mirrors.163.com/debian jessie/main amd64 libk5crypto3 amd64 1.12.1+dfsg-19+deb8u4 [116 kB]
Get:12 http://mirrors.163.com/debian jessie/main amd64 libkrb5-3 amd64 1.12.1+dfsg-19+deb8u4 [303 kB]
Get:13 http://mirrors.163.com/debian jessie/main amd64 libgssapi-krb5-2 amd64 1.12.1+dfsg-19+deb8u4 [152 kB]
Get:14 http://mirrors.163.com/debian jessie/main amd64 libidn11 amd64 1.29-1+deb8u2 [136 kB]
Get:15 http://mirrors.163.com/debian jessie/main amd64 libsasl2-modules-db amd64 2.1.26.dfsg1-13+deb8u1 [67.1 kB]
Get:16 http://mirrors.163.com/debian jessie/main amd64 libsasl2-2 amd64 2.1.26.dfsg1-13+deb8u1 [105 kB]
Get:17 http://mirrors.163.com/debian jessie/main amd64 libldap-2.4-2 amd64 2.4.40+dfsg-1+deb8u4 [218 kB]
Get:18 http://mirrors.163.com/debian jessie/main amd64 librtmp1 amd64 2.4+20150115.gita107cef-1+deb8u1 [60.0 kB]
Get:19 http://mirrors.163.com/debian jessie/main amd64 libssh2-1 amd64 1.4.3-4.1+deb8u1 [125 kB]
Get:20 http://mirrors.163.com/debian jessie/main amd64 libcurl3 amd64 7.38.0-4+deb8u11 [260 kB]
Get:21 http://mirrors.163.com/debian jessie/main amd64 krb5-locales all 1.12.1+dfsg-19+deb8u4 [2649 kB]
Get:22 http://mirrors.163.com/debian jessie/main amd64 openssl amd64 1.0.1t-1+deb8u8 [664 kB]
Get:23 http://mirrors.163.com/debian jessie/main amd64 ca-certificates all 20141019+deb8u3 [207 kB]
Get:24 http://mirrors.163.com/debian jessie/main amd64 curl amd64 7.38.0-4+deb8u11 [201 kB]
Get:25 http://mirrors.163.com/debian jessie/main amd64 libsasl2-modules amd64 2.1.26.dfsg1-13+deb8u1 [101 kB]
Fetched 7883 kB in 1s (4228 kB/s)      
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package libssl1.0.0:amd64.
(Reading database ... 7027 files and directories currently installed.)
Preparing to unpack .../00-libssl1.0.0_1.0.1t-1+deb8u8_amd64.deb ...
Unpacking libssl1.0.0:amd64 (1.0.1t-1+deb8u8) ...
Selecting previously unselected package libgmp10:amd64.
Preparing to unpack .../01-libgmp10_2%3a6.0.0+dfsg-6_amd64.deb ...
Unpacking libgmp10:amd64 (2:6.0.0+dfsg-6) ...
Selecting previously unselected package libnettle4:amd64.
Preparing to unpack .../02-libnettle4_2.7.1-5+deb8u2_amd64.deb ...
Unpacking libnettle4:amd64 (2.7.1-5+deb8u2) ...
Selecting previously unselected package libhogweed2:amd64.
Preparing to unpack .../03-libhogweed2_2.7.1-5+deb8u2_amd64.deb ...
Unpacking libhogweed2:amd64 (2.7.1-5+deb8u2) ...
Selecting previously unselected package libffi6:amd64.
Preparing to unpack .../04-libffi6_3.1-2+deb8u1_amd64.deb ...
Unpacking libffi6:amd64 (3.1-2+deb8u1) ...
Selecting previously unselected package libp11-kit0:amd64.
Preparing to unpack .../05-libp11-kit0_0.20.7-1_amd64.deb ...
Unpacking libp11-kit0:amd64 (0.20.7-1) ...
Selecting previously unselected package libtasn1-6:amd64.
Preparing to unpack .../06-libtasn1-6_4.2-3+deb8u3_amd64.deb ...
Unpacking libtasn1-6:amd64 (4.2-3+deb8u3) ...
Selecting previously unselected package libgnutls-deb0-28:amd64.
Preparing to unpack .../07-libgnutls-deb0-28_3.3.8-6+deb8u7_amd64.deb ...
Unpacking libgnutls-deb0-28:amd64 (3.3.8-6+deb8u7) ...
Selecting previously unselected package libkeyutils1:amd64.
Preparing to unpack .../08-libkeyutils1_1.5.9-5+b1_amd64.deb ...
Unpacking libkeyutils1:amd64 (1.5.9-5+b1) ...
Selecting previously unselected package libkrb5support0:amd64.
Preparing to unpack .../09-libkrb5support0_1.12.1+dfsg-19+deb8u4_amd64.deb ...
Unpacking libkrb5support0:amd64 (1.12.1+dfsg-19+deb8u4) ...
Selecting previously unselected package libk5crypto3:amd64.
Preparing to unpack .../10-libk5crypto3_1.12.1+dfsg-19+deb8u4_amd64.deb ...
Unpacking libk5crypto3:amd64 (1.12.1+dfsg-19+deb8u4) ...
Selecting previously unselected package libkrb5-3:amd64.
Preparing to unpack .../11-libkrb5-3_1.12.1+dfsg-19+deb8u4_amd64.deb ...
Unpacking libkrb5-3:amd64 (1.12.1+dfsg-19+deb8u4) ...
Selecting previously unselected package libgssapi-krb5-2:amd64.
Preparing to unpack .../12-libgssapi-krb5-2_1.12.1+dfsg-19+deb8u4_amd64.deb ...
Unpacking libgssapi-krb5-2:amd64 (1.12.1+dfsg-19+deb8u4) ...
Selecting previously unselected package libidn11:amd64.
Preparing to unpack .../13-libidn11_1.29-1+deb8u2_amd64.deb ...
Unpacking libidn11:amd64 (1.29-1+deb8u2) ...
Selecting previously unselected package libsasl2-modules-db:amd64.
Preparing to unpack .../14-libsasl2-modules-db_2.1.26.dfsg1-13+deb8u1_amd64.deb ...
Unpacking libsasl2-modules-db:amd64 (2.1.26.dfsg1-13+deb8u1) ...
Selecting previously unselected package libsasl2-2:amd64.
Preparing to unpack .../15-libsasl2-2_2.1.26.dfsg1-13+deb8u1_amd64.deb ...
Unpacking libsasl2-2:amd64 (2.1.26.dfsg1-13+deb8u1) ...
Selecting previously unselected package libldap-2.4-2:amd64.
Preparing to unpack .../16-libldap-2.4-2_2.4.40+dfsg-1+deb8u4_amd64.deb ...
Unpacking libldap-2.4-2:amd64 (2.4.40+dfsg-1+deb8u4) ...
Selecting previously unselected package librtmp1:amd64.
Preparing to unpack .../17-librtmp1_2.4+20150115.gita107cef-1+deb8u1_amd64.deb ...
Unpacking librtmp1:amd64 (2.4+20150115.gita107cef-1+deb8u1) ...
Selecting previously unselected package libssh2-1:amd64.
Preparing to unpack .../18-libssh2-1_1.4.3-4.1+deb8u1_amd64.deb ...
Unpacking libssh2-1:amd64 (1.4.3-4.1+deb8u1) ...
Selecting previously unselected package libcurl3:amd64.
Preparing to unpack .../19-libcurl3_7.38.0-4+deb8u11_amd64.deb ...
Unpacking libcurl3:amd64 (7.38.0-4+deb8u11) ...
Selecting previously unselected package krb5-locales.
Preparing to unpack .../20-krb5-locales_1.12.1+dfsg-19+deb8u4_all.deb ...
Unpacking krb5-locales (1.12.1+dfsg-19+deb8u4) ...
Selecting previously unselected package openssl.
Preparing to unpack .../21-openssl_1.0.1t-1+deb8u8_amd64.deb ...
Unpacking openssl (1.0.1t-1+deb8u8) ...
Selecting previously unselected package ca-certificates.
Preparing to unpack .../22-ca-certificates_20141019+deb8u3_all.deb ...
Unpacking ca-certificates (20141019+deb8u3) ...
Selecting previously unselected package curl.
Preparing to unpack .../23-curl_7.38.0-4+deb8u11_amd64.deb ...
Unpacking curl (7.38.0-4+deb8u11) ...
Selecting previously unselected package libsasl2-modules:amd64.
Preparing to unpack .../24-libsasl2-modules_2.1.26.dfsg1-13+deb8u1_amd64.deb ...
Unpacking libsasl2-modules:amd64 (2.1.26.dfsg1-13+deb8u1) ...
Setting up libssl1.0.0:amd64 (1.0.1t-1+deb8u8) ...
debconf: unable to initialize frontend: Dialog
debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 76.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (Can't locate Term/ReadLine.pm in @INC (you may need to install the Term::ReadLine module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.24.1 /usr/local/share/perl/5.24.1 /usr/lib/x86_64-linux-gnu/perl5/5.24 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.24 /usr/share/perl/5.24 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base .) at /usr/share/perl5/Debconf/FrontEnd/Readline.pm line 7.)
debconf: falling back to frontend: Teletype
Setting up libsasl2-modules-db:amd64 (2.1.26.dfsg1-13+deb8u1) ...
Setting up libsasl2-2:amd64 (2.1.26.dfsg1-13+deb8u1) ...
Setting up libtasn1-6:amd64 (4.2-3+deb8u3) ...
Setting up libgmp10:amd64 (2:6.0.0+dfsg-6) ...
Setting up libssh2-1:amd64 (1.4.3-4.1+deb8u1) ...
Setting up krb5-locales (1.12.1+dfsg-19+deb8u4) ...
Setting up libnettle4:amd64 (2.7.1-5+deb8u2) ...
Setting up openssl (1.0.1t-1+deb8u8) ...
Setting up libffi6:amd64 (3.1-2+deb8u1) ...
Setting up libkeyutils1:amd64 (1.5.9-5+b1) ...
Setting up libsasl2-modules:amd64 (2.1.26.dfsg1-13+deb8u1) ...
Setting up ca-certificates (20141019+deb8u3) ...
debconf: unable to initialize frontend: Dialog
debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 76.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (Can't locate Term/ReadLine.pm in @INC (you may need to install the Term::ReadLine module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.24.1 /usr/local/share/perl/5.24.1 /usr/lib/x86_64-linux-gnu/perl5/5.24 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.24 /usr/share/perl/5.24 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base .) at /usr/share/perl5/Debconf/FrontEnd/Readline.pm line 7.)
debconf: falling back to frontend: Teletype
Updating certificates in /etc/ssl/certs... 174 added, 0 removed; done.
Setting up libidn11:amd64 (1.29-1+deb8u2) ...
Setting up libhogweed2:amd64 (2.7.1-5+deb8u2) ...
Setting up libkrb5support0:amd64 (1.12.1+dfsg-19+deb8u4) ...
Setting up libp11-kit0:amd64 (0.20.7-1) ...
Setting up libgnutls-deb0-28:amd64 (3.3.8-6+deb8u7) ...
Setting up libk5crypto3:amd64 (1.12.1+dfsg-19+deb8u4) ...
Setting up librtmp1:amd64 (2.4+20150115.gita107cef-1+deb8u1) ...
Setting up libldap-2.4-2:amd64 (2.4.40+dfsg-1+deb8u4) ...
Setting up libkrb5-3:amd64 (1.12.1+dfsg-19+deb8u4) ...
Setting up libgssapi-krb5-2:amd64 (1.12.1+dfsg-19+deb8u4) ...
Setting up libcurl3:amd64 (7.38.0-4+deb8u11) ...
Setting up curl (7.38.0-4+deb8u11) ...
Processing triggers for ca-certificates (20141019+deb8u3) ...
Updating certificates in /etc/ssl/certs... 0 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d....done.
root@acf79798ce19:/# curl -k https://www.baidu.com
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus=autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn" autofocus></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=https://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');
                </script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

安装好后commit并且推送到仓库

[root@alice ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
acf79798ce19        nginx:1.12.2        "nginx -g 'daemon of…"   About an hour ago   Up About an hour    0.0.0.0:84->80/tcp   nginx_with_baidu
6ce9e4bb303b        nginx:1.12.2        "nginx -g 'daemon of…"   About an hour ago   Up About an hour    0.0.0.0:83->80/tcp   nginx
[root@alice ~]# docker commit -p acf79798ce19 mmdghh/nginx:curl
sha256:84b7a98f5ee209f0139febe7cac04a7edaaca7254ddf1c043e8ac779504204ba
[root@alice ~]# docker push docker.io/mmdghh/nginx:curl 
The push refers to repository [docker.io/mmdghh/nginx]
bbadc5b62281: Pushed 
4258832b2570: Mounted from library/nginx 
683a28d1d7fd: Pushed 
d626a8ad97a1: Mounted from library/nginx 
curl: digest: sha256:f86f97bacf0ff37e3cc09f98dfb8153c486ee1e8bb9caad5046ed6aa58c43c50 size: 1160
[root@alice ~]#

image.png