使用 Docker 在 ubuntu 映像中安装库

Installing libraries in ubuntu image using Docker

我正在尝试在我的 ubuntu:14.04 图像中安装 'sl' 包。这是我的 Dockerfile 的内容:

FROM ubuntu:14.04
MAINTAINER xyz <xyz@gmail.com>
RUN echo 'Going to install sl now'
RUN apt-get install -y sl

这是我用来构建图像的命令:

sudo docker build -t xyz/ubuntu:14.04 .

但我收到此错误消息:

Sending build context to Docker daemon 2.048 kB
Sending build context to Docker daemon 
Step 0 : FROM ubuntu:14.04
 ---> d0955f21bf24
Step 1 : MAINTAINER xyz <xyz@gmail.com>
 ---> Using cache
 ---> a6e08926e788
Step 2 : RUN echo 'Going to install sl now'
 ---> Using cache
 ---> 1bf0bc6b3092
Step 3 : RUN apt-get install -y sl
 ---> Running in f7e57167443f
Reading package lists...
Building dependency tree...
Reading state information...
E: Unable to locate package sl
INFO[0004] The command [/bin/sh -c apt-get install -y sl] returned a non-zero code: 100 

我哪里错了?

您需要 运行 apt-get update 例如:

RUN apt-get update && apt-get install -y sl

您也可以自己整理一下以节省一些磁盘空间space:

RUN apt-get update && apt-get install -y sl && rm -r /var/lib/apt/lists/*

在安装任何其他软件包之前,您需要更新本地 ubuntu 存储库中的索引。正确的做法是:

RUN apt-get update
RUN apt-get install -y <package-name>

正如 Adrian 在他的回答中提到的那样,删除下载的列表可以用来在磁盘上保存一些 space。当您将图像推回集线器时,这尤其有用。