Search code examples
dockerdocker-composedockerfileubuntu-18.04docker-registry

How to use the installed packages from a built image in a new one on docker containers?


I am a very newbie on docker, I spend several days without success trying to do the following:

I want to create an image from a "Dockerfile 1" in order to use the packages installed when building another image on the "Dockerfile 2", the following are the minimal setup to reproduce the issue

Step 1, create the image from the following Dockerfile (Dockerfile 1)

FROM ubuntu:18.04
RUN apt update -y; apt upgrade -y; apt install maven -y

Step 2, tag the built image

docker tag my_custom_image:1-0 external/my_custom_image:1.0

Step 3, Push the built image

docker push external/my_custom_image:1.0

I would expect that the following could works:

Step 4, create a new image from the build image in the Dockerfile 2

FROM external/my_custom_image:1.0
RUN mvn --version

The main idea is to be able to use the maven package installed from the first image in the second image, but the second Dockerfile shows that the mvn command is not found.


Solution

  • There is no mvn package available in ubuntu:18.04, maven should be used instead.

    So, your Dockerfile 1 should look like this:

    FROM ubuntu:18.04
    RUN apt update -y; apt upgrade -y; apt install maven -y
    

    Moreover, the given line of apt commands returns this:

    E: Unable to locate package mvn
    The command '/bin/sh -c apt update -y; apt upgrade -y; apt install mvn -y' returned a non-zero code: 100
    

    This means the image can't even be built with this command.

    EDIT

    given the following files in the same directory:

    Dockerfile1

    FROM ubuntu:18.04
    RUN apt update -y; apt upgrade -y; apt install maven -y
    

    Dockerfile2

    FROM from-test-docker-file-1
    RUN mvn --version
    

    and running these commands:

    docker build -t from-test-docker-file-1 -f Dockerfile1 .
    docker build -t from-test-docker-file-2 -f Dockerfile2 . 
    

    should return the following for the 2nd docker build command:

    Sending build context to Docker daemon  3.072kB
    Step 1/2 : FROM from-test-docker-file-1
     ---> 821db5f3628e
    Step 2/2 : RUN mvn --version
     ---> Running in 57b94babccca
    Apache Maven 3.6.0
    Maven home: /usr/share/maven
    Java version: 11.0.11, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
    Default locale: en_US, platform encoding: ANSI_X3.4-1968
    OS name: "linux", version: "5.11.0-34-generic", arch: "amd64", family: "unix"