1,从这里你将学到编写Dockerfile的4个重要指令RUN,EXPOSE,ADD,ENTRYPOINT
2,在Dockerfile中编写拷贝文件至容器的方法
3, 安装一个nginx server,并修改默认的站点路径

环境:CentOS7,Docker CE

1,Use below commands to prepare folders and testing files.

使用如下指令准备文件夹和测试文件

  1. sudo mkdir -p firstnginx/test
  2. sudo chmod -Rf firstnginx
  3. cd firstnginx
  4. echo this is the test1.html > test1.html
  5. echo this is the test2.html > test/test2.html

2,Edit Dockerfile

编辑Dockerfile

  1. sudo mkdir -p docker
  2. sudo chmod -Rf docker
  3. sudo vi docker/Dockerfile

Copy the the content below  to the dockerfile:

#拷贝如下内容至Dockerfile

  1. FROM centos:
  2. MAINTAINER Liping<tlping@.com>
  3.  
  4. #add nginx repo and install nginx package
  5. RUN bash -c "rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm && \
  6. yum install -y nginx.x86_64"
  7.  
  8. #copy your customized nginx.conf to the container
  9. ADD ./docker/nginx.conf /etc/nginx/nginx.conf
  10. #create nginx log folder
  11. RUN mkdir -p /etc/nginx/logs
  12.  
  13. #copy full project to nginx web site folder
  14. ADD ./ /var/www/html/public/
  15.  
  16. # publish the container port
  17. EXPOSE
  18.  
  19. #copy your shell script to the container
  20. COPY ./docker/my-init.sh /usr/bin/my-init.sh
  21. #set execution access right
  22. RUN chmod +x /usr/bin/my-init.sh
  23.  
  24. #refresh the the container folder files
  25. RUN bash -c 'touch /var/www/html/*'
  26.  
  27. #execute the initializaton script
  28. ENTRYPOINT ["my-init.sh"]

3,Edit nginx.conf file ,we need change the default website location of nginxserver

  1. sudo vi docker/nginx.conf

Copy below content to the nginx.conf and Save.

