Select Page

Introduction

Alpine is a very lightweight Linux distribution, which uses the musl library for running C applications, instead of the standard libc library. This causes it to lack support for C++ applications out of the box. Nevertheless, the small footprints of Alpine Linux allows it to be the go-to choice for Docker images. In fact, there is an Alpine Linux build dedicated for use in Docker, right from their official website.

However, every good thing requires a price to be paid – that is, many commonly used applications in other Linux distributions are not available in Alpine Linux, especially those written in C++. Despite the fact that git is pretty much 100% written in C only, you still might not be able to install git in your Alpine Linux instance right away (there’s a reason why I said might not). 

In this article, we’ll discuss all the possible ways of installing git in a standard system wide installation of Alpine Linux, as well as inside a Docker image.


Update your System

In a lot of cases, the repositories that host the programs need to be updated before you can use a package manager to install them in your Linux machine. You might want to first check whether it is only the git package which you’re unable to install, or if it’s the same if you try to install some other package as well. 

If you’re unable to install any programs at all, the apparent fix to that in Alpine Linux (as well as in any other Linux distributions), would be to update the repository using the package manager. To do this, run the following line of command in your machine:

$ sudo apk update

This should pretty much do the job. Once the update is over, you can now try to install git once again. The following sections will discuss that in detail, where you’re using a standard installation of Alpine Linux in a hard disk, or inside a docker image.


Install git in standard Alpine Linux installation

In previous versions of Alpine Linux, git was not available in the official repositories. But quite recently, git has finally been added as an official package, and can directly be installed using the Alpine Package manager (apk).

To install the official git package in a standard installation of Alpine Linux, run the following command in your terminal:

$ sudo apk add git

Notice the use of sudo in the above command. It is necessary given that you’re using a different user than root. Once done, verify that the installation was successful by running:

$ git --version

If you see a positive output that includes a version number, and no errors, git has successfully been installed in your Alpine Linux machine.

There’s still one big aspect of installing git that we haven’t covered just yet – that is, to install git inside a Docker Container of Alpine Linux. This one might actually be more common among users as Alpine is the go-to distro for Docker containers. 

The next section will discuss the different ways by which you can set up a docker container which contains git, installed inside a running Alpine Linux image.


Install git in Alpine Linux Docker Image

Install Git inside an already running Alpine Linux Container

The first situation is when you need to install git inside an already running (or preinstalled) container of Alpine Linux. To do this, you’d simply follow the very same procedure as shown in previous section, which is to run the following command, from inside your container:

$ sudo apk add git

But there’s a problem. It is very much possible that your version of Alpine inside the container is outdated, and does not have the git package in its repo. To fix this, you’d need to update your containerized system to the latest version. First try updating the system by running:

$ sudo apk update

If this does not help, you will first need to remove the existing image of alpine and install the latest version, before you can install git in it.


Using Dockerfile to create an Image with Git installed

The second option is to create a new image of alpine linux which contains git, by using Dockerfile. What’s good about this method is that using a single Dockerfile, you can install git with your own configuration of the system’s image, again and again without needing to follow the same process every time. Moreover, you can share the file with others so they can get their own containers with git installed.

Let us start off by creating a very basic Dockerfile. You can extend it later on by adding up your own spices, but for that you’ll need a bit more knowledge in creating Dockerfiles. For now, these are the steps you’d follow:

Step 1

Enter a directory where you’d like to store the Dockerfile (this is crucial)

Step 2

Create a dockerfile by using the text editor of your choice. For example, using nano:

$ nano Dockerfile

Step 3

Inside this file, insert these following texts which consists of a barebones setup for an image with git installed:

FROM alpine:latest
RUN apk update
RUN apk add git

Save and then exit the file, and follow the next step.

Step 4

While you’re inside the same directory as the Dockerfile, run the following command to build the image:

$ sudo docker build -t alpine-container .

Step 5

Now verify that the image has successfully been built and has been assigned an Image ID, by running:

$ sudo docker images

Step 6

You can now finally run the container by appending the name you had assigned the image to the following command:

$ sudo docker run -it <name_of_container>

which in our case is:

$ sudo docker run -it alpine-container

Et Voila! You should now be able to run git from inside this newly created alpine linux container.