system-administration docker-on-linux docker-installation containerization docker-deployment dockerfile docker-compose docker-volumes docker-networks
Using Docker on Linux: From Installation to Deployment
Introduction
Docker has revolutionized the way we develop, deploy, and manage applications. By packaging software into standardized units called containers, Docker allows you to run applications consistently across different environments. This tutorial will guide you through using Docker on Linux, from installation to deploying your first containerized application.
Section 1: Introduction to Docker
1.1 What is Docker?
Docker is an open-source platform that automates the deployment of applications inside lightweight, portable containers. Containers include everything an application needs to run, such as libraries, dependencies, and configurations, ensuring that it behaves the same regardless of where it is deployed.
1.2 Why Use Docker?
- Consistency: Docker containers ensure that applications run the same way across different environments.
- Efficiency: Containers are lightweight and share the host OS kernel, making them more resource-efficient than virtual machines.
- Scalability: Docker makes it easier to scale applications, allowing you to deploy multiple containers across a cluster.
Section 2: Installing Docker on Linux
2.1 Prerequisites
Before installing Docker, ensure your Linux distribution is up-to-date:
sudo apt-get update
sudo apt-get upgrade
2.2 Installing Docker on Ubuntu
To install Docker on Ubuntu, follow these steps:
- Install Docker Dependencies:
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
- Add Docker’s Official GPG Key:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
- Add the Docker Repository:
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
- Install Docker:
sudo apt-get update
sudo apt-get install docker-ce
- Verify Docker Installation:
sudo systemctl status docker
You should see Docker running.
2.3 Installing Docker on CentOS
To install Docker on CentOS:
- Remove any old Docker versions:
sudo yum remove docker docker-common docker-selinux docker-engine
- Install Required Packages:
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
- Add the Docker Repository:
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
- Install Docker:
sudo yum install docker-ce
- Start Docker:
sudo systemctl start docker
- Enable Docker to Start at Boot:
sudo systemctl enable docker
- Verify Installation:
sudo docker --version
Section 3: Basic Docker Commands
3.1 Running Your First Container
To run a simple Docker container using the hello-world
image:
sudo docker run hello-world
This command downloads the hello-world
image from Docker Hub (if not already available) and runs it in a container.
3.2 Listing Docker Containers
To list running containers:
sudo docker ps
To list all containers (running and stopped):
sudo docker ps -a
3.3 Stopping and Removing Containers
To stop a running container:
sudo docker stop container_id
To remove a stopped container:
sudo docker rm container_id
Section 4: Working with Docker Images
4.1 Pulling Images from Docker Hub
Docker Hub is a cloud-based repository where you can find container images. To pull an image:
sudo docker pull ubuntu
This command downloads the official Ubuntu image.
4.2 Building Custom Docker Images
To build a custom Docker image, you need to create a Dockerfile
. Here's an example:
# Use an official Ubuntu as a parent image
FROM ubuntu:latest
# Install necessary packages
RUN apt-get update && apt-get install -y nginx
# Expose port 80
EXPOSE 80
# Start Nginx when the container launches
CMD ["nginx", "-g", "daemon off;"]
Build the Docker image using the following command:
sudo docker build -t my-nginx-image .
This command builds an image from the Dockerfile in the current directory and tags it as my-nginx-image
.
4.3 Pushing Images to Docker Hub
To push an image to Docker Hub:
- Log in to Docker Hub:
sudo docker login
- Tag the Image:
sudo docker tag my-nginx-image your-dockerhub-username/my-nginx-image
- Push the Image:
sudo docker push your-dockerhub-username/my-nginx-image
Section 5: Deploying Applications with Docker
5.1 Deploying a Web Application
Let's deploy a simple web application using Docker.
- Create a Dockerfile:
FROM python:3.8-slim
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY . .
EXPOSE 5000
CMD ["python", "app.py"]
- Build the Docker Image:
sudo docker build -t my-python-app .
- Run the Docker Container:
sudo docker run -d -p 5000:5000 my-python-app
This command runs your Python web application in a container and maps port 5000 on the host to port 5000 in the container.
- Access the Application:
Open your web browser and navigate to http://localhost:5000
to see your web application in action.
5.2 Using Docker Compose for Multi-Container Applications
Docker Compose is a tool that allows you to define and manage multi-container Docker applications.
- Create a
docker-compose.yml
file:
version: '3'
services:
web:
image: my-python-app
ports:
- "5000:5000"
redis:
image: "redis:alpine"
- Start the Multi-Container Application:
sudo docker-compose up
- Stop the Application:
sudo docker-compose down
Section 6: Managing Docker Volumes and Networks
6.1 Working with Docker Volumes
Volumes are used to persist data generated by Docker containers. To create a volume:
sudo docker volume create my-volume
To use this volume in a container:
sudo docker run -d -v my-volume:/app/data my-python-app
6.2 Configuring Docker Networks
Docker networks allow containers to communicate with each other. To create a custom network:
sudo docker network create my-network
To run a container on this network:
sudo docker run -d --network=my-network my-python-app
Conclusion
Docker simplifies the process of deploying, managing, and scaling applications by using containers. From installation to deployment, mastering Docker on Linux opens up a world of possibilities for application development and system management. By following this guide, you'll be well on your way to becoming proficient in using Docker to streamline your development and operations workflows.
Comments
Please log in to leave a comment.