Hi! Have you heard of Docker? π It’s revolutionizing software development and deployment, and while many people think it’s complicated and difficult, installing Docker on Ubuntu is actually simpler than you think! In this guide, we’ll break down the process of installing Docker on Ubuntu so that even beginners can easily follow along. No more saying “I can’t do that on my computer…”! π ββοΈ
—]
π¦ What is Docker and why do I need it?
Docker is a container-based virtualization platform. Unlike traditional virtual machines (VMs), it doesn’t virtualize an entire OS, but instead packages everything you need to run an application (code, runtime, system tools, libraries, etc.) into a self-contained environment called a container.
Why do you need Docker:
- Consistent environments: You can keep your development, testing, and deployment environments the same everywhere, solving the problem of “If it works on my computer, why not on the server?” π―.
- Fast Deployment: Containers are lightweight and quick to create, start, stop, and delete, making it easy to deploy and scale applications.
- Resource Efficient: You can run more containers with much less overhead than virtual machines, making efficient use of system resources.
- Isolation: Each container is independent, so problems in one container won’t affect others.
—.
π Before You Begin: Check Your Preparations!
Before you start your Docker installation, you’ll need some basic preparation.
- Ubuntu Operating System: This guide is written based on a Long Term Support (LTS) version of Ubuntu, such as 20.04 (Focal Fossa) or 22.04 (Jammy Jellyfish). Other versions are similarly applicable.
- Sudo Permissions: A user account with
sudo
permissions is required for installation and setup. - Internet connection: A stable internet connection is required to download the Docker packages.
—.
π Installing Docker on Ubuntu: A Complete Step-by-Step Guide
Let’s take a closer look at the most recommended method: installing from the official Docker repository, which ensures a stable installation and management of the latest version of Docker.
You can find the official repository here: #### Step 1: Uninstall your existing Docker version (optional)
If you have previously installed Docker, it is recommended that you uninstall it first to avoid conflicts. If this is your first installation, skip this step.
In “`bash sudo apt-get remove docker docker-engine docker.io containerd runc
#### Step 2: Install the required packages
Install the necessary utilities to download and validate packages from the Docker official repository.
```bash
sudo apt update # Update the system package list
sudo apt install ca-certificates curl gnupg lsb-release # Install the necessary tools to transfer data over HTTPS and manage GPG keys
ca-certificates
: Contains certificates needed to verify the authenticity of the server over SSL/TLS.curl
: A command-line tool to transfer data from a URL. Used to download the Docker GPG key.gnupg
: A core component of the GNU Privacy Guard (GnuPG) program that provides encryption and signing capabilities. It is used to verify the authenticity of downloaded packages.lsb-release
: Provides Linux Standard Base (LSB) information. It is mainly used to verify the OS version.
For more information, see: #### Step 3: Add the official Docker GPG key
To verify the integrity and authenticity of Docker packages, add Docker’s official GNU Privacy Guard (GPG) key to your system. This key allows you to verify that the packages you download have not been tampered with.
# Create a directory to store the Docker GPG key (set to permission 755)
sudo install -m 0755 -d /etc/apt/keyrings
# Download the Docker GPG key, decrypt it, and store it in the keyrings directory
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
# Set the permissions of the Docker GPG key file to be readable by all users
sudo chmod a+r /etc/apt/keyrings/docker.gpg
Step 4: Set up your Docker repository
Now add the official repository where you can download Docker packages to the Advanced Package Tool (APT) source list.
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
deb
: Indicates the package repository for Debian-based systems.arch=$(dpkg --print-architecture)
: Automatically detects and sets the architecture of the current system (e.g. amd64).signed-by=/etc/apt/keyrings/docker.gpg
: Tells Docker to verify that the packages you download from this repository are signed with the GPG key you added above.https://download.docker.com/linux/ubuntu
: The official URL to download the Docker package from.$(. /etc/os-release && echo "$VERSION_CODENAME")
: Automatically gets the codename of the current Ubuntu version (e.g.jammy
for Ubuntu 22.04,focal
for Ubuntu 20.04).stable
: Means that you want to use the stable version of Docker.sudo tee ... > /dev/null
: Write the generated repository information to the file/etc/apt/sources.list.d/docker.list
. The> /dev/null
discards the standard output so that nothing unnecessary is printed to the terminal.
Step 5: Install Docker Engine
Now that everything is ready, it’s time to install Docker Engine and its associated tools.
sudo apt update # Update the list of packages in the newly added Docker repository.
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
docker-ce
: The Docker Community Edition (CE) engine.docker-ce-cli
: The Docker command-line interface (CLI). It is used to run Docker commands.containerd.io
: The container runtime. It is the core component that Docker needs to run and manage containers.docker-buildx-plugin
: A plugin used to build images for different architectures using Dockerfiles.docker-compose-plugin
: A plugin version of Docker Compose, a tool for defining and running multiple container applications. (We recommend using this plugin instead of the existingdocker-compose
standalone.)
After the installation is complete, the Docker service is automatically started and activated.
For more information, see: #### Step 6: Verify your Docker installation
To verify that the installation completed successfully, run the hello-world
image.
sudo docker run hello-world
If you see a message similar to the one below when you run this command, then your Docker installation is successful! π₯³ .
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:d1725b8a1c9291c5644e569660100d90653068e16e453779d59244fe24754a6b
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker server.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub. 3. The Docker daemon pulled the "hello-world" image from the Docker Hub.
3. The Docker daemon created a new container from that image which runs the
executable that produces the "Hello from Docker!" output.
4. The Docker daemon streamed that output back to the Docker client.
For more examples and ideas, visit:
https://docs.docker.com/get-started/
Step 7: Use Docker commands without Sudo (required!) β
Currently, you can only run the docker
command by adding sudo
. Typing sudo
every time is very cumbersome and prone to mistakes. To solve this, we need to add the current user to the docker
group.
sudo usermod -aG docker $USER
usermod
: Commands to modify user accounts.-aG
: Adds a user to a specific group (-a
), indicating that the group is a secondary group (-G
).docker
: The name of the group you want to add. It is automatically generated when you install Docker.$USER
: An environment variable that represents the username you are currently logged in as.
After running this command, you must open a new terminal or log out and log back in for the changes to take effect. (Alternatively, you can use the newgrp docker
command to apply them immediately to the current session only, but we recommend re-logging in for full effect).
After re-logging in, try running the following command without sudo
to verify:
docker run hello-world
If it works without sudo
, you’re good to go! π.
Step 8: Set up automatic startup of Docker services (optional)
By default, the Docker service is set to start automatically after installation and activate automatically on boot. But just in case, you can check it or set it manually.
- Start the Docker service: **Start the Docker service with
sudo systemctl start docker
- Enable automatic startup of the Docker service on boot: Enable automatic startup of the Docker service on boot.
sudo systemctl enable docker
- Check the status of the Docker service: Check the status of the Docker service.
sudo systemctl status docker
If it says
Active: active (running)
, it is running normally.
—]
π Example of Docker in action: Spinning up an Nginx web server
Now that we have Docker installed, let’s spin up an Nginx web server as a container as a simple example.
docker run -d -p 80:80 --name my-nginx nginx
docker run
: The command to run a new container.-d
: Runs the container in the background (detached mode).-p 80:80
: Connect (map) port 80 on the host (your Ubuntu) to port 80 on the container.--name my-nginx
: Give the container the namemy-nginx
.nginx
: The name of the Docker image to run. Docker Hub will automatically download and use thenginx:latest
image.
Running this command will start the Nginx container. Open a web browser and try connecting to http://localhost
or http://
. You should be able to see the “Welcome to nginx!” page! π .
Commands for viewing and managing your container: **Command.
- To see a list of running containers:
docker ps
- Check Nginx container logs:
docker logs my-nginx
- Stop the Nginx container:
docker stop my-nginx
- Delete an Nginx container:
docker rm my-nginx
(To delete a container, you must first stop it).
—]
β οΈ Common issues and workarounds
-
**Docker commands won’t run without
sudo
!- Cause: The user is not included in the
docker
group, or changes have not been applied. - Solution: After running the command
sudo usermod -aG docker $USER
, make sure to log out and log back in. -
- (Workaround: The
newgrp docker
command can be applied to the current terminal session only, but re-login is required for continued use).
- (Workaround: The
- Cause: The user is not included in the
-
Cannot connect to the Docker daemon. Is the docker daemon running on this host?"
error occurs.- Cause: The Docker service is not running.
-
- Solution: Check the status with the
sudo systemctl status docker
command, and start the service with thesudo systemctl start docker
command. It is recommended that you also runsudo systemctl enable docker
so that it starts automatically at boot time.
- Solution: Check the status with the
-
**I get the error
Permission denied while trying to connect to the Docker daemon socket
.- Cause: This could be a permissions issue with the
docker
group, or a permissions issue with thedocker.sock
file. - Solution: This is usually resolved by running
sudo usermod -aG docker $USER
and re-logging in. If that doesn’t work, you can trysudo chmod 666 /var/run/docker.sock
, but this is not a good security practice and should be used as a temporary solution.
- Cause: This could be a permissions issue with the
—]
π In closing
Congratulations! π You now have Docker successfully installed on your Ubuntu system, and you’ve even gotten some hands-on experience running containers. Docker is a powerful tool that revolutionizes the way we build development environments and deploy applications.
We hope this guide has helped you take your first steps with Docker, and we look forward to seeing you utilize it to make your development life more efficient and enjoyable. If you have any questions, don’t hesitate to ask!
As a next step, we recommend you try creating your own images with Dockerfiles, or learn how to use Docker Compose to manage multiple containers at once. Happy Dockering! π