Docker is a popular tool for packaging and distributing applications as containers. Docker images are at the heart of this process. An image is a pre-built package that contains everything needed to run an application, including the code, dependencies, and configuration. Docker images can be downloaded from public or private registries, or created locally. In this lab, you will learn how to create custom Docker images that add value to your applications by including additional software, libraries, or configurations.
The first step is to create a simple Docker image that runs a simple web server. We will use the nginx web server as an example.
Create a new directory called docker for this lab at this path /home/labex/project, and navigate to it.
cd /home/labex/project
mkdir docker
cd docker
Create a new file called Dockerfile in docker directory, and add the following lines:
FROM nginx
COPY index.html /usr/share/nginx/html/
This Dockerfile defines a new image based on the official nginx image, and copies a file called index.html to the default document root directory of nginx.
Create a new file called index.html in docker directory, and add some simple HTML content, such as:
Verify that the image was created successfully by running the following command:
docker images
You should see the my-nginx image listed in the output.
Run a container based on the new image using the following command:
docker run -p 8080:80 my-nginx
This command starts a new container based on the my-nginx image, and maps port 8080 on the host to port 80 in the container. You should be able to access the web server by navigating to http://localhost:8080 in your web browser.
Open the index.html file in the container and view the result:
Add Custom Software to the Image
The second step is to add custom software to the Docker image. We will use the curl utility as an example.
Modify the Dockerfile to include the following lines:
FROM nginx
RUN apt-get update && apt-get install -y curl
COPY index.html /usr/share/nginx/html/
This Dockerfile adds a new RUN instruction that updates the package index and installs the curl utility using the apt-get package manager.
Rebuild the Docker image using the following command:
docker build -t my-nginx-curl .
This command builds a new Docker image with the tag my-nginx-curl that includes the curl utility.
Verify that the image was created successfully by running the following command:
docker images
You should see the my-nginx-curl image listed in the output.
Run a container based on the new image using the following command:
docker run -d --name curl -it my-nginx-curl
-it: This is a combination of the two options, -i to run the container interactively, and -t to allocate a pseudo-terminal (tty).
This command starts a new container based on the my-nginx-curl image, and opens a Bash shell inside the container.
Test the curl utility by running the following command inside the container:
You can type exit to exit the container's Bash shell.
Use Environment Variables in the Image
The third step is to use environment variables in the Docker image to customize its behavior. We will use the NGINX_PORT variable as an example.
Modify the Dockerfile to include the following lines:
FROM nginx
ENV NGINX_PORT 8080
RUN sed -i "s/listen\s*80;/listen $NGINX_PORT;/g" /etc/nginx/conf.d/default.conf
COPY index.html /usr/share/nginx/html/
This Dockerfile adds a new ENV instruction that sets the value of the NGINX_PORT variable to 8080. It also adds a new RUN instruction that replaces the default listen directive in the nginx configuration file with a new directive that uses the value of the NGINX_PORT variable.
Rebuild the Docker image using the following command:
docker build -t my-nginx-env .
This command builds a new Docker image with the tag my-nginx-env that uses environment variables.
Verify that the image was created successfully by running the following command:
docker images
You should see the my-nginx-env image listed in the output.
Run a container based on the new image using the following command:
docker run -p 8080:8080 -e NGINX_PORT=8080 my-nginx-env
This command starts a new container based on the my-nginx-env image, maps port 8080 on the host to port 8080 in the container, and sets the value of the NGINX_PORT variable to 8080.
Verify that the web server is running by navigating to http://localhost:8080 in your web browser.
Summary
In this lab, you learned how to create custom Docker images that add value to your applications by including additional software, libraries, or configurations. You also learned how to use environment variables to customize the behavior of Docker images. With these skills, you can create custom Docker images that meet the specific needs of your applications and deploy them easily and reliably using Docker.
We use cookies for a number of reasons, such as keeping the website reliable and secure, to improve your experience on our website and to see how you interact with it. By accepting, you agree to our use of such cookies. Privacy Policy