9、Dockerfile实战-Nginx
上一节我们详解Dockerfile之后,现在来进行实战。我们通过docker build来进行镜像制作。
build有如下选项:
[root@localhost ~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 [])
[root@localhost ~]#
主要参数有:
-t, --tag list #给镜像打tag,比如nginx:v1
-f, --file string # -f Dockerfile文件名字,默认为当前路径下的Dockerfile

一、构建Nginx镜像
1、 Nginx安装步骤
- 安装依赖包
- 编译安装nginx三步骤:编译、make、make install
- 配置配置文件和环境变量
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的上下文,.为当前目录,就是在build镜像时,如果有涉及到需要拷贝文件之类的,都是从当前目录拷贝。


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

我们可以基于此镜像作为基础镜像进行容器构建:
[root@localhost 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
基于基础镜像进行新镜像构建:

基于新镜像构建容器:
[root@localhost src]# docker run -itd -h custom_nginxv2 -p 1234:80 custom_nginx:v2
155225793ee06151601047152a1fe25f02691001de0b85d3d017c5d4f657ad7f
[root@localhost src]#
测试:访问宿主机的1234端口

一切都是期望的样子!
9、Dockerfile实战-Nginx的更多相关文章
- docker Dockerfile实战
目录 Dockerfile实战 基础pm2 Dockerfile keymetrics/pm2:8-alpine keymetrics/pm2:12-alpine pm2 node Dockerfil ...
- 《实战Nginx》读书笔记
最近今天读了一本书叫做<实战Nginx:取代Apache的高性能Web服务器>,看后对Nginx 了解了不少.但是还有很多地方不是很了解.不过此书可以作为一本参考手册来使用,里面的讲解很详 ...
- 【摘自张宴的"实战:Nginx"】nginx配置
user nobody;worker_processes 2; #error_log logs/error.log;error_log logs/error.log notice;#error_log ...
- 使用dockerfile构建nginx镜像
使用dockerfile构建nginx镜像 docker构建镜像的方法: commit.dockerfile 1.使用commit来构建镜像: commit是基于原有镜像基础上构建的镜像,使用此方 ...
- Dockerfile构建nginx镜像
Dockerfile构建nginx镜像 [root@bogon ~]# mkdir /opt/docker-file [root@bogon ~]# cd /opt/docker-file/ [roo ...
- Linux centosVMware 自动化运维Ansible介绍、Ansible安装、远程执行命令、拷贝文件或者目录、远程执行脚本、管理任务计划、安装rpm包/管理服务、 playbook的使用、 playbook中的循环、 playbook中的条件判断、 playbook中的handlers、playbook实战-nginx安装、管理配置文件
一.Ansible介绍 不需要安装客户端,通过sshd去通信 基于模块工作,模块可以由任何语言开发 不仅支持命令行使用模块,也支持编写yaml格式的playbook,易于编写和阅读 安装十分简单,ce ...
- Docker Alpine Dockerfile 安装nginx,最小镜像
Docker Alpine Dockerfile 安装nginx,最小镜像 FROM alpine MAINTAINER will ## 将alpine-linux:apk的安装源改为国内镜像 RUN ...
- Dockerfile之nginx(六)
一.Dokcerfile的基本指令 1)From 指定构建镜像的基础镜像 2)MAINTAINER 指定镜像的作者 3)RUN 使用前一条指令创建的镜像生产容器,并在容器中执行命令,执行结束后会自 ...
- Docker数据持久化及实战(Nginx+Spring Boot项目+MySQL)
Docker数据持久化: Volume: (1)创建mysql数据库的container docker run -d --name mysql01 -e MYSQL_ROOT_PASSWORD= my ...
随机推荐
- bufferIO,Direct io,mmap, ZeroCopy
1 bufferIO(传统IO),Direct io(干掉内核cache),mmap(大数据映射),zeroCopy(网络IO) 2 linux 5种IO 3NIO 相关知识 这张图展示了mmap() ...
- 万恶的浏览器缓存 Vuex state里面的成员改名后浏览器不会马上更新
今天在用Vuex的时候,在state里面加了个名叫rootUrl的属性 但是怎么都取不到值,重新启动程序,ctrl+f5浏览器刷新都不行,纠结了大半上午,于是用console.log(store.ge ...
- 表迁移工具的选型-复制ibd的方法-传输表空间
1.1. 场景 有的时候开放人员自己的库需要帮忙导一些数据,但是表的数据量又很大.虽然说使用mysqldump或mysqlpump也可以导.但是这耗时需要比较久. 记得之前建议开放人员可以直接使用na ...
- Android中两个Activity之间简单通信
在Android中,一个界面被称为一个activity,在两个界面之间通信,采用的是使用一个中间传话者(即Intent类)的模式,而不是直接通信. 下面演示如何实现两个activity之间的通信. 信 ...
- 【转】Java学习---算法那些事
[更多参考] LeetCode算法 每日一题 1: Two Sum ----> 更多参考[今日头条--松鼠游学] 史上最全的五大算法总结 Java学习---7大经典的排序算法总结实现 程序员都应 ...
- libcurl.dll 7.60.0静态库包含openssl/zlib
最近做个QT的小程序需要使用LIBCURL支持HTTPS,结果查资料发现官方默认的是不支持的,需要手动重新编译,编译的时候加入SSL支持. 看着就好麻烦的样子, 找了一圈,还真有现成的,但是现在写程序 ...
- 1.js基础(以通俗易懂的语言解释JavaScript)
1.JavaScript组成: ECMAScript: 解释器.翻译 -->几乎没有兼容问题 DOM: Document Object Model -->有一些操作不兼容 BOM: Bro ...
- UML学生成绩管理系统需求分析
学生成绩管理系统工作室高校教育工作的一项重要内容.教务管理工作是指学校管理人员按照一定的教育方针,运用先进的管理手段,组织.协调.指挥并指导各用户活动,一边高效率.高质量地完成各项教学任务,完成国家所 ...
- Volley源码分析(五)Volley源码总结篇
volley关键的代码这里已经分析完了,下面梳理一下完整的Volley流程 Volley的使用从构造Request对象开始,Volley默认提供了四种request的实现,StringRequest, ...
- gcd以及exgcd入门讲解
gcd就是最大公约数,gcd(x, y)一般用(x, y)表示.与此相对的是lcm,最小公倍数,lcm(x, y)一般用[x, y]表示. 人人都知道:lcm(x, y) = x * y / gcd( ...