Home > Blockchain >  How to print override data with using multiple docker images in single Dockerfile?
How to print override data with using multiple docker images in single Dockerfile?

Time:09-29

I foubd out that if I use multiple docker images in single Dockerfile , the second one would always override or delete the data installed in former one , for example :

FROM nvidia/cuda:10.2-cudnn8-devel-ubuntu18.04
FROM python:3.8.10
CMD ["/bin/bash"]

The cuda-10.2 installed from former one is gone...But what I want is to have data installed from both docker images I use . Is there any way to achieve it ? Thanks

CodePudding user response:

This concept is called "multi-stage builds" and it works in a different way from what you expect in your Dockerfile. It allows you to build multiple things in a single Dockerfile, and then hand-pick the parts you need in a single final image:

With multi-stage builds, you use multiple FROM statements in your Dockerfile. Each FROM instruction can use a different base, and each of them begins a new stage of the build. You can selectively copy artifacts from one stage to another, leaving behind everything you don’t want in the final image.

To achieve what you want, you might try using multi-stage builds and COPY --from statements, but it probably won't work (for example if the base images use different OS distributions, or if you accidentally miss some files while copying).

What would work is writing a new Dockerfile using the instructions from both other Dockerfiles (python and cuda) and building an image from it. Note that you might need to adapt the commands executed in every one of the base files if they don't work as expected out of the box.

CodePudding user response:

You can use multi-stage Docker builds. Need copy the data between stages but it possible. Read more about it:

https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
  • Related