DEV Community

Janak Shrestha
Janak Shrestha

Posted on

Create a Docker Image From Container

One of the Nautilus developer was working to test new changes on a container. He wants to keep a backup of his changes to the container. A new request has been raised for the DevOps team to create a new image from this container. Below are more details about it:

a. Create an image apps:devops on Application Server 1 from a container ubuntu_latest that is running on same server.


A Step-by-Step Guide for Nautilus DevOps


📌 Introduction

In the world of containerization, there are times when developers make changes inside a running container and need to preserve those modifications for future use. Whether it's for testing, backup, or deployment, creating a new image from an existing container is a valuable skill.

In this blog post, I'll walk you through the process of creating a Docker image from a running container, using a real-world scenario from the Nautilus DevOps project.


🎯 The Task

Requirement: Create an image named apps:devops from the ubuntu_latest container running on Application Server 1 (stapp01).

Why? A developer has been testing new changes in the container and needs a backup of their work.


🛠️ Step-by-Step Process

1️⃣ Connect to the Server

First, SSH into the target server with appropriate credentials:

ssh tony@stapp01
# Password: Ir0nM@n
Enter fullscreen mode Exit fullscreen mode

Then switch to root for Docker operations:

sudo su -
# Password: Ir0nM@n
Enter fullscreen mode Exit fullscreen mode

2️⃣ Verify the Container is Running

Before creating an image, ensure the container exists and is running:

docker ps | grep ubuntu_latest
Enter fullscreen mode Exit fullscreen mode

Expected output:

ff6daf286489   ubuntu    "/bin/bash"   3 minutes ago   Up 3 minutes             ubuntu_latest
Enter fullscreen mode Exit fullscreen mode

Check: Container ID is ff6daf286489, running on Ubuntu image.

3️⃣ Create the Image Using docker commit

The docker commit command creates a new image from a container's current state:

docker commit ubuntu_latest apps:devops
Enter fullscreen mode Exit fullscreen mode

Output:

sha256:0b92b855a62f1c411ea519898656c3c02fe15878a54ad452930ca09e91606bbc
Enter fullscreen mode Exit fullscreen mode

Check: New image created with ID 0b92b855a62f

4️⃣ Verify the Image Was Created

docker images | grep apps
Enter fullscreen mode Exit fullscreen mode

Output:

apps         devops    0b92b855a62f   18 seconds ago   142MB
Enter fullscreen mode Exit fullscreen mode

Check: Image apps:devops exists with size 142MB.

5️⃣ Test the New Image

Run a test container from the new image:

docker run -it --rm apps:devops /bin/bash
Enter fullscreen mode Exit fullscreen mode

Inside the container, verify your files and configurations are preserved:

root@container:/# ls -la
root@container:/# exit
Enter fullscreen mode Exit fullscreen mode

Check: The image works correctly and preserves all changes.


🔍 Understanding docker commit

What It Does

  • Captures container state - All file system changes, installed packages, and configuration
  • Creates a new image - Based on the container's current layer
  • Preserves changes - Useful for backups and snapshots

Command Syntax

docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Enter fullscreen mode Exit fullscreen mode

Common Options

Option Description
-a, --author Add author information
-m, --message Add a commit message
-c, --change Apply Dockerfile instructions

Example with Options

docker commit \
  -a "Nautilus DevOps" \
  -m "Backup of development changes" \
  ubuntu_latest \
  apps:devops
Enter fullscreen mode Exit fullscreen mode

📈 Visualizing the Process

┌─────────────────────────────────────────────────────────────┐
│                     DOCKER DAEMON                          │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  ┌───────────────────────────────────────────────────────┐ │
│  │  ubuntu:latest (Base Image)                          │ │
│  └───────────────────────────────────────────────────────┘ │
│                            │                               │
│                            ▼                               │
│  ┌───────────────────────────────────────────────────────┐ │
│  │  ubuntu_latest (Running Container)                   │ │
│  │  • File modifications                                │ │
│  │  • Installed packages                                │ │
│  │  • Configuration changes                             │ │
│  └───────────────────────────────────────────────────────┘ │
│                            │                               │
│                    docker commit                           │
│                            │                               │
│                            ▼                               │
│  ┌───────────────────────────────────────────────────────┐ │
│  │  apps:devops (New Image)                             │ │
│  │  • Captures all container changes                    │ │
│  │  • Layers: ubuntu base + modifications               │ │
│  │  • Size: 142MB                                       │ │
│  └───────────────────────────────────────────────────────┘ │
│                                                             │
└─────────────────────────────────────────────────────────────┘
Enter fullscreen mode Exit fullscreen mode

💡 Key Takeaways

✅ When to Use docker commit

  • Quick backups of container states
  • Testing and debugging changes
  • Hotfixes in development environments
  • Temporary snapshots before major changes

❌ When NOT to Use docker commit

  • Production deployments (use Dockerfiles instead)
  • Long-term versioning (hard to track changes)
  • Team collaboration (no version control)
  • Automated pipelines (prefer declarative builds)

🔑 Best Practices

  1. Add meaningful commit messages for traceability
  2. Test the new image before using it
  3. Tag images properly for version management
  4. Consider Dockerfiles for repeatable builds

🔧 Advanced Examples

Commit with Custom Command

docker commit \
  --change 'CMD ["/bin/bash"]' \
  ubuntu_latest \
  apps:devops
Enter fullscreen mode Exit fullscreen mode

Commit with Environment Variables

docker commit \
  --change 'ENV APP_ENV=production' \
  --change 'ENV APP_PORT=8080' \
  ubuntu_latest \
  apps:devops
Enter fullscreen mode Exit fullscreen mode

Save and Share the Image

# Save as tar
docker save -o apps_devops.tar apps:devops

# Compress
docker save apps:devops | gzip > apps_devops.tar.gz

# Transfer to another server
scp apps_devops.tar tony@stapp02:/tmp/

# Load on another server
docker load -i apps_devops.tar
Enter fullscreen mode Exit fullscreen mode

🚨 Troubleshooting

Issue Solution
Container not found Check docker ps -a for stopped containers
Image already exists Remove with docker rmi apps:devops
Permission denied Use sudo or add user to docker group
Container has mounts Volumes are not saved; use Dockerfile instead

📝 Conclusion

Creating a Docker image from a running container is a simple but powerful technique. In this post, we successfully:

  1. ✅ Connected to App Server 1
  2. ✅ Verified the ubuntu_latest container
  3. ✅ Created the apps:devops image
  4. ✅ Verified and tested the new image

The apps:devops image is now ready for the Nautilus development team to use for their testing and deployment needs.


📚 Quick Reference

# Commit container to image
docker commit CONTAINER_NAME IMAGE_NAME:TAG

# View image history
docker history IMAGE_NAME:TAG

# Test image
docker run -it --rm IMAGE_NAME:TAG /bin/bash

# Save image
docker save -o filename.tar IMAGE_NAME:TAG

# Load image
docker load -i filename.tar
Enter fullscreen mode Exit fullscreen mode

Top comments (0)