Docker之容器命令

说明:我们有了镜像才可以创建容器,我们下载一个Centos来进行学习

docker pull centos

新建容器并启动
docker run [参数] [镜像名称]

例如:[root@liuyang ~]# docker run -it centos

#参数说明 
--name="Name"       容器名字 用来区分容器
-d                  后台方式运行
-it                 使用交互方式运行,进入容器查看内容
-p                  指定容器端口
-P                  随机指定端口
[root@liuyang ~]# docker run -it centos /bin/bash
[root@7870b4071753 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@7870b4071753 /]#

退出容器:
1、停止退出exit
2、不停止退出 ctrl+q+p

[root@7870b4071753 /]# exit
exit

列出所有运行中的容器

ps  命令:列出当前正在运行的容器
ps -a命令:列出所有运行过的容器
-n=?命令:显示最近创建的容器
-q   命令:只显示容器编号

[root@liuyang ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED         STATUS                            PORTS     NAMES
7870b4071753   centos         "/bin/bash"   3 minutes ago   Exited (127) About a minute ago             suspicious_hugle
c56c5d929af5   centos         "/bin/bash"   4 minutes ago   Exited (127) 4 minutes ago                  happy_chandrasekhar
84a1689a238f   centos         "/bin/bash"   4 hours ago     Exited (0) 4 hours ago                      ecstatic_bhabha
48ac51ffcaa7   bf756fb1ae65   "/hello"      21 hours ago    Exited (0) 21 hours ago                     thirsty_kowalevski
caa0029c0594   centos         "/bin/bash"   23 hours ago    Up 23 hours                                 heuristic_lehmann
3a9eab4a366b   centos         "/bin/bash"   23 hours ago    Exited (0) 23 hours ago                     optimistic_mclaren
11d415340840   centos         "/bin/bash"   23 hours ago    Exited (0) 23 hours ago                     great_swirles
9587b7e2a94f   centos         "/bin/bash"   23 hours ago    Exited (0) 23 hours ago                     quizzical_torvalds
3a3b28b3ae9a   centos         "/bin/bash"   23 hours ago    Exited (0) 23 hours ago                     naughty_jemison
b165f2e0518d   centos         "/bin/bash"   23 hours ago    Exited (0) 23 hours ago                     busy_villani
a5bf60326fcb   centos         "/bin/bash"   24 hours ago    Exited (0) 23 hours ago                     heuristic_darwin
2f4ab7bdb433   bf756fb1ae65   "/hello"      24 hours ago    Exited (0) 24 hours ago                     suspicious_allen
db50c01e42f7   bf756fb1ae65   "/hello"      24 hours ago    Exited (0) 24 hours ago                     eager_ptolemy
[root@liuyang ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED        STATUS        PORTS     NAMES
caa0029c0594   centos    "/bin/bash"   23 hours ago   Up 23 hours             heuristic_lehmann
[root@liuyang ~]#

删除容器:

docker rm 容器id  (不能删除正在运行的容器 若要删除 -f)
docker rm -f $(docker ps -aq)  

启动和停止容器的操作

docker start	[容器ID]	    #启动容器
docker restart				#重启容器
docker stop					#停止正在运行的容器
docker kill 				#强制停止容器

常用其他指令:
后台启动容器

# 命令 docker run -d 镜像名
[root@liuyang ~]# docker run -d centos
4d2c016b7a32de6b846ff8c0c050cc0406716c2046f39ead291abc04f215fa6e

问题: docker ps :发现容器停止了
#常见的坑:docker使用-d后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止
#nginx,容器启动后,发现自己没有提供服务,就会立刻停止,没程序了;

查看日志:

docker logs -f -t --tail 容器

查看容器进程信息 PID
#docker top 容器ID

[root@liuyang ~]# docker  top  9ce79c396b87
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                7709                7694                0                   10:24               pts/0               00:00:00            /bin/bash
root                8120                7694                0                   10:29               ?                   00:00:00            /bin/bash
root                8170                7694                0                   10:30               ?                   00:00:00            /bin/bash
[root@liuyang ~]#

查看一个镜像的元数据:

[root@liuyang ~]#docker inspect 容器ID

进入容器的方式:
docker exec -it 镜像ID 路径
docker attach

例如:
[root@liuyang ~]# docker exec -it 9ce79c396b87 /bin/bash
[root@9ce79c396b87 /]#
或者
[root@liuyang ~]# docker attach 51a8720be201

从容器拷贝文件到主机
docker cp 容器id:容器内路径 目的主机路径

[root@liuyang ~]# docker cp c67b2a575a6e:/usr/local/tomcat/webapps  /usr/local/
[root@liuyang ~]# cd ..
[root@liuyang /]# cd usr/local/
[root@liuyang local]# ls
aegis  bin  etc  games  include  lib  lib64  libexec  mysql  mysql-5.7.11-linux-glibc2.5-x86_64.tar.gz  sbin  share  src  webapps
[root@liuyang local]#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘小羊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值