牛刀小试之docker容器系列(二)

docker容器增加删除

牛刀小试之docker容器系列(二)

docker容器增加删除

什么是容器?

	容器是docker镜像运行的一个实例,所不同的是,他带有额外的可写文件

	docker的容器十分轻量级,用户可以随意删除或者创建

docker创建容器的方式:

	默认情况下docker create创建的容器是停止状态,一般我们可以使用docker start命令来启动容器,使用docker stop命令来停止容器。

docker容器创建:

一、新建容器:

[root@docker ~]# docker create -it ubuntu:latest
713e34e2b4638d824cf663ee0ec7dfc1a3664d6d0ba2fafd72202d3bfdde6a50
[root@docker ~]#
[root@docker ~]# docker ps -a
CONTAINER ID        IMAGE                    COMMAND                CREATED             STATUS                         PORTS                     NAMES
713e34e2b463        ubuntu:14.04             "/bin/bash"            18 seconds ago                                                               cocky_bell
[root@docker ~]#

二、启动容器有两种方式

	1.基于镜像新建一个容器并且启动
	2.把在终止的状态容器重新启动
	docker run 相当于 docker create + docker start

三、如下:

	[root@docker ~]# docker run ubuntu /bin/echo "hello world"
	hello world
	You have new mail in /var/spool/mail/root
	[root@docker ~]#

四、登陆到交互式模式下面,

	-t   让docker分配一个伪终端,并绑定到容器的标准输出上
	-i   让容器的标准输出打开
	可以是用crtl+d 或者exit退出,需要注意的是,退出意味着终止。
	如下:
	[root@docker ~]# docker run -ti ubuntu:14.04 /bin/bash
	root@10f115a0aa25:/# ls
	bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
	root@10f115a0aa25:/# pwd
	/
	root@10f115a0aa25:/# ps
	   PID TTY          TIME CMD
	     1 ?        00:00:00 bash
	    20 ?        00:00:00 ps
	root@10f115a0aa25:/#

docker容器守护状态运行:

	有时候为了业务方便可以使用”-d“参数来后台启动
	如下:
	1.首先在后台运行。
	[root@docker ~]# docker run -d ubuntu /bin/bash -c "while true; do echo Hello world; sleep 1; done"
	6aa9905ab9bc2693b991001065c4a81c2a5fdee7a04a548146cf8b73ba753e9e
	[root@docker ~]#
	2.查看状态
	[root@docker ~]# docker ps
	CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               NAMES
	6aa9905ab9bc        ubuntu:14.04        "/bin/bash -c 'while   18 seconds ago      Up 18 seconds                           jovial_elion
	[root@docker ~]#
	3.查看日志
	[root@docker ~]# docker logs 6aa 连接的ID
	Hello world
	Hello world
	Hello world
	Hello world
	Hello world
	Hello world
	...
	[root@docker ~]# 
	4.终止
	[root@docker ~]# docker stop 6aa
	6aa
	[root@docker ~]#

	5.查看处于终止状态的容器ID
	[root@docker ~]# docker ps -a -q
	6aa9905ab9bc
	10f115a0aa25
	e192cf610bda
	c535b42dc3f2
	713e34e2b463
	c72ad6c5adcd
	[root@docker ~]#
	6.启动以及重新启动
	[root@docker ~]# docker start 6aa
	6aa
	[root@docker ~]# docker ps
	CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               NAMES
	6aa9905ab9bc        ubuntu:14.04        "/bin/bash -c 'while   4 minutes ago       Up 4 seconds                            jovial_elion
	[root@docker ~]# docker restart 6aa 会将运行的容器终止,然后再重新启动它。
	6aa
	[root@docker ~]# docker ps
	CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               NAMES
	6aa9905ab9bc        ubuntu:14.04        "/bin/bash -c 'while   4 minutes ago       Up 1 seconds                            jovial_elion
	[root@docker ~]#

