Home Explore Blog CI



docker

2nd chunk of `content/get-started/docker-concepts/building-images/understanding-image-layers.md`
d771c39d7478745553960046c08bbc33d7680e1fc4b984b20000000100000ea5
When the union filesystem is created, in addition to the image layers, a directory is created specifically for the running container. This allows the container to make filesystem changes while allowing the original image layers to remain untouched. This enables you to run multiple containers from the same underlying image.

## Try it out

In this hands-on guide, you will create new image layers manually using the [`docker container commit`](https://docs.docker.com/reference/cli/docker/container/commit/) command. Note that you’ll rarely create images this way, as you’ll normally [use a Dockerfile](./writing-a-dockerfile.md). But, it makes it easier to understand how it’s all working.

### Create a base image

In this first step, you will create your own base image that you will then use for the following steps.

1. [Download and install](https://www.docker.com/products/docker-desktop/) Docker Desktop.


2. In a terminal, run the following command to start a new container:

    ```console
    $ docker run --name=base-container -ti ubuntu
    ```

    Once the image has been downloaded and the container has started, you should see a new shell prompt. This is running inside your container. It will look similar to the following (the container ID will vary):

    ```console
    root@d8c5ca119fcd:/#
    ```

3. Inside the container, run the following command to install Node.js:

    ```console
    $ apt update && apt install -y nodejs
    ```

    When this command runs, it downloads and installs Node inside the container. In the context of the union filesystem, these filesystem changes occur within the directory unique to this container. 

4. Validate if Node is installed by running the following command:

    ```console
    $ node -e 'console.log("Hello world!")'
    ```

    You should then see a “Hello world!” appear in the console.

5. Now that you have Node installed, you’re ready to save the changes you’ve made as a new image layer, from which you can start new containers or build new images. To do so, you will use the [`docker container commit`](https://docs.docker.com/reference/cli/docker/container/commit/) command. Run the following command in a new terminal:

    ```console
    $ docker container commit -m "Add node" base-container node-base
    ```

6. View the layers of your image using the `docker image history` command:

    ```console
    $ docker image history node-base
    ```

    You will see output similar to the following:

    ```console
    IMAGE          CREATED          CREATED BY                                      SIZE      COMMENT
    d5c1fca2cdc4   10 seconds ago   /bin/bash                                       126MB     Add node
    2b7cc08dcdbb   5 weeks ago      /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B
    <missing>      5 weeks ago      /bin/sh -c #(nop) ADD file:07cdbabf782942af0…   69.2MB
    <missing>      5 weeks ago      /bin/sh -c #(nop)  LABEL org.opencontainers.…   0B
    <missing>      5 weeks ago      /bin/sh -c #(nop)  LABEL org.opencontainers.…   0B
    <missing>      5 weeks ago      /bin/sh -c #(nop)  ARG LAUNCHPAD_BUILD_ARCH     0B
    <missing>      5 weeks ago      /bin/sh -c #(nop)  ARG RELEASE                  0B
    ```

    Note the “Add node” comment on the top line. This layer contains the Node.js install you just made.

7. To prove your image has Node installed, you can start a new container using this new image:

    ```console
    $ docker run node-base node -e "console.log('Hello again')"
    ```

    With that, you should get a “Hello again” output in the terminal, showing Node was installed and working.

8. Now that you’re done creating your base image, you can remove that container:

Title: Creating a Base Image with Node.js
Summary
This section guides you through creating a base image with Node.js installed by manually committing container changes. You'll start an Ubuntu container, install Node.js, and then use `docker container commit` to save the changes as a new image layer. Verifying the image history shows the added Node.js layer. Finally, you'll run a new container from the created image to confirm Node.js is working and remove the initial container.