Docker simplifies how we build, ship, and run applications by following a clear lifecycle.
Let’s break it down into three main stages with an easy-to-follow example using Ubuntu and a small Python app.
1️⃣ Create the Dockerfile
A Dockerfile is like a recipe for your application. It tells Docker:
- Which base image to start from
- What dependencies to install
- What command to run when the container starts
Example:
# Start with an official Ubuntu image
FROM ubuntu:22.04
# Install Python
RUN apt-get update && apt-get install -y python3
# Copy our app to the container
COPY app.py /app/app.py
# Set working directory
WORKDIR /app
# Run the app when container starts
CMD ["python3", "app.py"]
2️⃣ Build the Docker Image
The Dockerfile is just instructions. To actually create something runnable, we build an image:
Using the Dockerfile, a Docker image is created through the docker build command. A Docker image is a lightweight, standalone, and executable package that includes everything needed to run an application, such as the runtime environment, system tools, libraries, and dependencies.
docker build -t ubuntu-simple-app .
What happens here?
- Docker takes the Ubuntu image
- Installs Python
- Copies app.py into it
- Creates a ready-to-use image named ubuntu-simple-app
3️⃣ Run the Docker Container
Once the image is built, a Docker container is created and executed using the docker run command. A container is an isolated environment where the application runs consistently across different systems. Containers leverage the lightweight nature of Docker images, ensuring minimal overhead and fast deployment.
docker run --name my-simple-app ubuntu-simple-app
Example app (app.py):
print("Hello from inside my Ubuntu-based Docker container!")
Output:
Hello from inside my Ubuntu-based Docker container!
🔄 Quick Recap
- Dockerfile → Your recipe for building the image
- Build Image → Turn recipe into a runnable package
- Run Container → Launch the app in an isolated environment
By following this lifecycle, you ensure your app runs consistently anywhere — your laptop, a server, or the cloud.
💡 Pro Tip:
You can share your image with others via Docker Hub:
docker tag ubuntu-simple-app your-dockerhub-username/ubuntu-simple-app
docker push your-dockerhub-username/ubuntu-simple-app
Even though it's so hard for me but so cool 😎