Learn Docker the Smart Way: A Comprehensive and Hands-on Guide for Beginners
Docker is a software platform that allows you to build, run, and share applications using containers. Containers are isolated environments that package up your code and all its dependencies, so that you can run your applications anywhere, from your laptop to the cloud.
Containers are lightweight, fast, and portable, which makes them ideal for modern development practices such as microservices, continuous integration, and continuous delivery.
How Does Docker Work?
Docker main components:
- Docker Engine: The core of Docker that runs on the host machine and manages the containers. It provides a command-line interface (CLI) and a RESTful API for interacting with Docker
- Docker CLI: Is the command-line interface that allows you to interact with the Docker Engine. You can use it to build, run, stop, inspect, push and pull images and containers, as well as perform other tasks related to Docker
- Docker Image: A read-only template that contains the application code and its dependencies. It is built from a Dockerfile, which is a text file that specifies the instructions for creating the image. You can store and share images on a registry, such as Docker Hub or your own private registry
- Docker Container: A running instance of an image that has its own filesystem, network, and processes. It is created from an image by using the docker run command. You can start, stop, attach, detach, inspect, and remove containers by using the docker CLI or API
- Docker Registry: Is a service that stores and distributes images. You can use either the public Docker Hub or a private registry to store your images. You can also create your own registry if you want more control over your images
To use Docker, you need to install the Docker Engine on your host machine. You can then use commands such as docker run
, docker build
, docker pull
and docker push
to interact with the Docker Engine and perform various tasks such as creating, running, stopping and removing containers, building and pulling images, and pushing images to the Docker Hub.
Docker also provides tools such as Docker Compose, Docker Swarm and Kubernetes to help you orchestrate multiple containers across multiple machines. These tools allow you to define your application’s architecture using configuration files and automate the deployment and scaling of your containers.
How to Get Started with Docker?
To get started with Docker, you need to install it on your host OS. You can find the installation instructions for different platforms on the official website.
Once you have installed Docker, you can test it by running the following command in your terminal:
docker run hello-world
This command will pull the hello-world
image from Docker Hub and run it as a container. You should see a message like this: Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
- The Docker client contacted the Docker daemon
- The Docker daemon pulled the
hello-world
image from the Docker Hub - The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading
- The Docker daemon streamed that output to the Docker client, which sent it to your terminal
To try something more ambitious, you can run an Ubuntu container with:
docker run -it ubuntu bash
This command will pull the ubuntu
image from Docker Hub and run it as a container with an interactive bash shell. You can then explore the container’s filesystem and run commands inside it.
To exit the container, type exit
.
Practical Example of Using Docker
To illustrate how to use Docker, let’s create a simple web application that displays “Hello World” using Python and Flask.
Setup Python Flask Application
Create a directory for your project and create a file named app.py
with the following code:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=80)
Create a file named requirements.txt
with the following content:
flask
Create a Dockerfile and Build an Image
Create a file named Dockerfile
with the following content:
# Use an official Python image as the base
FROM python:3.9
# Set the working directory in the container
WORKDIR /app
# Copy the requirements file to the container
COPY requirements.txt .
# Install the dependencies using pip
RUN pip install -r requirements.txt
# Copy the rest of the code to the container
COPY . .
# Expose port 80 for external access
EXPOSE 80
# Define the command to run when the container starts
CMD ["python", "app.py"]
Build your image using the docker build command:
docker build -t hello-world .
This will create an image named hello-world
based on your Dockerfile.
Run Docker Container from the Image
Run your container using the docker run command:
docker run -p 8080:80 hello-world
This will create and start a container from your image map port 8080 on your host machine to port 80 in your container (-p).
Visit http://localhost:8080 in your browser and you should see “Hello World” displayed.
Push to Docker Registry
Push your image to a registry using the docker push command. You need to specify the name and tag of your image, and the name of the registry. For example, if you want to push your image to Docker Hub, you need to prefix your image name with your username. You also need to log in to the registry using the docker login
command before pushing:
docker push myusername/my-app:latest
This command will push the image myusername/my-app:latest
to Docker Hub.
Pull from Docker Registry
Pull your image from a registry using the docker pull command. You need to specify the name and tag of your image, and the name of the registry. For example, if you want to pull your image from Docker Hub, you can use the same name as before:
docker pull myusername/my-app:latest
This command will pull the image myusername/my-app:latest
from Docker Hub.
Conclusion
Docker is a powerful tool that can help you create and run containerized applications with ease and efficiency. It offers many benefits such as consistency, reproducibility, isolation, portability, scalability, and security. It also has a simple and intuitive interface that allows you to build, run, and manage your containers with minimal effort. You can also leverage tools such as Docker Compose, Docker Swarm and Kubernetes to orchestrate your containers across multiple machines.