Exercise Dockerfile
Table of Contents
Introduction
This lab will take you through the Docker image creation with "Dockerfiles"; Let us create two Docker images:
One for an Apache server running on Ubuntu
Another is for an Nginx server running on CentOS
Exercise 1: Apache Server on Ubuntu
First, create a new directory for the Apache server setup:
Then, create a Dockerfile:
Insert the following content into the Dockerfile:
Next, create an Apache configuration file (my-httpd.conf):
Insert the following content into the my-httpd.conf file:
Finally, build and run the Docker container using the following commands:
To stop and remove the Docker container, use the following commands:
Go back to the parent directory to get ready for the Exercise 2:
Exercise 2: Nginx Server on Fedora
First, create a new directory for the Nginx server setup:
Then, create a Dockerfile:
Insert the following content into the Dockerfile:
Next, create an Nginx configuration file (my-nginx.conf) using the vi command:
Insert the following content into the my-nginx.conf file:
Finally, build and run the Docker container using the following commands:
To stop and remove the Docker container, use the following commands:
Dockerfile vs Docker Commit
While docker commit can be useful for creating Docker images, using a Dockerfile is generally considered a better practice for several reasons:
Reproducibility: Dockerfiles are structured ways to build an image. We can reproduce the image anywhere provided that has Docker installed.
Version Control: Due to its structured format, the Dockerfile enables easy code review and version control, whereas docker commit does not.
Documentation: The Dockerfile is self-documented and easy to follow, making it simple to reference in the future.
Automation and Integration: Dockerfiles can be easily integrated with automated build systems, such as CI/CD pipelines, to create a new image whenever the Dockerfile is updated.
In contrast, docker commit doesn't provide insight into the steps required to create the image. It makes it harder to reproduce the image. docker commit is helpful for quick changes. But not for building consistent and reproducible Docker images. For that, Dockerfiles are a more reliable and flexible solution.
References
Last updated