How to commit changes to a docker image

If you'd like to use docker images as a template for efficient container deployment, Jack Wallen shows you how to commit changes to a running container to create a new docker image.

Image: Jack Wallen

One way your company can take advantage of docker containers is by creating template images to be used for your containers. Say, for instance, you use containers for development purposes and want to ensure that all deployed containers start out with the same basic tools. You could pull down an image, run the container, modify the container to meet your needs, and then begin developing with that container, only to have to run through the same process, every time you want to deploy that same container.

Or, you could pull down an image, run the container, modify the container to meet the basic needs of your developers, and commit those changes to a new image. Now all your developers need to do is run a new container, based on the modified image, and they're good to go. No need to modify the original image to meet their needs.

If that sounds like a process you'd want to take advantage of, you're in luck — it's really quite simple. Let's say you need an image, based on the latest from NGINX, with PHP, build-essential, and nano installed. I'll walk you through the process of pulling the image, running the container, accessing the container, adding the software, and committing the changes to a new image that can then be easily used as a base for your dev containers.

Pulling the image and running the container

The first step is to pull the latest NGINX image. This is done with the command:

sudo docker pull nginx

Once the image has downloaded, we're going to run it such that we can use the terminal window like so:

sudo docker run --name nginx-template-base -p 8080:80 -e TERM=xterm -d nginx

I've named this nginx-template-base as that will be what our template will be based on.

Accessing and modifying the container

Next we need to access the container. When you ran the docker run command, it will have presented you with a long ID number. You'll need that number to access the image. Run the command:

sudo docker exec -it CONTAINER_ID bash

Where CONTAINER_ID is the ID presented to you when you ran the run command.

After running this command you will find yourself in the terminal of the running container. Now, let's add the necessary software for the template. To do this, issue the following commands:

apt-get install nano
​apt-get install build-essential
​apt-get install php5

NOTE: For the official NGINX image, PHP 7 is not available for installation without adding a repository.

Exit the container and commit the changes

Now that we've modified the container we have to commit the changes. First exit the container with the command exit. To commit the changes and create a new image based on said changes, issue the command:

sudo docker commit CONTAINER_ID nginx-template

Where CONTAINER_ID is the ID given to you when you initially ran the container.

If you issue the command docker images, you should now see the new container (Figure B).

Figure B

Figure A

The newly created template image is ready for use.

At this point, you can spin up a new container, using the new image, and have all the modifications already in place. Remember, when you run the new container, the command would look something like:

sudo docker run --name nginx-dev -p 8080:80 -e TERM=xterm -d nginx-template

If you access this new running container (using the sudo docker exec command), you will now see all of the modifications are in place and ready to be used.

An easy route to container templates

If you've been looking for a way to make working with docker containers a bit more efficient, this should go a long way to providing just that. Once you get used to creating template images, developing with Docker containers becomes significantly easier.

Also see

About Jack Wallen

Jack Wallen is an award-winning writer for TechRepublic and He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website

Editor's Picks

Free Newsletters, In your Inbox