Docker 安装 nginx

拉取镜像

[root@localhost ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
ae13dd578326: Pull complete 
6c0ee9353e13: Pull complete 
dca7733b187e: Pull complete 
352e5a6cac26: Pull complete 
9eaf108767c7: Pull complete 
be0c016df0be: Pull complete 
Digest: sha256:4ed64c2e0857ad21c38b98345ebb5edb01791a0a10b0e9e3d9ddde185cdbd31a
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@localhost ~]# 

启动nginx容器

[root@localhost ~]# docker run --name my_nginx_2022 -d -p 81:80 --restart=always -e TZ="Asia/Shanghai" nginx:latest
fef1a18521feb095bd917ca28d4da711857c5b84bb1505838e26f761043194c3
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                               NAMES
fef1a18521fe   nginx:latest   "/docker-entrypoint.…"   12 seconds ago   Up 10 seconds   0.0.0.0:81->80/tcp, :::81->80/tcp   my_nginx_2022
[root@localhost ~]# 

访问 nginx

http://192.168.70.131:81/

# 以上访问正常,但如果要配置自己的项目文件,或者修改访问路径文件需要登录到容器中去,还需要安装vi等,所以我们下面使用挂载文件挂载到宿主机,一切修改在宿主机操作,还可以避免配置丢失。
# docker exec -it my_nginx_2020 bash。进入/etc/nginx 查看文件列表,其中nginx.conf 配置nginx的全局基础属性配置。conf.d 存放访问路径的配置文件,本文不介绍nginx相关使用介绍,请自行搜索配置参数含义~~

[root@localhost ~]# docker exec it my_nginx_2022 bash
Error: No such container: it
[root@localhost ~]# docker exec -it my_nginx_2022 bash
root@fef1a18521fe:/# 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@fef1a18521fe:/#  /etc/nginx/
bash: /etc/nginx/: Is a directory
root@fef1a18521fe:/# cd /etc/nginx/
root@fef1a18521fe:/etc/nginx# ls
conf.d  fastcgi_params  mime.types  modules  nginx.conf  scgi_params  uwsgi_params

查看 nginx 配置

root@fef1a18521fe:/etc/nginx# more nginx.conf 

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}
root@fef1a18521fe:/etc/nginx# 
  • 我们首先需要在宿主机创建用于存放nginx日志、配置文件和相关静态资源的目录,并将其挂载到容器内对应路径。
  • 后续更新我们只需要更改宿主机目录下的配置文件或者静态文件就可以更新容器内资源,这样可以确保容器挂掉只需要重新启动一个容器挂载上数据去就完美无缺的还原,这也是容器轻量快速方便的原因。不只是nginx容器,其余的像mysql容器也一定要记得挂载/data数据文件,防止容器宕掉丢失数据。

退出容器,并复制配置文件

root@localhost /]# cd /usr/local/
[root@localhost local]# ls
bin  etc  games  include  lib  lib64  libexec  NJDK  sbin  share  src  XXFB
[root@localhost local]# mkdir www
[root@localhost local]# ls
bin  etc  games  include  lib  lib64  libexec  NJDK  sbin  share  src  www  XXFB
[root@localhost local]# cd ww
-bash: cd: ww: No such file or directory
[root@localhost local]# cd www/
[root@localhost www]# ls
[root@localhost www]# mkdir my_nginx
[root@localhost www]# cd my_nginx/
[root@localhost my_nginx]# 

开始复制容器内的配置

[root@localhost my_nginx]# docker cp my_nginx_2022:/etc/nginx/nginx.conf nginx.conf
[root@localhost my_nginx]# ls
nginx.conf
[root@localhost my_nginx]# docker cp my_nginx_2022:/etc/nginx/conf.d conf.d
[root@localhost my_nginx]# docker cp my_nginx_2022:/usr/share/nginx/html html
[root@localhost my_nginx]# docker cp my_nginx_2022:/var/log/nginx log
[root@localhost my_nginx]# ls
conf.d  html  log  nginx.conf
[root@localhost my_nginx]# 

需改配置文件,首先修改 nginx.conf,修改配置文件

  • 修改前:
[root@localhost my_nginx]# cat nginx.conf 

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}
  • 修改后:
[root@localhost my_nginx]# vi nginx.conf 
[root@localhost my_nginx]# cat nginx.conf 


user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    server {
        listen 80;
        server_name  www.roes.top;
            location / {
                root   /usr/share/nginx/html;
                index  index.html index.htm;
            }
        }

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}
[root@localhost my_nginx]# 
  • 把项目放入 html
  • 然后删除原来启动的nginx容器,重新运行一个挂载文件的容器
[root@localhost my_nginx]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                               NAMES
fef1a18521fe   nginx:latest   "/docker-entrypoint.…"   44 minutes ago   Up 44 minutes   0.0.0.0:81->80/tcp, :::81->80/tcp   my_nginx_2022
[root@localhost my_nginx]# docker stop my_nginx_2022
my_nginx_2022
[root@localhost my_nginx]# docker rm my_nginx_2022
my_nginx_2022
[root@localhost my_nginx]# 

docker run --name my_nginx_2022 -d -p 81:80 --restart=always -e TZ="Asia/Shanghai" -v /usr/local/www/my_nginx/html:/usr/share/nginx/html -v /usr/local/www/my_nginx/log:/var/log/nginx -v /usr/local/www/my_nginx/nginx.conf:/etc/nginx/nginx.conf:ro nginx:latest
参数解析:
-d 后台运行容器,并返回容器ID;
--name 为容器起一个容易区分且容易书写的名字
-p 映射宿主机端口到容器端口,宿主机端口:容器端口
--restart=always 机器重启时自动启动容器
-e       设定一些必须的环境变量。
          -e TZ="Asia/Shanghai" 设定时区为上海,强烈建议国内设定,否则容器内打印的所有日志时间都会差8小时。
-v       挂载宿主机文件到容器。
          -v /www/my_nginx/html:/usr/share/nginx/html  挂载html等静态文件
          -v /www/my_nginx/log:/var/log/nginx        挂载日志文件
          -v /www/my_nginx/nginx.conf:/etc/nginx/nginx.conf:ro  挂载配置文件,其中:ro表示只读

[root@localhost my_nginx]# pwd
/usr/local/www/my_nginx
[root@localhost my_nginx]# docker run --name my_nginx_2022 -d -p 81:80 --restart=always -e TZ="Asia/Shanghai" -v /usr/local/www/my_nginx/html:/usr/share/nginx/html -v /usr/local/www/my_nginx/log:/var/log/nginx -v /usr/local/www/my_nginx/nginx.conf:/etc/nginx/nginx.conf:ro nginx:latest
4194e389a5e140ef76ecbcd60f4eacfa26dbea10580468d33b1cb2230e7aadd2
[root@localhost my_nginx]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS                               NAMES
4194e389a5e1   nginx:latest   "/docker-entrypoint.…"   7 seconds ago   Up 6 seconds   0.0.0.0:81->80/tcp, :::81->80/tcp   my_nginx_2022
[root@localhost my_nginx]# 
  • 访问即可看到最新页面

推送到dockerhub 上()

  • 访问dockerhub
  • 注册新建,然后如下操作
