Step 1: Install Dependencies
Before installing Docker, make sure your packages are updated and that necessary dependencies are installed.
sudo apt install apt-transport-https ca-certificates curl software-properties-common -y
Step 2: Add Docker’s Official GPG Key
Add Docker's GPG key to your system to verify the authenticity of the packages.
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Step 3: Add Docker Repository
Add the Docker repository to your APT sources list.
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
Step 4: Install Docker Engine
Update your package index and install Docker Engine.
sudo apt update
sudo apt install docker-ce -y
Step 5: Verify Docker Installation
Check the status of Docker to ensure it is running.
sudo systemctl status docker
Docker should now be up and running on your server.
Basic Docker Commands
-
Run a Container To start a new container, use the
docker run
command. For example:bashsudo docker run hello-world
-
List Running Containers To see all currently running containers:
bashsudo docker ps
-
List All Containers (Including Stopped) To view all containers, including those that are stopped:
bashsudo docker ps -a
-
Run a Container in Interactive Mode To start a container and open an interactive terminal session:
bashsudo docker run -it ubuntu bash
-
Stop a Container To stop a running container:
bashsudo docker stop container_id
-
Remove a Container To remove a container:
bashsudo docker rm container_id
Docker Configuration
-
Add User to Docker Group (Optional) To use Docker without
sudo
, add your user to the Docker group:bashsudo usermod -aG docker $USER
Log out and log back in, or restart your session for the changes to take effect.
Example Usage of Docker
-
Install and Run Nginx To start an Nginx container and map port 80 from the host to the container:
bashsudo docker run -d -p 80:80 nginx
-
Stop and Remove Nginx Container To stop and remove the Nginx container:
bashsudo docker stop container_id sudo docker rm container_id
Conclusion
Docker provides powerful tools for deploying and managing containerized applications. By following the steps above, you can install and start using Docker on your Debian or Ubuntu server.