Chuck's Academy

Docker

Images in Docker: Creation and Management

In Docker, images are immutable templates that contain all the elements needed to run an application, such as the operating system, code, libraries, and dependencies. This chapter explores how to create and manage images in Docker using Dockerfiles and CLI commands.

What is a Docker Image?

A Docker image is an immutable package that includes the complete environment required to run an application. Images are created from a file called a Dockerfile, which defines a series of instructions to build the application's environment.

This image shows the workflow from a Dockerfile to an image and then a containerThis image shows the workflow from a Dockerfile to an image and then a container

Building an Image with a Dockerfile

The Dockerfile is a text file that contains step-by-step instructions to create an image. Each line in the Dockerfile represents a layer in the image, making it easy to create efficient and reusable images.

Basic Structure of a Dockerfile

Below is a basic example of a Dockerfile for a Node.js application:

dockerfile
"This Dockerfile starts with FROM node fourteen, establishing a Node.js base image. It sets the working directory to app with WORKDIR, copies the code to the container with COPY dot space dot, installs dependencies using RUN npm install, and sets the main command with CMD."

Main Commands in a Dockerfile

  • FROM: Defines the base image, which is the foundation for building the environment.
  • WORKDIR: Sets the working directory inside the container.
  • COPY: Copies files from the host to the container.
  • RUN: Executes commands necessary to configure the application, like installing dependencies.
  • CMD: Defines the main command that will be executed when the container starts.

Building an Image from the Dockerfile

Once the Dockerfile has been written, you can build the image using the following command:

bash
"This command builds an image using the Dockerfile in the current directory. The dash t option my dash node dash app sets the image name to my-node-app."

Managing Images in Docker

Docker images can be managed with various commands that allow you to list, remove, and optimize the images stored on the system.

Listing Images

To see all available images on the system, use the command:

bash
"The docker images command lists all images stored locally on the system, showing the name, tag, and size of each."

Removing Images

To remove an image that is no longer needed, use the docker rmi command followed by the image name or ID:

bash
"The docker rmi command followed by the name my-node-app removes the image named my-node-app from the system, freeing up space."

Reducing Image Size

Docker images can be optimized to reduce their size. Some tips include:

  • Use a lightweight base image, like alpine.
  • Minimize the number of files copied to the container.
  • Combine multiple RUN commands into a single line to reduce layers.

Example of an optimized Dockerfile:

dockerfile
"This Dockerfile uses the base image node fourteen dash alpine, which is a lightweight version. It copies only the package dot json files before installing dependencies, and then copies the rest of the code. This reduces the image size by avoiding unnecessary layers."

Running an Image

Once an image is created, it can be run in a container using the docker run command:

bash
"This command runs the my-node-app image in a container, mapping port three thousand in the container to port three thousand on the host. The d flag runs the container in the background."

Conclusion

Images are the core of Docker as they encapsulate the entire application and its dependencies. With the knowledge of how to build and manage images, we are ready to explore containers and learn how to optimize their performance in the next chapter.


Ask me anything