Home > Enterprise >  Clarify how docker command COPY behaves relative to docker build context
Clarify how docker command COPY behaves relative to docker build context

Time:07-20

I want to copy the contents of a parent directory (relative to the position of the Dockerfile) into my image.

This is the folder structure:

app-root/
  docker/
    php81aws/
      some-folder
      Dockerfile
      start-container
      supervisord.conf
  app_folders
  app_files

I'm calling docker build as follows:

app-root#> docker build -t laravel -f docker/php81aws/Dockerfile .

Or from docker compose with:

services: 
  laravel:
     build:
       context: ./
       dockerfile: docker/php81aws/Dockerfile

Therefore, the context should be in the app-root directory. In the dockerfile, I'm using COPY like so:

COPY docker/php81aws/start-container /usr/local/bin/start-container
COPY docker/php81aws/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
RUN chmod  x /usr/local/bin/start-container

COPY --chown=www:www . /var/www/

It always gives me this error:

failed to compute cache key: "/start-container" not found: not found

I tried to use COPY ./docker/php81aws/start-container and COPY start-container but the error is always the same. Of course copying the parent directory also fails.

CodePudding user response:

You mention in a comment that your top-level app-root directory has a .dockerignore file that excludes the entire docker directory. While the Dockerfile will still be available, nothing else in that tree can be COPYed into the image, and if you COPY ./ ./ to copy the entire build context into the image, that directory won't be present.

Deleting this line from the .dockerignore file should fix your issue.

In general you want the .dockerignore file to include anything that's part of your host build-and-test environment, but should not by default be included in an image, possibly because the Dockerfile is going to rebuild it. In a Node context, for example, you almost always want to exclude the host's node_modules directory, or in a Java context often the Gradle build or Maven target directories. Anything you do want to include in the image needs to not be listed in .dockerignore.

  • Related