Skip to content

Instantly share code, notes, and snippets.

@mauvehed
Created November 21, 2023 14:48
Show Gist options
  • Save mauvehed/9a2a62225e90aa8a3cc445cbe75d8e4f to your computer and use it in GitHub Desktop.
Save mauvehed/9a2a62225e90aa8a3cc445cbe75d8e4f to your computer and use it in GitHub Desktop.

Revisions

  1. mauvehed created this gist Nov 21, 2023.
    62 changes: 62 additions & 0 deletions docker_image_save.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    ## 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/