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 上()

[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]# 
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容