如何在docker容器上的新映像中使用已安装的包?



我是一个非常新手的docker,我花了几天时间没有成功地尝试做以下事情:

我想创建一个图像从"Dockerfile 1"为了使用在"Dockerfile 2"上构建另一个映像时安装的包,以下是重现问题的最小设置

步骤1,从以下Dockerfile (Dockerfile 1)

创建镜像
FROM ubuntu:18.04
RUN apt update -y; apt upgrade -y; apt install maven -y

步骤2,标记生成的图像

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

步骤3,Push build image

docker push external/my_custom_image:1.0

我希望下面可以工作:

步骤4,从Dockerfile中的构建映像创建一个新映像2

FROM external/my_custom_image:1.0
RUN mvn --version

主要思想是能够在第二个映像中使用从第一个映像安装的maven包,但是第二个Dockerfile显示没有找到mvn命令。

ubuntu:18.04没有mvn包,应该使用maven。

所以,你的Dockerfile 1应该是这样的:
FROM ubuntu:18.04
RUN apt update -y; apt upgrade -y; apt install maven -y

此外,给定的apt命令行返回如下内容:

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

这意味着映像甚至不能用这个命令构建。

编辑

给定同一目录下的以下文件:

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

和运行这些命令:

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

对于第二个docker构建命令应该返回以下内容:

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"

最新更新