[root@localhost my_nginx]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS                               NAMES
4194e389a5e1   nginx:latest   "/docker-entrypoint.…"   7 seconds ago   Up 6 seconds   0.0.0.0:81->80/tcp, :::81->80/tcp   my_nginx_2022
[root@localhost my_nginx]# docker stop my_nginx_2022
my_nginx_2022
[root@localhost my_nginx]# docker images
REPOSITORY                             TAG                IMAGE ID       CREATED        SIZE
dockernjdk                             latest             6650bd7b294a   27 hours ago   548MB
dockerdemo                             latest             a42c8ba3496d   2 days ago     212MB
nginx                                  latest             f2f70adc5d89   5 days ago     141MB
mcr.microsoft.com/dotnet/aspnet        3.1                f0220ee3f874   6 days ago     208MB
mcr.microsoft.com/dotnet/core/aspnet   2.1-stretch-slim   1a48139335f8   7 months ago   253MB
[root@localhost my_nginx]# docker tag nginx:latest 1426429456/my_nginx:v1
[root@localhost my_nginx]# docker images
REPOSITORY                             TAG                IMAGE ID       CREATED        SIZE
dockernjdk                             latest             6650bd7b294a   27 hours ago   548MB
dockerdemo                             latest             a42c8ba3496d   2 days ago     212MB
1426429456/my_nginx                    v1                 f2f70adc5d89   5 days ago     141MB
nginx                                  latest             f2f70adc5d89   5 days ago     141MB
mcr.microsoft.com/dotnet/aspnet        3.1                f0220ee3f874   6 days ago     208MB
mcr.microsoft.com/dotnet/core/aspnet   2.1-stretch-slim   1a48139335f8   7 months ago   253MB
[root@localhost my_nginx]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: 1426429456
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[root@localhost my_nginx]# docker push 1426429456/my_nginx:v1
The push refers to repository [docker.io/1426429456/my_nginx]
24037b645d66: Mounted from library/nginx 
d00147ef6763: Mounted from library/nginx 
2793e885dc34: Mounted from library/nginx 
8b8ecda1d12d: Mounted from library/nginx 
30c00b5281a1: Mounted from library/nginx 
3a626bb08c24: Mounted from library/nginx 
v1: digest: sha256:1a763cbd30ef4dbc7f8e3fa2e6670fd726f4bddb0ef58868a243c0cb8b35cde1 size: 1570
[root@localhost my_nginx]# 
  • 同理推送到阿里云镜像仓库
[root@localhost my_nginx]# docker login --username=139****6687 registry.cn-hangzhou.aliyuncs.com
Password: 
Error response from daemon: Get "https://registry.cn-hangzhou.aliyuncs.com/v2/": unauthorized: authentication required
[root@localhost my_nginx]# docker login --username=13952306687 registry.cn-hangzhou.aliyuncs.com
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[root@localhost my_nginx]# docker tag nginx registry.cn-hangzhou.aliyuncs.com/rina/my_nginx:v1
[root@localhost my_nginx]# docker images
REPOSITORY                                        TAG                IMAGE ID       CREATED        SIZE
dockernjdk                                        latest             6650bd7b294a   27 hours ago   548MB
dockerdemo                                        latest             a42c8ba3496d   2 days ago     212MB
registry.cn-hangzhou.aliyuncs.com/rina/my_nginx   v1                 f2f70adc5d89   5 days ago     141MB
1426429456/my_nginx                               v1                 f2f70adc5d89   5 days ago     141MB
nginx                                             latest             f2f70adc5d89   5 days ago     141MB
mcr.microsoft.com/dotnet/aspnet                   3.1                f0220ee3f874   6 days ago     208MB
mcr.microsoft.com/dotnet/core/aspnet              2.1-stretch-slim   1a48139335f8   7 months ago   253MB
[root@localhost my_nginx]# docker push registry.cn-hangzhou.aliyuncs.com/rina/my_nginx:v1
The push refers to repository [registry.cn-hangzhou.aliyuncs.com/rina/my_nginx]
24037b645d66: Pushed 
d00147ef6763: Pushed 
2793e885dc34: Pushed 
8b8ecda1d12d: Pushed 
30c00b5281a1: Pushed 
3a626bb08c24: Pushed 
v1: digest: sha256:1a763cbd30ef4dbc7f8e3fa2e6670fd726f4bddb0ef58868a243c0cb8b35cde1 size: 1570
[root@localhost my_nginx]# 

你可能感兴趣的:(Docker 安装 nginx)