Create and Commit an Ubuntu Container with Git Installed

arif_hossain

Arif Hossain

Posted on November 9, 2024

Create and Commit an Ubuntu Container with Git Installed

Create and Commit an Ubuntu Container with Git Installed
This session will guide us through creating an Ubuntu container, installing Git, and committing the changes to a new image. Additionally, we'll learn how to set an entrypoint to make using the image more efficient.

Task
We will perform the following steps:

  • Create an ubuntu container and open a bash session.
  • Install git inside the container and verify the git.
  • Create new image by using commit.
  • Set the entrypoint for the new image to make it easier to use.

Simple Explanation of the Process
In this lab, we will start by creating a container from the Ubuntu image and open a bash session within it. Inside this container, we will install git and verify that the installation was successful by checking the git version. After exiting the container, we will commit these changes to create a new Docker image that includes git. We will run a container from that image.

Image description

Finally, we will set an entrypoint for this new image to make it easier to use git directly without needing to specify the git command each time we start a container from this image.

Image description

Steps
Create an Ubuntu container and open a bash session:

docker run -it --name image-dev ubuntu:latest /bin/bash
Enter fullscreen mode Exit fullscreen mode

This command creates a new container named image-dev from the ubuntu:latest image and opens an interactive bash session.

Install Git inside the container:

apt-get update
apt-get install -y git
Enter fullscreen mode Exit fullscreen mode

This updates the package list and installs Git in the container.

Verify the Git installation by checking its version:

git --version
Expected output:

root@d15b204bcec5:/# git --version
git version 2.43.0
Enter fullscreen mode Exit fullscreen mode

This command confirms that Git was installed correctly.

Exit the container:

exit
Enter fullscreen mode Exit fullscreen mode

This command exits the interactive bash session and returns to the host terminal.

Review the filesystem changes and commit these changes to create a new image:

docker container commit -a "@arif" -m "Added git" image-dev ubuntu-git
Enter fullscreen mode Exit fullscreen mode

This command commits the changes made in the image-dev container to a new image named ubuntu-git with an author tag and a commit message.

Remove the modified container:

docker container rm -vf image-dev
Enter fullscreen mode Exit fullscreen mode

This command forcefully removes the image-dev container to clean up.

Verify the new image by checking the Git version in a new container:

docker container run --rm ubuntu-git git --version
Enter fullscreen mode Exit fullscreen mode

This command runs a temporary container from the ubuntu-git image to verify that Git is installed correctly.

Setting the Entrypoint to Git
Create a new container with the entrypoint set to Git:

docker container run --name cmd-git --entrypoint git ubuntu-git
Enter fullscreen mode Exit fullscreen mode

This command creates a new container named cmd-git with the entrypoint set to git, showing the standard Git help and exiting.

Image description

Commit the new image with the entrypoint:

docker container commit -m "Set CMD git" -a "@poridhi" cmd-git ubuntu-git
This command commits the changes to the ubuntu-git image, setting the entrypoint to Git.

Remove the modified container:

docker container rm -vf cmd-git
Enter fullscreen mode Exit fullscreen mode

This command forcefully removes the cmd-git container to clean up.

Test the new image:

docker container run --name cmd-git ubuntu-git version
Enter fullscreen mode Exit fullscreen mode

This command runs a new container from the ubuntu-git image, verifying that the entrypoint is set correctly and showing the Git version:

git version 2.43.0
Enter fullscreen mode Exit fullscreen mode

Image description

This setup ensures that any container started from the ubuntu-git image will automatically use Git as the entrypoint, making it easier for users to work with Git directly.

💖 💪 🙅 🚩
arif_hossain
Arif Hossain

Posted on November 9, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related