user3442828
user3442828

Reputation: 51

How do I restrict which directories and files are copied by Docker?

I have a Dockerfile that explicitly defines which directores and files from the context directory are copied to the app directory. But regardless of this Docker tries to copy all files in the context directory.

The Dockerfile is in the context directory.

My test code and data files are in directories directly below the context directory. It attempts to copy everything in the context directory, not just the directories and files specified by my COPY commands. So I get a few hundred of these following ERROR messages, except specifying each and every file in every directory and sub directory:

ERRO[0043] Can't add file /home/david/gitlab/etl/testdata/test_s3_fetched.csv to tar: archive/tar: missed writing 12029507 bytes
...
ERRO[0043] Can't close tar writer: archive/tar: missed writing 12029507 bytes 
Sending build context to Docker daemon  1.164GB
Error response from daemon: Error processing tar file(exit status 1): unexpected EOF

My reading of the reference is that it only copies all files and directories if there are no ADD or COPY directives.

I have tried with the following COPY patterns

COPY ./name/ /app/name
COPY name/ /app/name
COPY name /app/name

WORKDIR /app
COPY ./name/ /name

WORKDIR /app
COPY name/ /name

WORKDIR /app
COPY name /name

My Dockerfile:

FROM python3.7.3-alpine3.9

RUN apk update && apk upgrade && apk add bash
# Copy app
WORKDIR /app
COPY app /app
COPY configfiles /configfiles
COPY logs /logs/
COPY errorfiles /errorfiles
COPY shell /shell
COPY ./*.py .

WORKDIR ../
COPY requirements.txt /tmp/

RUN pip install -U pip && pip install -U sphinx && pip install -r /tmp/requirements.txt

EXPOSE 22 80 8887

I expect it to only copy my files without the errors associated with trying to copy files I have not specified in COPY commands. Because the Docker output scrolls off my terminal window due to aqll thew error messages I cannot see if it succeeded with my COPY commands.

Upvotes: 2

Views: 595

Answers (2)

Opnauticus
Opnauticus

Reputation: 670

All files at and below the build directory are coppied into the initial layer of the docker build context.
Consider using a .dockerignore file to exclude files and directories from the build.

Upvotes: 2

Apoorv Goyal
Apoorv Goyal

Reputation: 82

Try to copy the files in the following manner-

 # set working directory
 WORKDIR /usr/src/app

 # add and install requirements
 COPY ./requirements.txt /usr/src/app/requirements.txt
 RUN pip install -r requirements.txt

 # add app
 COPY ./errorfiles /usr/src/app

Also, you will have to make sure that your docker-compose.yml file is correctly built-

 version: "3.6"
 services:
      users:
           build:
                context: ./app
                dockerfile: Dockerfile
           volumes:
                - "./app:/usr/src/app"

Here, I'm assuming that your docker-compose.yml file is inside the parent directory of your app.

See if this works. :)

Upvotes: 0

Related Questions