Docker error: Unable to locate package git

2019-03-22 17:33发布

问题:

I'm using an image nginx which is based on dockerfile/ubuntu. On attaching to the docker container's shell

docker exec -it <container_id> /bin/bash

I want to do a git pull so I tried installing git but apt is unable to find the package:

root@a71e45d5cd40:/# apt-get install git
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package git

How can we install git from that image and why is it missing?


cat /etc/apt/sources.list

deb http://httpredir.debian.org/debian wheezy main
deb http://httpredir.debian.org/debian wheezy-updates main
deb http://security.debian.org wheezy/updates main
deb http://nginx.org/packages/mainline/debian/ wheezy nginx

cat /etc/apt/sources.list.d/*

cat: /etc/apt/sources.list.d/*: No such file or directory

apt-cache madison git

N: Unable to locate package git

回答1:

This is happening because the apt repository is not yet updated, it is common practice to clean your apt repositories and tmp files after creating an image, which your base image is probably doing.

To fix this, you are going to want to run apt-get update prior to installing git, it is good practice to combine the update and install command at the same time to bust cache on the update if the install line changes:

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

Using -y is convenient to automatically answer yes to all the questions.