如何通过Dockerfile使用apt-get安装多个软件包 [英] How to install multiple packages using apt-get via a Dockerfile
本文介绍了如何通过Dockerfile使用apt-get安装多个软件包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我试图制作一个基本的Dockerfile,但是当我运行它时,它说
So I am trying to make a basic Dockerfile, but when I run this it says
The command bin/sh -c sudo apt-get install git python-yaml python-jinja2 returned a non-zero code: 1
我的问题是我在这里做错什么,甚至可以从Dockerfile中执行 cd和 source之类的命令吗?
My question is what am I doing wrong here, and is it even allowed to do commands like 'cd' and 'source' from the Dockerfile?
FROM Ubuntu
MAINTAINER example
#install and source ansible
RUN sudo apt-get update
RUN sudo apt-get install git python-yaml python-jinja2 python-pycurl
RUN sudo git clone https://github.com/ansible/ansible.git
RUN sudo cd ansible
RUN sudo source ./hacking/env-setup
推荐答案
指针/注释在这里:
- 它是
ubuntu
而不是Ubuntu
- 从基础
ubuntu
(不幸的是,很多图像)中,您不需要使用sudo
,默认最终用户是root
(在ubuntu
中,sudo
是 - 您希望您的
apt-get更新
和apt-get安装
作为同一命令的一部分,必须RUN
,以防止Docker的层缓存出现问题 - 您需要使用
-y
标志可在Docker构建过程非交互运行时易于安装 - 关于清理您的其他几点apt-cache和其他不需要的工件在运行命令后,但这应该足以继续进行
- It's
ubuntu
notUbuntu
- From base
ubuntu
(and unfortunately, a lot of images) you don't need to usesudo
, the default user isroot
(and inubuntu
,sudo
is not included anyway) - You want your
apt-get update
andapt-get install
to beRUN
as part of the same command, to prevent issues with Docker's layer cache - You need to use the
-y
flag to apt-get install as the Docker build process runs non-interactively - Few other points I could make about clearing up your apt-cache and other non-required artifacts after running the commands, but this should be enough to get going on
新Dockerfile(以考虑到上述情况)如下所示:
New Dockerfile (taking into account the above) would look something like:
FROM ubuntu
MAINTAINER example
#install and source ansible
RUN apt-get update && apt-get install -y \
git \
python-yaml \
python-jinja2 \
python-pycurl
RUN git clone https://github.com/ansible/ansible.git
WORKDIR ansible/hacking
RUN chmod +x env-setup; sync \
&& ./env-setup
您可能还会发现阅读 Dockerfile最佳实践。
编辑: Larsks答案还提出了一些关于容器状态在各层之间不持久的有用的观点所以你也应该投票给他!
Larsks answer also makes some useful points about the state of the container not persisting between layers so you should go upvote him too!
这篇关于如何通过Dockerfile使用apt-get安装多个软件包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文