Creating a Docker Image with Git Installed

Version control is one of the most important aspects of any software development project and when we talk about version control, there is no better tool than Git. The majority of the developers depend upon Git to manage and share their project components among the team members. 

Even if you are running your project on Docker, you can still access your git account inside Docker Containers. All you need to do is just install Git inside your Docker Container. In this article, we will discuss exactly the same. We will create an Ubuntu Image, install Git inside it, create a Container associated with the Image, and verify whether Git has been installed or not.

What is Git?

Git is a distributed version control system (DVCS) that allows developers to track changes in their codebase, collaborate with others, and manage different versions of their projects efficiently. For more detail view about the git refer this link.

What is Docker Image?

Docker images are built using the Dockerfile which consists of a set of instructions that are required to containerize an application. The docker image includes the following to run a piece of software. A docker image is a platform-independent image that can be built in the Windows environment and it can be pushed to the docker hub and pulled by others with different OS environments like Linux. For more detail view about the docker image refer this link.

Create the Dockerfile

Create the dockerfile by using the below command. It creates the dockerfile, and we need to add the instructions to the dockerfile.

touch Dockerfile

Select a Base Image

Select the base image based on your requirements. Here we have selected Ubuntu as the base image for the latest version.

FROM ubuntu:latest

Install Git

First we need to update the package manager because the images are light weight. Next we need to add the git install command with out manual approve.

RUN apt-get -y update
RUN apt-get -y install git
  • RUN: This instruction is used to execute commands inside the Docker container during the image build process.
  • apt-get: This is the package management tool used in Ubuntu-based systems for installing, upgrading, and removing software packages.
  • -y: This flag tells apt-get to assume “yes” to all prompts and proceed with the installation without asking for user confirmation.
  • update: This sub-command for apt-get updates the package lists for available software packages and their versions. It does not upgrade the packages themselves but ensures that the system knows what packages are available and what versions are currently installed.
  • install git: This sub-command for apt-get installs the Git package.
  • git: Git is a widely used distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

Contents of the Dockerfile

Here are the contents in the dockerfile for installation of the git using the dockerfile. Based on your base image and requirements you can change the contents on the dockerfile.

FROM ubuntu:latest
RUN apt-get -y update
RUN apt-get -y install git

In the above Dockerfile, we have specified instructions to pull the Ubuntu base image, update the OS, and install Git inside it.

Build the Docker Image

After creating the Dockerfile, we can build the Docker Image using the Docker build command.

sudo docker build -t sample-image .
  • sudo: This command is used to execute the subsequent command with administrative privileges. It grants the user permission to perform administrative tasks.
  • docker: This is the command-line interface (CLI) tool used to interact with Docker, which is a platform for developing, shipping, and running applications inside containers.
  • build: This is a Docker subcommand that is used to build an image from a Dockerfile and a context. The context is the set of files located in the specified path or directory.
  • -t sample-image: The -t option tags the resulting image with a name. In this case, sample-image is the name chosen for the image. This allows you to reference the image by name later on when running containers.
  • .: The dot (.) at the end of the command specifies the build context. It indicates the current directory as the build context. The build context includes all files in the current directory and its subdirectories. These files are sent to the Docker daemon during the build process.

Checking Docker Images

To verify whether the image has been built or not, you can list all the Images.

sudo docker images

Run the Docker Container

After you have built the Image, you can run the Container associated with the Image, using the Docker Run command.

sudo docker run -it sample-image bash
  • sudo: This command is used to execute subsequent commands with superuser privileges. Docker typically requires superuser privileges to run, hence the use of sudo.
  • docker: This is the command-line interface (CLI) tool used to interact with the Docker daemon. It allows you to build, manage, and run Docker containers.
  • run: This subcommand is used to run a command in a new container.
  • -it: These are two separate options combined together.
    • The -i option stands for “interactive” and keeps STDIN open even if not attached.
    • The -t option allocates a pseudo-TTY, which allows you to interact with the container through a terminal interface.

    Together, -it makes the container run in an interactive mode, allowing you to input commands and see their output as if you were directly using the terminal inside the container.

  • sample-image: This is the name of the Docker image you want to run a container from. In our case, it’s the image we built earlier, named “sample-image”.
  • bash: This is the command to execute inside the container. In this case, we’re telling Docker to run the Bash shell within the container. This will give you a shell prompt inside the container, allowing you to interact with it and execute commands.

The above command creates and runs a Container and fires up the bash of the Docker Container.

Verifying the Installation

After you have the bash opened up, you can verify whether Git has been installed or not by checking the version of Git.

git --version

This command returns the version of the git installed.

Try to Use Git on the Docker

Here we have tried few commands on inside the docker container refer the below image for your reference.

Conclusion

we discussed the integration of Git within Docker containers to facilitate version control in software development projects. By creating an Ubuntu image with Git installed, building the Docker image, and running a container, developers can seamlessly manage their projects. This streamlined process ensures accessibility to Git functionalities within Docker environments, enhancing collaboration and code management.

Docker Image with Git Installed – FAQs

How to create a Docker image with git installed?

To create a Docker image with Git installed, write a Dockerfile with instructions to pull the Ubuntu base image and install Git using apt-get, then build the image using docker build.

How to commit Docker image to GitHub?

To commit a Docker image to GitHub, tag the image with your GitHub repository URL, then push it using the Docker CLI:

docker tag image_name username/repository_name:tag
docker push username/repository_name:tag

How to connect docker with Git?

To connect Docker with Git, install Git inside a Docker container using a Dockerfile, build the image, and run the container with Git installed.

How do I add an image to my GitHub repository?

To add an image to your GitHub repository, navigate to the repository, click on “Upload files,” then select the image and commit changes.

How to set Git config in docker?

To set Git config in Docker, use the following commands:

RUN git config --global user.name "Your Name"
RUN git config --global user.email "your.email@example.com"