1. 端口映射
docker run -p 容器外端口:容器内端口
示例:
[root@localhost ~]# docker pull nginx:1.12.21.12.2: Pulling from library/nginxf2aa67a397c4: Pull completee3eaf3d87fe0: Pull complete38cb13c1e4c9: Pull completeDigest: sha256:72daaf46f11cc753c4eab981cbf869919bd1fee3d2170a2adeac12400f494728Status: Downloaded newer image for nginx:1.12.2docker.io/library/nginx:1.12.2[root@localhost ~]#[root@localhost ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEalpine latest 6dbb9cc54074 3 days ago 5.61MBnginx 1.12.2 4037a5562b03 2 years ago 108MB[root@localhost ~]# docker run --rm --name mynginx -d -p81:80 nginx:1.12.22929a027e8db1a8154237076a8b6fe5f72ab0caab4356f209182b8f906fe8694[root@localhost ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES2929a027e8db 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[root@localhost ~]#
在宿主机直接访问宿主机IP:nginx 81端口 :

2. 挂载目录
docker run -v 容器外目录:容器内目录
3. 传递环境变量
docker run -e 环境变量key=环境变量value
[root@localhost ~]# docker run --rm -e E_OPTS=qwert docker.io/mmdghh/alpine:latest printenvPATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/binHOSTNAME=62db172fe9daE_OPTS=qwertHOME=/root[root@localhost ~]# 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:/binHOSTNAME=3ac265a1cf85E_OPTS=qwertC_OPTS=12345HOME=/root[root@localhost ~]#
4. 容器内安装软件(工具)
yum/apt-get/apt 等- 红帽系 yum- debian系 apt-get- alpine apt
[root@localhost ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES2929a027e8db 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[root@localhost ~]# docker exec -it mynginx bashroot@2929a027e8db:/#root@2929a027e8db:/#root@2929a027e8db:/#root@2929a027e8db:/# curlbash: curl: command not found # 容器内部没有curl命令root@2929a027e8db:/#root@2929a027e8db:/# tee /etc/apt/sources.list << EOF # 更换为163的源> deb http://mirrors.163.com/debian/ jessie main non-free contrib> deb http://mirrors.163.com/debian/ jessie-updates main non-free contrib> EOFdeb http://mirrors.163.com/debian/ jessie main non-free contribdeb http://mirrors.163.com/debian/ jessie-updates main non-free contribroot@2929a027e8db:/#root@2929a027e8db:/#root@2929a027e8db:/# apt-get update && apt-get install curl -y # 更新apt-get并安装curl命令... # 省略安装过程root@2929a027e8db:/#root@2929a027e8db:/# curlcurl: try 'curl --help' or 'curl --manual' for more informationroot@2929a027e8db:/#root@2929a027e8db:/#root@2929a027e8db:/# 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&tpl=mn&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>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必读</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a> 京ICP证030173号 <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>root@2929a027e8db:/#
固化到镜像(容器保存为镜像)并推送至docker公网(方便后续使用):
[root@localhost ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES2929a027e8db 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[root@localhost ~]# docker commit -p 2929a027e8db zhanghb123/nginx:curl # 容器提交即保存为镜像sha256:529d5d7f2096679fca40d995173b1abb235ce09c886ca62b30be9b5890a54909[root@localhost ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEzhanghb123/nginx curl 529d5d7f2096 14 seconds ago 136MBalpine latest 6dbb9cc54074 3 days ago 5.61MBnginx 1.12.2 4037a5562b03 2 years ago 108MB[root@localhost ~]# docker push zhanghb123/nginx:curl # 推送至远程仓库,即docker官网The push refers to repository [docker.io/zhanghb123/nginx]4b8c26e11d1d: Pushed4258832b2570: Mounted from library/nginx683a28d1d7fd: Mounted from library/nginxd626a8ad97a1: Mounted from library/nginxcurl: digest: sha256:15a5e0938ec117d02c5219545b8ff22ac3789d17acd71065cd30d5a168013401 size: 1160[root@localhost ~]## 再次使用只需要pull即可docker pull docker.io/zhanghb123/nginx:curl
5. 容器的生命周期
- 检查本地是否存在镜像,如果不存在即从远端仓库检索- 利用镜像启动容器- 分配一个文件系统,并在只读的镜像层外挂载一层读写层- 从宿主机配置的网桥接口中 桥接一个虚拟接口道容器- 执行用户指定命令- 执行完毕后容器终止
