I have 2 containers: web
and nginx
. When I build web
container, static assets for frontend are generated within the container.
Now, I want to share those assets between web
and nginx
without using a volume on the host machine. Otherwise, I'll have to build those static assets on the host side and then include as a volume into the web
container and share it with nginx
container. This is undesirable from my build system's standpoint.
Is there a way to build static assets in the web
container and then share them with nginx
?
Docker creates a local volume by default. However, we can use a volume diver to share data across multiple machines. Finally, Docker also has –volumes-from to link volumes between running containers. It may help for data sharing or more general backup usage.
If you are running more than one container, you can let your containers communicate with each other by attaching them to the same network. Docker creates virtual networks which let your containers talk to each other. In a network, a container has an IP address, and optionally a hostname.
Using Volumes to Share a Directory Mounting a volume to a filesystem path within a container provides read-write access to the volume's data. Volumes can be attached to multiple containers simultaneously. This facilitates seamless data sharing and persistence that's managed by Docker.
Otherwise, I'll have to build those static assets on the host side and then include as a volume into the web container and share it with nginx container.
This statement seems incorrect.
If the static assets are generated as part of the build process, then just mount a volume on top of that directory at runtime. Docker will take care of copying the underlying content into the volume, after which you can access it in your nginx container using --volumes-from
.
For example, if I start with this Dockerfile
for my web container:
FROM alpine
RUN apk add --update darkhttpd
COPY assets /assets
CMD ["darkhttpd", "/assets"]
I now have a directory /assets
that contains my static assets. If I
run this image as:
docker run -v /assets --name web web
Then /assets
will (a) be a volume and (b) contain the contents of
the /assets
directory.
Now you can start an nginx container and share this data with it:
docker run --volumes-from web nginx
The nginx container will have a /assets
directory that contains your
static assets.
I've put together a small example here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With