Skip to content

Instantly share code, notes, and snippets.

@mauvehed
Created November 21, 2023 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mauvehed/9a2a62225e90aa8a3cc445cbe75d8e4f to your computer and use it in GitHub Desktop.
Save mauvehed/9a2a62225e90aa8a3cc445cbe75d8e4f to your computer and use it in GitHub Desktop.
Save a running docker as a new image

Save a running docker as a new image

The following will show you how to create a new docker image from a running container that you have modified. The image will only exist locally, meaning you can only access and use it on the computer you create it. It will not be associated with any container registry.

We will start with the ubuntu container image as our base.

Run and attach to a container

docker run -a stdin -a stdout -it ubuntu /bin/bash

Make and save changes

Make whatever changes you want to the running container, save the changes, but do not stop the container. This likely means you'll need to run the commands after this point from another terminal:

docker commit <container name>

Find the untagged image

This creates a new image with no repository or tag (easy to find). Search for it with the following command:

docker images | head

Add a tag to the image

Now tag the newly created image created from the previous command

docker tag IMAGE_ID ubuntu-my-changes

See it all in action

docker run -a stdin -a stdout -it ubuntu /bin/bash
❯ docker ps |grep ubuntu
90674bed3b2e   ubuntu   "/bin/bash"   3 minutes ago   Up 3 minutes   quizzical_almeida
❯ docker commit quizzical_almeida
sha256:a31f3a7f9ffe1be20ba85e5dad6e996bd1c95c1aa3dbf6f00d6e99a134479879
❯ docker images | head -3
REPOSITORY                           TAG              IMAGE ID       CREATED         SIZE
<none>                               <none>           a31f3a7f9ffe   7 minutes ago   397MB
❯ docker tag a31f3a7f9ffe ubuntu-my-changes
❯ docker images | head -3
REPOSITORY                           TAG              IMAGE ID       CREATED         SIZE
ubuntu-my-changes                     latest           a31f3a7f9ffe   8 minutes ago   397MB
docker container stop quizzical_almeida
docker container rm quizzical_almeida
docker run -a stdin -a stdout -it ubuntu-my-changes /bin/bash

And we're done!

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment