Building a Custom Docker Image
To build a custom Docker image, you need to create a Dockerfile, which is a text file that contains instructions for building the image.
Creating a Dockerfile
Here's an example Dockerfile that builds a custom image based on the Ubuntu 22.04 base image and installs the Apache web server:
## Use the Ubuntu 22.04 base image
FROM ubuntu:22.04
## Update the package index and install Apache
RUN apt-get update && apt-get install -y apache2
## Set the default command to run when the container starts
CMD ["apache2ctl", "-D", "FOREGROUND"]
Building the Docker Image
To build the Docker image from the Dockerfile, run the following command in the same directory as the Dockerfile:
docker build -t my-apache-image .
This command will create a new Docker image named my-apache-image
based on the instructions in the Dockerfile.
Inspecting the Docker Image
After building the image, you can list all the images on your system using the following command:
docker images
This will show you the list of Docker images, including the my-apache-image
you just created.
You can also inspect the details of the image using the following command:
docker inspect my-apache-image
This will display the metadata and configuration of the Docker image.
Now that you have a custom Docker image, you can proceed to running a container based on this image.