Skip to content

fix(container): honor .dockerignore when building a container #277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

norbjd
Copy link
Contributor

@norbjd norbjd commented Jun 27, 2025

Summary

What's changed?

When building a container, honor .dockerignore present at the root of the build context.

Also bumping the version in the same PR (0.4.15 => 0.4.16 since it's a bug fix).

Bonus: we don't need tar-fs library anymore!

Why do we need this?

Today, .dockerignore file at the root of the build context is ignored. Whatever file you list inside it, it will be present in the built image if you do COPY . . for example.

The library used under the hood (dockerode) supports this, but not if we pass a tar stream when building. We need to pass a list of files so files in .dockerignore can be excluded.

So, the only way seems to be to list all the files in the build context, and pass them to dockerode so it can filter afterwards.

How have you tested it?

Pretty simple example:

serverless.yml:

service: test
configValidationMode: off
provider:
  name: scaleway

plugins:
  - serverless-scaleway-functions

package:
  patterns:
    - "!node_modules/**"
    - "!.gitignore"
    - "!.git/**"

custom:
  containers:
    test:
      directory: container
      port: 8000
      memoryLimit: 1000
      cpuLimit: 1000

The container/ folder looks like:

container/
  |-- .dockerignore
  |-- Dockerfile
  |-- dir1
       |-- dir2
            |-- test1
            |-- test2
  |-- hello
  |-- world

Dockerfile is:

FROM python:3.13-alpine

WORKDIR /app

COPY . .

CMD ["python3", "-m", "http.server"]

And .dockerignore contains:

world
dir1/dir2/test2

Once deployed (serverless deploy), the files shown are just: hello and dir1/dir2/test. Other files have been excluded. This can also be checked in the built image.

Checklist

  • I have reviewed this myself
  • There is a unit test covering every change in this PR
  • I have updated the relevant documentation

Details

@norbjd norbjd requested a review from Bemilie June 27, 2025 13:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant