Getting Started with Docker: A Developer's Introduction
A practical guide for developers to get started with Docker. Learn what containers are, why they are useful, and how to use the essential Docker commands to build and run your first application.
If you've been in the software world for any length of time, you've heard the phrase, "But it works on my machine!" Docker is the technology that was created to end that problem once and for all.
Docker is a platform for developing, shipping, and running applications in containers. A container is a lightweight, standalone, and executable package of software that includes everything needed to run it: the code, a runtime, system tools, system libraries, and settings.
Why Docker? The Problem it Solves
Before containers, developers would run an application on their machine, and it would work fine. But when they deployed it to a server, it would fail because of subtle differences in the environment—a different operating system, a missing library, or a different version of a dependency.
Virtual Machines (VMs) were an early solution. A VM is a complete emulation of a computer system. While they solve the consistency problem, they are very heavy. Each VM includes a full copy of an operating system, which can be gigabytes in size and slow to start.
Containers are the evolution of this idea. They virtualize the operating system instead of the hardware. This means containers are much more lightweight and faster. They share the host system's kernel but have their own isolated user space. A container might be only tens of megabytes in size and can start in seconds.
Core Docker Concepts
There are two fundamental concepts you need to understand:
Image: An image is a read-only template with instructions for creating a Docker container. It's like a blueprint or a recipe. An image is built from a file called a
Dockerfile
.Container: A container is a runnable instance of an image. You can create, start, stop, move, and delete containers. It's the running application.
You can have many running containers based on the same single image.
Getting Started: Your First Dockerfile
Let's create a simple Python web application and dockerize it.
1. The Python App (app.py
)
First, you need a web framework like Flask. Make sure to install it:
pip install Flask
Now, create a simple app.py
:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, Docker!'
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
2. The Dockerfile
A Dockerfile
is a text file that contains the commands to assemble an image. Create a file named Dockerfile
in the same directory as your app.py
.
# Start from an official Python base image
FROM python:3.9-slim
# Set the working directory inside the container
WORKDIR /app
# Copy the requirements file into the container
COPY requirements.txt .
# Install the Python dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Copy the rest of the application code into the container
COPY . .
# Expose the port the app runs on
EXPOSE 5000
# Define the command to run the application
CMD ["python", "app.py"]
Create a requirements.txt
file:
Flask==2.0.1
Building and Running Your Container
Now that you have your Dockerfile
, you can build your image and run it as a container.
1. Build the Image
Open your terminal in the directory with your Dockerfile
and run:
docker build -t my-python-app .
docker build
: The command to build an image.-t my-python-app
: This tags (names) your imagemy-python-app
..
: This tells Docker to look for theDockerfile
in the current directory.
2. Run the Container
Once the image is built, you can run it as a container:
docker run -p 5000:5000 my-python-app
docker run
: The command to run a container.-p 5000:5000
: This publishes (maps) port 5000 on your host machine to port 5000 inside the container. This allows you to access the application from your browser.my-python-app
: The name of the image to run.
Now, open your web browser and go to http://localhost:5000
. You should see "Hello, Docker!"
Essential Docker Commands
Here are a few more essential commands you'll use all the time:
docker ps
: List all running containers.docker images
: List all images on your machine.docker stop <container_id>
: Stop a running container.docker rm <container_id>
: Remove a stopped container.docker rmi <image_id>
: Remove an image.
Conclusion
Docker and containers have fundamentally changed the way we build and ship software. They provide a consistent and reproducible environment that eliminates the "it works on my machine" problem. By learning the basics of Dockerfile
, docker build
, and docker run
, you have taken the first step towards mastering this essential tool for modern software development.