Home Explore Blog CI



docker

4th chunk of `content/get-started/docker-concepts/building-images/understanding-image-layers.md`
dfe1b4df08a1a50298c97f1223e56822e7ce647d5f038b620000000100000c51
### 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`.

4. In a terminal outside of the container, run the following command to view the updated layers:

    ```console
    $ docker image history sample-app
    ```

    You’ll then see output that looks like the following. Note the top layer comment has “Add app” and the next layer has “Add node”:

    ```console
    IMAGE          CREATED              CREATED BY                                      SIZE      COMMENT
    c1502e2ec875   About a minute ago   /bin/bash                                       33B       Add app
    5310da79c50a   4 minutes 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
    ```

5. Finally, start a new container using the brand new image. Since you specified the default command, you can use the following command:

    ```console
    $ docker run sample-app
    ```

    You should see your greeting appear in the terminal, coming from your Node program.

6. Now that you’re done with your containers, you can remove them using the following command:

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

## Additional resources

If you’d like to dive deeper into the things you learned, check out the following resources:

* [`docker image history`](/reference/cli/docker/image/history/)
* [`docker container commit`](/reference/cli/docker/container/commit/)


## Next steps

As hinted earlier, most image builds don’t use `docker container commit`. Instead, you’ll use a Dockerfile which automates these steps for you.

{{< button text="Writing a Dockerfile" url="writing-a-dockerfile" >}}

Title: Building and Running an App Image, Additional Resources, and Next Steps
Summary
This section details how to build a Node.js app image, named `sample-app`, from the `node-base` image by creating an `app.js` file and committing the changes. It also explains how to view the image history to observe the different layers. The built image is then run, demonstrating the execution of the Node.js application. Finally, it recommends using Dockerfiles for automating image builds and provides links to relevant resources and the next tutorial on writing Dockerfiles.