一.Docker 部署 Nginx 以及端口映射
Docker 部署 Nginx,首先需要下载nginx镜像,然后启动这个镜像,就运行了一个nginx的容器了
1.下载 nginx 镜像并启动容器
#查看是否存在nginx镜像:发现没有nginx镜像
[root@localhost zph]# docker images | grep nginx
#下载nginx镜像
[root@localhost zph]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a378f10b3218: Pull complete
5b5e4b85559a: Pull complete
508092f60780: Pull complete
59c24706ed13: Pull complete
1a8747e4a8f8: Pull complete
ad85f053b4ed: Pull complete
3000e3c97745: Pull complete
Digest: sha256:add4792d930c25dd2abf2ef9ea79de578097a1c175a16ab25814332fe33622de
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
#再次查看是否存在nginx镜像:发现下载成功了nginx镜像
[root@localhost zph]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 593aee2afb64 22 hours ago 187MB
#启动nginx容器,在后台运行,不用加/bin/bash命令,因为nignx底层会执行相关命令
[root@localhost zph]# docker run -it -d --name mynginx 593aee2afb64
e0773deea60048d82729038535fad45308204e181da6c7a35fb44090cc552077
#查看启动的容器:发现nginx已经启动
[root@localhost zph]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e0773deea600 593aee2afb64 "/docker-entrypoint.…" 5 seconds ago Up 2 seconds 80/tcp mynginx
[root@localhost zph]#
启动nginx容器成功后,这样就创建了一个web服务了,验证这个容器是否成功: 进入nginx容器访问url,验证nginx
#进入nginx容器访问url,验证nginx
[root@localhost zph]# docker exec -it e0773deea600 /bin/bash
#curl 验证是否可以打印出nginx相关html:发现可以打印,说明nginx容器创建成功
root@e0773deea600:/# curl 127.0.0.1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
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@e0773deea600:/#
nginx容器创建成功后,那么怎么在外部访问这个容器的url呢?
2. -p 映射端口
想在外部访问容器里面的 nginx 这个时候就需要映射端口
可以通过 -p 参数来映射端口,语法:
docker run -p 容器外端口:容器内端口 容器名/容器ID
#查看运行的容器
[root@localhost zph]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e0773deea600 593aee2afb64 "/docker-entrypoint.…" 15 minutes ago Up 15 minutes 80/tcp mynginx
#查看镜像
[root@localhost zph]# docker images | grep nginx
nginx latest 593aee2afb64 23 hours ago 187MB
# 再运行一个nginx容器(一个镜像可以运行多个容器),并进行端口映射,让外部也可以访问nginx容器中的html
[root@localhost zph]# docker run -it -d --name mynginx_port -p 81:80 593aee2afb64
36bf218c9754446319299c61f919d54aeebf0d3734ecc0de56183972ea7d6614
#查看运行的容器:发现mynginx_port容器已在运行了
[root@localhost zph]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
36bf218c9754 593aee2afb64 "/docker-entrypoint.…" 5 seconds ago Up 3 seconds 0.0.0.0:81->80/tcp, :::81->80/tcp mynginx_port
e0773deea600 593aee2afb64 "/docker-entrypoint.…" 16 minutes ago Up 16 minutes 80/tcp mynginx
#在linux系统中访问nginx中的html:发现成功访问了
[root@localhost zph]# curl 127.0.0.1:81
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
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>
#查看当前linux对外的ip:发现是ens33中的:192.168.0.6
[root@localhost zph]# ip addr
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
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:c3:3d:27 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.6/24 brd 192.168.0.255 scope global dynamic noprefixroute ens33
valid_lft 171516sec preferred_lft 171516sec
inet6 fe80::20c:29ff:fec3:3d27/64 scope link noprefixroute
valid_lft forever preferred_lft forever
#在linux中,通过192.168.0.6本地ip访问发现访问成功
[root@localhost zph]# curl 192.168.0.6:81
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
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 zph]#
二.Docker 部署 Nginx 挂载数据卷
1.-v 指定路径挂载数据卷
挂载数据卷后,当容器外的数据发生变化,容器内的数据也会跟随变化,当容器内的数据发生变化后,也会同步到容器外基本语法:docker run -v 容器外目录 : 容器内目录 容器 ID
#查看镜像
[root@localhost www]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 593aee2afb64 37 hours ago 187MB
#端口映射以及挂载数据
[root@localhost www]# docker run -it -d --name mynginx_pv -p 82:80 -v /var/www/:/usr/local/nginx/html 593aee2afb64
ce47906a57afb7c24a1847211f60e7d778e86a2e0b3c65bbf2bed7cb48123ad8
#查看var/www下文件
root@localhost www]# ll
总用量 12
-rw-r--r-- 1 root root 5 10月 26 07:37 index.html
-rw-r--r-- 1 root root 10 10月 25 07:54 test2.txt
-rw-r--r-- 1 root root 12 10月 25 07:51 test.txt
#查看运行的容器
[root@localhost www]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ce47906a57af 593aee2afb64 "/docker-entrypoint.…" 45 seconds ago Up 42 seconds 0.0.0.0:82->80/tcp, :::82->80/tcp mynginx_pv
#进入容器
[root@localhost www]# docker exec -it ce47906a57af /bin/bash
#进入挂载的数据目录
root@ce47906a57af:/# cd /usr/local/nginx/html
#查看文件
root@ce47906a57af:/usr/local/nginx/html# ls
index.html test.txt test2.txt
#删除一个文件
root@ce47906a57af:/usr/local/nginx/html# rm -rf test.txt
root@ce47906a57af:/usr/local/nginx/html# ls
index.html test2.txt
#退出
root@ce47906a57af:/usr/local/nginx/html# exit
#查看外面数据文件是否被删除:发现已经被删除了,操作成功
[root@localhost www]# ll
总用量 8
-rw-r--r-- 1 root root 5 10月 26 07:37 index.html
-rw-r--r-- 1 root root 10 10月 25 07:54 test2.txt
2.docker inspect 命令查看容器运行的细节
查看详细的映射关系
[root@localhost wwwroot]# docker inspect 3408e7d7a430
3.-v 匿名挂载数据卷
基本语法:
docker run -v 容器内目录 容器 ID
-P 表示随机映射端口
docker run -d -P --name nginx01 -v /etc/nginx nginx
[root@localhost ~]# docker run -d -P --name nginx01 -v /etc/nginx nginx
e6aea0a9c58cf4f31ce0ebdd43b071845ba5b1ee772949a5cf02da5f40e2245c
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATU
S PORTS NAMES
e6aea0a9c58c nginx "/docker-entrypoint.…" 2 seconds ago Up 1 second
0.0.0.0:49155->80/tcp, :::49155->80/tcp nginx01
查看挂载详情
[root@localhost ~]# docker volume ls
DRIVER VOLUME NAME
local 3f55b08bbafb39845c98efe1cf18dfa7fedea21c2fad24cb14759dced3872ba2
local 4de46af05120a375cb5270af59790f432c64758517352963f55e67cd3bfa56d8
local 5a8a57ea0fe775429577c451d23687f76634b5ee845c95e6e30a099b888ac2c1
local 5c97447224de838eab94272de4cb556cea8e79a4469b618c00cba1b826e0d37e
local 6b8cfbcf9727dc2c3f404cf9d830de7971fe7d46a56c5a0a42222182917fd6ae
local 6b9adae890deb91efcf5a52913f6ab61d8c22a83c2130fc21c9a3f44f32919
[root@localhost ~]# docker inspect e6aea0a9c58c | grep volume
"Type": "volume", "Source": "/var/lib/docker/volumes/3f55b08bbafb39845c98efe1cf18dfa7fed
ea21c2fad24cb14759dced3872ba2/_data
4.-v 具名挂载数据卷
基本语法:
docker run -v 卷名称:容器内目录 容器 ID
docker run -d -P --name nginx01 -v juming:/etc/nginx nginx
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATU
S PORTS NAMES
57764c6d6901 nginx "/docker-entrypoint.…" 5 seconds ago Up 4 seconds
0.0.0.0:49156->80/tcp, :::49156->80/tcp nginx01
[root@localhost ~]# docker volume ls
DRIVER VOLUME NAME
local 3f55b08bbafb39845c98efe1cf18dfa7fedea21c2fad24cb14759dced3872ba2
local fdf7b0c7847f62ffe2326ef8a11b746efbb8b9fd212970cd31ccca5385fc9865
local juming
查看挂载详情
[root@localhost ~]# docker volume inspect juming
[
{ "CreatedAt": "2021-07-28T05:09:13-04:00", "Driver": "local", "Labels": null, "Mountpoint": "/var/lib/docker/volumes/juming/_data",
Name": "juming", "Options": null, "Scope": "local"
}
]
5.Docker 运行容器传递环境变量
语法:docker run -e 变量名 = 变量值 容器 IDprintenv: 打印出环境变量
#传递一个环境变量
[root@localhost www]# docker run --rm --name centos8_env -e TEST_DOMAIN=www.xxx.com 5d0da3dc9764 printenv
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=1d078b1e2069
TEST_DOMAIN=www.xxx.com
HOME=/root
#传递多个环境变量
[root@localhost www]# docker run --rm --name centos8_env -e TEST_DOMAIN=www.xxx.com -e TEST_ADDR=192.68.1.111 5d0da3dc9764 printenv
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=1c3d6eaef5b7
TEST_DOMAIN=www.xxx.com
TEST_ADDR=192.68.1.111
HOME=/root
6.Docker 容器内安装软件
Docker 容器内安装软件和linux 中安装软是一样的,Centos 系列用yum安装,Ubuntu 系列用apt-get 安装
#进入容器
[root@localhost ~]# docker exec -it centos_v /bin/bash
#查看
[root@095f45520b1e /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin
srv sys tmp usr var
#使用ifocnfig容器ip:发现没有ifconfig包以及命令
[root@095f45520b1e /]# ifconfig
bash: ifconfig: command not found
#安装net-tools
[root@095f45520b1e /]#yum install -y net-tools
#查看容器的 ip
[root@095f45520b1e /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
RX packets 7805 bytes 14138450 (13.4 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4715 bytes 259714 (253.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0