How to install Docker on Ubuntu 22.04?
Docker is the leading containerization platform, allowing developers to build, share, deploy, and manage applications in a consistent environment. It simplifies the application development process by providing isolated containers that can run anywhere. Docker has become immensely popular among developers and organizations due to its ability to streamline development and operations workflows.
This step-by-step tutorial will guide you on how to install Docker on Ubuntu 22.04, as well as how you can manage Docker containers and images.
What is a Docker Container?
A Docker container is an isolated environment that packages an application along with all its dependencies, libraries, and configuration files. This ensures that the application runs consistently across different environments, from development to production.
What is a Docker Image?
A Docker image is a read-only template that defines the contents and setup of containers. When you run a container, Docker creates a read-write layer on top of the image, allowing multiple containers to share the same image while maintaining their unique changes.
Why Docker is Popular? (Key Benefits of Using Docker)
Docker enhances portability, consistency, and efficiency, making it a valuable tool for developers. Here are some key benefits:
- Portability: Docker containers can run on any system with Docker installed.
- Consistency: Ensures reliable application transfers from development to testing to production.
- Loose Coupling: Encapsulates applications and their dependencies into modular containers.
- Lightweight: Docker containers share resources and use less storage than traditional virtual machines by sharing the host's kernel.
- Efficiency: Optimizes resource usage by sharing the host operating system.
- Automation: Docker images and Dockerfiles provide automated methods for consistently building and running applications.
- Scalability: Containers can be scaled horizontally by deploying new instances across different machines without affecting existing ones.
- Fast Deployment: New containers can be quickly cloned and deployed from updated Docker images, offering a faster alternative to traditional virtualization methods.
By leveraging Docker, developers can ensure their applications run reliably and efficiently in any environment.
Prerequisites
To install Docker on Ubuntu 22.04, make sure your system should meet the following requirements:
- An instance of Ubuntu 22.04 with SSH access.
- A user with sudo privileges is configured on the server.
Let's get started!
How to Install Docker on Ubuntu 22.04 (Step-By-Step Docker Installation)
You can install Docker on Ubuntu 22.04 using a few simple commands. Follow these steps to set up Docker on your system:
Step 1: Update the System and Install Dependencies
First, log in to your server instance via SSH. Once connected, update the local package index. This step is particularly important if you're working with a fresh installation of Ubuntu 22.04.
$ sudo apt update
Next, install the necessary dependency packages for Docker using the following command:
$ sudo apt install apt-transport-https ca-certificates curl software-properties-common
After installing these dependencies, you can proceed to the next step.
Step 2: Install Docker on Ubuntu 22.04
To ensure you install the latest version of Docker, it's best to use the official Docker repository rather than the Ubuntu default repository, which may not always have the most up-to-date version. Follow these steps to install Docker on Ubuntu 22.04 from the official repository:
First, add the GPG signing key for the Docker repository using the curl command:
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Next, add the Docker APT repository to your system by creating a new entry in the sources.list.d directory:
$ 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
To ensure the system recognizes the newly added Docker repository, update the local package index again:
$ sudo apt update
Now, select Community Edition to install Docker on Ubuntu 22.04, which is free to download and use. The -y option allows for a non-interactive installation:
$ sudo apt install docker-ce -y
Once the installation is complete, the Docker daemon should be running. To verify this, check the status of the Docker service:
$ sudo systemctl status docker
You should see output confirming that Docker is up and running successfully.
Step 3: Add User to Docker Group
By default, Docker commands can only be executed by the root user or a user in the Docker group created during installation. If you try to run a Docker command like docker run as a non-root user, you will encounter a permissions error.
To avoid this, you can add the currently logged-in user to the Docker group. This allows the user to run Docker commands without needing to use sudo. In this example, we'll add the user “samreena” to the Docker group.
First, run the following command to add the user to the Docker group:
$ sudo usermod -aG docker ${USER}
Next, verify that the user has been added to the Docker group by running:
$ groups ${USER}
You should see docker listed among the groups the user belongs to. To apply this change, close and reopen your terminal session, or run:
su - ${USER}
Now, you should be able to run Docker commands without needing root privileges.
Step 4: Test Docker Installation
To verify that Docker is installed correctly, we need to test its functionality. Docker is set up to download images from Docker Hub by default, which is the largest repository of container images maintained by software vendors and the open-source community.
To ensure you can access and pull images from Docker Hub, we’ll run a simple container called hello-world. This can be done with the following command:
$ docker run hello-world
When you run this command, Docker will check for the hello-world image locally. If it doesn't find it, Docker will contact the Docker daemon to pull the image from Docker Hub. The Docker daemon will then create a container from the image and display the output in your terminal. Once the output is shown, the container will stop running.
The output will begin with "Hello from Docker" and continue until the shell prompt reappears.
How to Use Docker on Ubuntu 22.04? (Run Docker Commands)
Docker commands generally follow this syntax:
$ docker [option] [command]
To see a list of all available Docker commands, simply run:
$ docker
You can also get detailed system-wide information about Docker, including the Docker version, running containers, and images, by running:
$ docker info
This will display comprehensive details about your Docker installation and its current state.
Working with Docker Images
In the previous step, we ran a Docker container from a Docker image. Let’s explore the difference between Docker containers and images.
- Docker Container: A standalone, isolated environment that includes an application and its dependencies, built on top of a Docker image.
- Docker Image: A file containing instructions to create a Docker container.
To search for an image on Docker Hub, use the docker search command followed by the image name. For instance, to search for the httpd image, run:
$ docker search httpd
Docker will search the registry and list matching images. The output will show details, including an "Official" column where an "OK" entry indicates that the image is provided by the official maintainer, such as the Apache HTTP Server for the httpd image.
To download an image, use the docker pull command:
$ docker pull httpd
You can list the images downloaded to your system with the docker images command:
$ docker images
The output will display information about the images, such as the httpd and hello-world images that you have downloaded or used.
Next, we’ll delve into working with Docker containers.
Working with Docker Containers
You can create a container from a locally downloaded image using the docker run command followed by the image name. For instance, to create a container from the httpd image, use the following command:
$ docker run -d -p 8080:80 httpd
Here, the -d option runs the container in the background, and the -p option maps port 80 of the container to port 8080 on your local machine. The terminal will display the container ID, which is a unique alphanumeric string.
If you want an interactive shell session within a container, you can use the -i and -t options together. For example, to run an Ubuntu container directly from Docker Hub, use the following command:
$ docker run -it ubuntu bash
Docker will search for the AlmaLinux image locally. If it’s not found, Docker will pull the image from Docker Hub, create a container, and open an interactive shell session. Your command prompt will change, indicating that you are now working inside the container as the root user.
Within the container, you can run any command. For example, to update the system, run:
# dnf update
Press y to proceed with the update.
To install the Redis database, run:
# dnf install redis -y
The output will confirm a successful installation.
Remember, any changes you make apply only to that container. To exit the container, simply run the exit command.
How to Manage Docker Containers and Images?
To list all Docker images on your system, use:
$ docker images
To view running containers, use:
$ docker ps
To list all containers, including those that have stopped, use:
$ docker ps -a
To view the most recently created container, use:
$ docker ps -l
To stop a running container, use:
$ docker stop [container_ID or name]
To start a stopped container, use:
$ docker start [container_ID or name]
To restart a container, use:
$ docker restart [container_ID or name]
To remove a container, ensure it is stopped first, then use:
$ docker rm [container_ID or name]
To force remove a running container, use:
$ docker rm -f [container_ID or name]
Conclusion
In this guide, we've covered the steps to install Docker on Ubuntu 22.04 and provided instructions on managing Docker containers and images. By following these steps, you can efficiently set up Docker and begin leveraging its powerful containerization capabilities. For further details and advanced usage, refer to the official Docker documentation.
Is your business outgrowing the capabilities of a VPS? Upgrade to a dedicated server today!
Experience a powerful, scalable, and customizable environment with unlimited traffic.
Take your business to the next level with our dedicated server solutions. Contact us now to learn more!