Dockerized Nginx: Can static assets be updated without stopping the container?
Here is an example that would move your npm run generate
from image build time to container run time. It is a minimal example to illustrate how moving the process to the run time makes the volume available to both the running container at startup and future ones at run time.
With the following docker-compose.yml
:
version: '3'services: web: image: ubuntu volumes: - site-assets:/app/dist command: bash -c "echo initial > /app/dist/file" restart: "no" nginx: image: ubuntu volumes: - site-assets:/app:ro command: bash -c "while true; do cat /app/file; sleep 5; done"volumes: site-assets:
We can launch it with docker-compose up
in a terminal. Our nginx
server will initially miss the data but the initial web
service will launch and generate our asset (with contents initial
):
❯ docker-compose up Creating network "multivol_default" with the default driverCreating volume "multivol_site-assets" with default driverCreating multivol_web_1 ... doneCreating multivol_nginx_1 ... doneAttaching to multivol_nginx_1, multivol_web_1nginx_1 | cat: /app/file: No such file or directorymultivol_web_1 exited with code 0nginx_1 | initialnginx_1 | initialnginx_1 | initialnginx_1 | initial
In another terminal we can update our asset (your npm run generate
command):
❯ docker-compose run web bash -c "echo updated > /app/dist/file"
And now we can see our nginx
service serving the updated content:
❯ docker-compose up Creating network "multivol_default" with the default driverCreating volume "multivol_site-assets" with default driverCreating multivol_web_1 ... doneCreating multivol_nginx_1 ... doneAttaching to multivol_nginx_1, multivol_web_1nginx_1 | cat: /app/file: No such file or directorymultivol_web_1 exited with code 0nginx_1 | initialnginx_1 | initialnginx_1 | initialnginx_1 | initialnginx_1 | updatednginx_1 | updatednginx_1 | updatednginx_1 | updated^CGracefully stopping... (press Ctrl+C again to force)Stopping multivol_nginx_1 ... done
Hope this was helpful to illustrate a way to take advantage of volume mounting at container run time.