rjray
rjray

Reputation: 6753

Using a Docker Image to Provide Static File Content

I thought I had seen examples of this before, but cannot find anything in my Docker-tagged bookmarks or in the Docker in Action book.

The system I am currently working on has a Docker container providing an nginx webserver, and a second container providing a Java application server. The Java app-server also handles the static content of the site (HTML, JS, CSS). Because of this, the building and deployment of changes to the non-Java part is tightly coupled to the app-server. I am trying to separate it from that.

My goal is to be able to produce a third container that provides only the (static) web-application files, something that can be easily updated and swapped out as needed. I have been reading up on Docker volumes, but I'm not 100% certain that is the solution I need here. I simply want a container with a number of files, that provides these files at some given mount-point to the nginx container, in a read-only fashion.

The rough steps would be:

Is this something that can be done? Or is there a better way to architect this in terms of the Docker ecosystem?

Upvotes: 3

Views: 3669

Answers (1)

David Maze
David Maze

Reputation: 160073

A Docker container fundamentally runs a program; it's not just a collection of files. Probably the thing you want to do with these files is serve them over HTTP, and so you can combine a multi-stage build with a basic Nginx server to do this. A Dockerfile like this would be fairly routine:

FROM node AS build
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install
COPY ./ ./
RUN yarn build

FROM nginx
COPY --from=build /app/dist/ /usr/share/nginx/html/

This could be its own container, or it could be the same Nginx you have as a reverse proxy already.

Other legitimate options include combining this multi-stage build with your Java application container, so you have a single server but the asset build pipeline is in Docker; or running this build sequence completely outside of Docker and uploading it to some sort of external service that can host your static files (Amazon S3 can work for this, if you're otherwise in an AWS environment).

Docker volumes are not a good match for something you would build and deploy. Uploading content into a named volume can be tricky (more so if you eventually move this to Kubernetes) and it's more straightforward to version an image than a collection of files in a volume.

Upvotes: 3

Related Questions