是否可以为具有相同配置的多个容器制作通用的dockerfile?

时间:2018-06-05 11:31:33

标签: docker docker-compose 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中的相同配置。

1 个答案:

答案 0 :(得分:4)

查看多阶段构建: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标志指定构建目标。