1. 端口映射

  1. docker run -p 容器外端口:容器内端口

示例:

  1. [root@localhost ~]# docker pull nginx:1.12.2
  2. 1.12.2: Pulling from library/nginx
  3. f2aa67a397c4: Pull complete
  4. e3eaf3d87fe0: Pull complete
  5. 38cb13c1e4c9: Pull complete
  6. Digest: sha256:72daaf46f11cc753c4eab981cbf869919bd1fee3d2170a2adeac12400f494728
  7. Status: Downloaded newer image for nginx:1.12.2
  8. docker.io/library/nginx:1.12.2
  9. [root@localhost ~]#
  10. [root@localhost ~]# docker images
  11. REPOSITORY TAG IMAGE ID CREATED SIZE
  12. alpine latest 6dbb9cc54074 3 days ago 5.61MB
  13. nginx 1.12.2 4037a5562b03 2 years ago 108MB
  14. [root@localhost ~]# docker run --rm --name mynginx -d -p81:80 nginx:1.12.2
  15. 2929a027e8db1a8154237076a8b6fe5f72ab0caab4356f209182b8f906fe8694
  16. [root@localhost ~]# docker ps
  17. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  18. 2929a027e8db nginx:1.12.2 "nginx -g 'daemon of…" 10 seconds ago Up 7 seconds 0.0.0.0:81->80/tcp, :::81->80/tcp mynginx
  19. [root@localhost ~]#

在宿主机直接访问宿主机IP:nginx 81端口 :

08_docker_nginx访问.png

2. 挂载目录

  1. docker run -v 容器外目录:容器内目录

3. 传递环境变量

  1. docker run -e 环境变量key=环境变量value
  1. [root@localhost ~]# docker run --rm -e E_OPTS=qwert docker.io/mmdghh/alpine:latest printenv
  2. PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  3. HOSTNAME=62db172fe9da
  4. E_OPTS=qwert
  5. HOME=/root
  6. [root@localhost ~]# docker run --rm -e E_OPTS=qwert -e C_OPTS=12345 docker.io/mmdghh/alpine:latest printenv #传递多个变量
  7. PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  8. HOSTNAME=3ac265a1cf85
  9. E_OPTS=qwert
  10. C_OPTS=12345
  11. HOME=/root
  12. [root@localhost ~]#

4. 容器内安装软件(工具)

  1. yum/apt-get/apt
  2. - 红帽系 yum
  3. - debian apt-get
  4. - alpine apt
  1. [root@localhost ~]# docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 2929a027e8db nginx:1.12.2 "nginx -g 'daemon of…" About an hour ago Up About an hour 0.0.0.0:81->80/tcp, :::81->80/tcp mynginx
  4. [root@localhost ~]# docker exec -it mynginx bash
  5. root@2929a027e8db:/#
  6. root@2929a027e8db:/#
  7. root@2929a027e8db:/#
  8. root@2929a027e8db:/# curl
  9. bash: curl: command not found # 容器内部没有curl命令
  10. root@2929a027e8db:/#
  11. root@2929a027e8db:/# tee /etc/apt/sources.list << EOF # 更换为163的源
  12. > deb http://mirrors.163.com/debian/ jessie main non-free contrib
  13. > deb http://mirrors.163.com/debian/ jessie-updates main non-free contrib
  14. > EOF
  15. deb http://mirrors.163.com/debian/ jessie main non-free contrib
  16. deb http://mirrors.163.com/debian/ jessie-updates main non-free contrib
  17. root@2929a027e8db:/#
  18. root@2929a027e8db:/#
  19. root@2929a027e8db:/# apt-get update && apt-get install curl -y # 更新apt-get并安装curl命令
  20. ... # 省略安装过程
  21. root@2929a027e8db:/#
  22. root@2929a027e8db:/# curl
  23. curl: try 'curl --help' or 'curl --manual' for more information
  24. root@2929a027e8db:/#
  25. root@2929a027e8db:/#
  26. root@2929a027e8db:/# curl -k https://www.baidu.com
  27. <!DOCTYPE html>
  28. <!--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>');
  29. </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>
  30. root@2929a027e8db:/#

固化到镜像(容器保存为镜像)并推送至docker公网(方便后续使用):

  1. [root@localhost ~]# docker ps -a
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 2929a027e8db nginx:1.12.2 "nginx -g 'daemon of…" About an hour ago Up About an hour 0.0.0.0:81->80/tcp, :::81->80/tcp mynginx
  4. [root@localhost ~]# docker commit -p 2929a027e8db zhanghb123/nginx:curl # 容器提交即保存为镜像
  5. sha256:529d5d7f2096679fca40d995173b1abb235ce09c886ca62b30be9b5890a54909
  6. [root@localhost ~]# docker images
  7. REPOSITORY TAG IMAGE ID CREATED SIZE
  8. zhanghb123/nginx curl 529d5d7f2096 14 seconds ago 136MB
  9. alpine latest 6dbb9cc54074 3 days ago 5.61MB
  10. nginx 1.12.2 4037a5562b03 2 years ago 108MB
  11. [root@localhost ~]# docker push zhanghb123/nginx:curl # 推送至远程仓库,即docker官网
  12. The push refers to repository [docker.io/zhanghb123/nginx]
  13. 4b8c26e11d1d: Pushed
  14. 4258832b2570: Mounted from library/nginx
  15. 683a28d1d7fd: Mounted from library/nginx
  16. d626a8ad97a1: Mounted from library/nginx
  17. curl: digest: sha256:15a5e0938ec117d02c5219545b8ff22ac3789d17acd71065cd30d5a168013401 size: 1160
  18. [root@localhost ~]#
  19. # 再次使用只需要pull即可
  20. docker pull docker.io/zhanghb123/nginx:curl

5. 容器的生命周期

  1. - 检查本地是否存在镜像,如果不存在即从远端仓库检索
  2. - 利用镜像启动容器
  3. - 分配一个文件系统,并在只读的镜像层外挂载一层读写层
  4. - 从宿主机配置的网桥接口中 桥接一个虚拟接口道容器
  5. - 执行用户指定命令
  6. - 执行完毕后容器终止