Back

Introduction to Docker: Containerizing Applications

Introduction

Docker is a platform that allows you to automate the deployment of applications inside containers, providing a lightweight, portable, and self-sufficient environment. This guide will introduce you to the fundamental concepts of Docker and show you how to use it to containerize an application.

Prerequisites

  • An updated Ubuntu system.
  • Access with sudo privileges.
Step 1: Install Docker

Install Docker using the following commands:

sudo apt update
sudo apt install docker.io -y
sudo systemctl start docker
sudo systemctl enable docker

Verify the installation by checking the Docker version:

docker --version
Step 2: Understand Basic Concepts
  • Images: Templates for creating containers. They are built from a Dockerfile.
  • Containers: Executable instances of images.
  • Docker Hub: An online repository where you can find Docker images.
Step 3: Run Your First Container

Run the test container hello-world:

sudo docker run hello-world

This command will download the hello-world image (if not present) and run the container.

Step 4: Run a Web Application with Docker

Run an Nginx container on port 80:

sudo docker run -d -p 80:80 nginx
  • -d runs the container in the background.
  • -p maps port 80 of the container to port 80 of the host.

Visit http://your_server_ip in the browser to see the Nginx default page.

Step 5: Manage Containers
  • List running containers:
sudo docker ps
  • Stop a container:
sudo docker stop [container_id]
  • Remove a container:
sudo docker rm [container_id]
Step 6: Create a Custom Image
  • Create a Dockerfile:
FROM ubuntu:latest
RUN apt-get update && apt-get install -y python3
CMD ["python3", "--version"]
  • Build the image:
sudo docker build -t my_python_image .
  • Run the container:
sudo docker run my_python_image

Conclusion

You have learned the basics of Docker and how to use it to run and create containers. Continue exploring to discover all the potential of Docker in application containerization.

Nexto
Nexto