Home Explore Blog CI



docker

3rd chunk of `content/get-started/docker-concepts/building-images/understanding-image-layers.md`
e3b43a8f5004f3fa6f9cec210e2486105322a0eaffb24d9500000001000009ee
    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:

    ```console
    $ docker rm -f base-container
    ```

> **Base image definition**
>
> A base image is a foundation for building other images. It's possible to use any images as a base image. However, some images are intentionally created as building blocks, providing a foundation or starting point for an application.
>
> In this example, you probably won’t deploy this `node-base` image, as it doesn’t actually do anything yet. But it’s a base you can use for other builds.


### Build an app image

Now that you have a base image, you can extend that image to build additional images.

1. Start a new container using the newly created node-base image:

    ```console
    $ docker run --name=app-container -ti node-base
    ```

2. Inside of this container, run the following command to create a Node program:

    ```console
    $ echo 'console.log("Hello from an app")' > app.js
    ```

    To run this Node program, you can use the following command and see the message printed on the screen:

    ```console
    $ node app.js
    ```

3. In another terminal, run the following command to save this container’s changes as a new image:

    ```console
    $ docker container commit -c "CMD node app.js" -m "Add app" app-container sample-app
    ```

    This command not only creates a new image named `sample-app`, but also adds additional configuration to the image to set the default command when starting a container. In this case, you are setting it to automatically run `node app.js`.

Title: Building an App Image from the Base Image
Summary
This section explains how to build an application image based on the previously created Node.js base image. It starts a new container from the `node-base` image, creates a simple Node.js program named `app.js` inside the container, and then uses `docker container commit` to save these changes as a new image named `sample-app`. The commit also sets the default command to automatically run `node app.js` when a container is started from the `sample-app` image. Finally, the original base container is removed.