Home > Net >  Trying to create docker image with bash script
Trying to create docker image with bash script

Time:10-05

I want to create docker images using next script

docker build - < $2 -t $1

the first parameter is the image name, second is the path to Dockerfile.

I have the next file structure for it.

enter image description here

I'm trying to run it but it doesn't work.

enter image description here

The problem is that it can't find files to copy. Maybe there is some WORKDIR required or smth else.

Dockerfile

FROM postgres
WORKDIR ./bacisImage
ENV POSTGRES_DB pointer
ENV POSTGRES_PASSWORD pointer
ENV POSTGRES_DB 123
COPY migrations/tables/* /docker-entrypoint-initdb.d/
COPY migrations/procedures/* /docker-entrypoint-initdb.d/

CodePudding user response:

rather than the path to a Dockerfile, use either basicImage or dataImage -- the directories which hold the Dockerfiles.

and modify your script to use the path

# ...
docker build -t $1 $2
# ...

from the docker-build docs the syntax for the build command is

$ docker build [OPTIONS] PATH | URL | -

PATH here is what provides the build command its context

A build’s context is the set of files located in the specified PATH or URL. The build process can refer to any of the files in the context. For example, your build can use a COPY instruction to reference a file in the context.

what you pass via - (stdin) is the Dockerfile, which only provides the instructions. with PATH being absent, the default context is ./

so the Dockerfile is trying to copy migrations/tables. but the context of ./ has only basicImage and dataImage; hence the COPY fails.

if instead you pass the PATH of basicImage, that is the new context. and the file Dockerfile within the context is used for building the image and everything would work as expected

  • Related