docker容器
1.新建容器并启动:
[root@localhost ~]# docker run -it centos:latest /bin/bash
-t伪终端 -i交互模式
2.容器的退出
(1.)退出后还显示UP状态:
Ctrl+p加ctrl+q 不要ctrl+p+q
(2.)退出后显示exit
Ctrl+c ctrl+d 或者直接命令行exit
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a837f8b605e6 centos:latest “/bin/bash” 5 minutes ago Exited (0) 5 seconds ago nice_meninsky
3.让一个终止的容器启动: docker start 容器id
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a837f8b605e6 centos:latest “/bin/bash” 5 minutes ago Exited (0) 5 seconds ago nice_meninsky
[root@localhost ~]# docker start a837
a837
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a837f8b605e6 centos:latest “/bin/bash” 6 minutes ago Up 1 second nice_meninsky
[root@localhost ~]#
4.终止一个运行中的容器: docker stop 容器id
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a837f8b605e6 centos:latest “/bin/bash” 6 minutes ago Up 1 second nice_meninsky
[root@localhost ~]# docker stop a837
a837
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a837f8b605e6 centos:latest “/bin/bash” 7 minutes ago Exited (0) 2 seconds ago nice_meninsky
5.杀死一个运行中的容器: docker kill 容器id (只能杀死运行中的容器 如果状态为exit则会报错)
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a837f8b605e6 centos:latest “/bin/bash” 9 minutes ago Up 2 seconds nice_meninsky
[root@localhost ~]# docker kill a837
a837
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a837f8b605e6 centos:latest “/bin/bash” 9 minutes ago Exited (137) 1 second ago nice_meninsky
6.重启一个容器:docker restart 容器id (如果容器终止则效果和docker start 相同 不然就是重新启动一下容器)
[root@localhost ~]# docker restart a837
a837
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a837f8b605e6 centos:latest “/bin/bash” 10 minutes ago Up 7 seconds nice_meninsky
7.后台守护运行容器:
[root@localhost ~]# docker run -d centos:latest /bin/bash -c “while true;do echo hello;sleep 1;done”
31c46bd2013e8ff284c7ee1742b10e50ca59f78e3dffb61c4534273f20589cb3
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
31c46bd2013e centos:latest “/bin/bash -c 'while…” About a minute ago Up About a minute epic_tesla
-d 代表后台运行容器 /bin/bash -c 则代表想让容器内部运行什么命令
8.查看容器的信息:docker inspect 容器id
[root@localhost ~]# docker inspect 31c4
[
{
“Id”: “31c46bd2013e8ff284c7ee1742b10e50ca59f78e3dffb61c4534273f20589cb3”,
“Created”: “2020-05-20T11:38:31.024613425Z”,
“Path”: “/bin/bash”,
“Args”: [
“-c”,
“while true;do echo hello;sleep 1;done”
],
等等还有很多信息
8.1用docker inspect 准确精简查看ip等信息:
[root@localhost ~]# docker inspect -f {{.NetworkSettings.IPAddress}} 31c4
172.17.0.3
8.2用docker inspect 准确精简查看运行状态:
[root@localhost ~]# docker inspect -f {{.State.Running}} 31c4
true
9.进入容器: docker attach 容器id (有一个缺陷就是开启多个终端进入同一个容器时 操作命令会同步 就是在终端1里ls 终端2也会自己ls的)
[root@localhost ~]# docker attach a837
[root@a837f8b605e6 /]# ls
9.进入容器2:docker exec [选项] 容器id shell (会进入一个新的shell 之前的命令等等 不会干扰这个新的shell)
[root@localhost ~]# docker exec -it a837 /bin/bash
[root@a837f8b605e6 /]#
9.进入容器3:nsenter -t 3833 -m -u -i -n -p
查看有没有这个包没有直接yum
[root@localhost ~]# rpm -qa |grep util-linux
util-linux-2.23.2-33.el7.x86_64
[root@localhost ~]# nsenter -t 3833 -m -u -i -n -p
[root@a837f8b605e6 /]# ls
-t代表 容器的pid号可以用([root@localhost ~]# docker inspect -f {{.State.Pid}} a837
3833)查看
用exit退出
下载bashrc可以快速查看pid和ip等等信息
Git clone https://github.com/dzckzeo/bashrc_docker.git
执行脚本 后发现多了很多命令
[root@localhost src]# docker
docker docker-cleanc dockerd docker-enter docker-ip docker-pid docker-status
docker-clean docker-cleani docker-dpid docker-init docker-kill docker-proxy docker-update
[root@localhost src]# docker-ip a837
172.17.0.2
10.导出容器:
进入容器添加一个文档:
[root@localhost src]# docker-enter a837
[root@a837f8b605e6 ~]# ls
anaconda-ks.cfg anaconda-post.log original-ks.cfg
[root@a837f8b605e6 ~]# touch text.txt
[root@a837f8b605e6 ~]# exit
导出容器:docker export -o 名字 容器id
[root@localhost src]# docker export -o centos.a83 a837
[root@localhost src]# ls
centos.a83 redis-4.0.11 redis-4.0.11.tar.gz
11.导入容器:
[root@localhost src]# cat centos.a83 |docker import - cent:text
sha256:57b13e3aaf9ca0044df99d809b67941be4fbf24b6f57410e6f84a36816379e4b
查看:
[root@localhost src]# docker run -it cent:text /bin/bash
[root@e0314f38006d /]# ls
aaaa bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@e0314f38006d /]# ls /root/
anaconda-ks.cfg anaconda-post.log original-ks.cfg text.txt
[root@e0314f38006d /]#
发现上面创建的text。Txt还在
12.删除容器:docker rm 容器id
[root@localhost src]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e0314f38006d cent:text “/bin/bash” 2 minutes ago Exited (0) 4 seconds ago cranky_lumiere
31c46bd2013e centos:latest “/bin/bash -c 'while…” 53 minutes ago Exited (137) 39 minutes ago epic_tesla
a837f8b605e6 centos:latest “/bin/bash” About an hour ago Up 43 minutes nice_meninsky
[root@localhost src]# docker rm 31c4
31c4
[root@localhost src]# docker rm e0314
e0314
[root@localhost src]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a837f8b605e6 centos:latest “/bin/bash” About an hour ago Up 44 minutes nice_meninsky