A Beginner’s Guide to Dockerfile: Building and Sharing Your First Docker Container
Table of contents
What is a Dockerfile?
A Dockerfile is a text file containing a series of commands and instructions used to build a Docker image. It serves as a blueprint for creating a container. With a Dockerfile, you can:
Specify a base image (like Python or Node.js).
Copy application files into the container.
Install dependencies.
Define what happens when the container starts (e.g., run a web server).
For example, if you’re creating a container for a Python application, the Dockerfile might tell Docker to use a Python base image, copy your application files, install required libraries, and start the application.
Task: Building and Sharing a Simple Python App with Docker
Follow these steps to create, run, and share a Docker container for a Python app:
Step 1: Create a Dockerfile for a simple application
This is the simple python ‘Hello world’ application for which we are going to create Dockerfile
Dockerfile
Step 2: Build the Docker Image
Use the Dockerfile to build a Docker image:
docker build -t harshsoni777/my-first-docker-image
Step 3: Run the Docker Container
Run the container using the newly built image:
docker run -it harshsoni777/my-first-docker-image:latest
Step 5: Share the Image on Docker Hub
Log in to Docker Hub
Before sharing the image, log in to your Docker Hub account:
docker login
Push the Image
Push the image to Docker Hub:
Your image is now available on Docker Hub and can be pulled by anyone using:
Why Use a Dockerfile?
Consistency: Ensure your app runs the same across all environments.
Portability: Share and deploy your app effortlessly.
Efficiency: Automate application setup and deployment.
Conclusion
With Docker and a simple Dockerfile, you can create, run, and share a fully functional containerized application. Whether you're developing a small web app or a large-scale system, Docker makes your work scalable, efficient, and collaborative.
Ready to try it yourself? Build your first Docker image, push it to Docker Hub, and share your learning journey!