Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apt-get not working within ubuntu dockerfile

I am fairly new to docker and am trying to learn by writing my own images and, for now, reading Docker in action (ISBN: 1633430235)

In both my own code and an example from the book (pg 146) I would like to install git via a dockerfile.

My code:

# set base image
FROM ubuntu:18.04
# author
MAINTAINER me
############## Begin installation ##########################

# update and upgrade
RUN apt-get update
RUN apt-get upgrade
# install git
RUN apt-get install -y git
***rest of code omitted***

Books code:

#An example Dockerfile for installing Git on Ubuntu
FROM Ubuntu:latest
MAINTAINER "[email protected]"
RUN apt-get install -y git
ENTRYPOINT ["git"]

However, in both I am getting an unable to locate package eror with a none-zeero code:

Reading package lists...
Building dependency tree...
Reading state information...
E: Unable to locate package git
The command '/bin/sh -c apt-get install -y git' returned a non-zero code: 100

So far I have tried.

1) Making apt-get update and apt-get upgrade into a single command. When I do this it fails at this point.

2) Installing apt-transport-https before installing git, as described here:

apt-get update' returned a non-zero code: 100

This will succeed in downloading but as soon as it gets to installing git again, I get the same error. This

3) Following the tutorial on https://docs.docker.com/engine/reference/builder/#dockerfile-examples. Although this is different it still installs the x11 server, again this also fails at installation.

4) Following another answer on stack overflow, Cannot install packages inside docker Ubuntu image I have tried to install curl.

Al these methods, with the exception of the first, let me update & upgrade only to fail once I try to install software. I also have no issues if I am updating, upgrading, installing from a terminal on the machine I am running docker from.

Any advice as to how I can rectify this would be greatly appreciated.

like image 615
Boots Avatar asked Dec 03 '25 09:12

Boots


1 Answers

Execute 'apt-get update' and 'apt-get install' in a single RUN instruction. This is done to make sure that the latest packages will be installed. If 'apt-get install' were in a separate RUN instruction, then it would reuse a layer added by 'apt-get update', which could have been created a long time ago.

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

Note: RUN instructions build your image by adding layers on top of the initial image.

like image 158
Nikita Jain Avatar answered Dec 05 '25 01:12

Nikita Jain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!