DEV Community

Cover image for Docker | EP01: Getting Started with Dockerfile
Booranasak Kanthong
Booranasak Kanthong

Posted on

Docker | EP01: Getting Started with Dockerfile

Are you just starting your journey into Docker and wondering what a Dockerfile is, or how docker-compose fits into the picture? This guide will walk you through the basics step-by-step, with clear examples and simple language. By the end, you'll understand how to write a basic Dockerfile, build an image, and then use that image with docker-compose.


What is a Dockerfile?

Think of a Dockerfile like a recipe. It tells Docker how to make a "container image" – a snapshot of your application and everything it needs to run.

Here’s a very basic example of a Dockerfile:

# Start from a base image with Python installed
FROM python:3.10-slim

# Set the working directory inside the container
WORKDIR /app

# Copy your code into the container
COPY . .

# Install any required dependencies
RUN pip install -r requirements.txt

# Run the app
CMD ["python", "main.py"]
Enter fullscreen mode Exit fullscreen mode

What’s happening here?

  • FROM: This line chooses a base image. We’re using Python 3.10.
  • WORKDIR: Sets the folder inside the container where your code will live.
  • COPY: Copies everything from your current folder into the container.
  • RUN: Executes a command – here, it installs dependencies.
  • CMD: Specifies what to run when the container starts.

Build the Docker Image

Once you have a Dockerfile, you can build the image:

docker build -t my-python-app .
Enter fullscreen mode Exit fullscreen mode

This command tells Docker to:

  • Look for the Dockerfile in the current directory (.),
  • Follow the steps inside it, and Create an image named my-python-app.

Now you have a Docker image named my-python-app!


Let's Build and Run a Real Dockerized App (Flask on Port 5000)

We learn how to build image let's try it for real:

Project Structure

my-python-app/
├── app.py
├── requirements.txt
└── Dockerfile
Enter fullscreen mode Exit fullscreen mode

app.py: A Simple Flask App
Create a file named app.py:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return "🎉 Hello from your Dockerized Flask app!"

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)
Enter fullscreen mode Exit fullscreen mode

host='0.0.0.0' makes it accessible from outside the container (important for Docker!).

requirements.txt
Add Flask as a dependency

flask
Enter fullscreen mode Exit fullscreen mode

Now, create your Dockerfile:

Dockerfile

Now, create your Dockerfile:

# Use official Python image
FROM python:3.10-slim

# Set working directory
WORKDIR /app

# Copy files into the container
COPY . .

# Install dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Run the app
CMD ["python", "app.py"]
Enter fullscreen mode Exit fullscreen mode

Build the Docker Image

Open your terminal in the my-python-app directory and run:

docker build -t my-python-app .
Enter fullscreen mode Exit fullscreen mode

Run the App and Expose Port 5000

docker run -p 5000:5000 my-python-app
Enter fullscreen mode Exit fullscreen mode

You should see output like:

Now open your browser and go to http://localhost:5000

Top comments (0)