Saving changes to a Container and push to DockerHub

Raghav Jindal
3 min readJan 18, 2021

When working with Docker images and containers, one of the basic features is committing changes to a Docker image. When you commit to changes, you essentially create a new image with an additional layer that modifies the base image layer.

Prerequisites

  • Access to a command-line/terminal window (Ctrl+Alt+T)
  • A user account with root or sudo privileges
  • Docker installed and configured

Pull a Docker Image

To illustrate how to commit changes, you first need to have an image to work with. In this article, we work with the latest Ubuntu image for Docker. Download the image from Docker’s library with:

sudo docker pull ubuntu

Deploy the Container

sudo docker run -it cf0f3ca922e0 bin/bash

The –it options instruct the container to launch in interactive mode and enable a terminal typing interface. Upon executing the command, a new container launches and moves you to a new shell prompt for working inside of it.

Modify the Container

Now that you are in the container, you can modify the image. In the following example, we add the apache HTTP server that delivers web content through the internet. By default sudo is not installed on Debian, but you can install it. First enable su-mode:su -

Install sudo by running:
apt-get install sudo -y

sudo apt update
sudo apt install apache2

The command will download the Nmap package and install it inside the running container.

You can verify the installation by running:

service apache2 status
service apache2 start

The next step is to commit these changes to the container.

Commit Changes to Image

You can first exit from the iterative mode by just entering the command — exit now do docker ps -a and note the container id of the image you were working upon

sudo docker commit 5ddef65as5 ubuntu-apache

Your newly created image should now be available on the list of local images. You can verify by checking the image list again:

sudo docker images

now before going further we will check that if the image is working properly and for that write the command

sudo docker run -it -p 80:80 -d ubuntu-apache
docker exec -it a24b781784ee bash
service apache2 start

Now open a web browser and search for localhost:80 ,you will see that apche web server page.

Pushing to Docker Hub

For that, you first need to authenticate your terminal with your docker hub credentials .Assuming that you have created a docker hub account on you terminal just write this command sudo docker loginand it will ask you about the login id and password.

sudo docker push ubuntu-apache

you may receive an error as your image name is not according to the nomenclature provided by docker. inc

dockertag ubuntu-apache raghav131/ubuntu-apache

All set. Now just visit your docker hub account and there you will be able to see your freshly created docker image with an apache web server installed in it.

--

--

Raghav Jindal

Cloud Computing || MERN Stack Development || AWS || Penetration testing || AI & ML || Virtualization||Data Science