skip to Main Content

Given a Git repository as Docker context:

my_project_dir
├── Dockerfile 
├── run_myapp.py
├── requirements.txt 
├── dir1
│   └── ... some files
└── dir2
    └── ... some files

I want to use COPY to move the run_myapp.py and requirements.txt but not the two directories. I want to keep my Docker image light so I don’t want to include the directories; those are used for other services.

Currently I have COPY used as follows:

...

COPY run_myapp.py run_myapp.py
COPY requirements.txt requirements.txt 
...

I don’t want to use COPY . . since this will copy everything.

I there a way to specify all docs but not directories?

What I’ve tried

I read Copy current directory in to docker image and some similar questions outside of StackOverflow like https://www.geeksforgeeks.org/docker-copy-instruction/ but none answer my question.

2

Answers


  1. The Dockerfile COPY syntax supports shell globs but doesn’t support any sort of matching on file type. You can copy all things with a given name *.py but not "only files". For the actual glob syntax it delegates to the Go path/filepath module which supports only the basic *, ?, and [a-z] characters as "special".

    You aren’t limited to a single file in a COPY command, though, as @JoachimSauer notes in a comment, and you don’t have to spell out the destination directory or filename on the right-hand side. A relative path like . is relative to the current WORKDIR. So here I might write

    WORKDIR /app
    COPY run_myapp.py requirements.txt .
    
    Login or Signup to reply.
  2. Using .dockerignore file with the following content should do the trick:

    */*
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search