Home > OS >  how to create a docker image/container from two existing containers
how to create a docker image/container from two existing containers

Time:02-22

In AWS ECR I have 2 images, and I wish to create an image that is the result of combining the contents of these two images. I have the following Dockerfile to do this:

FROM *insert image uri of image #1* as a

COPY --from=a . /a

FROM *insert image uri of image #2* as b

COPY --from=b . /b

When I try to run docker build . -t final-image:latest --file ., I get Error response from daemon: unexpected error reading Dockerfile: read /var/lib/docker/tmp/docker-builder590433020: is a directory. Would anyone know what the correct way to build this image would be?

CodePudding user response:

You are passing a directory in place of the file argument.

Since you are used the dot, which is the current dir, you can omit the file argument, given the assumption your Dockerfile is called Dockerfile and exists in the context dir. Otherwise, point to it to the actual file.

In any case, use the dot as (last) positional argument to provide the context dir.

Run one of:

docker build --tag myimge .
docker build --tag myimage --file /path/to/Dockerfile .

Afterwards, your Dockerfile doesn't make a lot of sense. If you want to copy from 2 existing images, you can reference them directly.

As example:

FROM busybox

COPY --from=docker.io/ubuntu /foo /a
COPY --from=docker.io/debian /bar /b

Another problem is that you COPY from the same stage you are currently in, twice:

FROM foo as a
# here you say copy from a but you are currently in a
# so that doesnt make sense
COPY --from=a . /a

If you want to copy from a previous stage, you need to name the previous stage, not the current one:

FROM foo as a
RUN /touch /myfile
FROM bar as b
COPY --from a /myfile /mycopy

You can chain this, of course, with more stages.

  • Related