5

Docker | 部署nginx服务 - 甜点cc

 1 year ago
source link: https://www.cnblogs.com/all-smile/p/16771412.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

容器时相互隔离的,docker启动的nginx是容器内的服务,不影响我原先服务器上的nginx服务,小伙伴们可大胆尝试😃

本篇实在linux环境下操作的,主要目的是夺人眼目,对使用Docker部署服务尝鲜,先体会一下Docker的便捷之处。

1、下载nginx镜像

docker pull nginx

2、查看下载的镜像

docker ps

3、启动nginx镜像

docker run -d --name nginx01 -p 3344:80 nginx(镜像名或者镜像id)

1037867-20221009105951571-1654743988.png

4、查看启动的nginx

docker ps

5、访问启动的nginx

在云服务器内使用 curl 测试访问

curl localhost:3344

1037867-20221009105952065-1831902023.png

5.1、linux开放防火墙端口

firewall-cmd位于firewalld包中

systemctl位于systemd包中

安装防火墙工具:

yum install firewalld systemd -y

常用命令:

# 查看firewall(防火墙)服务状态
systemctl status firewalld

#开启防火墙
systemctl start firewalld

#重启防火墙
service firewalld  restart

#关闭防火墙
service firewalld  stop

#查看防火墙规则
firewall-cmd --list-all     #查看全部信息
firewall-cmd --list-ports     #只看端口信息

#添加防火墙开放端口
firewall-cmd --add-port=8001/tcp --permanent
or
firewall-cmd --zone=public --add-port=8080/tcp --permanent
命令含义:
  --zone          #作用域(public标识公共的)
  --add-port=80/tcp   #添加端口, 格式为:端口/通讯协议
  --permanent       #永久生效,没有此参数重启后失效
  
# 不要忘记reload!
firewall-cmd --reload

5.2、阿里云服务器需要配置安全组规则

配置完之后就可以在浏览器访问公网ip+3344端口号查看了

5.3、原理

端口暴露,linux端口映射到容器端口

0.0.0.0:3344->80/tcp

1037867-20221009105952584-1919717768.png

6.进入nginx容器

docker exec -it 0a7ebd6bc291 /bin/bash
root@0a7ebd6bc291:/# ls
bin  boot  dev	docker-entrypoint.d  docker-entrypoint.sh  etc	home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@0a7ebd6bc291:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@0a7ebd6bc291:/# cd /etc/nginx/
root@0a7ebd6bc291:/etc/nginx# ls
conf.d	fastcgi_params	mime.types  modules  nginx.conf  scgi_params  uwsgi_params
root@0a7ebd6bc291:/etc/nginx# 

每次改动nginx配置,都需要进入容器内部修改,十分麻烦,能否在容器外部提供一个映射路径,达到修改配置文件,容器内部就会自动修改?

数据卷技术

nginx.conf配置文件,注意Nginx默认是后台运行的,但Docker需要其在前台运行,否则直接退出容器。配置文件中添加daemon off;关闭后台运行。可以通过Dockerfile配置CMD命令实现

感谢阅读,本篇完,数据卷技术下文继续分享!


1037867-20221009105953162-794003220.png

我是 甜点cc

热爱前端,也喜欢专研各种跟本职工作关系不大的技术,技术、产品兴趣广泛且浓厚,等待着一个创业机会。本号主要致力于分享个人经验总结,希望可以给一小部分人一些微小帮助。

希望能和大家一起努力营造一个良好的学习氛围,为了个人和家庭、为了我国的互联网物联网技术、数字化转型、数字经济发展做一点点贡献。数风流人物还看中国、看今朝、看你我。

__EOF__


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK