运行docker容器-2
- Kubernetes
- 2024-04-24
- 1873热度
- 0评论
运行docker容器-2
以交互式方法启动并进入容器,输入 exit,退出容器,退出之后容器也会停止,不会再前台运行
[root@docker-master ~]# docker run --name=interaction -it centos /bin/bash
[root@5fe47ff37013 /]# exit
exit
[root@docker-master ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5fe47ff37013 centos "/bin/bash" About a minute ago Exited (0) 7 seconds ago interaction
参数 | 解释 |
---|---|
--name | 容器的名称 |
-i | 交互式 |
-t | 分配伪终端 |
centos | 启动 docker 需要的镜像 |
/bin/bash | shell 类型为 bash,bash shell 是最常用的一种 shell, 是大多数 Linux 发行版默 认的 shell。 此外还有 C shell 等其它 shell |
以守护进程方式退出容器
[root@docker-master ~]# docker run --name=interaction_d -td centos
992d6658b8f44e9558babf0f920368a55e03d252ebe5603e3088415036c837a8
[root@docker-master ~]# docker ps | grep interaction_d
992d6658b8f4 centos "/bin/bash" 10 seconds ago Up 10 seconds interaction_d
参数 | 解释 |
---|---|
-d | 在后台运行 docker |
查看正在运行的容器
[root@docker-master ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
992d6658b8f4 centos "/bin/bash" 3 minutes ago Up 3 minutes interaction_d
查看所有容器,包括运行和退出状态的容器
[root@docker-master ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
40f68a85b7e4 centos "/bin/bash" 3 seconds ago Exited (0) 1 second ago interaction
992d6658b8f4 centos "/bin/bash" 4 minutes ago Up 4 minutes interaction_d
停止容器
[root@docker-master ~]# docker stop interaction_d
interaction_d
启动已经停止的容器
[root@docker-master ~]# docker start interaction
interaction
进入容器
[root@docker-master ~]# docker exec -it interaction /bin/bash
[root@40f68a85b7e4 /]#
删除容器,容器需要处于退出状态,否则会出现错误提示
[root@docker-master ~]# docker rm interaction
Error response from daemon: cannot remove container "/interaction": container is running: stop the container before removing or force remove
[root@docker-master ~]# docker stop interaction
interaction
[root@docker-master ~]# docker rm interaction
interaction
查看docker帮助命令
[root@docker-master ~]# docker --help
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Common Commands:
run Create and run a new container from an image
exec Execute a command in a running container
ps List containers
...省略部分输出...
通过docker部署nginx服务
运行以centos为基础,名称为nginx的容器
[root@docker-master ~]# docker run --name=nginx -p 80 -itd centos
3331fc2004eb247a9f6038eb1be763005b4889a142dfc57e8d50aea0df099aa8
参数 | 解释 |
---|---|
-p | 把容器端口随机在物理机随机映射一个端口 |
查看物理机映射的端口号和容器运行状态
[root@docker-master ~]# docker ps | grep nginx
3331fc2004eb centos "/bin/bash" 17 seconds ago Up 16 seconds 0.0.0.0:32768->80/tcp, :::32768->80/tcp nginx
进入容器内部,并查看容器自身的IP地址
[root@docker-master ~]# docker exec -it nginx /bin/bash
[root@3331fc2004eb /]# ip address
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 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
16: eth0@if17: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
yum命令安装nginx服务、wget和vim工具,但此处会报错(某些镜像可能不会报错,视镜像而定)
[root@3331fc2004eb /]# yum -y install wget nginx vim
Failed to set locale, defaulting to C.UTF-8
CentOS Linux 8 - AppStream 65 B/s | 38 B 00:00
Error: Failed to download metadata for repo 'appstream': Cannot prepare internal mirrorlist: No URLs in mirrorlist
删除旧仓库文件,下载阿里云的仓库文件
[root@3331fc2004eb /]# rm -rf /etc/yum.repos.d/*
[root@3331fc2004eb /]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2495 100 2495 0 0 21508 0 --:--:-- --:--:-- --:--:-- 21324
重新下载软件
[root@3331fc2004eb /]# yum -y install wget nginx vim
Failed to set locale, defaulting to C.UTF-8
CentOS-8.5.2111 - Base - mirrors.aliyun.com 497 kB/s | 4.6 MB 00:09
CentOS-8.5.2111 - Extras - mirrors.aliyun.com 43 kB/s | 10 kB 00:00
...忽略部分输出...
创建静态页面
[root@3331fc2004eb /]# mkdir -p /var/www/html
[root@3331fc2004eb /]# vim /var/www/html/index.html
<html>
<head>
<title>nginx in docker</title>
</head>
<body>
<h1>hello,I am Caijx</h1>
</body>
</html>
修改nginx配置文件中root路径
[root@3331fc2004eb /]# vim /etc/nginx/nginx.conf
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /var/www/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
启动nginx服务
[root@3331fc2004eb /]# /usr/sbin/nginx
通过物理主机IP+映射端口或容器IP+网页端口访问网页
[root@docker-master ~]# curl 192.168.132.167:32768
<html>
<head>
<title>nginx in docker</title>
</head>
<body>
<h1>hello,I am Caijx</h1>
</body>
</html>
[root@docker-master ~]# curl 172.17.0.2:80
<html>
<head>
<title>nginx in docker</title>
</head>
<body>
<h1>hello,I am Caijx</h1>
</body>
</html>
流量走向:访问物理节点 ip:port(容器在物理节点映射的端口)--->容器 ip:port(容器里部署的服务的端口)--->就可以访问到容器里部署的应用了