[关闭]
@22221cjp 2016-08-17T15:26:41.000000Z 字数 10609 阅读 3270

docker基本命令

docker


image_1amb6qm4b8tv1crc1s5an6l1aha9.png-95.3kB

交互式容器

  1. //查看运行中的容器
  2. chenjp@chenjp ~> docker ps
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. //以交互的方式启动一个容器
  5. chenjp@chenjp ~> docker run -i -t ubuntu /bin/bash
  6. root@e7c0d3fb4db2:/# ls
  7. bin boot core dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
  8. root@e7c0d3fb4db2:/# exit
  9. exit
  10. //指定一个名字给容器,并以交互方式启动
  11. chenjp@chenjp ~> docker run --name=container01 -i -t ubuntu /bin/bash
  12. root@43f4830b9979:/# exit
  13. exit
  14. chenjp@chenjp ~>
  15. //查看所有容器,包括没有运行的
  16. chenjp@chenjp ~> docker ps -a
  17. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  18. 43f4830b9979 ubuntu "/bin/bash" 6 seconds ago Exited (0) 3 seconds ago container01
  19. e7c0d3fb4db2 ubuntu "/bin/bash" 4 minutes ago Exited (0) 44 seconds ago kickass_galileo
  20. a472c6897f70 ubuntu "/bin/bash" 5 minutes ago Exited (0) 5 minutes ago nauseous_northcutt
  21. a8c5642b8917 ubuntu "echo 'hello world'" 17 hours ago Exited (0) 17 hours ago elated_rosalind
  22. //查看一个容器的详细信息
  23. chenjp@chenjp ~> docker inspect container01
  24. [
  25. {
  26. "Id": "43f4830b9979dc67eadb4c59a3b7623eff64df4f10d7320893ee5d08791eb727",
  27. "Created": "2016-06-29T04:10:53.973189611Z",
  28. "Path": "/bin/bash",
  29. "Args": [],
  30. ......
  31. ]
  32. chenjp@chenjp ~> docker ps -a
  33. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  34. 43f4830b9979 ubuntu "/bin/bash" 49 seconds ago Exited (0) 46 seconds ago container01
  35. e7c0d3fb4db2 ubuntu "/bin/bash" 5 minutes ago Exited (0) About a minute ago kickass_galileo
  36. a472c6897f70 ubuntu "/bin/bash" 6 minutes ago Exited (0) 5 minutes ago nauseous_northcutt
  37. a8c5642b8917 ubuntu "echo 'hello world'" 17 hours ago Exited (0) 17 hours ago elated_rosalind
  38. chenjp@chenjp ~>
  39. //启动一个已有的容器
  40. chenjp@chenjp ~> docker start -i container01
  41. root@43f4830b9979:/# exit
  42. exit
  43. //删除一个容器
  44. chenjp@chenjp ~> docker rm a472c6897f70
  45. a472c6897f70
  46. chenjp@chenjp ~> docker ps -a
  47. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  48. 43f4830b9979 ubuntu "/bin/bash" 2 minutes ago Exited (0) 34 seconds ago container01
  49. e7c0d3fb4db2 ubuntu "/bin/bash" 6 minutes ago Exited (0) 2 minutes ago kickass_galileo
  50. a8c5642b8917 ubuntu "echo 'hello world'" 17 hours ago Exited (0) 17 hours ago elated_rosalind
  51. chenjp@chenjp ~>

对容器的理解

docker容器可以理解为在沙盒中运行的进程。这个沙盒包含了该进程运行所必须的资源,包括文件系统、系统类库、shell 环境等等。但这个沙盒默认是不会运行任何程序的。你需要在沙盒中运行一个进程来启动某一个容器。这个进程是该容器的唯一进程,所以当该进程结束的时候,容器也会完全的停止。
所以每次运行一个命令,就会启动一个新的容器,命令执行完,容器也停止运行。

命令

在容器中运行"echo"命令,输出"hello word"
$docker run image_name echo "hello word"

交互式进入容器中
$docker run -i -t image_name /bin/bash

在容器中安装新的程序
$docker run image_name apt-get install -y app_name

Note: 在执行apt-get 命令的时候,要带上-y参数。如果不指定-y参数的话,apt-get命令会进入交互模式,需要用户输入命令来进行确认,但在docker环境中是无法响应这种交互的。apt-get 命令执行完毕之后,容器就会停止,但对容器的改动不会丢失。

列出当前所有正在运行的container
$docker ps
列出所有的container
$docker ps -a
列出最近一次启动的container
$docker ps -l

守护式容器

  1. //先启动一个交互式容器
  2. chenjp@chenjp ~> docker run -i -t ubuntu /bin/bash
  3. root@2382e8592761:/#
  4. root@2382e8592761:/#
  5. //ctrl+p,ctrl+q退出容器,这样容器在后台还是运行的
  6. root@2382e8592761:/# chenjp@chenjp ~>
  7. //可以看到正在运行的容器
  8. chenjp@chenjp ~> docker ps
  9. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  10. 2382e8592761 ubuntu "/bin/bash" 10 seconds ago Up 9 seconds thirsty_shockley
  11. //attach命令进入正在运行的容器
  12. chenjp@chenjp ~> docker attach 2382e859276
  13. root@2382e8592761:/#
  14. root@2382e8592761:/# exit
  15. exit
  16. chenjp@chenjp ~> docker ps
  17. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  18. //-d 选项启动一个后台运行的容器
  19. chenjp@chenjp ~> docker run --name dc1 -d ubuntu /bin/bash -c "while true;do echo hello world;sleep 1;done"
  20. be5d51c595ad3d48d2d3f4cc59d0b66b7d37ddf880c932c43a8dc231652f7fb8
  21. chenjp@chenjp ~> docker ps
  22. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  23. be5d51c595ad ubuntu "/bin/bash -c 'while " 4 seconds ago Up 4 seconds dc1
  24. //查看后台运行进程的输出
  25. chenjp@chenjp ~> docker logs dc1
  26. hello world
  27. hello world
  28. hello world
  29. hello world
  30. hello world
  31. //-t 参数加上时间戳
  32. chenjp@chenjp ~> docker logs -t dc1
  33. 2016-06-29T04:39:27.577351874Z hello world
  34. 2016-06-29T04:39:28.586155642Z hello world
  35. 2016-06-29T04:39:29.592244369Z hello world
  36. 2016-06-29T04:39:30.596451294Z hello world
  37. 2016-06-29T04:39:31.599440130Z hello world
  38. 2016-06-29T04:39:32.600847783Z hello world
  39. 2016-06-29T04:39:33.602869388Z hello world
  40. 2016-06-29T04:39:34.606732679Z hello world
  41. 2016-06-29T04:39:35.611214345Z hello world
  42. 2016-06-29T04:39:36.615387406Z hello world
  43. 2016-06-29T04:39:37.619017518Z hello world
  44. 2016-06-29T04:39:38.622770002Z hello world
  45. 2016-06-29T04:39:39.629660081Z hello world
  46. 2016-06-29T04:39:40.633735385Z hello world
  47. 2016-06-29T04:39:41.639099510Z hello world
  48. 2016-06-29T04:39:42.643882643Z hello world
  49. 2016-06-29T04:39:43.648979443Z hello world
  50. 2016-06-29T04:39:44.652386880Z hello world
  51. 2016-06-29T04:39:45.658231737Z hello world
  52. 2016-06-29T04:39:46.661891560Z hello world
  53. //-f参数结尾处追踪日志变化
  54. chenjp@chenjp ~> docker logs -tf dc1
  55. 2016-06-29T04:39:27.577351874Z hello world
  56. 2016-06-29T04:39:28.586155642Z hello world
  57. 2016-06-29T04:39:29.592244369Z hello world
  58. 2016-06-29T04:39:30.596451294Z hello world
  59. 2016-06-29T04:39:31.599440130Z hello world
  60. 2016-06-29T04:39:32.600847783Z hello world
  61. 2016-06-29T04:39:33.602869388Z hello world
  62. 2016-06-29T04:39:34.606732679Z hello world
  63. 2016-06-29T04:39:35.611214345Z hello world
  64. 2016-06-29T04:39:36.615387406Z hello world
  65. 2016-06-29T04:39:37.619017518Z hello world
  66. 2016-06-29T04:39:38.622770002Z hello world
  67. 2016-06-29T04:39:39.629660081Z hello world
  68. 2016-06-29T04:39:40.633735385Z hello world
  69. 2016-06-29T04:39:41.639099510Z hello world
  70. 2016-06-29T04:39:42.643882643Z hello world
  71. 2016-06-29T04:39:43.648979443Z hello world
  72. 2016-06-29T04:39:44.652386880Z hello world
  73. 2016-06-29T04:39:45.658231737Z hello world
  74. 2016-06-29T04:39:46.661891560Z hello world
  75. 2016-06-29T04:39:47.666111271Z hello world
  76. 2016-06-29T04:39:48.672527925Z hello world
  77. 2016-06-29T04:39:49.691585586Z hello world
  78. 2016-06-29T04:39:50.701563422Z hello world
  79. 2016-06-29T04:39:51.708281812Z hello world
  80. ^C
  81. //-tail 10 查看最后10条日志,并追踪日志结尾的变化
  82. chenjp@chenjp ~> docker logs -tf --tail 10 dc1
  83. 2016-06-29T04:41:45.253501976Z hello world
  84. 2016-06-29T04:41:46.257801323Z hello world
  85. 2016-06-29T04:41:47.265063096Z hello world
  86. 2016-06-29T04:41:48.270734023Z hello world
  87. 2016-06-29T04:41:49.277254086Z hello world
  88. 2016-06-29T04:41:50.282140475Z hello world
  89. 2016-06-29T04:41:51.292175575Z hello world
  90. 2016-06-29T04:41:52.298688571Z hello world
  91. 2016-06-29T04:41:53.302544576Z hello world
  92. 2016-06-29T04:41:54.307738459Z hello world
  93. 2016-06-29T04:41:55.310530133Z hello world
  94. 2016-06-29T04:41:56.312541632Z hello world
  95. 2016-06-29T04:41:57.316037117Z hello world
  96. 2016-06-29T04:41:58.318919076Z hello world
  97. 2016-06-29T04:41:59.321621337Z hello world
  98. 2016-06-29T04:42:00.325557087Z hello world
  99. ^C
  100. //只看最新的日志,且追踪
  101. chenjp@chenjp ~> docker logs -tf --tail 0 dc1
  102. 2016-06-29T04:42:05.349032121Z hello world
  103. 2016-06-29T04:42:06.353431628Z hello world
  104. 2016-06-29T04:42:07.360419029Z hello world
  105. 2016-06-29T04:42:08.361967427Z hello world
  106. ^C
  107. //查看容器中的进程
  108. chenjp@chenjp ~> docker top dc1
  109. PID USER TIME COMMAND
  110. 1936 root 0:00 /bin/bash -c while true;do echo hello world;sleep 1;done
  111. 2133 root 0:00 sleep 1
  112. //在容器中启动新的进程
  113. chenjp@chenjp ~> docker exec -i -t dc1 /bin/bash
  114. root@be5d51c595ad:/# chenjp@chenjp ~>
  115. chenjp@chenjp ~> docker top dc1
  116. PID USER TIME COMMAND
  117. 1936 root 0:00 /bin/bash -c while true;do echo hello world;sleep 1;done
  118. 2292 root 0:00 /bin/bash
  119. 2308 root 0:00 sleep 1
  120. chenjp@chenjp ~> docker ps
  121. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  122. be5d51c595ad ubuntu "/bin/bash -c 'while " 6 minutes ago Up 6 minutes dc1
  123. //再启动一个后台运行的容器
  124. chenjp@chenjp ~> docker run -i -t ubuntu /bin/bash
  125. root@121e68b5999a:/#
  126. root@121e68b5999a:/# chenjp@chenjp ~>
  127. chenjp@chenjp ~> docker ps
  128. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  129. 121e68b5999a ubuntu "/bin/bash" 6 seconds ago Up 6 seconds goofy_pike
  130. be5d51c595ad ubuntu "/bin/bash -c 'while " 6 minutes ago Up 6 minutes dc1
  131. //stop温和的方式停止一个后台容器
  132. chenjp@chenjp ~> docker stop 121e68b5999a
  133. 121e68b5999a
  134. chenjp@chenjp ~> docker ps
  135. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  136. be5d51c595ad ubuntu "/bin/bash -c 'while " 7 minutes ago Up 7 minutes dc1
  137. //kill 直接杀死后台运行的容器
  138. chenjp@chenjp ~> docker kill be5d51c595ad
  139. be5d51c595ad
  140. chenjp@chenjp ~>
  141. chenjp@chenjp ~> docker ps
  142. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  143. chenjp@chenjp ~>

image_1amd7391l1h2v19splhh1osnso69.png-31.5kB
image_1amb71n3ub2hesu1ngkercjc113.png-470.8kB
image_1amb70tdr10s71jcle1l1nakpvsm.png-78.1kB
image_1amb7533amdr1otd10bik0d5pj1g.png-141.6kB
image_1amb77bgl4jq19fge535dh1ht21t.png-56.7kB
image_1amb78uut1qi91bt1iha18fr1ef52a.png-108kB

image_1amb7b6161p7rujul92i5s1l22n.png-80.2kB

image_1amb7f7nt9jb3m10a51c1k4f834.png-132.4kB

在容器中部署静态网站

  1. //启动一个容器,将容器的80端口映射出来,这种方式映射到宿主机的随机端口
  2. chenjp@chenjp ~> docker run -p 80 --name=web -i -t ubuntu /bin/bash
  3. //在容器中安装nginx,发现装不上
  4. root@d424f8733558:/# apt-get install -y nginx
  5. Reading package lists... Done
  6. Building dependency tree
  7. Reading state information... Done
  8. E: Unable to locate package nginx
  9. //udpate下
  10. root@d424f8733558:/# apt-get update
  11. Get:1 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB]
  12. ......
  13. Fetched 23.5 MB in 2min 15s (174 kB/s)
  14. Reading package lists... Done
  15. //这下可以正常安装nginx了
  16. root@d424f8733558:/# apt-get install -y nginx
  17. Reading package lists... Done
  18. Building dependency tree
  19. Reading state information... Done
  20. The following additional packages will be installed:
  21. ......
  22. Processing triggers for systemd (229-4ubuntu6) ...
  23. root@d424f8733558:/#
  24. chenjp@chenjp ~> docker ps -a -l
  25. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  26. d424f8733558 ubuntu "/bin/bash" 7 hours ago Exited (0) 8 minutes ago 0.0.0.0:32768->80/tcp web
  27. chenjp@chenjp ~> docker attach d424f8733558
  28. You cannot attach to a stopped container, start it first
  29. //这中间隔了好久。容器已经停止了,重新启动下
  30. chenjp@chenjp ~> docker start -i d424f8733558
  31. root@d424f8733558:/# whereis nginx
  32. nginx: /usr/sbin/nginx /etc/nginx /usr/share/nginx
  33. root@d424f8733558:/# cd /etc/nginx/conf.d/
  34. root@d424f8733558:/etc/nginx/conf.d# ls
  35. root@d424f8733558:/etc/nginx/conf.d# cd ..
  36. //没有vim命令,安装下
  37. root@d424f8733558:/etc/nginx# vim nginx.conf
  38. bash: vim: command not found
  39. root@d424f8733558:/etc/nginx# apt-get install -y vim
  40. Reading package lists... Done
  41. Building dependency tree
  42. Reading state information... Done
  43. The following additional packages will be installed:
  44. ......
  45. Processing triggers for libc-bin (2.23-0ubuntu3) ...
  46. root@d424f8733558:/etc/nginx# whereis vim
  47. vim: /usr/bin/vim /usr/bin/vim.basic /etc/vim /usr/share/vim /usr/share/man/man1/vim.1.gz
  48. root@d424f8733558:/etc/nginx# ls
  49. conf.d fastcgi.conf fastcgi_params koi-utf koi-win mime.types nginx.conf proxy_params scgi_params sites-available sites-enabled snippets uwsgi_params win-utf
  50. root@d424f8733558:/etc/nginx# vim nginx.conf
  51. root@d424f8733558:/etc/nginx# mkdir -p /var/www/html root@d424f8733558:/etc/nginx# cd /var/www/html/
  52. // 这个路径/var/www/html就是nginx的默认服务路径
  53. root@d424f8733558:/var/www/html# ls
  54. index.nginx-debian.html
  55. root@d424f8733558:/var/www/html# vim index.html
  56. //启动nginx
  57. root@d424f8733558:/var/www/html# service nginx status
  58. * nginx is not running
  59. root@d424f8733558:/var/www/html# service nginx start
  60. * Starting nginx nginx [ OK ]
  61. root@d424f8733558:/var/www/html# service nginx status
  62. * nginx is running
  63. root@d424f8733558:/var/www/html# ⏎ chenjp@chenjp ~>
  64. chenjp@chenjp ~>
  65. //ctrl+p和ctrl+q退出,后台运行容器
  66. chenjp@chenjp ~> docker ps
  67. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  68. d424f8733558 ubuntu "/bin/bash" 7 hours ago Up 8 minutes 0.0.0.0:32768->80/tcp web
  69. //查看容器的映射端口
  70. chenjp@chenjp ~> docker port web
  71. 80/tcp -> 0.0.0.0:32768
  72. //查看容器中的进程
  73. chenjp@chenjp ~> docker top web
  74. PID USER TIME COMMAND
  75. 1574 root 0:00 /bin/bash
  76. 1864 root 0:00 nginx: master process /usr/sbin/nginx
  77. 1865 xfs 0:00 nginx: worker process
  78. 1866 xfs 0:00 nginx: worker process chenjp@chenjp ~> curl http://127.0.0.1:32768
  79. <!DOCTYPE html>
  80. <html>
  81. <head>
  82. <title>Welcome to nginx!</title>
  83. <style>
  84. body {
  85. width: 35em;
  86. margin: 0 auto;
  87. font-family: Tahoma, Verdana, Arial, sans-serif;
  88. }
  89. </style>
  90. </head>
  91. <body>
  92. <h1>Welcome to nginx!</h1>
  93. <h1>Nginx in docker</h1>
  94. <p>If you see this page, the nginx web server is successfully installed and
  95. working. Further configuration is required.</p>
  96. <p>For online documentation and support please refer to
  97. <a href="http://nginx.org/">nginx.org</a>.<br/>
  98. Commercial support is available at
  99. <a href="http://nginx.com/">nginx.com</a>.</p>
  100. <p><em>Thank you for using nginx.</em></p>
  101. </body>
  102. </html>
  103. chenjp@chenjp ~>

浏览器访问截图如下:
image_1ame3tu05deof9b6a9al05tl13.png-89.5kB

本节课中的命令截图

image_1amd8dcslfgsqat1h8b4141vddm.png-27.5kB
image_1amb7khgv7nhsrvooks6cmna3u.png-369kB
image_1amb7lucdvqmobr19v22fdg064b.png-239.2kB

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注