1. Introduction
In this tutorial, we aim to introduce you to Docker and containers, two pivotal concepts in the world of modern software development. We will guide you through creating, running, and managing Docker containers, which are lightweight, isolated environments where applications run independently from the host system.
By the end of this tutorial, you will learn:
- What Docker and containers are
- How to install Docker
- How to create and run a Docker container
- How to manage Docker containers
Before you start, make sure you have a basic understanding of command-line interfaces and a working knowledge of Linux commands. Familiarity with software development and deployment concepts is useful but not strictly necessary.
2. Step-by-Step Guide
2.1 Docker and Containers
Docker is an open-source platform for automating the deployment, scaling, and management of applications. It uses containerization, which packages an application and its dependencies into a virtual container that can run on any Linux, Windows, or Mac machine.
2.2 Installing Docker
To install Docker on your machine, follow the instructions on the official Docker website for your specific OS.
2.3 Creating and Running a Docker Container
To create a Docker container, you need to write a Dockerfile
. A Dockerfile
is a script containing commands that are executed in order. The general structure of a Dockerfile
is:
# This is a comment
# Start from a base image
FROM ubuntu:18.04
# Install dependencies
RUN apt-get update && apt-get install -y \
python3 \
python3-pip
# Set the working directory
WORKDIR /app
# Copy the current directory contents into the container
ADD . /app
# Run the command when the container launches
CMD ["python3", "app.py"]
Once you've written your Dockerfile
, you can build your Docker image using the docker build
command, and then run a container from this image using docker run
.
3. Code Examples
3.1 Building a Docker Image
In your terminal, navigate to the directory containing your Dockerfile
, then run:
docker build -t my-first-image .
This command builds a Docker image from your Dockerfile
and tags it as my-first-image
.
3.2 Running a Docker Container
To run a container from your image, use:
docker run -d -p 5000:5000 my-first-image
This command runs your container in detached mode (-d
) and maps port 5000 of your container to port 5000 of your host machine (-p 5000:5000
).
4. Summary
In this tutorial, we've introduced Docker and containers, guided you through installing Docker, and shown you how to create and manage Docker containers. For further learning, try creating Docker images with different base images, installing different dependencies, and running different applications.
For more information on Docker, refer to the official Docker documentation.
5. Practice Exercises
5.1 Exercise 1
Create a Docker image that runs a simple "Hello, World!" program in Python.
5.2 Solution
Create the Python script:
print("Hello, World!")
Next, create your Dockerfile
:
FROM python:3.7
WORKDIR /app
ADD . /app
CMD ["python", "hello.py"]
Build and run your Docker container:
docker build -t hello-world .
docker run hello-world
5.3 Exercise 2
Create a Docker image that serves a simple web application using Flask.
5.4 Solution
Create your Flask application:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
Next, write your Dockerfile
:
FROM python:3.7
WORKDIR /app
ADD . /app
RUN pip install flask
CMD ["python", "app.py"]
Build and run your Docker container:
docker build -t flask-app .
docker run -p 5000:5000 flask-app
These exercises should give you a starting point for experimenting and learning more about Docker.