1

I want to deploy vueJS app inside a docker nginx container but before that container runs the vueJS source has to be compiled via npm run build I want to compilation to run in a container and then exit leaving only the compiled result for the nginx container.

Every time docker-compose up is run the vueJS app has to be recompiled as there is a .env file on the host OS that has to be volume mounted and the variables in here could be updated.

The ideal way I think would be some way of creating stages for docker compose like in gitlab ci so there would be a build stage and when that's finished the nginx container starts. But when I looked this up I couldn't see a way to do this.

What would be the best way to compile my vueJS app every time docker-compose up is run?

2 Answers 2

3

If you're already building your Vue.js app into a container (with a Dockerfile), you can make use of the build directive in your docker-compose.yml file. That way, you can use docker-compose build to create containers manually, or use run --build to build containers before they launch.

For example, this Compose file defines a service using a container build file, instead of a prebuilt image:

version: '3'
services:
  vueapp:
    build: ./my_app  # There should be a Dockerfile in this directory

That means I can both build containers and run services separately:

docker-compose build
docker-compose up

Or, I can use the build-before-run option:

# Build containers, and recreate if necessary (build cache will be used)
docker-compose up --build

If your .env file changes (and containers don't pick up changes on restart), you might consider defining them in container build file. Otherwise, consider putting the .env file into a directory (and mount the directory, not the file, because some editors will use a swap file and change the inode - and this breaks the mount). If you mount a directory and change files within the directory, the changes will reflect in the container, because the parent directory's inode didn't change.

Sign up to request clarification or add additional context in comments.

3 Comments

Does using env_file: .env have the same problem regarding swap files?
No, but I'm not sure if docker-compose actually watches them for changes. But, if the container is re-created via some other process (like if it's container image is rebuilt), then it won't matter anyways, because the new .env file will be always used.
Edit: I tested this, and docker-compose does actually pay attention to changes to .env files. So, no - there will be no problems with swap files.
0

I ended up having an nginx container that reads the files from a volume mount and a container that builds the app and places the files in the same volume mount. While the app is compiling, nginx reads the old version and when the compilation is finished the files get replaced with the new ones.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.