basforge

Search titles, text and the “If it doesn’t work” cases.

    RU

    .dockerignore — before the first COPY . .

    COPY . . copies everything from the build directory into the image except what’s listed in .dockerignore: .env with passwords, .git with its whole history, node_modules built for the laptop’s OS. Put the file next to the Dockerfile before the first build, not after the first leak.

    What goes along without it

    What Why it’s bad in the image
    .env, .env.* passwords and tokens end up in an image layer, available to everyone who pulled it. RUN rm .env further down doesn’t help: the file stays in the layer beneath, and docker save gets it out
    .git the whole history, with every secret ever committed. And every commit changes .git, so COPY . . never comes from the cache
    node_modules overwrites what npm ci installed. Modules with binaries — esbuild, sharp — are built for the laptop’s OS, and the build fails on Linux
    dist, build an old build result from the laptop: confusing if the new build didn’t overwrite something

    How to write it

    .git
    .env
    .env.*
    **/node_modules
    dist

    The patterns look like .gitignore, but they’re relative to the context root: node_modules excludes only ./node_modules, not packages/web/node_modules in a monorepo. For any depth, use **/node_modules.

    You can list the Dockerfile and .dockerignore itself there too: the build gets them anyway, and COPY . . won’t put them into the image.

    If it doesn’t work

    The build in the container fails: You installed esbuild for another platform than the one you're currently using

    Cause
    COPY . . overwrote the node_modules installed by npm ci with the laptop’s folder, where esbuild is built for Windows or macOS.
    Fix
    Add node_modules to .dockerignore. Only npm ci inside the build installs dependencies.

    .env was added to .dockerignore, but it’s still in the old image

    Cause
    The file is in a layer of the already built image and in the registry.
    Fix
    Rebuild and push the image again, delete the old tags from the registry, and change the passwords from .env: whoever pulled the image has seen them.

    How to check

    What goes into the context is visible only from inside the build: build a throwaway image that copies the context and prints it:

    docker build --no-cache --progress=plain -f- . <<'EOF'
    FROM busybox
    COPY . /ctx
    RUN find /ctx -maxdepth 1
    EOF

    The RUN step output lists the files and directories at the context root. .env, .git and node_modules shouldn’t be there.