<blockquote>
  <p>But is there a way to achieve the same result without copying first the (potentially large) dump into the container?</p>
</blockquote>

<p>The only way that ables you to do that (which I know), is :</p>

<pre><code>RUN gunzip &lt; $(curl -SL http://docker-localhost/db-dump.gz) \
        | mysql
</code></pre>

<p>For this you need to have your dump available on a website, which isn't ideal (probably not as fast as what you are doing)
If you look at the good Dockerfiles (public ones), they mostly get archive files from the web, then remove the archives.</p>

<p>Your commands in <code>Dockerfile</code> seem very good. You could just add a <code>&amp;&amp; rm /tmp/db-dump.gz</code> after <code>mysql</code>.</p>

CC BY-SA 4.0
2021-02-23T22:48:21.737Z
This looks fine until there is a different LOCALE between docker-localhost http server and the local environment
nicocesar
CC BY-SA 4.0
2019-01-24T08:26:37.230Z
2019-01-24T08:26:37.230Z
jayooin
<p>Another idea would be to mount a volume which would give the container access to a directory on the local filesystem. This would allow you to access the zip without copying the archive into the container; however, you wouldn't be able to ship the container with the data. </p>

CC BY-SA 4.0
2019-01-24T13:46:06.837Z
Normally, you cannot mount volumes during a docker build.
BMitch
CC BY-SA 4.0
2019-01-24T15:18:08.120Z
Right. You would have to use docker-compose to add the volume to the built container. You would omit the zip file from the build.
J. Davis
CC BY-SA 4.0
2019-01-24T13:32:00.690Z
2019-01-24T13:32:00.690Z
J. Davis
<p>You can do this with BuildKit and the experimental frontend. As of 18.09, BuildKit can be enabled with either:</p>

<pre><code>export DOCKER_BUILDKIT=1
</code></pre>

<p>for a single shell, or to change the default for the host, you can add to /etc/docker/daemon.json:</p>

<pre><code>{
  "features": {"buildkit": true}
}
</code></pre>

<p>You'll need to reload the docker engine after changing the above.</p>

<hr>

<p>With BuildKit enabled, your Dockerfile would look like:</p>

<pre><code># syntax=docker/dockerfile:experimental

RUN --mount=type=bind,source=db-dump.gz,target=/tmp/db-dump.gz \
    zcat /tmp/db-dump.gz | mysql
</code></pre>

<p>You then build with a <code>docker build .</code> command as before, but with BuildKit enabled.</p>

<p>To see more about BuildKit's experimental features, see <a href="https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/experimental.md" rel="noreferrer">https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/experimental.md</a></p>

CC BY-SA 4.0
2019-01-24T15:08:53.893Z
Thanks a lot, @BMitch: this is exactly what I was looking for. I wasn't at all aware of BuildKit before you mentioned it so I browsed https://github.com/moby/buildkit/issues/528 and https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/experimental.md. But it is not clear to me if this new syntax is frozen and planned for an upcoming release or it is still subject to change. Do you know more about that?
Sylvain Leroux
CC BY-SA 4.0
2019-01-24T15:52:09.813Z
@SylvainLeroux The experimental syntax means they can change things. I don't picture them changing this syntax, but if they do, you can bind to a specific version of an experimental syntax. https://hub.docker.com/r/docker/dockerfile/tags/
BMitch
CC BY-SA 4.0
2019-01-24T13:43:23.013Z
2019-01-24T13:43:23.013Z
BMitch
<p>I need to build a Docker image containing a pre-populate a database.
For now, I am using the following commands in our <code>Dockerfile</code>:</p>

<pre><code>COPY db-dump.gz /tmp
RUN zcat /tmp/db-dump.gz | mysql
</code></pre>

<p>But is there a way to achieve the same result without copying first the (potentially large) dump into the container?</p>

<p>At shell-level, I could pipe the data into the container using something along the lines of (untested):</p>

<pre><code>zcat db-dump.gz | docker exec -i $CID mysql
</code></pre>

<p>But I don't know if there is any similar option available in a Dockerfile.</p>

<docker><dockerfile>
In a Dockerfile, is there a way to avoid copying files to make them accessible to the RUN command?
Sylvain Leroux
# In a Dockerfile, is there a way to avoid copying files to make them accessible to the RUN command?

(Asked by: username_0 on 2019-01-23)

I need to build a Docker image containing a pre-populate a database.
For now, I am using the following commands in our Dockerfile:
COPY db-dump.gz /tmp
RUN zcat /tmp/db-dump.gz | mysql

But is there a way to achieve the same result without copying first the (potentially large) dump into the container?
At shell-level, I could pipe the data into the container using something along the lines of (untested):
zcat db-dump.gz | docker exec -i $CID mysql

But I don't know if there is any similar option available in a Dockerfile.

**Tags:** docker, dockerfile


---

## 3 answers

### Answer by username_4 on 2019-01-24. Score: 6 | Accepted answer

You can do this with BuildKit and the experimental frontend. As of 18.09, BuildKit can be enabled with either:
export DOCKER_BUILDKIT=1

for a single shell, or to change the default for the host, you can add to /etc/docker/daemon.json:
{
  "features": {"buildkit": true}
}

You'll need to reload the docker engine after changing the above.

With BuildKit enabled, your Dockerfile would look like:
# syntax=docker/dockerfile:experimental

RUN --mount=type=bind,source=db-dump.gz,target=/tmp/db-dump.gz \
    zcat /tmp/db-dump.gz | mysql

You then build with a docker build . command as before, but with BuildKit enabled.
To see more about BuildKit's experimental features, see https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/experimental.md

> **Comments:**
> **username_0**: Thanks a lot, @username_4: this is exactly what I was looking for. I wasn't at all aware of BuildKit before you mentioned it so I browsed https://github.com/moby/buildkit/issues/528 and https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/experimental.md. But it is not clear to me if this new syntax is frozen and planned for an upcoming release or it is still subject to change. Do you know more about that?  
> **username_4**: @SylvainLeroux The experimental syntax means they can change things. I don't picture them changing this syntax, but if they do, you can bind to a specific version of an experimental syntax. https://hub.docker.com/r/docker/dockerfile/tags/  

---

### Answer by username_1 on 2019-01-24. Score: 1

But is there a way to achieve the same result without copying first the (potentially large) dump into the container?

The only way that ables you to do that (which I know), is :
RUN gunzip < $(curl -SL http://docker-localhost/db-dump.gz) \
        | mysql

For this you need to have your dump available on a website, which isn't ideal (probably not as fast as what you are doing)
If you look at the good Dockerfiles (public ones), they mostly get archive files from the web, then remove the archives.
Your commands in Dockerfile seem very good. You could just add a && rm /tmp/db-dump.gz after mysql.

> **Comments:**
> **username_2**: This looks fine until there is a different LOCALE between docker-localhost http server and the local environment  

---

### Answer by username_3 on 2019-01-24. Score: 1

Another idea would be to mount a volume which would give the container access to a directory on the local filesystem. This would allow you to access the zip without copying the archive into the container; however, you wouldn't be able to ship the container with the data.

> **Comments:**
> **username_4**: Normally, you cannot mount volumes during a docker build.  
> **username_3**: Right. You would have to use docker-compose to add the volume to the built container. You would omit the zip file from the build.  

---

