1

#yyds干货盘点#如何写好Dockerfile

 2 years ago
source link: https://blog.51cto.com/u_12040959/5066515
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

#yyds干货盘点#如何写好Dockerfile

原创

峰啊疯了 2022-03-03 09:21:02 博主文章分类:运维 ©著作权

文章标签 docker nginx ubuntu 文章分类 Linux 系统/运维 阅读数414

1.什么是Dockerfile

Dockerfile 相当于是一个脚本,不过是可以在这个脚本文件里使用一些特定的命令(关键字)完成一些自动化任务的脚本。其主要目的是创建用户自定义镜像。

如果不用Dockerfile创建一个自定义的镜像,大概步骤是:

  • 找一个基础镜像(共有仓库或者私有仓库)
  • docker pull path(下载镜像)
  • docker yun 生成一个容器container
  • 进入container,做各种操作和配置,一般都是装三方插件,yum install等等
  • docker tag, docker push 提交成一个新的docker container,docker save ,docker load

如果把以上步骤都写进一个文件里,然后让docker自己去实现就显得方便很多,所以就有了Dockerfile.

#yyds干货盘点#如何写好Dockerfile_nginx

2.Dockerfile的文件组成

## Dockerfile文件格式

# This dockerfile uses the ubuntu image
# VERSION 2 - EDITION 1
# Author: docker_user
# Command format: Instruction [arguments / command] ..

# 1、第一行必须指定 基础镜像信息
FROM ubuntu

# 2、维护者信息
MAINTAINER docker_user [email protected]

# 3、镜像操作指令
RUN echo "deb http://archive.ubuntu.com/ubuntu/ raring main universe" >> /etc/apt/sources.list
RUN apt-get update && apt-get install -y nginx
RUN echo "\ndaemon off;" >> /etc/nginx/nginx.conf

# 4、容器启动执行指令
CMD /usr/sbin/nginx

#yyds干货盘点#如何写好Dockerfile_ubuntu_02

3.构建镜像

进到Dockerfile文件目录

docker build -t nginx/v3 .
# nginx/v3是镜像标签,支持添加多个标签
docker build -t nginx/v3:1.0.2 -t nginx/v3:latest .

如果不想进到Dockerfile文件目录,可以指定文件路径

docker build -f /path/to/a/Dockerfile .

​​. 是上下文路径​​

  • 上下文路径,是指 docker 在构建镜像,有时候想要使用到本机的文件(比如复制),docker build 命令得知这个路径后,会将路径下的所有内容打包。

解析​:由于 docker 的运行模式是 C/S。我们本机是 C,docker 引擎是 S。实际的构建过程是在 docker 引擎下完成的,所以这个时候无法用到我们本机的文件。这就需要把我们本机的指定目录下的文件一起打包提供给 docker 引擎使用。

如果未说明最后一个参数,那么默认上下文路径就是 Dockerfile 所在的位置。

注意​:上下文路径下不要放无用的文件,因为会一起打包发送给 docker 引擎,如果文件过多会造成过程缓慢。

4.查看镜像

docker images

#yyds干货盘点#如何写好Dockerfile_nginx_03

  • 打赏
  • 收藏
  • 评论
  • 分享
  • 举报

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK