How to install git on a docker ubuntu image?
Asked Answered
C

3

38

I have a simple docker file of

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

I can use it:

$ 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:/# 

and as you see my files were copied and aliases created for root. However git was not found (hence the err msg) and not installed.
How do I install it given my attempt below failed?

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
Copyread answered 12/4, 2019 at 20:49 Comment(4)
I'm curious why you want Git in your image. Typically version control would happen outside of the container, and I don't see why docker run would invoke git here. Are you trying to show Git info in your bash prompt or something?Retharethink
good qusestion. I am playing around. Trying to find the rigth match of a development environment where the docker image is the env i work inCopyread
Fair enough, Michael. FWIW I find Docker works best when each container does a single thing. Instead of e.g. a single VM with version control, an IDE, a web server, a database server, and whatever language you work in you might have one Docker container that hosts the application code, another for your database, and use an editor or IDE, version control, web browser, etc. on your host machine. I think this is more in line with the project's goals, too. Whatever you end up with I hope you find something satisfactory.Retharethink
To satisfy the curiosity, a good reason would be if your deployed application has a dependency that is taken directly from e.g. github instead of a deployed package (let's say if you forked a package)Necrotomy
C
51

This Dockerfile works for me

FROM ubuntu:18.04
RUN apt update
RUN apt install -y git

Inside the container

$ which git
/usr/bin/git
Calculator answered 12/4, 2019 at 20:55 Comment(1)
You can also run apt update inside of the container if you don't want to rebuild it. But installing what you need when building container itself as shown here is the better approach.Tutelage
G
8
apt-get update
apt-get install git
Globe answered 18/10, 2021 at 12:42 Comment(1)
This doesn't appear to add anything to what's already in the accepted answer. Yes, you can use apt-get instead of apt (and on really old systems you have to).Tifanytiff
G
0

it's better to clean the docker layer after install

RUN  apt-get update \
  && apt-get install -y git\
  && rm -rf /var/lib/apt/lists/*

Grume answered 5/7 at 10:52 Comment(1)
This should probably be a comment on one of the existing answers rather than a separate new answer.Tifanytiff

© 2022 - 2024 — McMap. All rights reserved.