Join our Telegram: @serverrental_wiki | BTC Analysis | Trading Signals | Telegraph
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 | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
- Set up the stable Docker repository:
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" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
- Update package lists again to include the new repository:
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-world
You 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-world
Similar 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 docker
Now 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 <image_name>`: Downloads a Docker image from a registry (like Docker Hub).
docker pull ubuntu:latest
- `docker images`: Lists all Docker images available locally.
docker images
- `docker run <image_name>`: Creates and starts a new container from an image.
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`: Lists all running containers.
docker ps
- `docker ps -a`: Lists all containers, including stopped ones.
docker ps -a
- `docker stop <container_id_or_name>`: Stops a running container.
docker stop my_running_container
- `docker rm <container_id_or_name>`: Removes a stopped container.
docker rm my_stopped_container
- `docker rmi <image_id_or_name>`: Removes a Docker image.
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 "<h1>Hello from my Dockerized Nginx!</h1>" > index.html
- Create the Dockerfile:
nano Dockerfile
- Add the following content to the Dockerfile:
# Use an official Nginx runtime as a parent image FROM nginx:latest # 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;"]
- Build the Docker image:
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-image
The `-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
- Docker daemon not running:
* Check status: `sudo systemctl status docker` * Start daemon: `sudo systemctl start docker` * Enable on boot: `sudo systemctl enable docker`
- Permission denied when running Docker commands:
* Ensure your user is in the `docker` group: `sudo usermod -aG docker $USER` * Log out and log back in, or use `newgrp docker`.
- `hello-world` container fails to run:
* Check network connectivity: Can you reach `https://registry-1.docker.io`? * Ensure Docker is installed correctly and the daemon is running.
- Port conflicts: If you cannot map a host port (e.g., `-p 8080:80`), another service might be using that port. Try a different host port (e.g., `-p 8081:80`). You can check port usage with `sudo netstat -tulnp | grep :8080`.