mpen
mpen

Reputation: 282865

Docker: How to share files with two other images?

I'm trying to containerize my app so that I can host it with Kubernetes.

I'm planning on splitting it like so:

  1. PHP image. Runs php-fpm, holds all application code.
  2. Nginx. Serves static content and defers to PHP service for everthing else.
  3. Webpack. This is where it gets hairy.

I'm going to make a Dockerfile just for Webpack which will build all my static assets, but after that I'm not sure how to copy them into the other two images. All the static files need to be copied into the Nginx image except for one JSON file which holds metadata about what files were generated so that I can write the appropriate HTML in PHP.

I could use a multi-stage build if 100% of the assets were needed in the Nginx image, but I need that one piece for the PHP image.

What's the best way to approach this?

N.B. I'd like to re-use this Webpack Dockerfile to run webpack-dev-server for development, but otherwise it won't be needed at all in prod. If this complicates things, then just ignore. That's a problem for later.

Upvotes: 1

Views: 94

Answers (1)

camba1
camba1

Reputation: 1820

You can just copy the information you need from one image to the next in multi-stage builds, you do not need to copy all of the files. Here is an example from the docker docs which does something similar:

FROM golang:1.7.3
WORKDIR /go/src/github.com/alexellis/href-counter/
RUN go get -d -v golang.org/x/net/html  
COPY app.go .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .

FROM alpine:latest  
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=0 /go/src/github.com/alexellis/href-counter/app .
CMD ["./app"]  

In this case it just copies one directory from the initial image to the final image.

You can also copy from an external image using the copy command. See the ‘use an external image as a stage’ in the docker docs: https://docs.docker.com/develop/develop-images/multistage-build/

It would be something like this in your other dockerfiles:

COPY —form=mywebpackimage /awesome/files /newimage/directory

Upvotes: 3

Related Questions