Getting Started with Docker

Getting Started with Docker

A beginner's guide to Docker, the world's leading containerization platform. Learn the fundamental concepts of images and containers, and walk through building and running your first containerized application.

If you are a software developer, you have almost certainly heard of Docker. Docker has revolutionized the way we build, ship, and run applications. It's a tool that allows you to package an application and its dependencies into a standardized, isolated environment called a container.

This solves the classic problem of "it works on my machine" and provides a consistent environment for your application, from development to production.

The Problem: Environment Inconsistency

Before containers, a developer would write an application on their development machine, which had a specific operating system, a specific version of a language runtime, and a specific set of libraries. When the application was deployed to a production server, that server might have subtle differences in its environment, leading to bugs and failures.

Virtual Machines (VMs) were an early solution. A VM emulates an entire computer, including a full guest operating system. While this provides isolation, VMs are very large (gigs in size) and slow to start.

The Solution: Containers

Containers are a more lightweight solution. Instead of virtualizing the hardware, containers virtualize the operating system. This means that multiple containers can share the host system's OS kernel, but they run in isolated user spaces. This makes them much smaller (megs in size) and faster to start.

Core Docker Concepts

To get started with Docker, you need to understand two fundamental concepts:

  1. Image: An image is a read-only template that contains the instructions for creating a container. It includes the application code, a runtime, libraries, and environment variables. You create an image from a Dockerfile.

  2. Container: A container is a runnable instance of an image. You can create, start, stop, and delete containers. It's the running application.

You can have many running containers all based on the same image.

Your First Dockerfile

A Dockerfile is a text file that contains the commands to build a Docker image. Let's create a simple one for a Python Flask application.

1. The Python App (app.py)

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello():
    return "Hello, Docker!"

if __name__ == '__main__':
    app.run(host='0.0.0.0')

2. The Dockerfile

# Use an official Python runtime as a parent image
FROM python:3.6-slim

# Set the working directory in the container
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in requirements.txt
RUN pip install -r requirements.txt

# Make port 5000 available to the world outside this container
EXPOSE 5000

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

Don't forget to create a requirements.txt file:

Flask==1.0.2

Building and Running Your Container

1. Build the Image

Open your terminal in the same directory as your Dockerfile and run:

# The -t flag tags (names) your image
docker build -t my-python-app .

2. Run the Container

Now, run the image as a container:

# The -p flag maps port 8080 on your host to port 5000 in the container
docker run -p 8080:5000 my-python-app

Open your web browser and go to http://localhost:8080. You should see "Hello, Docker!"

Essential Docker Commands

  • 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.

Conclusion

Docker has become an essential tool for modern software development. It provides a simple and powerful way to create consistent, portable, and isolated environments for your applications. By mastering the basics of images, containers, and the Dockerfile, you can streamline your development workflow and simplify your deployments.