docker安装nginx

2022-10-27 13:37:58 浏览数 (1)

docker部署nginx非常简单 难的是如何定义nginx.conf与部署html下的前端文件

# 拉取镜像

代码语言:javascript复制
[root@localhost ~]# docker pull nginx:1.14.0
[root@localhost ~]# docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
nginx                1.14.0              ecc98fc2f376        2 years ago         109MB

# 启动

代码语言:javascript复制
[root@localhost ~]# docker run --name nginx-test -p 8080:80 -d nginx:1.14.0
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
8657715d04a5        nginx:1.14.0        "nginx -g 'daemon of…"   5 hours ago         Up 45 minutes       0.0.0.0:8080->80/tcp   nginx-test
  • 这时候访问本机ip:8080 就能看到熟悉的Welcome to nginx!

# 自定义nginx

  1. 进入容器
代码语言:javascript复制
[root@localhost ~]# docker exec -it 8657715d04a5 /bin/bash
root@8657715d04a5:/# ls
bin  boot  dev	etc  home  lib	lib64  media  mnt  opt	proc  root  run  sbin  srv  sys  tmp  usr  var
root@8657715d04a5:/# 
  1. 查找对应路径
代码语言:javascript复制
root@8657715d04a5:/# find / -name nginx
/etc/default/nginx
/etc/init.d/nginx
/etc/logrotate.d/nginx
/etc/nginx # 配置文件路径
/usr/lib/nginx
/usr/sbin/nginx # 启停
/usr/share/doc/nginx 
/usr/share/nginx # 默认html存放路径
/var/cache/nginx 
/var/log/nginx  # 默认日志文件      
root@8657715d04a5:/# find / -name nginx.conf
/etc/nginx/nginx.conf  # 编辑该配置文件哦
  1. 复制前端文件到容器中
代码语言:javascript复制
[root@localhost ~]# docker cp  /home/summer/nginx/html 8657715d04a5:/usr/share/nginx/html
  1. 处理默认配置
代码语言:javascript复制
root@8657715d04a5:/etc/nginx/conf.d# pwd
/etc/nginx/conf.d
root@8657715d04a5:/etc/nginx/conf.d# mv default.conf default
  1. 编辑定制nginx.conf
代码语言:javascript复制
root@8657715d04a5:/# vim /etc/nginx/nginx.conf
  1. 重新加载
代码语言:javascript复制
root@8657715d04a5:/usr/sbin# ./nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
root@8657715d04a5:/usr/sbin# ./nginx -s reload
  1. 保存定制的镜像
代码语言:javascript复制
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
8657715d04a5        nginx:1.14.0        "nginx -g 'daemon of…"   5 hours ago         Up 31 minutes       0.0.0.0:8080->80/tcp   nginx-test
[root@localhost ~]# docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
summer/centos7-ssh   latest              1c0967200c5d        8 days ago          346MB
centos               centos7.5.1804      cf49811e3cdb        20 months ago       200MB
nginx                1.14.0              ecc98fc2f376        2 years ago         109MB
[root@localhost ~]# docker commit 8657715d04a5 summer/nginx-k8s
sha256:92e3cc68a48d7e626cbf122dfff9dbfa35de50153a7f9aace0295f9856c708c9
[root@localhost ~]# docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
summer/nginx-k8s     latest              92e3cc68a48d        12 seconds ago      479MB
summer/centos7-ssh   latest              1c0967200c5d        8 days ago          346MB
centos               centos7.5.1804      cf49811e3cdb        20 months ago       200MB
nginx                1.14.0              ecc98fc2f376        2 years ago         109MB
[root@localhost ~]# docker save -o nginx-k8s.tar summer/nginx-k8s:latest
[root@localhost ~]# ll
-rw-------  1 root root 495302656 Nov 27 22:43 nginx-k8s.tar
  1. 装载镜像
代码语言:javascript复制
docker load -i nginx-k8s.tar

0 人点赞