• Docker 安装 Nginx容器 配置以及重新生成镜像


    目录

    1、查看是否存在nginx镜像

    2、搜索可用的nginx镜像,下载nginx镜像

    3、创建并启动nginx容器

    4、进入容器,对相关文件进行配置

    5、更新镜像

    6、将由容器生成的镜像push到镜像仓库docker hub


    基本思路:先下载Nginx镜像,然后运行一个Nginx容器,在容器中配置相关参数,最后把配置好的容器制作成一个镜像,后期发布到服务器上可以省去重复配置。

    1、查看是否存在nginx镜像

    docker images

    发现还没有下载过nginx镜像

    2、搜索可用的nginx镜像,下载nginx镜像

    首先查询可用的nginx镜像:

    docker search nginx

    就下载第一个nginx:

    docker pull nginx

     

     不指定版本号,默认就是最新版本

    3、创建并启动nginx容器

    docker run --name mynginx -d -p 8989:80 nginx

    # --name 给容器起一个名字

    # -d 在后台运行

    # -p 8989:80 把容器内的80端口映射到宿主机的8989端口

    查看容器运行状态:

    docker ps

     访问验证:使用外部端口8989可以访问该容器nginx

    4、进入容器,对相关文件进行配置

    进入容器:

    docker exec -it mynginx /bin/bash 

    # -i: 交互式操作。

    # -t: 终端。

    # mynginx : nginx镜像。

    # /bin/bash:放在镜像名后的是命令,这里我们希望有个交互式 Shell,因此用的是 /bin/bash。

    1. C:\Users\Administrator>docker exec -it mynginx /bin/bash
    2. root@1eb487ead85e:/# ls
    3. bin dev docker-entrypoint.sh home lib64 mnt proc run srv tmp var
    4. boot docker-entrypoint.d etc lib media opt root sbin sys usr
    5. root@1eb487ead85e:/# whereis nginx
    6. nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
    7. root@1eb487ead85e:/# cd /etc/nginx
    8. root@1eb487ead85e:/etc/nginx# ls
    9. conf.d fastcgi_params mime.types modules nginx.conf scgi_params uwsgi_params
    10. root@1eb487ead85e:/etc/nginx# cd conf.d
    11. root@1eb487ead85e:/etc/nginx/conf.d# ls
    12. default.conf
    13. root@1eb487ead85e:/etc/nginx/conf.d# cat default.conf
    14. server {
    15. listen 80;
    16. listen [::]:80;
    17. server_name localhost;
    18. #access_log /var/log/nginx/host.access.log main;
    19. location / {
    20. root /usr/share/nginx/html;
    21. index index.html index.htm;
    22. }
    23. #error_page 404 /404.html;
    24. # redirect server error pages to the static page /50x.html
    25. #
    26. error_page 500 502 503 504 /50x.html;
    27. location = /50x.html {
    28. root /usr/share/nginx/html;
    29. }
    30. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    31. #
    32. #location ~ \.php$ {
    33. # proxy_pass http://127.0.0.1;
    34. #}
    35. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    36. #
    37. #location ~ \.php$ {
    38. # root html;
    39. # fastcgi_pass 127.0.0.1:9000;
    40. # fastcgi_index index.php;
    41. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    42. # include fastcgi_params;
    43. #}
    44. # deny access to .htaccess files, if Apache's document root
    45. # concurs with nginx's one
    46. #
    47. #location ~ /\.ht {
    48. # deny all;
    49. #}
    50. }
    51. root@1eb487ead85e:/etc/nginx/conf.d# cd /usr/share/nginx/html
    52. root@1eb487ead85e:/usr/share/nginx/html# ls
    53. 50x.html index.html

    可以看到nginx的默认配置是在/etc/nginx/conf.d/default.conf配置文件里,通过配置文件里

    location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }

    可以知道nginx的html文件目录,这样就可以把我们自己的代码拷贝到html文件夹下:

    docker cp d:/html 1eb487ead85e:/usr/share/nginx

    # docker cp 本机文件路径 容器id:容器内的路径

    5、更新镜像

    我们对nginx进行配置后,需要使用当前容器生成一个新的镜像,

    我们可以通过命令 docker commit 来提交容器副本。

    docker commit -m="更新配置" -a=lwpoor 1eb487ead85e lwpoor/nginx:1.0

    # -m: 提交的描述信息

    # -a: 指定镜像作者

    # 1eb487ead85e :容器 ID

    # lwpoor/nginx:1.0: 指定要创建的目标镜像名

    1. C:\Users\Administrator>docker commit -m="更新配置" -a=lwpoor 1eb487ead85e lwpoor/nginx:1.0
    2. sha256:110f8f64ca1ea47ba61b3c773b3fe5a07c13492a17e6378455dc6d254f17177e
    3. C:\Users\Administrator>docker images
    4. REPOSITORY TAG IMAGE ID CREATED SIZE
    5. lwpoor/nginx 1.0 110f8f64ca1e 9 seconds ago 224MB

    6、将由容器生成的镜像push到镜像仓库docker hub

    首先需要登录 docker hub:

    docker login 

    推送到镜像仓库:

    docker push lwpoor/nginx:1.0

    1. C:\Users\Administrator>docker login
    2. Authenticating with existing credentials...
    3. Login Succeeded
    4. Logging in with your password grants your terminal complete access to your account.
    5. For better security, log in with a limited-privilege personal access token. Learn more at https://docs.docker.com/go/access-tokens/
    6. C:\Users\Administrator>docker push lwpoor/nginx:1.0
    7. The push refers to repository [docker.io/lwpoor/nginx]
    8. c269977a08d5: Pushed
    9. d874fd2bc83b: Mounted from library/nginx
    10. 32ce5f6a5106: Mounted from library/nginx
    11. f1db227348d0: Mounted from library/nginx
    12. b8d6e692a25e: Mounted from library/nginx
    13. e379e8aedd4d: Mounted from library/nginx
    14. 2edcec3590a4: Mounted from library/nginx
    15. 1.0: digest: sha256:fe4d4e8f68cace0f19cc7d070c84030487b31a585cdcd5969afe6f8848f80aca size: 1782

    可以看到已经推送到镜像仓库了:

  • 相关阅读:
    2023年中国反射膜产量及市场规模分析:随着太阳能产业快速发展,规模持续扩大[图]
    ctfshow web入门部分题目 (更新中)
    PHP基于thinkphp的网上书店管理系统#毕业设计
    Doris入门到精通-阶段一(简介&安装&使用)
    统计信号处理基础 习题解答6-11
    python curl2pyreqs 生成接口脚本
    PyTorch0.4.1 GPU版本安装完整步骤(Anaconda3 + Python3.6 + gpu版本,含CUDA9.2安装、环境配置)
    es的nested查询
    TalkingData数据统计:洞察数字世界的关键工具
    汇编逆向-入门
  • 原文地址:https://blog.csdn.net/lwpoor123/article/details/127750510