Docker Images

Docker Images

Docker Images

A Docker image is a lightweight, stand-alone, executable package that contains everything needed to run an application: the code, libraries, dependencies, configuration files, and environment variables. Images are read-only templates used to create containers.

Key Concepts About Docker Images:

  1. Layers: Docker images are made up of layers. Each layer represents a set of instructions in the Dockerfile (such as adding a file or installing software).

  2. Read-Only: Once an image is created, it is immutable (read-only). When a container is created from an image, it can be modified, but the image itself remains unchanged.

  3. Built from a Dockerfile: Images are typically built using a Dockerfile, which contains a set of instructions on how to build the image.

  4. Shared across Containers: Multiple containers can be created from the same image, allowing for efficient use of resources.

Docker Images

Step 1: Pulling a Docker Image

You can pull Docker images from Docker Hub or other registries. Docker Hub is the default image registry, where you can find a wide range of pre-built images for different applications.

For example, to pull the MySQL image:

docker pull mysql

This command will download the official MySQL image from Docker Hub.

Step 2: Listing Docker Images

To list all the Docker images available on your system, use:

docker images

The output will show you a list of images along with their repository, tag, image ID, creation date, and size.

Example output:

REPOSITORY TAG IMAGE ID CREATED SIZE mysql latest 9e3e5d74b378 2 days ago 544MB nginx latest f1f2a25e9be0 5 days ago 132MB

Step 3: Building a Docker Image from a Dockerfile

You can create a Docker image by defining the instructions in a Dockerfile. A Dockerfile contains a set of commands and arguments used to assemble a Docker image.

Here’s an example Dockerfile to create an image for a simple Node.js application:

  1. Create a Dockerfile:

# Use the official Node.js image from Docker Hub as the base image FROM node:14 # Set the working directory inside the container WORKDIR /app # Copy package.json and install dependencies COPY package.json /app RUN npm install # Copy the rest of the application files COPY . /app # Expose port 3000 EXPOSE 3000 # Start the application CMD ["npm", "start"]
  1. Build the Image:

To build the image from the Dockerfile, navigate to the directory where the Dockerfile is located and run:

docker build -t my-node-app .
  • -t my-node-app: Tags the image with the name my-node-app.

  • .: Refers to the current directory where the Dockerfile is located.

After running the command, Docker will process the instructions in the Dockerfile and create an image.

Step 4: Tagging Docker Images

You can add tags to your Docker images to version them or identify different configurations.

To tag an image:

docker tag my-node-app my-node-app:v1.0

This will tag the my-node-app image with the version v1.0.

Step 5: Pushing Docker Images to Docker Hub

To share an image with others, you can push it to a Docker registry like Docker Hub.

  1. Login to Docker Hub:

docker login

This will prompt you to enter your Docker Hub credentials.

  1. Push the Image to Docker Hub:

First, you need to tag your image with your Docker Hub username. For example:

docker tag my-node-app <your-username>/my-node-app:v1.0

Then push the image:

docker push <your-username>/my-node-app:v1.0

This will upload your image to your Docker Hub repository.

Step 6: Running a Container from an Image

Once you have an image, you can run it as a container using the docker run command.

For example, to run a container from the mysql image:

docker run --name mysql-container -e MYSQL_ROOT_PASSWORD=root -d mysql

This command does the following:

  • --name mysql-container: Gives the container a name (mysql-container).

  • -e MYSQL_ROOT_PASSWORD=root: Sets an environment variable for the root password of MySQL.

  • -d: Runs the container in detached mode (in the background).

  • mysql: Specifies the image to use.

Step 7: Removing Docker Images

If you no longer need a Docker image, you can remove it.

  1. Remove a specific image:

docker rmi <image-name>

For example, to remove the mysql image:

docker rmi mysql
  1. Remove all unused images:

If you want to remove all images that are not being used by any containers, run:

docker image prune

You can also add the -a flag to remove all unused images (not just dangling ones):

docker image prune -a

Step 8: Inspecting Docker Images

If you want to get detailed information about a specific Docker image, you can use:

docker inspect <image-name>

This will provide a detailed JSON output with all the metadata about the image, such as its configuration, layers, environment variables, and more.

Docker Image Best Practices

  1. Keep Images Small: Minimize the number of layers and dependencies in your image to keep it lightweight.

  2. Use Official Images: Whenever possible, use official images from Docker Hub to ensure security and reliability.

  3. Leverage Caching: Docker uses caching to speed up the build process. Try to order your Dockerfile instructions to maximize cache reuse (e.g., installing dependencies first).

  4. Clean Up After Yourself: Remove temporary files and unused dependencies to keep images small.

Summary of Docker Image Commands

  • Pull an image: docker pull <image-name>

  • List images: docker images

  • Build an image: docker build -t <image-name> .

  • Tag an image: docker tag <image-name> <new-image-name>

  • Push an image to Docker Hub: docker push <username>/<image-name>

  • Run a container: docker run <options> <image-name>

  • Remove an image: docker rmi <image-name>

  • Inspect an image: docker inspect <image-name>

Souy Soeng

Souy Soeng

Hi there 👋, I’m Soeng Souy (StarCode Kh)
-------------------------------------------
🌱 I’m currently creating a sample Laravel and React Vue Livewire
👯 I’m looking to collaborate on open-source PHP & JavaScript projects
💬 Ask me about Laravel, MySQL, or Flutter
⚡ Fun fact: I love turning ☕️ into code!

Post a Comment

CAN FEEDBACK
close