Hussain Murtaza
Hussain Murtaza

Reputation: 407

How to use .dockeringore file with different environments?

I am a newbie at Docker just started learning it.

So I have three build files for an Angular app. The Angular app is just a startup template created using ng new 'projectName'

Dockerfile.dev
Dockerfile.qa
Dockerfile.prod

Now these files work. I have tested them.

Sample Dockerfile.dev

FROM node:alpine

WORKDIR /srv/app
COPY package.json .
RUN npm install
COPY . .
CMD ["npm","run","start"]

But the issue I am facing is how to use the .dockerignore file to exclude the node_modules

I already have the .dockerignore file Which is just a copy of my .gitignore file.

This .dockerignore file works with Dockerfile but not with Dockerfile.qa/dev/prod

I use the following commands to build it

docker build -f Dockerfile.qa .

But the files I want to ignore aren't ignored and are copied over using the

Copy . . 

Thank you.

Upvotes: 0

Views: 1428

Answers (1)

huytmb
huytmb

Reputation: 4274

You can try this:

  1. From Docker 19.03, you can try Docker BuildKit, to set the DOCKER_BUILDKIT=1 environment variable when invoking the docker build command: $ DOCKER_BUILDKIT=1 docker build .

You also need to prefix the name of your .dockerignore file with the Dockerfile name. If your Dockerfile is called Dockerfile.dev the ignore file needs to be called Dockerfile.dev.dockerignore.

The Docker client tries to load <dockerfile-name>.dockerignore first and then falls back to .dockerignore if it can't be found. So docker build -f Dockerfile.dev . first tries to load Dockerfile.dev.dockerignore.

  1. Split your build folder into subdirectories that each have their own Dockerfile and .dockerignore files: dev/qa/prod

Upvotes: 1

Related Questions