首页 > 解决方案 > 是否可以为具有相同配置的多个容器制作通用 dockerfile?

问题描述

让我们举个例子:

Dockerfile for container1 : 

FROM ubuntu:14.04
RUN apt-get update
RUN apt-get install -y php5 php5-mysql

Dockerfile for container2 : 

FROM ubuntu:14.04
RUN apt-get update
RUN apt-get install -y php5 php5-mysql php5-dev php5-gd php5-memcache 
php5-pspell

所以需要避免,

FROM ubuntu:14.04
RUN apt-get update
RUN apt-get install -y php5 php5-mysql

因为这在两个 Dockerfile 中都是相同的配置。

标签: dockerdocker-composedockerfile

解决方案


查看多阶段构建:https ://docs.docker.com/develop/develop-images/multistage-build/

您的示例将变为:

FROM ubuntu:14.04 as image1
RUN apt-get update
RUN apt-get install -y php5 php5-mysql

FROM image1 as image2
RUN apt-get install -y php5-dev php5-gd php5-memcache php5-pspell

您的构建命令需要使用--target标志指定构建目标。


推荐阅读