VOOZH about

URL: https://thenewstack.io/tutorial-create-a-docker-image-from-a-running-container/

⇱ Tutorial: Create a Docker Image from a Running Container - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2022-01-17 05:00:45
Tutorial: Create a Docker Image from a Running Container
tutorial,
Cloud Native Ecosystem / Containers

Tutorial: Create a Docker Image from a Running Container

What we're going to do is deploy a container, for an NGINX server, modify it, and then create a new image from that running container that you can then use to base new containers from.
Jan 17th, 2022 5:00am by Jack Wallen
👁 Featued image for: Tutorial: Create a Docker Image from a Running Container

If you’re just beginning your journey with containers, chances are pretty good it will start with Docker. Why? Because Docker actually makes learning and working with container technology pretty easy. If you were instead to jump right into Kubernetes, you’d get lost really fast.

So, most people should seriously consider taking their first steps with Docker. And today, I’m going to walk you through some of the first things you might do with Docker containers. What we’re going to do is deploy a container, for an NGINX server, modify it, and then create a new image from that running container that you can then use to base new containers from.

Why would you do this? Say, for example, you do use NGINX for most of your web-based container deployments. Instead of having to always deploy a new NGINX container and then modify it to meet your baseline needs, you can create a single image that already contains that baseline and avoid a lot of repetitious work.

It’s all in the name of efficiency.

With that said, let’s get to work.

Install Docker

On the off-chance you don’t already have Docker installed, let’s do so. I’ll be demonstrating on my go-to Ubuntu Server (version 20.04). If you use a different distribution of Linux for your container deployments, you’ll need to only modify the installation steps for this process.

To install Docker on your Ubuntu Server, log in and issue the following command:

sudo apt-get install docker.io -y

After the installation completes, add your user to the docker group with:

sudo usermod -aG docker $USER

Log and out log back in so the changes take effect.

Create the New Container

With Docker ready, let’s create the new container. This will be a very basic web server, using NGINX. The command looks like this:

docker create --name nginx-base -p 80:80 nginx:alpine

For those who’ve never worked with Docker, that command does the following:

Creates a new container with the name nginx-base, that runs on internal (guest) port 80 and external (host) port 80, and uses the nginx:alpine image that it will have to pull down from DockerHub.

The command will respond with the container ID, which is a long string of random characters, indicating the deployment was successful. If you open a web browser and point it to the IP address of the hosting server, you should see the NGINX welcome page (Figure A).

👁 The NGINX welcome page, as produced by our Docker container.

Figure A: The NGINX welcome page, as produced by our Docker container.

Modify the Existing Container

Now, it’s time to modify our existing container. We’re only going to do a very basic modification (which you can later expand on to your heart’s content). What we’re going to do is create a new index.html page for NGINX to serve.

To do this, let’s create the new page with the command:

nano index.html

In that file, paste the following contents (you can modify it to say whatever you want):

<html>
<head>
<title>Hello, New Stack!</title>
</head>
<body>
<h1>Hello, New Stack!</1>
</body>

Save and close the file.

As you can see, the new welcome page will say “Hello, New Stack! Anyone that’s created a “Hello, World” application will recognize that immediately.

Okay, for our next trick, we’re going to copy that file to the running container. With the NGINX image, the document root (the base directory that houses web pages) is /usr/share/nginx/html. And because Docker has a built-in copy command, adding the file to the running container is very simple.

Remember, our container is called nginx-base. The command to copy index.html to the document root on nginx-base is:

docker cp index.html nginx-base:/usr/share/nginx/html/index.html

If you refresh the page in your web browser, you should see it now displays the new welcome message (Figure B).

👁 Image

Figure B: Our new welcome message within our running Docker container.

Create a New Image—++++

Alrighty then, we have the newly modified (and running) container. How do we create a new image that includes the changes? It’s actually very simple.

First, we commit the changes with the command:

docker commit nginx-base

What this will do is create a new image without a repository or tag. List out the current images with the command:

docker images

What you should see is something like this:

REPOSITORY   TAG       IMAGE ID         CREATED              SIZE
<none>      <none>    7fb3d1656c23   About a minute ago   23.5MB
nginx       alpine    cc44224bfe20   7 days ago           23.5MB

The bottom image is the one we used to create our new container. The top image is the one we just created. Notice that it doesn’t have either a REPOSITORY (the first column) or a TAG (the second column). For this image to be usable, we have to tag it. In order to tag the image, we have to use the IMAGE ID as an identifier, so tag the image (we’ll name it docker-base-image) like this:

docker tag IMAGE_ID nginx-base-container

Where IMAGE_ID is the actual ID of your new container.

Now, if you list the images (using the docker images command), you’ll see something like this:

REPOSITORY               TAG       IMAGE ID         CREATED         SIZE
nginx-base-container   latest    7fb3d1656c23   6 minutes ago      23.5MB
nginx                   alpine    cc44224bfe20   7 days ago        23.5MB

There you go, you’ve created a new Docker image from a running container. Let’s stop and remove the original container. To do that, locate the ID of the original with the command:

docker ps -a

With the first 4 characters of the original container ID, stop it with:

docker stop ID

Where ID is the first four digits of the original container.

Remove the container with the command:

docker rm ID

Where ID is the first four digits of the original container.

You could then deploy a container from the new image with a command like:

docker create --name nginx-new -p 80:80 nginx-base-container:latest

Refresh your web browser and you should, once again, see the new Hello, New Stack! welcome page. The new image includes our modifications to the index.html page, so every container created from it will reflect that change.

And that’s how easy it is to create a Docker image from a running container. This skill will come in very handy (especially as you build on it as you go).

TRENDING STORIES
Jack Wallen is what happens when a Gen Xer mind-melds with present-day snark. Jack is a seeker of truth and a writer of words with a quantum mechanical pencil and a disjointed beat of sound and soul. Although he resides...
Read more from Jack Wallen
SHARE THIS STORY
TRENDING STORIES
NGINX is a sponsor of The New Stack.
TNS owner Insight Partners is an investor in: Docker.
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.