Docker is an application that simplifies the process of managing application processes in containers. Containers let you run your applications in resource-isolated processes. They’re similar to virtual machines, but containers are more portable, more resource-friendly, and more dependent on the host operating system.
For a detailed introduction to the different components of a Docker container, check out The Docker Ecosystem: An Introduction to Common Components.
In this tutorial, you’ll install and use Docker Community Edition (CE) on Ubuntu. You’ll install Docker itself, work with containers and images, and push an image to a Docker Repository. If you’re using Ubuntu 22.04, you can follow our guide on How To Install and Use Docker on Ubuntu 22.04 instead.
This article will walk you through installing Docker on an Ubuntu server. If you wanted a 1-click way to deploy a Docker application to a live server, take a look at DigitalOcean App Platform.
To follow this tutorial, you will need the following:
The Docker installation package available in the official Ubuntu repository may not be the latest version. To ensure we get the latest version, we’ll install Docker from the official Docker repository. To do that, we’ll add a new package source, add the GPG key from Docker to ensure the downloads are valid, and then install the package.
First, update your existing list of packages:
Next, install a few prerequisite packages which let apt
use packages over HTTPS:
Then add the GPG key for the official Docker repository to your system:
Add the Docker repository to APT sources:
This will also update our package database with the Docker packages from the newly added repo.
Make sure you are about to install from the Docker repo instead of the default Ubuntu repo:
You’ll see output like this, although the version number for Docker may be different:
Notice that docker-ce
is not installed, but the candidate for installation is from the Docker repository for Ubuntu (focal
).
Finally, install Docker:
Docker should now be installed, the daemon started, and the process enabled to start on boot. Check that it’s running:
The output should be similar to the following, showing that the service is active and running:
Installing Docker now gives you not just the Docker service (daemon) but also the docker
command line utility, or the Docker client. We’ll explore how to use the docker
command later in this tutorial.
By default, the docker
command can only be run the root user or by a user in the docker group, which is automatically created during Docker’s installation process. If you attempt to run the docker
command without prefixing it with sudo
or without being in the docker group, you’ll get an output like this:
If you want to avoid typing sudo
whenever you run the docker
command, add your username to the docker
group:
To apply the new group membership, log out of the server and back in, or type the following:
You will be prompted to enter your user’s password to continue.
Confirm that your user is now added to the docker group by typing:
If you need to add a user to the docker
group that you’re not logged in as, declare that username explicitly using:
The rest of this article assumes you are running the docker
command as a user in the docker group. If you choose not to, please prepend the commands with sudo
.
Let’s explore the docker
command next.
Using docker
consists of passing it a chain of options and commands followed by arguments. The syntax takes this form:
To view all available subcommands, type:
As of Docker 19, the complete list of available subcommands includes:
To view the options available to a specific command, type:
To view system-wide information about Docker, use:
Let’s explore some of these commands. We’ll start by working with images.
Docker containers are built from Docker images. By default, Docker pulls these images from Docker Hub, a Docker registry managed by Docker, the company behind the Docker project. Anyone can host their Docker images on Docker Hub, so most applications and Linux distributions you’ll need will have images hosted there.
To check whether you can access and download images from Docker Hub, type:
The output will indicate that Docker in working correctly:
Docker was initially unable to find the hello-world
image locally, so it downloaded the image from Docker Hub, which is the default repository. Once the image downloaded, Docker created a container from the image and the application within the container executed, displaying the message.
You can search for images available on Docker Hub by using the docker
command with the search
subcommand. For example, to search for the Ubuntu image, type:
The script will crawl Docker Hub and return a listing of all images whose name match the search string. In this case, the output will be similar to this:
In the OFFICIAL column, OK indicates an image built and supported by the company behind the project. Once you’ve identified the image that you would like to use, you can download it to your computer using the pull
subcommand.
Execute the following command to download the official ubuntu
image to your computer:
You’ll see the following output:
After an image has been downloaded, you can then run a container using the downloaded image with the run
subcommand. As you saw with the hello-world
example, if an image has not been downloaded when docker
is executed with the run
subcommand, the Docker client will first download the image, then run a container using it.
To see the images that have been downloaded to your computer, type:
The output will look similar to the following:
As you’ll see later in this tutorial, images that you use to run containers can be modified and used to generate new images, which may then be uploaded (pushed is the technical term) to Docker Hub or other Docker registries.
Let’s look at how to run containers in more detail.
The hello-world
container you ran in the previous step is an example of a container that runs and exits after emitting a test message. Containers can be much more useful than that, and they can be interactive. After all, they are similar to virtual machines, only more resource-friendly.
As an example, let’s run a container using the latest image of Ubuntu. The combination of the -i and -t switches gives you interactive shell access into the container:
Your command prompt should change to reflect the fact that you’re now working inside the container and should take this form:
Note the container id in the command prompt. In this example, it is d9b100f2f636
. You’ll need that container ID later to identify the container when you want to remove it.
Now you can run any command inside the container. For example, let’s update the package database inside the container. You don’t need to prefix any command with sudo
, because you’re operating inside the container as the root user:
Then install any application in it. Let’s install Node.js:
This installs Node.js in the container from the official Ubuntu repository. When the installation finishes, verify that Node.js is installed:
You’ll see the version number displayed in your terminal:
Any changes you make inside the container only apply to that container.
To exit the container, type exit
at the prompt.
Let’s look at managing the containers on our system next.
After using Docker for a while, you’ll have many active (running) and inactive containers on your computer. To view the active ones, use:
You will see output similar to the following:
In this tutorial, you started two containers; one from the hello-world
image and another from the ubuntu
image. Both containers are no longer running, but they still exist on your system.
To view all containers — active and inactive, run docker ps
with the -a
switch:
You’ll see output similar to this:
To view the latest container you created, pass it the -l
switch:
To start a stopped container, use docker start
, followed by the container ID or the container’s name. Let’s start the Ubuntu-based container with the ID of 1c08a7a0d0e4
:
The container will start, and you can use docker ps
to see its status:
To stop a running container, use docker stop
, followed by the container ID or name. This time, we’ll use the name that Docker assigned the container, which is quizzical_mcnulty
:
Once you’ve decided you no longer need a container anymore, remove it with the docker rm
command, again using either the container ID or the name. Use the docker ps -a
command to find the container ID or name for the container associated with the hello-world
image and remove it.
You can start a new container and give it a name using the --name
switch. You can also use the --rm
switch to create a container that removes itself when it’s stopped. See the docker run help
command for more information on these options and others.
Containers can be turned into images which you can use to build new containers. Let’s look at how that works.
When you start up a Docker image, you can create, modify, and delete files just like you can with a virtual machine. The changes that you make will only apply to that container. You can start and stop it, but once you destroy it with the docker rm
command, the changes will be lost for good.
This section shows you how to save the state of a container as a new Docker image.
After installing Node.js inside the Ubuntu container, you now have a container running off an image, but the container is different from the image you used to create it. But you might want to reuse this Node.js container as the basis for new images later.
Then commit the changes to a new Docker image instance using the following command.
The -m switch is for the commit message that helps you and others know what changes you made, while -a is used to specify the author. The container_id
is the one you noted earlier in the tutorial when you started the interactive Docker session. Unless you created additional repositories on Docker Hub, the repository
is usually your Docker Hub username.
For example, for the user sammy, with the container ID of d9b100f2f636
, the command would be:
When you commit an image, the new image is saved locally on your computer. Later in this tutorial, you’ll learn how to push an image to a Docker registry like Docker Hub so others can access it.
Listing the Docker images again will show the new image, as well as the old one that it was derived from:
You’ll see output like this:
In this example, ubuntu-nodejs
is the new image, which was derived from the existing ubuntu
image from Docker Hub. The size difference reflects the changes that were made. And in this example, the change was that NodeJS was installed. So next time you need to run a container using Ubuntu with NodeJS pre-installed, you can just use the new image.
You can also build Images from a Dockerfile
, which lets you automate the installation of software in a new image. However, that’s outside the scope of this tutorial.
Now let’s share the new image with others so they can create containers from it.
The next logical step after creating a new image from an existing image is to share it with a select few of your friends, the whole world on Docker Hub, or other Docker registry that you have access to. To push an image to Docker Hub or any other Docker registry, you must have an account there.
This section shows you how to push a Docker image to Docker Hub. To learn how to create your own private Docker registry, check out How To Set Up a Private Docker Registry on Ubuntu or How To Set Up a Private Docker Registry on Ubuntu.
To push your image, first log into Docker Hub.
You’ll be prompted to authenticate using your Docker Hub password. If you specified the correct password, authentication should succeed.
Note: If your Docker registry username is different from the local username you used to create the image, you will have to tag your image with your registry username. For the example given in the last step, you would type:
Then you may push your own image using:
To push the ubuntu-nodejs
image to the sammy repository, the command would be:
The process may take some time to complete as it uploads the images, but when completed, the output will look like this:
After pushing an image to a registry, it should be listed on your account’s dashboard, like that show in the image below.
If a push attempt results in an error of this sort, then you likely did not log in:
Log in with docker login
and repeat the push attempt. Then verify that it exists on your Docker Hub repository page.
You can now use docker pull sammy/ubuntu-nodejs
to pull the image to a new machine and use it to run a new container.
While Docker lets you build and run containers, managing multi-container applications with just Docker can be tedious. That’s where Docker Compose comes in.
Docker Compose is a tool for defining and running multi-container Docker applications using a YAML file. Instead of manually running separate containers for a web server, database, and caching layer, you can define them all in a docker-compose.yml
file and run them with:
For a detailed guide on using Docker Compose, including how to set up complex multi-container applications and manage their configurations, check out our tutorial on How To Install and Use Docker Compose on Ubuntu. This guide will walk you through creating and managing multi-container applications with Docker Compose, from basic setups to more complex configurations.
Feature | Docker CLI | Docker Compose |
---|---|---|
Usage | Single container operations | Multi-container orchestration |
Configuration | CLI commands | YAML configuration file |
Dependency handling | Manual | Handles linked services automatically |
Best use case | Testing isolated containers | Local development and staging setups |
For more, see How To Install and Use Docker Compose on Ubuntu.
docker: command not found
Fix: The Docker CLI isn’t in your $PATH
. Reinstall Docker or ensure /usr/bin
is included.
Cannot connect to the Docker daemon
Fix: Docker isn’t running, or your user isn’t in the docker
group.
Then log out and log back in.
Fix: If the keyserver or Docker GPG key changes, refer to Docker’s official docs for the latest key and repository steps. If you’re using Ubuntu 22.04, you may want to check our guide on How To Install and Use Docker on Ubuntu 22.04 for version-specific instructions. For automated installation that can help avoid these issues, consider using our guide on How To Use Ansible to Install and Set Up Docker on Ubuntu.
Docker Desktop is now available in beta for Linux distributions like Ubuntu. It provides a GUI, bundled Docker Engine, and Kubernetes support.
To install Docker Desktop on Ubuntu:
Refer to Docker Desktop for Linux docs for prerequisites and download links.
Note: Docker Desktop is suited for development environments. For server-side installs, use Docker CE.
For automated environments, install Docker using a Dockerfile
. Here’s an example:
For more advanced automation options, you can also use Ansible to install and configure Docker. Check out our guide on How To Use Ansible to Install and Set Up Docker on Ubuntu. This is particularly useful if you need to manage Docker installations across multiple servers or want to maintain consistent configurations in your infrastructure.
To remove Docker from your system, run:
The recommended and most reliable way to install Docker on Ubuntu is by using Docker’s official repository. This approach ensures you always receive the latest stable version, complete with security patches and updates directly from Docker. To do this, you first update your package index and install prerequisites like apt-transport-https
, ca-certificates
, curl
, gnupg
, and lsb-release
. Next, you add Docker’s official GPG key and set up the stable repository. After updating your package index again, you can install Docker Engine and related components using
This method is preferred over using the default Ubuntu repositories, which may contain outdated versions. By following Docker’s official installation steps, you ensure compatibility with the latest features, bug fixes, and security enhancements, making your Docker experience on Ubuntu both robust and secure.
By default, Docker requires root privileges to run its commands, which means you need to prepend sudo
to every Docker command. However, you can avoid this by adding your user to the docker
group, which grants permission to run Docker commands without sudo
. To do this, execute
and then log out and log back in for the changes to take effect. This step modifies your user’s group memberships, allowing Docker to be run as a non-root user. Keep in mind that adding users to the docker
group grants them root-level access to the Docker daemon, so only add trusted users. If you skip this step, you’ll encounter permission errors when running Docker commands without sudo
. For security and convenience, it’s best to add your user to the docker
group if you use Docker frequently.
To confirm that Docker is installed and running properly on your Ubuntu system, you can use a couple of simple commands. First, run
to display detailed information about the Docker installation, including server version, storage driver, and running containers. If Docker is running, this command will return a wealth of information; if not, you’ll see an error. Another common test is to run
This command downloads a test image from Docker Hub and runs it in a container. If everything is set up correctly, you’ll see a message indicating that Docker is working as expected. If you encounter errors, check that the Docker service is running with
and review any error messages for troubleshooting. These steps help ensure your Docker installation is functional and ready for use.
Yes, you can install a specific version of Docker on Ubuntu, which is useful if you need compatibility with certain applications or want to avoid the latest changes. First, list all available Docker versions in the repository using
This command will display a list of version strings. Once you identify the desired version, install it by running
replacing <VERSION_STRING>
with the exact version number (for example, 5:20.10.7~3-0~ubuntu-focal
). This approach ensures you have precise control over the Docker version on your system. Remember to also specify matching versions for
and
if needed. Pinning a specific version can help maintain stability in production environments or when working with legacy projects that require a particular Docker release.
Docker and Docker Compose are related but serve different purposes. Docker is a platform that allows you to build, ship, and run individual containers, each encapsulating an application and its dependencies. It’s ideal for running single services or applications in isolation. Docker Compose, on the other hand, is a tool designed to define and manage multi-container applications. With Compose, you use a YAML file (docker-compose.yml
) to specify how multiple containers should interact, their configurations, networks, and volumes. This makes it easy to orchestrate complex applications consisting of several interconnected services, such as a web server, database, and cache. While Docker alone is sufficient for simple use cases, Docker Compose streamlines the process of managing multi-container setups, making development, testing, and deployment of complex applications much more efficient and reproducible.
To completely uninstall Docker from your Ubuntu system, you need to remove the Docker packages and delete any associated data. Start by running
to remove the Docker Engine, CLI, and container runtime. This command deletes the installed packages but leaves configuration files and data directories intact. To fully clean up, remove Docker’s data directories with
and
These directories store images, containers, volumes, and other persistent data. If you added your user to the docker
group, you may also want to remove the group with
After these steps, Docker and all its data will be removed from your system. Always back up important data before uninstalling to avoid accidental loss.
Yes, Docker Desktop is now available in beta for Linux distributions, including Ubuntu. Docker Desktop provides a graphical user interface (GUI) for managing containers, images, volumes, and networks, making it easier for users who prefer not to work exclusively with the command line. It also bundles the Docker Engine and offers integrated Kubernetes support, which is useful for development and testing environments. To install Docker Desktop on Ubuntu, download the appropriate .deb
package from Docker’s official website and install it using
However, Docker Desktop is primarily intended for development and not recommended for production servers. For server-side or headless environments, it’s better to use Docker Engine Community Edition (CE) via the command line. Always check Docker’s documentation for prerequisites and the latest installation instructions before proceeding.
In this comprehensive tutorial, you’ve successfully installed Docker on Ubuntu, mastered the fundamentals of container management, and learned how to work with Docker images and containers. You’ve also gained hands-on experience with Docker Hub by pushing a modified image to the registry. These skills form a solid foundation for container-based development and deployment. To further enhance your Docker expertise, we encourage you to explore the other Docker tutorials in the DigitalOcean Community, where you’ll find advanced topics like container orchestration, networking, and security best practices.
Continue building with DigitalOcean Gen AI Platform.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
I have installed Ubuntu 20.04 server and am struggling to install Docker. I am getting stuck on the step to add the GPG key for the official Docker repository; when I run this command:
I get this response:
I can’t work out how to move forward, so any ideas would be appreciated, thanks in advance.
I have a brand new droplet and there appears to be something wrong with the “stable” release using these instructions. When I run
sudo systemctl status docker
I get a bunch of error and it throws me into Vi:Although the last message claims Docker has started, I can’t run any containers. Any suggestions would be great.
Thx… Nice Demo… worked right out of the box… easy to follow…
sudo add-apt-repository “deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable”
When I run the above command on an Ubuntu 20.4 instance on AWS, then i get the following response
"E: The repository ‘https://download.docker.com/linux/ubuntu/gpg focal Release’ does not have a Release file. N: Updating from such a repository can’t be done securely, and is therefore disabled by default. N: See apt-secure(8) manpage for repository creation and user configuration details. " I am trying this on January 2021, about 6 months after the article. Yet the Release file is not available.
How should we proceed
Thx very helpful tutorial. However I got error during apt-key command
I have installed ubuntu 20.04 from ubuntu official website and Docker desktop from Docker offical website. When I try checking the status of docker using the command - sudo systemctl status docker It fails with the message - System has not been booted with systemd as init system (PID 1). Can’t operate. Failed to connect to bus: Host is down I would request Brian Hogan(original poster of this page) to help solve this issue first as rest of the process does not hold any meaning for me. This issue of systemctl and systemd not available with WSL2 is a general issue and needs to be fixed first before venturing into installing docker through ubuntu 20.04 Thanks.
Anyone knows why this error?
Thanks for sharing, worked great.
This comment has been deleted
After running the Command:
I Get this massage:
“Ubuntu 20.04.4 LTS”