Installing Docker on Linux
= Installing Docker on Linux =
This guide provides step-by-step instructions for installing Docker Community Edition (CE) on various Linux distributions and introduces fundamental Docker commands and concepts, including creating a simple Dockerfile. This is an essential skill for modern application deployment and management, especially when leveraging the power of dedicated servers from PowerVPS for your containerized workloads.
Prerequisites
Before you begin, ensure you have: # A Linux server with root or sudo privileges. Dedicated Servers from PowerVPS offer excellent performance and full root access, ideal for running Docker. # A stable internet connection to download packages. # Basic familiarity with the Linux command line. # For this guide, we will cover installation on Debian/Ubuntu and CentOS/RHEL-based systems.Installing Docker CE
Docker CE can be installed using the official repositories. This is the recommended method as it ensures you receive timely updates.
On Debian/Ubuntu
# Update package lists:sudo apt update# Install prerequisite packages: These allow apt to use a repository over HTTPS.
sudo apt install apt-transport-https ca-certificates curl software-properties-common gnupg lsb-release# Add Docker's official GPG key:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg# Set up the stable Docker repository:sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) stable"# Update package lists again to include the new repository:sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update# Install Docker Engine, containerd, and Docker Compose:
sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin# Verify the installation:
sudo docker run hello-worldYou should see a message indicating that your installation appears to be working correctly.
On CentOS/RHEL/Fedora
# Uninstall older versions (if any):sudo yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine podman runc# Install required packages:
sudo yum install -y yum-utils device-mapper-persistent-data lvm2# Add Docker's stable repository:
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo# Install Docker Engine, containerd, and Docker Compose:
sudo yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin# Start the Docker daemon:
sudo systemctl start docker# Enable Docker to start on boot:
sudo systemctl enable docker# Verify the installation:
sudo docker run hello-worldSimilar to Debian/Ubuntu, you should see a confirmation message.
Post-installation Steps
Manage Docker as a non-root user
To run Docker commands without `sudo`, add your user to the `docker` group. # Add user to the docker group:sudo usermod -aG docker $USER# Apply the group changes: You will need to log out and log back in, or run:
newgrp dockerNow you can run Docker commands directly:
docker run hello-world
Basic Docker Commands
Here are some fundamental Docker commands to get you started:- `docker --version`: Displays the installed Docker version.
- `docker info`: Provides detailed information about your Docker installation, including the number of containers, images, and storage driver.
- `docker pull
` : Downloads a Docker image from a registry (like Docker Hub).
docker pull ubuntu:latest
docker images
docker run -it ubuntu bash
The `-it` flags allocate a pseudo-TTY and keep STDIN open, allowing you to interact with the container.
docker ps
docker ps -a
docker stop my_running_container
docker rm my_stopped_container
docker rmi ubuntu:latest
Creating a Dockerfile
A Dockerfile is a script that contains instructions for building a Docker image. It's the blueprint for your containerized application.
Let's create a simple Dockerfile for a basic Nginx web server.
# Create a directory for your project:
mkdir my-nginx-app cd my-nginx-app# Create a simple HTML file to serve:
echo "# Create the Dockerfile:Hello from my Dockerized Nginx
" > index.html
nano Dockerfile# Add the following content to the Dockerfile:
# Use an official Nginx runtime as a parent image FROM nginx:latest# Build the Docker image:# Copy the current directory contents into the container at /usr/share/nginx/html COPY . /usr/share/nginx/html
# Expose port 80 to the outside world EXPOSE 80
# Command to run when the container launches CMD ["nginx", "-g", "daemon off;"]
docker build -t my-nginx-image .The `.` at the end specifies the build context (the current directory). # Run a container from your new image:
docker run -d -p 8080:80 my-nginx-imageThe `-d` flag runs the container in detached mode. `-p 8080:80` maps port 8080 on your host to port 80 inside the container. # Verify by accessing your web server: Open a web browser and navigate to `http://your_server_ip:8080`. You should see "Hello from my Dockerized Nginx
Troubleshooting
Further Reading
Category:Containerization Category:Docker Category:System Administration