9、Dockerfile实战-Nginx

上一节我们详解Dockerfile之后,现在来进行实战。我们通过docker build来进行镜像制作。

build有如下选项:

[[email protected] ~a]# docker build --help

Usage:  docker build [OPTIONS] PATH | URL | -

Build an image from a Dockerfile

Options:
      --add-host list           Add a custom host-to-IP mapping (host:ip)
      --build-arg list          Set build-time variables
      --cache-from strings      Images to consider as cache sources
      --cgroup-parent string    Optional parent cgroup for the container
      --compress                Compress the build context using gzip
      --cpu-period int          Limit the CPU CFS (Completely Fair Scheduler) period
      --cpu-quota int           Limit the CPU CFS (Completely Fair Scheduler) quota
  -c, --cpu-shares int          CPU shares (relative weight)
      --cpuset-cpus string      CPUs in which to allow execution (0-3, 0,1)
      --cpuset-mems string      MEMs in which to allow execution (0-3, 0,1)
      --disable-content-trust   Skip image verification (default true)
  -f, --file string             Name of the Dockerfile (Default is ‘PATH/Dockerfile‘)
      --force-rm                Always remove intermediate containers
      --iidfile string          Write the image ID to the file
      --isolation string        Container isolation technology
      --label list              Set metadata for an image
  -m, --memory bytes            Memory limit
      --memory-swap bytes       Swap limit equal to memory plus swap: ‘-1‘ to enable unlimited swap
      --network string          Set the networking mode for the RUN instructions during build (default "default")
      --no-cache                Do not use cache when building the image
      --pull                    Always attempt to pull a newer version of the image
  -q, --quiet                   Suppress the build output and print image ID on success
      --rm                      Remove intermediate containers after a successful build (default true)
      --security-opt strings    Security options
      --shm-size bytes          Size of /dev/shm
  -t, --tag list                Name and optionally a tag in the ‘name:tag‘ format
      --target string           Set the target build stage to build.
      --ulimit ulimit           Ulimit options (default [])
[[email protected] ~]#

  

主要参数有:

-t, --tag list  #给镜像打tag,比如nginx:v1
-f, --file string  # -f  Dockerfile文件名字,默认为当前路径下的Dockerfile

一、构建Nginx镜像

1、 Nginx安装步骤

  1. 安装依赖包
  2. 编译安装nginx三步骤:编译、make、make install
  3. 配置配置文件和环境变量

2、Dockerfile文件编写

FROM centos:7
MAINTAINER QUNXUE
RUN yum install -y gcc gcc-c++ make     openssl-devel pcre-devel gd-devel     iproute net-tools telnet wget curl &&     yum clean all &&     rm -rf /var/cache/yum/*
RUN wget http://nginx.org/download/nginx-1.15.5.tar.gz &&     tar zxf nginx-1.15.5.tar.gz &&     cd nginx-1.15.5 &&    ./configure --prefix=/usr/local/nginx     --with-http_ssl_module     --with-http_stub_status_module &&     make -j 4 && make install &&     rm -rf /usr/local/nginx/html/* &&     echo "ok" >> /usr/local/nginx/html/status.html &&     cd / && rm -rf nginx-1.15.5* &&     ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

ENV PATH $PATH:/usr/local/nginx/sbin
COPY nginx.conf /usr/local/nginx/conf/nginx.conf
WORKDIR /usr/local/nginx
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

  

3、注意事项及dockerfile编写最佳实践

a、尽量让镜像文件更小

清理残留文件,比如build完成后,要删掉源码包、yum缓存等。

b、尽量减少Dockerfile指令

因为我们知道,一个Dockerfile指令,就是一层镜像,我们使用shell里面的&&符号进行拼接成一行,让RUN指令尽可能少。

c、在测试中编写Dockerfile

我们随便启动一个容器:docker run -it  centos,进入容器后,就可以对我们写的Dockerfile指令进行逐行执行

d、Dockerfile常用指令

基本上按照表格中的顺序进行编写

4、构建基础镜像(nginx)

docker build -t custom_nginx:v1 -f dockerfile-nginx .

  注意:命令最后面一个点,用于指定docker build的上下文。

可以看到,最后我们构建成功了。

我们可以基于此镜像作为基础镜像进行容器构建:

[[email protected] src]# docker run -itd -h web001 -p 8889:80 custom_nginx:v1
2ad7070295ed0d4cc97319696176e3bfaf75dde9ccc54e3990e251907ce16ebd

  

尝试访问我们自己写的主页,status.html:

说明我们的启动的docker容器是预想中的效果,very good!

5、基于基础镜像测试

我们制作了基础镜像,此时,我们可以基于此基础镜像进行测试环境发布了。

编写Dockerfile

FROM custom_nginx:v1
COPY index.html  /usr/local/nginx/html

  

这里我们把index.html文件作为整个测试环境的内容,当然,测试环境肯定不止一个文件,我们这里用于测试。

echo "This is My First Project,Welcome! Guy." >index.html

  

基于基础镜像进行新镜像构建:

基于新镜像构建容器:

[[email protected] src]# docker run -itd -h custom_nginxv2 -p 1234:80 custom_nginx:v2
155225793ee06151601047152a1fe25f02691001de0b85d3d017c5d4f657ad7f
[[email protected] src]#

  

测试:访问宿主机的1234端口

一切都是期望的样子!

原文地址:https://www.cnblogs.com/skyflask/p/10068420.html

时间: 2024-08-14 00:23:00

9、Dockerfile实战-Nginx的相关文章

DockerFile实战(二):DockerFile编写要求与基本风格

之前分享了一个Nginx的Dockerfile实战文章,但这是基于原有镜像的基础上去添加修改的,那么本文 来详细讲解一下,如何从ubuntu镜像生成一个Nginx镜像 Step1: #最开始,还是需要先搜索一个可用的镜像 $docker search ubuntu #在这里,可以看到许多的相关镜像,但这里我们安装第一个就可以,可以看STARS评级很高的那个 $docker pull ubuntu #如果网络情况够好的话,稍等片刻,一个可用的镜像就下载完毕了 Step2: 下载完镜像以后,我们需要

实战nginx前端反代mogfilefs及负载均衡

实战nginx前端反代mogfilefs及负载均衡 =============================================================================== 实验描述: 使用Nginx代理请求至tackers,实现通过键就可以访问到文件: 在配置nginx做反代将用户的请求调度至后端的MogileFS,此实验依赖于nginx-mogilefs-module模块,需在编译时加上此模块. 实验环境: 再准备一台CentOS 7的虚拟主机,来作为前

LNMP架构应用实战——Nginx配置虚拟主机

LNMP架构应用实战--Nginx配置虚拟主机        前面介绍了nginx服务的安装与配置文件,今天介绍下它的另一种实用配置--"虚拟主机",每个虚拟主机可以是一个独立的网站,可以具有独立的域名,同一台服务器上的不同的虚拟主机之间是独立的,用户访问不同虚拟主机如同访问不同的服务器一样,因此它不需要为一个单独的WEB站点提供单独一个nginx服务器和一个单独的nginx进程 1.nginx虚拟主机简单介绍 同apache服务一样,它也有三种不同的虚拟主机,基于域名的虚拟主机.基于

LNMP架构应用实战——Nginx服务配置文件介绍

LNMP架构应用实战--Nginx服务配置文件介绍 nginx的配置文件比较简单,但功能相当强大,可以自由灵活的进行相关配置,因此,还是了解下其配置文件的一此信息 1.Nginx服务目录结构介绍 安装完成后,在安装路径下就会有Nginx目录信息 [[email protected] application]# tree nginx nginx +-- client_body_temp +-- conf          #nginx服务配置文件目录 |   +-- fastcgi.conf  

实战Nginx:Nginx服务器的安装与配置

----------------------------------------------------------------------------------------------- Nginx下载地址:http://www.nginx.net [email protected] 黑眼诗人 <www.chenwei.ws>---------------------- Nginx在Window下的安装 => '开始' - '运行' - 'cmd',执行如下DOS命令 d: cd d

8.Docker之使用dockerfile创建nginx镜像

一.前言 看了很多人的dockerfile,都是长篇大论,解释的又很少,对于初学者来说根本不知道指令的意思,哪怕知道指令的意思,也不知道指令后面配置的一大串东西来自于哪里,而这一大串又无需去记忆,例如: 开头的RUN指令都能根据dockerfile的指令说明可以知道该指令是干嘛用的,但是后面这一串这么长的东西又是来自于哪里? 所以在编写dockerfile之前,你必须懂的nginx(该文章是基于dockerfile创建nginx镜像)在linux上的安装流程,否则,需要安装什么依赖等都不知道,就

DockerFile实战(一):定制一个简单的nginx服务

第一步,下载nginx的镜像 docker pull nginx 第二步,部分修改 #This is a nginx dockerfile #Source image FROM    nginx #Author MAINTAINER      LeonLong from dockerpool.conf  [email protected] #mount point VOLUME /opt/nginx/conf VOLUME /var/log/nginx VOLUME /opt/nginx/www

实战Nginx负载均衡高冗余高可用WEB架构

最近公司主力网站之一改版完成终于上线了,牵扯了我大半年的时间,现在终于有时间坐下来写点东西,总结沉淀一下自己的技术心得.此次,根据服务器的数量和质量,我采用负载均衡高冗余的架构,考虑单点故障,WEB也抛弃了apache,而使用的是nginx,数据库还是使用主.从架构.该架构目前承载80W的PV,没有大的压力. 这里简单谈一下web的选择疑问,是使用nginx还是apache,很多朋友在规划网站的时候都出现难以选择的问题,甚至有朋友在建设初用apache后期改成nginx.接下来我说一下我的规划选

使用Dockerfile创建nginx服务容器镜像

1.下载nginx配置文件 wget http://www.apelearn.com/study_v2/.nginx_conf 2. vim Dockerfile #本地有centos的镜像,可以直接指定镜像名称:如果需要从docker官网下载其他版本镜像,请自行修改版本号 FROM centos:6.8 MAINTAINER wyman [email protected] #根据实际需要选择安装 RUN yum install -y pcre-devel wget net-tools gcc