<p>What I have, are multi similar and simple <code>dockerfile</code>s</p>
<p>But what I want is to have a single base <code>dockerfile</code> and my <code>dockerfile</code>s
pass their variables into it.</p>
<p>In my case the only difference between <code>dockerfile</code>s are
simply their <code>EXPOSE</code>, so I think it's better to keep a base <code>dockerfile</code> and other <code>dockerfile</code>s only inject that variables into base <code>dockerfile</code> like a template engine</p>
<p>A sample <code>dockerfile</code>:</p>
<pre><code>FROM golang:1.17 AS builder
WORKDIR /app
COPY . .
RUN go mod download
RUN go build -o /bin/app ./cmd/root.go

FROM alpine:latest
WORKDIR /bin/
COPY --from=builder /bin/app .
EXPOSE 8080
LABEL org.opencontainers.image.source=&quot;https://github.com/mohammadne/bookman-auth&quot;
ENTRYPOINT [&quot;/bin/app&quot;]
CMD [&quot;server&quot;, &quot;--env=dev&quot;]
</code></pre>

<h1><code>IMPORT</code> directive will never be implemented</h1>
<p>A long time ago there was proposed <code>IMPORT</code> directive for <code>Docker</code></p>
<p>Unfortunately, issues are closed while PR's are still open:</p>
<ul>
<li><a href="https://stackoverflow.com/questions/69234878/using-shared-dockerfile-for-multiple-dockerfiles">docker - using shared dockerfile for multiple dockerfiles - Stack Overflow</a></li>
<li><a href="https://github.com/moby/moby/issues/735" rel="nofollow noreferrer">Proposal: Dockerfile add INCLUDE · Issue #735 · moby/moby</a></li>
<li><a href="https://github.com/moby/moby/issues/974" rel="nofollow noreferrer">implement an INCLUDE verb/instruction for docker build files · Issue #974 · moby/moby</a></li>
<li><a href="https://github.com/moby/moby/issues/40165" rel="nofollow noreferrer">Add INCLUDE feature · Issue #40165 · moby/moby</a></li>
<li><a href="https://github.com/moby/moby/pull/2108" rel="nofollow noreferrer">docker build: initial work on the include command by flavio · Pull Request #2108 · moby/moby</a></li>
<li><a href="https://www.isaac.nl/en/developer-blog/dockerfile-templating-to-automate-image-creation/index.html" rel="nofollow noreferrer">Dockerfile templating to automate image creation</a></li>
<li><a href="https://blog.dockbit.com/templating-your-dockerfile-like-a-boss-2a84a67d28e9" rel="nofollow noreferrer">Templating your Dockerfile like a boss! | by Ahmed ElGamil | Dockbit</a></li>
</ul>
<h1>Solution for your case</h1>
<p>But for your case, all you need - is just a bit of <code>sed</code></p>
<p>E.g.:</p>
<pre><code># Case1: inplace templating
EXPOSED_PORT=8081 sed -i &quot;s/EXPOSE 8080/EXPOSE $EXPOSED_PORT/&quot; Dockerfile

# Case2: generating Dockerfile from template
sed &quot;s/EXPOSE 8080/EXPOSE $EXPOSED_PORT/&quot; Dockerfile.template &gt; Dockerfile
</code></pre>
<p>Explanation:</p>
<ul>
<li><code>EXPOSED_PORT=8081</code> declares local <code>bash</code> variable</li>
<li><code>sed</code> is a tool for text manipulation</li>
<li><code>sed -i &quot;s/EXPOSE 8080/EXPOSE $EXPOSED_PORT/&quot; Dockerfile</code> replaces <code>EXPOSE 8080</code> to <code>EXPOSE 8081</code></li>
<li><code>sed &quot;s/EXPOSE 8080/EXPOSE $EXPOSED_PORT/&quot; Dockerfile.template &gt; Dockerfile</code> generates the new <code>Dockerfile</code> from <code>Dockerfile.template</code></li>
</ul>
