dockerdocker-compose

COPY instruction does not COPY certain files


I'm trying to build an image for local development. To do so, I'm using a specific Dockerfile and a specific docker-compose file.

This is the Dockerfile.local file:

FROM golang:1.23-alpine3.19
RUN apk add --no-cache curl vim ca-certificates unzip git

WORKDIR /app

RUN go install github.com/air-verse/air@v1.52.3

COPY ./src/go.mod ./src/go.sum ./
RUN go mod download

COPY ./environments/file.env ./environments/

CMD ["air", "--build.cmd", "go build -o ./tmp/main ./cmd/api", "--build.bin", "./tmp/main"]


The problem is that the file.env is not being copied in /app/environments/file.env.

However, go.mod and go.sum are, so it seems some COPY instructions do work and other don't.

The docker-compose-local.yaml is this:

services:
  postgres:
    ...
  api:
    build:
      context: .
      dockerfile: Dockerfile.local
    ports:
      - 8000:8000
    restart: always
    volumes:
      - ./src:/app
    depends_on:
      - postgres

volumes:
  database:

And the folder structure is this:

Dockerfile.local
docker-compose-local.yaml
environments/
├── file.env
src/
├── go.sum
├── go.mod
├── ...

I've deleted the .dockerignore, so it's not possible that the problem is coming from there.

I'm just running out of ideas. I've tried to change the file name, move where is located, use absolute paths for dst, basically all possible combinations of the copy parameters. None of them have worked. I've also tried to copy the file with .txt extension instead of .env, but it did not make any difference.

I can't understand why the .env is not being copied while the go.* files are.


Solution

  • In your compose file, you have

        volumes:
          - ./src:/app
    

    This hides any content in the /app directory and replaces it with the ./src directory on the host. So it doesn't matter what you COPY into the /app directory in the image, because it's hidden when you run the container.

    Try removing the volume mapping and you should see the files you've copied into the image.

    See this for documentation on how volume mappings hide data in the image.