Dockerfile - Docker In Practice

在Docker中,创建镜像有4中标准方法
  1. docker的手工命令,docker commit 一个容易创建
  2. Dockerfile,这次的主题
  3. Dockerfile及配置管理工具,与Dockerfile相同,但是将构建的控制权交给配置管理工具
  4. 在空白的镜像导入tar文件
Dockerfile常用命令

FROM - 定义基础镜像
MAINTAINER - 声明维护人员(可以偷懒不写)
RUN - 跑CMD命令
WORKDIR - 定义下面命令的工作目录
EXPOSE - 声明暴露端口,
CMD - 启动镜像时运行的命令
ENTRYPOINT - 入口点,和CMD很像,具体见下面解析
COPY - 将本地文件复制到镜像中
ADD - 类似COPY,但是会自动将tar文件解压,也可以跟url,会下载url的内容,但如果url的内容是压缩文件它是不会自动解压的
VOLUME - 声明卷
ENV - 设置环境变量

简单实例 - todo APP
FROM node  # 定义基础镜像
MAINTAINER yourselfmain@mail.com  # 声明维护人员(可以偷懒不写)
RUN git clone -q https://github.com/docker-in-practice/todo.git  # 相当于在命令行跑命令,没啥区别
WORKDIR todo # 定义命令的工作目录
RUN npm install > /dev/null
EXPOSE 8000  # 暴露8000端口,用来给使用者做提示的,运行该镜像是要暴露8000端口,映射到宿主机上
CMD ["npm", "start"]  # 启动镜像的命令

构建todo镜像并使用

docker build -t todo .
docker run -p 8000:8000 todo

在浏览器上访问localhost:8000
todo app

ENTRYPOINT实例 - 清理log的工具
FROM ubuntu:14.04
ADD clean_log /usr/bin/clean_log # 将clean_log脚本拷贝到镜像中
RUN chmod +x /usr/bin/clean_log
ENTRYPOINT ["/usr/bin/clean_log"] # 相当于在shell运行clean_log脚本
CMD ["7"]  # 默认传参数“7” 给clean_log,可以在启动镜像的时候将其替换

其中clean_log脚本如下

#!/bin/bash
echo "Cleaning logs over $1 days old"
find /log_dir -ctime "$1" -name '*log' -exec rm {} \;

构建镜像并使用

docker build -t clean_log .
docker run --rm -v /tmp/log_dir:/log_dir clean_log 365

命令解析:
--rm 会在容器停止后删除
-v /tmp/log_dir:/log_dir 将宿主机的tmp/log_dir映射到容器里的/log_dir中,这样清理日志容器来删除容器里的/log_dir下的目录时相当于清理宿主机的/tmp/log_dir
365指删除一年的日志,即替换里默认的7天

官方资料

https://docs.docker.com/develop/develop-images/dockerfile_best-practices/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
In September 2013, while browsing Hacker News, I stumbled across an article in Wired about a new technology called “Docker.” 1 As I read it, I became increasingly excited as I realized Docker’s revolutionary potential. The company I’d worked at for over a decade was struggling to deliver software quickly enough. Provisioning environments was a costly, time-consuming, manual, and inelegant affair. Continuous integration was barely existent, and setting up devel- opment environments was an exercise in patience. As my job title included the words “DevOps Manager,” I was peculiarly motivated to solve these problems! I recruited a couple of motivated coworkers (one of them now my coauthor) via a company mailing list, and together our skunkworks team labored to turn a beta tool into a business advantage, reducing the high costs of VM s and enabling new ways of thinking about building and deploying software. We even built and open sourced an automation tool (ShutIt) to suit our organization’s delivery needs. Docker gave us a packaged and maintained tool that solved many problems that would have been effectively insuperable had we taken it upon ourselves to solve them. This was open source at its best, empowering us to take on a challenge using our spare time, overcoming technical debt, and learning lessons daily. Lessons not only about Docker, but about continuous integration, continuous delivery, packaging, automa- tion, and how people respond to speedy and disruptive technological change. 1 http://www.wired.com/2013/09/docker/ PREFACE xviii For us, Docker is a remarkably broad tool. Wherever you run software using Linux, Docker can impact it. This makes writing a book on the subject challenging, because the landscape is as broad as software itself. The task is made more onerous by the extraordinary rate at which the Docker ecosystem is producing solutions to meet the needs that emerge from such a fundamental change in software production. Over time, the shape of problems and solutions became familiar to us, and in this book we’ve endeavored to pass on this experience. This will enable you to figure out solu- tions to your particular technical and business constraints. When giving talks at meetups, we’re struck by how quickly Docker has become effective within organizations willing to embrace it. This book mirrors how we used Docker, going from our desktops, through the DevOps pipeline, and all the way to production. As a consequence, this book is sometimes unorthodox, but as engineers we believe that purity must sometimes give way to practicality, especially when it comes to saving money! Everything in this book is based on real lessons from the field, and we hope you benefit from our hard-won experience.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值