容器的基本操作
作者:互联网
1、启动容器
方法一(不推荐):
先创建一个容器:docker create 镜像名
再启动容器:docker start 容器名
方法二:docker run 镜像名
(1)格式:
[root@localhost ~]# docker run --help
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
(2)options常用命令选项
-t :打开一个终端,像使用交换机一样使用容器
-i:交互式访问
--name:容器名字
--network:指定网络
--rm:容器一停,自动删除
-d:剥离与当前终端的关系;否则会一直占据着终端
-p: 端口映射,将容器内服务的端口映射在宿主机的指定端口 (3) 示例:运行一个容器[root@localhost ~]# docker run --name web1 -d -p 8888:80 nginx:1.14-alpine
274e54cc5f7b4b08a0cae39ab4885d54dfd6c8bbca57e17c342331b285b4c052
[root@localhost ~]# docker port web1 #查询dockers端口映射
80/tcp -> 0.0.0.0:8888
[root@localhost ~]# docker run --name b1 -it busybox /bin/sh #在运行容器时,交互式进入容器
/ # ls
bin dev etc home proc root sys tmp usr var
/ # exit
(4)查询容器运行状态命令(以下两条命令都可)
docker ps 只能看见运行中的容器
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
274e54cc5f7b nginx:1.14-alpine "nginx -g 'daemon ..." 2 minutes ago Up 2 minutes 0.0.0.0:8888->80/tcp web1
[root@localhost ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
274e54cc5f7b nginx:1.14-alpine "nginx -g 'daemon ..." 2 minutes ago Up 2 minutes 0.0.0.0:8888->80/tcp web1
注:
容器内的第一个进程必须一直处于运行的状态,否则这个容器,就会处于退出状态
-a:查询所有容器
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
db52fbf12e58 busybox "/bin/sh" 3 minutes ago Exited (0) 3 minutes ago b1
274e54cc5f7b nginx:1.14-alpine "nginx -g 'daemon ..." 3 minutes ago Up 3 minutes 0.0.0.0:8888->80/tcp web1
2、停止运行的容器
关闭运行的容器:docker stop 容器名
杀死运行的容器:docker kill 容器名 (-s指定信号。和kill用法一样,-9强制杀死)
[root@localhost ~]# docker kill web1
web1
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
db52fbf12e58 busybox "/bin/sh" 7 minutes ago Exited (0) 7 minutes ago b1
274e54cc5f7b nginx:1.14-alpine "nginx -g 'daemon ..." 7 minutes ago Exited (137) 13 seconds ago web1
3、激活关闭的容器
docker start 容器名
(1)格式
[root@localhost ~]# docker start --help
Usage: docker start [OPTIONS] CONTAINER [CONTAINER...]
(2)常用选项
-a:附加到当前终端
-i:交互式
(3)实例
[root@localhost ~]# docker start web1
web1
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
274e54cc5f7b nginx:1.14-alpine "nginx -g 'daemon ..." 10 minutes ago Up 3 seconds 0.0.0.0:8888->80/tcp web1
4、查看容器的详细信息
[root@localhost~]# docker inspect web1
5、删除容器
[root@localhost ~]# docker kill web1
web1
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
db52fbf12e58 busybox "/bin/sh" 12 minutes ago Exited (0) 12 minutes ago b1
274e54cc5f7b nginx:1.14-alpine "nginx -g 'daemon ..." 13 minutes ago Exited (137) 9 seconds ago web1
[root@localhost ~]# docker rm web1
web1
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
db52fbf12e58 busybox "/bin/sh" 13 minutes ago Exited (0) 13 minutes ago b1
删除所有容器,-f 强制删除
[root@localhost ~]# docker rm -f `docker ps -a -q`
db52fbf12e58
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6、对运行的容器执行指定命令
使用docker exec / docker container exec
(1)格式
[root@localhost ~]# docker exec --help
Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
(2)选项常用
-d:在后台运行命令
-e:设置环境变量
-i:交互式
-t:打开一个终端
-u:用户名或 UID (3):实例[root@localhost ~]# docker run --name web1 -d nginx:1.14-alpine
5e509b05061ec742e757bb49b8c7ab342127c85b1ea37106d5af3d070a419c6d
[root@localhost ~]# docker exec -it web1 /bin/sh #交互式
/ # ls
bin etc lib mnt proc run srv tmp var
dev home media opt root sbin sys usr
/ # exit
[root@localhost ~]# docker exec web1 ip add #非交互式
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
10: eth0@if11: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.2/16 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::42:acff:fe11:2/64 scope link
valid_lft forever preferred_lft forever
7、查询容器内部日志
[root@localhost ~]# curl 172.17.0.2
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@localhost ~]# docker logs web1
172.17.0.1 - - [05/Aug/2021:12:45:38 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
标签:容器,nginx,web1,基本操作,docker,root,localhost 来源: https://blog.csdn.net/weixin_45993951/article/details/119414152