Install Docker on Ubuntu 22.04: A Complete Step-by-Step Guide
Want to streamline application management? Learn how to install Docker on Ubuntu 22.04 with this easy-to-follow tutorial. We'll cover everything from installation to running your first container.
Why Use Docker?
Docker simplifies application management through containerization. These containers offer numerous benefits:
- Portability: Run applications consistently across different environments.
- Resource Efficiency: Containers share the host OS kernel, minimizing overhead compared to virtual machines.
- Isolation: Applications run in isolated processes, preventing conflicts.
Perfect for developers and system administrators looking to improve workflow and deployment efficiency.
Prerequisites: Setting Up Your Ubuntu Server
Before you install Docker on Ubuntu, you'll need:
- An Ubuntu 22.04 server with a non-root user with
sudo
privileges and a firewall enabled. - A Docker Hub account (optional, for creating and pushing your own images).
Step 1: Installing Docker: Getting the Latest Version
The default Ubuntu repository might not have the newest Docker version. Here’s how to install Docker CE (Community Edition) from the official Docker repository for Ubuntu 22.04 (Jammy Jellyfish):
-
Update Package List: Refresh your package index.
sudo apt update
-
Install Prerequisite Packages: These packages allow
apt
to use repositories over HTTPS.sudo apt install apt-transport-https ca-certificates curl software-properties-common
-
Add Docker's GPG Key: Verify the authenticity of the Docker packages.
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
-
Add the Docker Repository: Tell APT where to find the Docker packages.
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 List (Again): Incorporate the new repository.
sudo apt update
-
Verify Docker Repo: Ensure you're installing from the official Docker repository.
apt-cache policy docker-ce
-
Install Docker CE: Install the Docker package.
sudo apt install docker-ce
Step 2: Managing Docker Permissions: Using Docker Without Sudo
By default, only root users and members of the 'docker' group can run Docker commands. Here’s how to grant a user the necessary permissions to execute Docker commands without sudo:
-
Add User to Docker Group: Add your username to the
docker
group.sudo usermod -aG docker ${USER}
-
Apply Changes: Log out and back in, or run the following command:
su - ${USER}
Now you can run Docker commands without preceding them with sudo
.
Step 3: Essential Docker Commands: Managing Images and Containers
Understanding fundamental Docker commands is key to managing Docker containers. Here are some of the most important:
docker images
: List all available Docker images on your system.docker ps
: List running Docker containers. Usedocker ps -a
to list all containers (running and stopped).docker run
: Create and run a new container from an image.docker stop
: Stop a running container.docker rm
: Remove a stopped container.docker pull
: Download a Docker image from Docker Hub.
Step 4: Running Your First Container: Hello World!
Let's test your Docker installation by running the "hello-world" image:
docker run hello-world
This command downloads the hello-world
image (if it's not already on your system) and runs a container from it. You should see a message confirming that Docker is working correctly!
Conclusion: You've Installed Docker!
Congratulations! You've successfully installed Docker on Ubuntu 22.04. You are now ready to explore the world of containerization and streamline your application deployments. Experiment with different images, build your own Dockerfiles, and enjoy the benefits of Docker's portability and efficiency.