Home > Net >  docker compose ignoring `.dockerignore`
docker compose ignoring `.dockerignore`

Time:10-07

Despite placing 2 .dockerignores in project root and in packages, docker compose copies node_modules in myapp.

.dockerignore

*node_modules*

Project directory looks like:

myproject
├── .dockerignore
├── docker-compose.yml
└── packages
    └── myapp
        ├── node_modules
        ├── .dockerignore
        ├── .gitignore
        ├── Dockerfile
        ├── README.md
        ├── package-lock.json
        ├── package.json
        ├── public
        │   ├── favicon.ico
        │   ├── index.html
        │   ├── logo192.png
        │   ├── logo512.png
        │   ├── manifest.json
        │   └── robots.txt
        └── src
            ├── App.css
            ├── App.js
            ├── App.test.js
            ├── index.css
            ├── index.js
            ├── logo.svg
            ├── reportWebVitals.js
            └── setupTests.js

docker-compose.yml

version: '3'

services:
  client:
    image: myapp
    build:
      context: '.'
      dockerfile: 'packages/myapp/Dockerfile'
    ports:
      - "3000:3000"

Dockerfile

FROM alpine
WORKDIR /usr/app
COPY . .
RUN apk add --update nodejs npm
RUN npm install
CMD ["npm", "start"]

No matter what I do, node_modules will be copied:

myproject % docker-compose build
[ ] Building 10.9s (5/9)                                                        
 => [internal] load build definition from Dockerfile                       0.0s
 => => transferring dockerfile: 31B                                        0.0s
 => [internal] load .dockerignore                                          0.0s
 => => transferring context: 55B                                           0.0s
 => [internal] load metadata for docker.io/library/alpine:latest           0.0s
 => [1/5] FROM docker.io/library/alpine                                    0.0s
 => CANCELED [internal] load build context                                10.7s
 => => transferring context: 53.88MB                                      10.7s
canceled

To generate project:

mkdir myproject
cd myproject
mkdir packages
cd packages
npx create-react-app myapp

and copy docker files included

CodePudding user response:

The .dockerignore file that is used is in the root of your context, in this case the same directory as your docker-compose.yml file. But the contents are invalid for ignoring that file. To ignore a node_modules folder in any subdirectory, the syntax should be:

**/node_modules

However, that would result in your COPY command pulling in the packages directory at the top. So you more likely want to change your context:

version: '3'

services:
  client:
    image: myapp
    build:
      context: 'packages/myapp'
    ports:
      - "3000:3000"

And then the .dockerignore in the myapp folder would only need:

node_modules
  • Related