docker部署nginx服务器

部署nginx服务器

1、使用docker拉取nginx
hi@ubuntu:~$ docker search nginx
NAME                               DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
nginx                              Official build of Nginx.                        15093     [OK]       
jwilder/nginx-proxy                Automated Nginx reverse proxy for docker con…   2037                 [OK]
richarvey/nginx-php-fpm            Container running Nginx + PHP-FPM capable of…   816                  [OK]
......
hi@ubuntu:~$ docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
b4d181a07f80: Already exists 
edb81c9bc1f5: Pull complete 
b21fed559b9f: Pull complete 
03e6a2452751: Pull complete 
b82f7f888feb: Pull complete 
5430e98eba64: Pull complete 
Digest: sha256:47ae43cdfc7064d28800bc42e79a429540c7c80168e8c8952778c0d5af1c09db
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
hi@ubuntu:~$ 

2、启动nginx
# 查看镜像文件
hi@ubuntu:~$ docker image ls
REPOSITORY       TAG       IMAGE ID       CREATED        SIZE
redis            latest    08502081bff6   8 days ago     105MB
nginx            latest    4f380adfc10f   8 days ago     133MB
mysql            5.7.33    450379344707   2 months ago   449MB
delron/fastdfs   latest    8487e86fc6ee   3 years ago    464MB
# 启动nginx
hi@ubuntu:~$ docker run -d -p 80:80 nginx
54ff16ea5f261ce866ac54442e07f874b57bedda622a3869fb0b37845cfae3e3
# 查看正在运行的容器
hi@ubuntu:~$ docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                               NAMES
54ff16ea5f26   nginx     "/docker-entrypoint.…"   6 seconds ago   Up 5 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   boring_feistel

这个时候就可以在浏览器中打开http://172.17.0.1:80页面查看了,

img

其他:nginx服务部署时,映射本地目录到nginx容器

1、创建本地目录,用于存放nginx的相关文件信息
hi@ubuntu:~$ sudo mkdir -p /home/nginx/www /home/nginx/logs /home/nginx/conf
[sudo] hi 的密码: 
#	www: 目录将映射为 nginx 容器配置的虚拟目录。
#	logs: 目录将映射为 nginx 容器的日志目录。
#	conf: 目录里的配置文件将映射为 nginx 容器的配置文件。
2、拷贝容器内 Nginx 默认配置文件到本地当前目录下的 conf 目录,容器ID可以查看 docker ps 命令输入中的第一列:
hi@ubuntu:~$ docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                               NAMES
54ff16ea5f26   nginx     "/docker-entrypoint.…"   26 minutes ago   Up 26 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp   boring_feistel
hi@ubuntu:~$ docker cp 54ff16ea5f26:/etc/nginx/nginx.conf /home/nginx/conf/
open /home/nginx/conf/nginx.conf: permission denied	#出现这个permission denied,加sudo重新执行就可以了
hi@ubuntu:~$ sudo docker cp 54ff16ea5f26:/etc/nginx/nginx.conf /home/nginx/conf/
3、部署
$ docker run --rm -d -p 8080:80 --name nginx-myweb \
  -v /home/nginx/www:/usr/share/nginx/html \
  -v /home/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
  -v /home/nginx/logs:/var/log/nginx \
  nginx
4、启动之后就可以编写自己的页面了
# 进入到网站文件目录
hi@ubuntu:~$ cd /home/nginx/www
# 现在还是空的
hi@ubuntu:/home/nginx/www$ ls
# 自己创建一个,名字为index.html,nginx会自动加载这个index.html,如果是其他名字需要别的配置
hi@ubuntu:/home/nginx/www$ sudo touch index.html
hi@ubuntu:/home/nginx/www$ ls
index.html
hi@ubuntu:/home/nginx/www$ sudo vim index.html		# 把下面的内容输入进去
<html>
  <head>
    <title>my website</title>
  </head>
  <body>
    <h1>Hello,World</h1>
    <p>这是我的nginx服务</p>
  </body>
</html>

打开http://172.17.0.1:8080查看

img

中文乱码了,哈哈哈哈,不过页面还是显示出来了,编码问题以后再解决

nginx更多配置可以看我另一篇博客
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值