First, update the package index to ensure you're working with the latest list of available packages:
sudo apt update
Now, install Docker using the docker.io package from the Ubuntu repository:
sudo apt install -y docker.io
This will install Docker, the Docker CLI, and other related tools.
After installation, start the Docker service and enable it to automatically start on boot:
sudo systemctl start docker
sudo systemctl enable docker
To confirm that Docker has been installed and is running correctly, check the version:
docker --version
You can also run a test container to ensure everything is working:
sudo docker run hello-world
To avoid using sudo for every Docker command, add your user to the docker group:
sudo usermod -aG docker $USER
After running this command, log out and log back in, or run:
newgrp docker
Now you can use Docker commands without sudo:
docker ps
To make sure Docker is running properly, try pulling and running an image, such as Nginx:
docker pull nginx
docker run -d -p 80:80 nginx
This will pull the Nginx image and start an Nginx web server on port 80.