<p><code>docker-compose</code> is just a glorified wrapper around <code>docker</code> which gives you a nice textual representation of all the options you would regularly give to <code>docker build</code> or <code>docker run</code>. Plus, of course, it bundles several docker images/containers together. That said, it has no provisions to run anything else than you would get by calling <code>docker</code>.</p>

<p>I don't see how you would use <code>docker-compose build</code> to populate a volume. Volumes are, by definition, not living inside docker images/containers, but outside (on the host system). <code>docker build</code> never writes to the host system, and you cannot mount volumes during <code>docker build</code>. <code>docker-compose build</code> basically only runs <code>docker build</code> for you, and also has no way to run or write anything on the host.</p>

<p>So, no, <code>docker-compose build</code> will not be able to create the static files.</p>

<p>You could make your compose file so that it runs your generator (inside a docker container, writing to a named volume), and runs your server (nginx or whatever) in another container, mounting the same named volume (this would be your case 2, I believe). Then, with <code>docker-compose up</code>, it would fire up both those containers. The first one would populate your static files; the second would serve. The first would hang around and do nothing afterwards, which you could just ignore, but which would certainly be ugly. Unfortunately, you cannot use something like <code>docker-compose up --rm</code> to automatically the unneccessary jekyll container.</p>

<p>So, your best bet is to start the jekyll container yourself (<code>docker run --rm jekyll:xyz -v ...:...</code>), the <code>--rm</code> will remove the container after it is finished. Then <code>docker-compose up</code> with the actual server(s).</p>

CC BY-SA 3.0
2018-02-11T08:12:44.807Z
I actually had to do a small work-around. First run Jekyll (SSG), copy the HTML site it output into a named volume via a busybox container, and then mount the volume. How and where should I post the relevant code?
Moritz
CC BY-SA 3.0
2018-02-11T09:46:06.103Z
@Moritz, glad that you got it to work. You can do a self-answer here, or just edit your question.
AnoE
CC BY-SA 3.0
2018-02-06T23:37:29.327Z
2018-02-06T23:59:22.513Z
2018-02-06T23:59:22.513Z
AnoE
<p>I currently have a Docker compose based web app that uses Nginx as a reverse proxy that forwards traffic to a Flask and Grafana docker instance. The task at hand is to integrate a SSG (static site generator) such as Jekyll into the setup.</p>

<p>The most straight forward approach is to:</p>

<ol>
<li>have a <a href="https://kristofclaes.github.io/2016/06/19/running-jekyll-locally-with-docker/" rel="noreferrer">Jekyll container</a> continually run and serve the static aspect of the site via Nginx redirects (<code>jekyll serve</code>).</li>
<li><a href="https://blog.nimbleci.com/2016/08/10/how-to-deploy-a-jekyll-blog-in-docker/" rel="noreferrer">Another approach</a> would be to run the build command in the Jekyll docker image and have the output be shared via docker volumes with Nginx.</li>
<li><a href="https://habd.as/simple-websites-jekyll-docker/" rel="noreferrer">Yet another</a> is to generate the site in the Nginx container</li>
</ol>

<p>The downside with the first two solutions is that they create a Jekyll docker instance that is only used once and then persists. In 1.'s case, the serving can be directly handled by Nginx. In 3.'s case, there is no separation between Jekyll and Nginx. This would be useful since Nginx is also responsible for routing traffic to the other Docker instances.</p>

<p>The question is, is there a way to populate a Docker volume with a Dockerfile by Jekyll so that this can then be mounted by Nginx? Step by step:</p>

<ol>
<li><code>docker-compose build</code>: build the site and populate the <em>static-site</em> volume</li>
<li><code>docker-compose up -d</code>: Nginx mounts the <em>static-site</em> volume and serves it</li>
</ol>

<p>Does this approach even make sense?</p>

<docker><docker-compose><nginx>
How to populate docker volumes in a build step to be used by a different container
Moritz
# How to populate docker volumes in a build step to be used by a different container

(Asked by: username_0 on 2018-02-06)

I currently have a Docker compose based web app that uses Nginx as a reverse proxy that forwards traffic to a Flask and Grafana docker instance. The task at hand is to integrate a SSG (static site generator) such as Jekyll into the setup.

The most straight forward approach is to:

1. have a [Jekyll container](https://kristofclaes.github.io/2016/06/19/running-jekyll-locally-with-docker/) continually run and serve the static aspect of the site via Nginx redirects (`jekyll serve`).
2. [Another approach](https://blog.nimbleci.com/2016/08/10/how-to-deploy-a-jekyll-blog-in-docker/) would be to run the build command in the Jekyll docker image and have the output be shared via docker volumes with Nginx.
3. [Yet another](https://habd.as/simple-websites-jekyll-docker/) is to generate the site in the Nginx container

The downside with the first two solutions is that they create a Jekyll docker instance that is only used once and then persists. In 1.'s case, the serving can be directly handled by Nginx. In 3.'s case, there is no separation between Jekyll and Nginx. This would be useful since Nginx is also responsible for routing traffic to the other Docker instances.

The question is, is there a way to populate a Docker volume with a Dockerfile by Jekyll so that this can then be mounted by Nginx? Step by step:

1. `docker-compose build`: build the site and populate the *static-site* volume
2. `docker-compose up -d`: Nginx mounts the *static-site* volume and serves it

Does this approach even make sense?

**Tags:** docker, docker-compose, nginx


---

## 1 answers

### Answer by username_1 on 2018-02-06. Score: 5 | Accepted answer

`docker-compose` is just a glorified wrapper around `docker` which gives you a nice textual representation of all the options you would regularly give to `docker build` or `docker run`. Plus, of course, it bundles several docker images/containers together. That said, it has no provisions to run anything else than you would get by calling `docker`.

I don't see how you would use `docker-compose build` to populate a volume. Volumes are, by definition, not living inside docker images/containers, but outside (on the host system). `docker build` never writes to the host system, and you cannot mount volumes during `docker build`. `docker-compose build` basically only runs `docker build` for you, and also has no way to run or write anything on the host.

So, no, `docker-compose build` will not be able to create the static files.

You could make your compose file so that it runs your generator (inside a docker container, writing to a named volume), and runs your server (nginx or whatever) in another container, mounting the same named volume (this would be your case 2, I believe). Then, with `docker-compose up`, it would fire up both those containers. The first one would populate your static files; the second would serve. The first would hang around and do nothing afterwards, which you could just ignore, but which would certainly be ugly. Unfortunately, you cannot use something like `docker-compose up --rm` to automatically the unneccessary jekyll container.

So, your best bet is to start the jekyll container yourself (`docker run --rm jekyll:xyz -v ...:...`), the `--rm` will remove the container after it is finished. Then `docker-compose up` with the actual server(s).

> **Comments:**
> **username_0**: I actually had to do a small work-around. First run Jekyll (SSG), copy the HTML site it output into a named volume via a busybox container, and then mount the volume. How and where should I post the relevant code?  
> **username_1**: @username_0, glad that you got it to work. You can do a self-answer here, or just edit your question.  

---

