Create a Docker Image From Running Container

Create a Docker Image From Running Container

Docker is a powerful tool for developers. It lets you package applications with all the necessary dependencies into containers. Containers are like lightweight virtual machines, but they share the host system’s kernel, making them much more efficient.

Sometimes, you might modify a container. Maybe you installed a new package or made some changes to the configuration. To save those changes, you can create a new Docker image from that container.

In this guide, I’ll show you how to create a Docker image from a container.

Why Create a Docker Image from a Container?

Here are some reasons:

  • Preserve Changes: You may have made changes inside a container that you want to keep. Creating an image saves those changes.
  • Debugging: If you’ve fixed an issue inside a container, you might want to save that fixed state.
  • Sharing: You might want to share a container with others, but you need to save it as an image first.

Creating an image from a container is a powerful way to capture the state of your application at any given moment.

Let’s dive in to create an image from a container.

Step 1: Creating a Container

Let’s start by creating a Docker container. We’ll use the Ubuntu image as an example:

 # docker run -it ubuntu /bin/bash

Once the container starts, you’ll see a command prompt. You’re now inside the container, running a Bash shell.

Let’s make some changes inside the container. For example, install the curl package:

 # apt update && apt install curl -y

Now, the container has curl installed. But what if you want to save this container’s state? That’s where creating an image from the container comes in handy.

Step 2: Identifying the Running Container

Before creating an image, you need to identify the running container. Use the docker ps command to list the running container:

 # docker ps

This command lists all running containers. You’ll see something like this:

CONTAINER ID   IMAGE     COMMAND       CREATED        STATUS        PORTS     NAMES
abc12345def6   ubuntu    "/bin/bash"   2 minutes ago  Up 2 minutes            amazing_sky

The CONTAINER ID is what you need. In this example, it’s abc12345def6.

Step 3: Committing a Container to an Image

Now, let’s create a new image from the running container using the docker commit command:

 # docker commit abc12345def6 my-ubuntu-image

Explanation:

  • abc12345def6 is the CONTAINER ID of the container you want to save.
  • my-ubuntu-image is the name of the new image you’re creating.

You can also add a commit message using:

 # docker commit -m "Added curl to Ubuntu" abc12345def6 my-ubuntu-image

This saves the container’s state as a new image. The changes you made, like installing curl, are now part of the image.

Step 4: Naming and Tagging the New Image

It’s a good idea to name and tag your image. This makes it easier to manage:

 # docker commit -m "Added curl to Ubuntu" -a "Your Name" abc12345def6 my-ubuntu-image:v1

Here’s what’s new:

  • -a “Your Name” adds an author name to the commit.
  • :v1 tags the image with version 1.

Tagging helps you keep track of different versions of your image. It’s a simple but powerful way to organize your Docker images.

Step 5: Verifying the Created Image

To make sure the image was created, list your Docker images:

 # docker images

You’ll see something like this:

REPOSITORY         TAG       IMAGE ID       CREATED             SIZE
my-ubuntu-image    v1        7d9495d03763   1 minute ago        122MB

Your new image is now ready to use!

Step 6: Using the New Image

You can use the new image to start a new container:

 # docker run -it my-ubuntu-image:v1 /bin/bash

This command starts a new container from the image you just created. Inside, you’ll find everything as you left it, including the installed curl package.

Conclusion

Creating a Docker image from a container is a helpful technique. It allows you to save a container’s state and reuse it later. Whether preserving changes, debugging, or sharing your work, this process is straightforward and powerful.

Now that you’ve learned how to do it, try it in your projects. It’s a skill that will come in handy.

FAQs

1. Can I create an image from a stopped container?

Yes, you can. The container doesn’t need to be running to commit it to an image.

2. What happens to the running container after I commit it to an image?

Nothing changes with the running container. It continues to run as usual. Committing just saves its current state as a new image.

3. How can I optimize the image size?

Remove unnecessary files and packages before committing. You can also use multi-stage builds for more control over image size.

4. Can I create a Docker image from multiple containers?

No, you can only create an image from one container at a time

About Hitesh Jethva

I am Hitesh Jethva, Founder and Author at Code2DevOps.com. With over 15 years of experience in DevOps and open source technologies, I am passionate about empowering teams through automation, continuous integration, and scalable solutions.

View all posts by Hitesh Jethva