How to Build Your Own Dockerfile, Image, and Container
This write-up will illustrate:
How to Build Dockerfile?
Dockerfile is an instruction file that helps to create a snapshot of the container. To create/make a Dockerfile, follow the provided steps.
Step 1: Create Program File
First, create an “index.html” program file and paste the below-provided code into it:
Step 2: Create Dockerfile
Then, create a new file named as “Dockerfile” and paste the below snippet into it to containerize the HTML program:
COPY index.html /usr/share/nginx/html/index.html
COPY linuxhint.png /usr/share/nginx/html
ENTRYPOINT ["nginx", "-g", "daemon off;"]
In the above code:
- “FROM” command is used to specify a base image for the container.
- “COPY” instruction pastes the “index.html” file and “linuxhint.png” image into the container path.
- “ENTRYPOINT” sets the execution point for the container.
How to Build Docker Image?
Docker images are snapshots or templates used to create and manage containers. Utilize the given-below command to build the Docker image from the Dockerfile:
Here, “-t” is used to specify the image name. For instance, we have specified the “linuximg” as a name for the Docker image:
Then, ensure that the image has been created successfully using the provided command:
In the below output, the Docker image can be seen, i.e., “linuximg”:
How to Build Docker Container?
Docker containers are lightweight and small executable packages of Docker used to containerize the application. To build and run a Docker container using the Docker image, execute the given-provided command:
Here:
- “–name” is used to specify the container name.
- “imgcontainer” is the name of the container.
- “-p” is utilized to assign the port to the container.
Another way to simply create or build a container is to utilize the “docker create” command:
Lastly, open the desired browser and redirect to the allocated port. Then, verify whether the application is deployed or not:
As you can see, we have successfully built the Dockerfile, image, and container.
Conclusion
Dockerfiles are normal text files that include sets and instructions to build docker images. To build docker images, the “docker build -t <image-name>” command is used. In order to create the Docker container from the Docker image, execute the “docker create –name <container-name> -p <port-no> <image-name>” command. Furthermore, if a user wants to create and run the container, utilize the “docker run” command. This write-up illustrated the procedure of building a Dockerfile, image, and container.
Source: linuxhint.com