#拷贝如下内容至nginx.conf 并且保存

  1. user nginx;
  2. worker_processes 1;
  3.  
  4. error_log /var/log/nginx/error.log warn;
  5. pid /var/run/nginx.pid;
  6.  
  7. events {
  8. worker_connections 1024;
  9. }
  10.  
  11. http {
  12. include /etc/nginx/mime.types;
  13. default_type application/octet-stream;
  14.  
  15. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  16. '$status $body_bytes_sent "$http_referer" '
  17. '"$http_user_agent" "$http_x_forwarded_for"';
  18.  
  19. access_log /var/log/nginx/access.log main;
  20.  
  21. sendfile on;
  22. #tcp_nopush on;
  23.  
  24. keepalive_timeout 65;
  25.  
  26. #gzip on;
  27.  
  28. server {
  29. listen 80;
  30. server_name localhost;
  31.  
  32. #charset koi8-r;
  33.  
  34. #access_log logs/host.access.log main;
  35. set $root "/var/www/html/public";
  36. root $root;
  37. location / {
  38. root $root;
  39. index index.html index.htm index.php l.php;
  40.  
  41. try_files $uri /index.php$uri;
  42.  
  43. }
  44.  
  45. error_log /var/www/html/err.txt error;
  46. #error_page 404 /404.html;
  47.  
  48. error_page 500 502 503 504 /50x.html;
  49. location = /50x.html {
  50. root "/var/www/html";
  51. }
  52.  
  53. location ~ \.php(.*)$ {
  54. root $root;
  55. fastcgi_pass 127.0.0.1:9000;
  56. fastcgi_index index.php;
  57.  
  58. fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
  59. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  60. fastcgi_param PATH_INFO $fastcgi_path_info;
  61. fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
  62. include fastcgi_params;
  63.  
  64. }
  65.  
  66. }
  67. #this statement must be put as the last line
  68. include /etc/nginx/conf.d/*.conf;
  69.  
  70. }

4,edit my-init.sh file

编辑my-init.sh

  1. sudo vi docker/my-init.sh

copy the content below to the file and save(拷贝如下内容至文件并保存)

  1. #!/bin/sh
  2.  
  3. echo 'start nginx message now'
  4. service nginx start
  5. echo 'finish'
  6. read

after you finish all above ,your current folder structure should be like this,

在你完成所有以上操作后,当前文件夹结构如下,

5,build the dockerfile(编译Dockerfile)

  1. sudo docker build -t firstnginx -f docker/Dockerfile .

6,run the image to start a container(运行Docker镜像以启动容器)

  1. sudo docker run -td --name first_nginx -p : firstnginx

7,

  1. sudo docker ps -a | grep first_nginx

8,

Verfiy the test1.html and  test/test2.html

Open browser

http://localhost:8080/test1.html

http://localhost:8080/test/test2.html

here use your docker host ip address to replace sys.beserp.com

打开浏览器验证test1.html, test/test2.html文件是否可以被正常访问

The nginx container is working!!

Nginx 运行起来了!

文章版权归属千分网络科技(重庆)有限公司

如何制作一个Nginx镜像的更多相关文章

  1. 【URLOS应用开发基础】10分钟制作一个nginx静态网站环境应用

    URLOS开发者功能已上线有一段时间了,目前通过部分开发者的使用体验来看,不得不说URLOS在服务器软件开发效率方面确实有着得天独厚的优势,凭借docker容器技术与其良好的应用生态环境,URLOS必 ...

  2. 制作一个docker镜像:mysql-8-x64-linux

    因为个人学习需要,为软件系统的虚拟容器化,以下将mysql制作为docker镜像,并记录下详细步骤. 欢迎大家学习交流和转载,同时写作不易,如果各位觉得不错,请点赞支持. 备注:以下代码和文章,欢迎复 ...

  3. Docker下制作一个容器镜像

    操作过程描述: (1)先基于centos的镜像启动一个centos容器 (2)在这个容器中安装nginx (3)然后把这个已经安装了nginx的容器制作成一个docker的镜像 操作:docker c ...

  4. vagrant package制作一个box镜像

    1.进入virtualbox安装目录,查看虚拟机的名称(第一列为虚拟机名称) # vboxmanage list vms 2. vagrant  package 打包命令 vagrant packag ...

  5. 使用Dockerfile创建一个tomcat镜像,并运行一个简单war包

    docker已经看了有一段时间了,对镜像和容器也有了一个大致了解,参考书上的例子制作一个tomcat镜像,并简单运行一个HelloWorld.war 1.首先下载linux环境的tomcat和jdk, ...

  6. Docker: docker 启动一个Nginx容器

    本文演示从官方镜像仓库拉取一个nginx镜像并启动docker run -d –p 8800:80 nginx (同一个镜像,可以启动N个容器, 比如说,一个nginx服务,可以在这个docker主机 ...

  7. docker学习之路-nginx镜像(翻译)

    本篇来自https://hub.docker.com/_/nginx/?tab=description 它是docker hub上nginx的官方网站,上面有关于nginx的使用描述等.从这里你可以找 ...

  8. 最简单的dockerfile使用教程 - 创建一个支持SSL的Nginx镜像

    什么是dockerfile?简单的说就是一个文本格式的脚本文件,其内包含了一条条的指令(Instruction),每一条指令负责描述镜像的当前层(Layer)如何构建. 下面通过一个具体的例子来学习d ...

  9. Docker 制作Nginx镜像

    参考文章:https://www.jianshu.com/p/dc4cd0547d1e 镜像的制作方式有两种,一种是下载别人的镜像之后再制作成自己的镜像,一种是从头开始制作自己的镜像 第一种,下载别人 ...

随机推荐

  1. iOS仿写下厨房

    把之前简书的博客搬到博客园了,还是放在一个地方看着舒服. 先看一下做的效果,是不是还不错?(可以看一下早餐那块的轮播,上面盖着一个都是点点的图片,但是它不是和轮播一起滚动的,是盖在轮播上面的,需要在那 ...

  2. .net core api服务端跨域配置

    第1步:添加包引用(.net core 2.2 已自带此包,可跳过此步骤) Install-Package Microsoft.AspNetCore.Cors 第2步:在Startup.cs文件的Co ...

  3. 小白专场-多项式乘法与加法运算-c语言实现

    目录 一.题意理解 二.求解思路 三.多项式的表示 3.1 数组 3.2 链表 四.程序框架搭建 五.如何读入多项式 六.如何将两个多项式相加 七.如何将两个多项式相乘 八.如何将多项式输出 一.题意 ...

  4. 【selenium】- 自动化测试必备工具FireBug&FirePath

    本文由小编根据慕课网视频亲自整理,转载请注明出处和作者. 1. FireBug FireBug的安装: 如果使用Firefox浏览器的话,推荐使用较低版本,比如27-32.否则会报错. 点击右上角的菜 ...

  5. POJ-3686 The Windy's KM算法 拆点题

    参考:https://blog.csdn.net/sr_19930829/article/details/40680053 题意: 有n个订单,m个工厂,第i个订单在第j个工厂生产的时间为t[i][j ...

  6. CF - 1106 E Lunar New Year and Red Envelopes DP

    题目传送门 题解: 首先要处理出每个时间点会选择哪一个线段. 对于这个问题,可以用multiset去维护信息. 当时间线开始的时候,往mutiset里面插入这个信息,当时间线结束的时候,删除这个信息. ...

  7. 图论之拓扑排序 poj 2367 Genealogical tree

    题目链接 http://poj.org/problem?id=2367 题意就是给定一系列关系,按这些关系拓扑排序. #include<cstdio> #include<cstrin ...

  8. 牛客小白月赛6 E 对弈 思维

    链接:https://www.nowcoder.com/acm/contest/136/E来源:牛客网 题目描述 善弈者谋势,不善弈者谋子.                               ...

  9. [USACO07OCT]障碍路线 & yzoj P1130 拐弯 题解

    题意 给出n* n 的图,A为起点,B为终点,* 为障碍,.可以行走,问最少需要拐90度的弯多少次,无法到达输出-1. 解析 思路:构造N * M * 4个点,即将原图的每个点分裂成4个点.其中点(i ...

  10. webstrom 内存溢出,软件崩溃卡死解决的方法

    今天用gulp搭建了一个工程,准备做一个体育h5的项目,其中需要用到sass代码压缩,加版本号等功能. gulpfile.js和package.json都是已经写好的.我用CMD命令窗口cnpm安装n ...