Container Basics: How to Commit Changes to a Docker Image

4 Jun 2019 12:00pm, by

Docker is a tool that can help make your company more agile and flexible. With containers, you can quickly roll out services based on pre-built or custom images. Even with this level of flexibility, there are many ways you can make Docker containers even more efficient. One such method is creating template images that will be used as the base for your containers. Say, for example, you need to be able to deploy containers all based on an image that contains a specific set of tools (for example an NGINX image with MySQL, PHP, build-essentials, and nano). Instead of having to build that same image every time you roll out a container, why not create a single image, with those exact tools, that can be used as a template for all of your containers?

Believe it or not, the creation of such a template isn’t all that hard. To do so, you build your image, commit the changes to the image, and you’re ready to start deploying containers.

Using this method makes for a much more efficient process, one that will go a long way to boost your container productivity.

Let’s make this happen.

I’ll be demonstrating the process using the official NGINX image and will be customizing the image from Docker Hub. I will assume you already have Docker up and running on your platform of choice and are ready to go.

Pulling the Official Image

The first step in the process is pulling down the official image from Docker Hub. To pull this image, issue the command:

The image should pull down fairly quickly (Figure A).

Figure A: Pulling down the official NGINX image from Docker Hub.

With the image downloaded, we can now deploy the container so that it can be customized to meet our specific needs.

Deploying the Container

What we have to do is deploy our new container in such a way that we have access to the associated bash prompt (so we can work within the container). To do that, we deploy with the command:

The above command break down look like this:

  • docker run instructs Docker that we are running a new container.
  • –name nginx-template instructs Docker to name the new container nginx-template
  • -p 8080:80 instructs Docker to expose the internal container port 80 to the network port 8080.
  • -e TERM=xterm defines our terminal variable.
  • -d launches the container in the background.
  • nginx is the name of the image to be used for the container.

Accessing and Modifying the Container

Our next step is to gain access to the container. When you issue the above command, Docker will report back the ID of the contain (Figure B). This ID is what you use to access the container.

Figure B: Our newly deployed container ID.

What we need to use is the first four digits of the container ID. So in our example, we’d use b1d5.

NOTE: When you run the command, you will be given a completely different ID.

At this point, you will find yourself at the bash prompt for the NGINX container (Figure C).

Figure C: The bash prompt for our newly-deployed container.

Installing Our Tools

The next step is installing the necessary tools. Remember, we’re going to install build-essential, PHP, MySQL, and nano. Before you attempt to install anything, you’ll first need to update apt with the command:

Once that command completes, install the necessary software with the following commands:

When you’ve completed the above commands exit from the NGINX bash prompt with the exit command.

Committing Your Changes

Time to commit your changes to create a new image based on our additions. To do this, we need to once again use the container ID (in our example the first four characters, b1d5). When we commit these changes we effectively create a new image that will include all of the additions that were made to the original image. The command to do this is:

This command will complete in less than 30 seconds. When it finishes, issue the command docker images to see that you now have a newly created NGINX image that contains MySQL, PHP, build-essential, and nano (Figure D).

Figure D: Our newly created image is listed.

Congratulations, you now have a customized image for which to base your containers. You could deploy a new container with this image using a command:

Once the container is deployed, access it’s bash prompt (in the same manner you did earlier) and you will see all of the additional software is there, ready to be used.

Simple Container Templates

This is just one route to making container deployment easier. If you tend to roll out containers, and find yourself having to constantly add the same base software to the image, you might want to consider using this approach, to make the process significantly more efficient.

Feature Image by StartupStockPhotos from Pixabay.

A newsletter digest of the week’s most important stories & analyses.