docker容器怎么删除容器:

	1.先查看容器
	[root@docker tmp]# docker ps -a
	CONTAINER ID        IMAGE                    COMMAND                CREATED             STATUS                         PORTS                     NAMES
	a457769fdd92        ubuntu:14.04             "/bin/bash"            24 minutes ago      Up 24 minutes                                            backstabbing_fermi
	7d3d35cb9c40        ubuntu:14.04             "/bin/bash"            40 minutes ago      Up 40 minutes                                            elegant_brattain
	6aa9905ab9bc        ubuntu:14.04             "/bin/bash -c 'while   50 minutes ago      Exited (-1) 39 minutes ago                               jovial_elion
	10f115a0aa25        ubuntu:14.04             "/bin/bash"            About an hour ago   Exited (0) 59 minutes ago                                compassionate_elion
	e192cf610bda        ubuntu:14.04             "/bin/echo test"       About an hour ago   Exited (0) About an hour ago                             goofy_pasteur
	c535b42dc3f2        ubuntu:14.04             "/bin/echo 'hello wo   About an hour ago   Exited (0) About an hour ago                             compassionate_wozniak
	713e34e2b463        ubuntu:14.04             "/bin/bash"            About an hour ago                                                            cocky_bell
	c72ad6c5adcd        ubuntu:14.04             "/bin/bash"            3 hours ago         Exited (0) 3 hours ago                                   suspicious_feynman
	8703418b2dec        training/webapp:latest   "echo 'hello world'"   3 hours ago         Exited (0) 3 hours ago                                   pensive_curie
	7f3491707dd4        ubuntu:14.04             "/bin/bash"            3 hours ago         Exited (0) 3 hours ago                                   ecstatic_poincare
	b89ee17998ec        ubuntu:14.04             "/bin/bash"            3 hours ago         Exited (0) 3 hours ago                                   tender_babbage
	0aced9db36e2        training/webapp:latest   "python app.py"        3 months ago        Exited (0) 9 weeks ago         0.0.0.0:49153->5000/tcp   web
	436efb7b9933        28a945b4333c             "/bin/bash"            3 months ago        Exited (0) 9 weeks ago                                   berserk_thompson
	[root@docker tmp]#

	2.删除容器
	[root@docker tmp]# docker rm -f 436efb7b9933 0aced9db36e2 b89ee17998ec
	436efb7b9933
	0aced9db36e2
	b89ee17998ec
	[root@docker tmp]#

	3.查看
	[root@docker tmp]# docker ps -a
	CONTAINER ID        IMAGE                    COMMAND                CREATED             STATUS                         PORTS               NAMES
	a457769fdd92        ubuntu:14.04             "/bin/bash"            25 minutes ago      Up 25 minutes                                      backstabbing_fermi
	7d3d35cb9c40        ubuntu:14.04             "/bin/bash"            41 minutes ago      Up 41 minutes                                      elegant_brattain
	6aa9905ab9bc        ubuntu:14.04             "/bin/bash -c 'while   52 minutes ago      Exited (-1) 41 minutes ago                         jovial_elion
	10f115a0aa25        ubuntu:14.04             "/bin/bash"            About an hour ago   Exited (0) About an hour ago                       compassionate_elion
	e192cf610bda        ubuntu:14.04             "/bin/echo test"       About an hour ago   Exited (0) About an hour ago                       goofy_pasteur
	c535b42dc3f2        ubuntu:14.04             "/bin/echo 'hello wo   About an hour ago   Exited (0) About an hour ago                       compassionate_wozniak
	713e34e2b463        ubuntu:14.04             "/bin/bash"            About an hour ago                                                      cocky_bell
	c72ad6c5adcd        ubuntu:14.04             "/bin/bash"            3 hours ago         Exited (0) 3 hours ago                             suspicious_feynman
	8703418b2dec        training/webapp:latest   "echo 'hello world'"   3 hours ago         Exited (0) 3 hours ago                             pensive_curie
	7f3491707dd4        ubuntu:14.04             "/bin/bash"            3 hours ago         Exited (0) 3 hours ago                             ecstatic_poincare
	[root@docker tmp]#
docker 

See also