2

What I'm trying to do:

I'm planning to create a build folder in my service repo, which should contain all docker-related files.

My service depends on postgres and rabbitmq, so as dev workflow I'm starting the service dependencies as docker contains and run my application by connecting to ports exposed by the containers.

My service already has SQL files needed to create DB, create tables, and create index under the deploy-scripts folder. ( please refer to the folder structure image attached below )

I'm planning to spin up a docker container with all tables, DB, and roles required by copying the deploy-scripts folder to docker-entry point script as mentioned in this link How to create User/Database in script for Docker Postgres

Details:

Docker Compose File

version: '3.4'
services: 
  database:
    build:
      context: ./postgres
      dockerfile: DockerFile
    ports: 
      - 5432:5432
    volumes: 
      - database-data:/var/lib/postgresql/data
volumes: 
  database-data:

Postgres Docker File

FROM postgres:10.17-buster
COPY ../deploy-scripts /docker-entrypoint-initdb.d/

This is my folder structure. Folder structure

When I execute the docker-compose -f build/docker-compose.dev.yml up command I'm getting the below error

Building database
[+] Building 0.2s (6/6) FINISHED
 => [internal] load build definition from DockerFile                                                                                                                                                       0.0s
 => => transferring dockerfile: 36B                                                                                                                                                                        0.0s
 => [internal] load .dockerignore                                                                                                                                                                          0.0s
 => => transferring context: 2B                                                                                                                                                                            0.0s
 => [internal] load metadata for docker.io/library/postgres:10.17-buster                                                                                                                                   0.0s
 => [internal] load build context                                                                                                                                                                          0.0s
 => => transferring context: 2B                                                                                                                                                                            0.0s
 => CACHED [1/2] FROM docker.io/library/postgres:10.17-buster                                                                                                                                              0.0s
 => ERROR [2/2] COPY ../deploy-scripts /docker-entrypoint-initdb.d/                                                                                                                                        0.0s
------
 > [2/2] COPY ../deploy-scripts /docker-entrypoint-initdb.d/:
------
failed to compute cache key: "/deploy-scripts" not found: not found
ERROR: Service 'database' failed to build: Build failed

Can any of you help me out? How to copy the deploy-scripts folder to postgres ?

Docker Compose has to use build folder context? Does Docker File have to use my service root folder context?

tecs-x
  • 308
  • 3
  • 12
Sathish
  • 2,056
  • 3
  • 26
  • 40
  • You cannot get files out of the build context during build. `..` is out of the build context. – Zeitounator Jul 11 '21 at 09:34
  • FYI: I have figured out the solution.. But i'm not able to add the solution as answer because people decided to close the question by voting :) – Sathish Jul 11 '21 at 14:50

1 Answers1

2

I think the error is related to the location of your deploy-scripts directory. You attempt to perform a copy from the parent folder and this is not possible.

The <src> path must be inside the context of the build; you cannot COPY ../something /something, because the first step of a docker build is to send the context directory (and subdirectories) to the docker daemon. src

It must be in a sub-directory of the directory containing your Dockerfile.

Romain
  • 19,910
  • 6
  • 56
  • 65
  • It must be in a subdirectory of the `build: { context: }` directory, so you can work around this by changing the context directory to be the parent, changing the `dockerfile:` location and `COPY` line to both be relative to that parent directory. – David Maze Jul 11 '21 at 09:32