如何在Docker Ubuntu镜像上安装git?

Mic*_*ant 6 git ubuntu containers docker dockerfile

我有一个简单的docker文件

FROM ubuntu:18.04
COPY . /usr/src/app/
COPY docker_files/.bash_aliases /root/

$ docker build -t dock .
Sending build context to Docker daemon  146.9kB
Step 1/3 : FROM ubuntu:18.04
 ---> 94e814e2efa8
Step 2/3 : COPY . /usr/src/app/
 ---> bf79eb6c42c1
Step 3/3 : COPY docker_files/.bash_aliases /root/
 ---> aedc97d5ee8b
Successfully built aedc97d5ee8b
Successfully tagged dock:latest
Run Code Online (Sandbox Code Playgroud)

我可以使用它:

$ docker run -it dock
bash: git: command not found
root@6a6bec871690:/# ls usr/src/app/
Dockerfile  Gemfile  Gemfile.lock  README.md  docker_files  go  ...
root@6a6bec871690:/# 
Run Code Online (Sandbox Code Playgroud)

如您所见,我的文件已复制,并且为root创建了别名。但是找不到git(因此err msg)并且未安装。
鉴于我在下面的尝试失败,如何安装它?

root@753b573271d5:/# git
bash: git: command not found
root@753b573271d5:/# apt-get install git
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package git
root@753b573271d5:/# sudo apt-get install git 
bash: sudo: command not found
Run Code Online (Sandbox Code Playgroud)

ear*_*ils 29

这个 Dockerfile 对我有用

FROM ubuntu:18.04
RUN apt update
RUN apt install -y git
Run Code Online (Sandbox Code Playgroud)

容器内部

$ which git
/usr/bin/git
Run Code Online (Sandbox Code Playgroud)

  • 如果您不想重建容器,也可以在容器内运行“apt update”。但是,在构建容器本身时安装所需的内容(如此处所示)是更好的方法。 (2认同)

小智 9

apt-get update

apt-get install git
Run Code Online (Sandbox Code Playgroud)