Creating Docker Images
Docker images are the building blocks of Docker containers. They are created using a set of instructions called a Dockerfile, which defines the steps to build the image. Here's a step-by-step guide on how to create a Docker image:
- Create a Dockerfile: A Dockerfile is a text file that contains the instructions for building a Docker image. It typically starts with a base image, followed by a series of commands to install dependencies, copy files, and configure the environment.
# Use a base image
FROM ubuntu:latest
# Update the package index and install necessary packages
RUN apt-get update && apt-get install -y \
apache2 \
&& rm -rf /var/lib/apt/lists/*
# Copy the website files to the container
COPY website/ /var/www/html/
# Expose the port that the web server will listen on
EXPOSE 80
# Set the default command to start the web server
CMD ["apache2", "-D", "FOREGROUND"]
- Build the Docker Image: Once you have created the Dockerfile, you can build the Docker image using the
docker build
command. This command reads the instructions in the Dockerfile and creates a new image.
docker build -t my-website .
This command will build a new Docker image with the tag my-website
using the Dockerfile in the current directory.
- Verify the Image: After the build process is complete, you can list the available Docker images on your system using the
docker images
command.
docker images
This will display a list of all the Docker images on your system, including the one you just created.