标签:sbin lib64 lang 访问 exp ipaddr 说明 就会 exit
方法一:不推荐使用 先创建一个容器 docker create 镜像名称 在启动容器 docker start 镜像名称 方法二:推荐的 docker run -d -p 宿主机端口:镜像端口 镜像名称:version run:创建并运行一个容器 -d:在后台运行 -p:端口映射,把宿主机的端口映射为容器系统的端口 最后接要启动的容器的镜像的名称:镜像版本 docker run == docker create + docker start
docker的命令格式
docker run [OPTIONS] IMAGE [COMMAND] [ARG...] options的参数 -t :打开一个终端,像使用交换机一样使用容器 -i:交互式访问 --name:容器名字 --network:指定网络 --rm:容器一停,自动删除 -d:剥离与当前终端的关系;否则会一直占据着终端 -p:端口映射,将容器内服务的端口映射在宿主机的指定端口 -p <container port> -p <hostport>:<container port> -p <hostip>:<hostport>:<container port> ps: 查看运行着的容器 docker ps == docker container ls ps -a: 查看所有容器
例子1:
docker run -d --name web1 -p 80:80 nginx:latest [root@inode3 ~]# docker run --name web1 -d -p 80:80 nginx:latest 03fcdd23c2ad9a9d81551ce3fd3bcf4f3a410c648a895fa6db5b497936515da1
查看运行容器的状态
docker ps [root@inode3 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 03fcdd23c2ad nginx:latest "nginx -g ‘daemon of…" 12 minutes ago Up 12 minutes 0.0.0.0:80->80/tcp web1 [root@inode3 ~]# docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 03fcdd23c2ad nginx:latest "nginx -g ‘daemon of…" 8 minutes ago Up 8 minutes 0.0.0.0:80->80/tcp web1
查看容器的端口映射及容器
[root@inode3 ~]# docker port web1 80/tcp -> 0.0.0.0:80
启动容器时,进入容器里面
docker run --name ywx -it busybox:latest /bin/sh [root@inode3 ~]# docker run --name ywx -it busybox:latest /bin/sh / # ls bin dev etc home proc root sys tmp usr var / #
另外启动一个xshell的窗口,查看docker 容器
[root@inode3 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b1840dcc4a4d busybox:latest "/bin/sh" About a minute ago Up About a minute ywx 03fcdd23c2ad nginx:latest "nginx -g ‘daemon of…" About an hour ago Up About an hour 0.0.0.0:80->80/tcp web1
可以发现ywx容器在运行
[root@inode3 ~]# docker run --name ywx -it busybox:latest /bin/sh / # ls bin dev etc home proc root sys tmp usr var / # exit [root@inode3 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b1840dcc4a4d busybox:latest "/bin/sh" 3 minutes ago Exited (0) 7 seconds ago ywx
停止容器有2中方法
第一种:关闭运行的容器 docker stop 容器名或CONTAINER ID 第二种:杀死运行的容器(不建议使用) docker kill 容器名或CONTAINER ID -s 指定信号; -9强制停止容器
关闭一个容器
[root@inode3 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 71dd5dd51ae4 nginx:latest "nginx -g ‘daemon of…" 4 seconds ago Up 3 seconds 0.0.0.0:81->80/tcp web2 03fcdd23c2ad nginx:latest "nginx -g ‘daemon of…" About an hour ago Up About an hour 0.0.0.0:80->80/tcp web1
有we1和we2两个运行的容器
使用docker stop来关闭运行的容器web1 docker stop web1 [root@inode3 ~]# docker stop web1 web1 [root@inode3 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 71dd5dd51ae4 nginx:latest "nginx -g ‘daemon of…" About a minute ago Up About a minute 0.0.0.0:81->80/tcp web2 [root@inode3 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 71dd5dd51ae4 nginx:latest "nginx -g ‘daemon of…" 3 minutes ago Up 3 minutes 0.0.0.0:81->80/tcp web2 03fcdd23c2ad nginx:latest "nginx -g ‘daemon of…" About an hour ago Exited (0) About a minute ago web1
使用docker kill来关闭容器 docker kill web2 [root@inode3 ~]# docker kill web2 web2 [root@inode3 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@inode3 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 71dd5dd51ae4 nginx:latest "nginx -g ‘daemon of…" 5 minutes ago Exited (137) 8 seconds ago web2 03fcdd23c2ad nginx:latest "nginx -g ‘daemon of…" About an hour ago Exited (0) 3 minutes ago web1
容器web2已经停止运行
命令格式:
docker start [option] 容器名称或容器的CONTAINER ID option -a 在当前终端启动 -i 交互式 使用容器名称来启动web1 [root@inode3 ~]# docker start web1 web1 [root@inode3 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 03fcdd23c2ad nginx:latest "nginx -g ‘daemon of…" About an hour ago Up 3 seconds 0.0.0.0:80->80/tcp web1 使用容器的容器的CONTAINER ID来启动web2 [root@inode3 ~]# docker start 71dd5dd51ae4 71dd5dd51ae4 [root@inode3 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 71dd5dd51ae4 nginx:latest "nginx -g ‘daemon of…" 9 minutes ago Up 2 seconds 0.0.0.0:81->80/tcp web2 03fcdd23c2ad nginx:latest "nginx -g ‘daemon of…" About an hour ago Up About a minute 0.0.0.0:80->80/tcp web1
docker inspect 容器名称或容器的CONTAINER ID 查看web1的ip信息 [root@inode3 ~]# docker inspect web1|grep -i "ipaddr" "SecondaryIPAddresses": null, "IPAddress": "172.17.0.2", "IPAddress": "172.17.0.2", [root@inode3 ~]# docker inspect web1 可以查看容器web1的所有信息
删除容器的步骤:
命令格式: docker rm 容器名称或容器的CONTAINER ID
1、停止容器
2、删除容器
删除容器web1 [root@inode3 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 71dd5dd51ae4 nginx:latest "nginx -g ‘daemon of…" 15 minutes ago Up 6 minutes 0.0.0.0:81->80/tcp web2 03fcdd23c2ad nginx:latest "nginx -g ‘daemon of…" About an hour ago Up 7 minutes 0.0.0.0:80->80/tcp web1 第一步:先停止web1容器 [root@inode3 ~]# docker stop web1 web1 [root@inode3 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 71dd5dd51ae4 nginx:latest "nginx -g ‘daemon of…" 16 minutes ago Up 7 minutes 0.0.0.0:81->80/tcp web2 03fcdd23c2ad nginx:latest "nginx -g ‘daemon of…" About an hour ago Exited (0) 5 seconds ago web1 第二部:删除已停止运行的web1容器 [root@inode3 ~]# docker rm web1 web1 [root@inode3 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 71dd5dd51ae4 nginx:latest "nginx -g ‘daemon of…" 18 minutes ago Up 8 minutes 0.0.0.0:81->80/tcp web2 强制删除运行中的容器web2 [root@inode3 ~]# docker rm -f web2 web2 [root@inode3 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 强制删除所有容器(包含运行和没有运行的容器) 环境说明:容器web1停止运行,web2运行正常 [root@inode3 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 615daf3e3c9d nginx:latest "nginx -g ‘daemon of…" 12 seconds ago Exited (0) 3 seconds ago web1 4dbf642e55c0 nginx:latest "nginx -g ‘daemon of…" About a minute ago Up About a minute 0.0.0.0:81->80/tcp web2 强制删除所有容器web1和web2 [root@inode3 ~]# docker rm -f `docker ps -a -q` 615daf3e3c9d 4dbf642e55c0 [root@inode3 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
命令格式:
docker exec [options] 容器名称或容器的CONTAINER ID Command [Ages]
options
-d:在后台运行
-e:设置环境变量
-i:交互式
-t:打开一个终端
-u:用户名或UID
例子:
[root@inode3 ~]# docker run --name web1 -d -p 80:80 nginx:latest 7c5d1590be345fffab80385e76ff04a8f5d565962e03e6753ebef5112ad34087 [root@inode3 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7c5d1590be34 nginx:latest "nginx -g ‘daemon of…" 21 seconds ago Up 20 seconds 0.0.0.0:80->80/tcp web1 1、交互式进入容器 [root@inode3 ~]# docker exec -it web1 /bin/bash root@7c5d1590be34:/# ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var 2、查看/目录信息 [root@inode3 ~]# docker exec web1 ls -l total 8 drwxr-xr-x 2 root root 4096 Feb 24 00:00 bin drwxr-xr-x 2 root root 6 Feb 1 17:09 boot drwxr-xr-x 5 root root 340 Mar 5 15:36 dev drwxr-xr-x 1 root root 66 Mar 5 15:36 etc drwxr-xr-x 2 root root 6 Feb 1 17:09 home drwxr-xr-x 1 root root 56 Feb 26 20:02 lib drwxr-xr-x 2 root root 34 Feb 24 00:00 lib64 drwxr-xr-x 2 root root 6 Feb 24 00:00 media drwxr-xr-x 2 root root 6 Feb 24 00:00 mnt drwxr-xr-x 2 root root 6 Feb 24 00:00 opt dr-xr-xr-x 111 root root 0 Mar 5 15:36 proc drwx------ 1 root root 27 Mar 5 15:39 root drwxr-xr-x 1 root root 23 Mar 5 15:36 run drwxr-xr-x 2 root root 4096 Feb 24 00:00 sbin drwxr-xr-x 2 root root 6 Feb 24 00:00 srv dr-xr-xr-x 13 root root 0 Mar 5 15:36 sys drwxrwxrwt 1 root root 6 Feb 26 20:02 tmp drwxr-xr-x 1 root root 66 Feb 24 00:00 usr drwxr-xr-x 1 root root 19 Feb 24 00:00 var
命令格式:
docker logs 容器名称或容器的CONTAINER ID
查询容器web1中日志信息
直接访问容器的logs [root@inode3 ~]# docker logs web1 没有任何日志的 先访问下容器web1 [root@inode3 ~]# curl 192.168.32.103 再查询容器的logs [root@inode3 ~]# docker logs web1 192.168.32.103 - - [05/Mar/2020:15:42:41 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
查询容器的内部日志,需要先访问该容器的功能,再查询该容器的日志
在运行docker容器时可以加如下参数来保证每次docker服务重启后容器也自动重启: --restart=always –restart具体参数值详细信息: no – 容器退出时,不重启容器; on-failure – 只有在非0状态退出时才从新启动容器; always – 无论退出状态是如何,都重启容器;
如果已经启动了则可以使用如下命令:
docker update --restart=always <CONTAINER ID>
从容器中拷贝文件到宿主机 docker cp 容器id:文件路径 宿主机文件路径 从宿主机拷贝文件到容器 docker cp 宿主机文件路径 容器id:文件路径
标签:sbin lib64 lang 访问 exp ipaddr 说明 就会 exit
原文地址:https://www.cnblogs.com/yaokaka/p/14178205.html