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.

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

sudo mkdir -p firstnginx/test
sudo chmod -Rf firstnginx
cd firstnginx
echo this is the test1.html > test1.html
echo this is the test2.html > test/test2.html

2,Edit Dockerfile

编辑Dockerfile

sudo mkdir -p docker
sudo chmod -Rf docker
sudo vi docker/Dockerfile

Copy the the content below  to the dockerfile:

#拷贝如下内容至Dockerfile

FROM centos:
MAINTAINER Liping<tlping@.com> #add nginx repo and install nginx package
RUN bash -c "rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm && \
yum install -y nginx.x86_64" #copy your customized nginx.conf to the container
ADD ./docker/nginx.conf /etc/nginx/nginx.conf
#create nginx log folder
RUN mkdir -p /etc/nginx/logs #copy full project to nginx web site folder
ADD ./ /var/www/html/public/ # publish the container port
EXPOSE #copy your shell script to the container
COPY ./docker/my-init.sh /usr/bin/my-init.sh
#set execution access right
RUN chmod +x /usr/bin/my-init.sh #refresh the the container folder files
RUN bash -c 'touch /var/www/html/*' #execute the initializaton script
ENTRYPOINT ["my-init.sh"]

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

sudo vi docker/nginx.conf

Copy below content to the nginx.conf and Save.

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

user  nginx;
worker_processes 1; error_log /var/log/nginx/error.log warn;
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; server {
listen 80;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main;
set $root "/var/www/html/public";
root $root;
location / {
root $root;
index index.html index.htm index.php l.php; try_files $uri /index.php$uri; } error_log /var/www/html/err.txt error;
#error_page 404 /404.html; error_page 500 502 503 504 /50x.html;
location = /50x.html {
root "/var/www/html";
} location ~ \.php(.*)$ {
root $root;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php; fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params; } }
#this statement must be put as the last line
include /etc/nginx/conf.d/*.conf; }

4,edit my-init.sh file

编辑my-init.sh

sudo vi docker/my-init.sh

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

#!/bin/sh

echo 'start nginx message now'
service nginx start
echo 'finish'
read

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

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

5,build the dockerfile(编译Dockerfile)

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

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

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

7,

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. list 分批

    public class TestList { public static void main(String[] args){ List<Integer> list = new Array ...

  2. BZOJ3170 [Tjoi2013]松鼠聚会 切比雪夫距离 - 曼哈顿距离 - 前缀和

    BZOJ3170 题意: 有N个小松鼠,它们的家用一个点x,y表示,两个点的距离定义为:点(x,y)和它周围的8个点即上下左右四个点和对角的四个点,距离为1.现在N个松鼠要走到一个松鼠家去,求走过的最 ...

  3. light 1205 - Palindromic Numbers(数位dp)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1205 题解:这题作为一个数位dp,是需要咚咚脑子想想的.这个数位dp方程可能不 ...

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

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

  5. poj 2115 求线性同余方程 C Looooops(好理解欧几里德扩展定理怎么应用)

    C Looooops Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29061   Accepted: 8360 Descr ...

  6. Vert.x Web之Router

    Vert.x Web 是一系列用于基于 Vert.x 构建 Web 应用的构建模块. Vert.x Web 的大多数特性被实现为了处理器(Handler),因此您随时可以实现您自己的处理器.我们预计随 ...

  7. 树莓派4B 安装CentOS

    刚入手了一个树莓派4B替换掉旧的3B搭Nas.吐槽下3B的网卡和USB速度真的太慢. 虽然官方推荐的是Debina,由于习惯了CentOS不想增加学习成本,我还是决定用CentOS. 镜像下载地址:h ...

  8. #umn 来美国近一个月的简单见闻

    时光如梭,到美国已经快要一个月了,从最初12+4飞行的劳累,到一开始每天吃了上顿没下顿的担心,到后来开始上课的不适,现如今生活已经基本步入了正轨,每天上上课写写作业,去rec center打打球健健身 ...

  9. .Ajax(async异步与sync同步)

    异步,不会阻碍代码的执行,它会等待所有的同步代码执行完毕后,再执行输出自己的同步结果.(原生js中,只有定时器,DOM,ajax三个东西是异步的.) 同步,代码只会从上到下依次执行,只要一步出错,接下 ...

  10. Linux系统卡死后紧急处理

    前言:Linux系统卡死了的情况有很多,最常见的是系统负载过高导致的.还可以运行内存耗用极大的程序(如虚拟机),也会迅速提升系统负载.注意:不能再试图依赖任何图形界面的东西,如 Gnome的系统监视器 ...