How to Install Docker and Run Containers on Your Server
Docker is an open-source platform used for automating the deployment, scaling, and management of applications within lightweight containers. Containers allow you to package applications with all their dependencies into a single unit that can be run anywhere. This guide will walk you through installing Docker on your dedicated server and running your first container.
Step 1: Update Your Server
Before installing Docker, it's important to ensure that your server is up to date with the latest security patches.
- For Ubuntu/Debian:
sudo apt update && sudo apt upgrade -y
- For CentOS/RHEL:
sudo yum update -y
Step 2: Install Docker
Docker provides an easy-to-install script, but you can also install it manually. Below are the installation steps for both Ubuntu/Debian and CentOS/RHEL.
-
Install Docker on Ubuntu/Debian:
- 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"
- Update your package index:
sudo apt update
- Install Docker:
sudo apt install docker-ce -y
- Add Docker’s official GPG key:
-
Install Docker on CentOS/RHEL:
- Install dependencies:
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
- Add Docker repository:
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
- Install Docker:
sudo yum install docker-ce -y
- Install dependencies:
Step 3: Start Docker Service
After Docker has been installed, you need to start the Docker service.
- For Ubuntu/Debian and CentOS/RHEL:
sudo systemctl start docker
- Enable Docker to start on boot:
sudo systemctl enable docker
Step 4: Verify Docker Installation
To confirm that Docker has been successfully installed, you can check its version:
- Run the following command to check the Docker version:
docker --version
If Docker is correctly installed, you should see the installed version displayed in the output.
Step 5: Run a Test Container
Now that Docker is installed, you can run your first container to ensure everything is working correctly.
- Run a test container (hello-world):
sudo docker run hello-world
Step 6: Managing Docker Containers
Now that Docker is running, you can start creating, managing, and running containers.
-
To run a container in the background:
sudo docker run -d [image_name]
For example, to run the popular Nginx web server container in the background:
sudo docker run -d nginx
-
List running containers:
sudo docker ps
-
List all containers (including stopped ones):
sudo docker ps -a
-
Stop a running container:
sudo docker stop [container_id or container_name]
-
Remove a container:
sudo docker rm [container_id or container_name]
Step 7: Pulling Images from Docker Hub
Docker Hub is a central repository where you can find thousands of container images for different applications. To use an image, you must first download it (pull it) to your server.
-
To pull an image from Docker Hub:
sudo docker pull [image_name]
For example, to pull the official Nginx image:
sudo docker pull nginx
-
List all available images on your server:
sudo docker images
Step 8: Running Docker Containers with Custom Settings
Docker allows you to configure containers in many ways, including setting ports, volumes, and environment variables.
-
To run a container with a specific port:
sudo docker run -d -p 8080:80 nginx
This will map port 80 inside the container to port 8080 on the host machine, making the web server accessible on
http://your-server-ip:8080
. -
To mount a volume (persistent storage):
sudo docker run -d -v /path/on/host:/path/in/container nginx
This mounts a directory from your host machine to the container, allowing data to persist even when the container is stopped.
Step 9: Docker Compose (Optional)
Docker Compose is a tool for defining and running multi-container Docker applications. If you plan on running multiple containers (e.g., a web server and database server), Docker Compose simplifies the process.
-
Install Docker Compose on Ubuntu/Debian:
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
-
Install Docker Compose on CentOS/RHEL:
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
-
Verify the installation:
docker-compose --version
Step 10: Secure Your Docker Installation
- Configure Docker to use TLS (Transport Layer Security):
- Follow the official Docker documentation on how to configure TLS for Docker here.
- Enable Docker’s built-in firewall:
- Docker automatically configures iptables, but you may want to add additional security measures depending on your use case.
Conclusion
You have successfully installed Docker on your dedicated server and learned how to run and manage containers. Docker is an excellent tool for deploying applications in isolated environments and is widely used in web hosting, development, and production environments. With Docker, you can easily deploy and scale applications on your dedicated server while ensuring consistency and isolation.