The Easiest Guide: How to Install Docker on Ubuntu 22.04 & Run Your First Container
Want to learn how to install Docker on Ubuntu 22.04? This guide provides a simple, step-by-step approach to getting Docker up and running, even if you're a complete beginner. You'll learn how to install the Docker Engine, manage Docker as a non-root user, and run your first container.
Why Use Docker on Ubuntu?
Docker simplifies software deployment and management by using containers. These containers package applications and their dependencies, ensuring consistent performance across different environments. Using Docker on Ubuntu allows you to isolate applications, improving security and resource utilization.
Prerequisites for Installing Docker
Before diving in, make sure you have:
- An Ubuntu 22.04 server set up.
- A
sudo
non-root user. - A basic understanding of the Linux command line.
Step 1: Installing Docker on Ubuntu 22.04
Let's get Docker installed using the official Docker repository for the latest version.
-
Update Package List: Start by updating your package lists to ensure you have the latest versions.
-
Install Prerequisite Packages: Next, install the packages that allow
apt
to use repositories over HTTPS. -
Add Docker's GPG Key: Add the official GPG key to ensure the authenticity of the Docker packages.
-
Add the Docker Repository Now, add the Docker repository to your APT sources.
-
Update Package List Again: Update your package lists to reflect the new repository.
-
Verify Docker Installation Source It's a good practice to verify that you're installing from the Docker repository.
Confirm the installation candidate points to the Docker repository.
-
Finally, Install Docker: Install Docker Community Edition (CE) with:
Step 2: Manage Docker as a Non-Root User (Optional)
Running Docker commands without sudo
is convenient. Here's how to configure it:
-
Add User to Docker Group: Add your user to the
docker
group. -
Apply Changes: Log out and back in or run the following command to apply the group membership changes.
-
Verify Group Membership: Confirm your user is in the
docker
group by typing:
Step 3: Verify Docker Installation
Ensure Docker is running correctly by checking its status:
A successful installation will show the service as active and running. With Docker successfully installed on Ubuntu, the next step is to check that you can download images.
Step 4: Running Your First Docker Container
Let's run a simple container to verify your Docker installation.
-
Run the 'hello-world' Image: This image tests your Docker setup.
If everything is set up correctly, you'll see a message confirming a successful installation!
Step 5: Working with Docker Images
Docker images are the foundation of containers. Here’s how to manage them:
-
Listing Images: See what images are available locally, use the command:
-
Pulling Images: Download an image from Docker Hub using docker pull. For example, to pull the latest Ubuntu image:
-
Running Containers: Create and run a container from an image with:
Understanding Docker Commands
Here's a quick reference to essential Docker commands:
docker ps
: List running containers.docker stop [container_id]
: Stop a running container.docker rm [container_id]
: Remove a stopped container.docker rmi [image_id]
: Remove an image.
Securing Your Docker Installation on Ubuntu
Security should be a priority. Here are a few tips:
- Keep Docker Updated: Regularly update Docker to patch vulnerabilities.
- Use Docker Secrets: Manage sensitive information like passwords securely.
- Limit User Privileges: Avoid running containers as root.
- Implement a Firewall: Use UFW to control network traffic.
Conclusion
Congratulations! You've successfully installed and used Docker on Ubuntu 22.04. You now have the foundation to start containerizing applications and streamlining your development workflow. From here, explore Docker Compose for multi-container applications and consider container orchestration tools like Kubernetes for managing deployments at scale.