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. Additionally, you’ll learn how to start, stop, and remove containers, as well as how to commit changes in a container to a new Docker image. This tutorial also covers how to install docker with GPU support, common errors and how to fix them, and security best practices.
Simplify deploying applications with DigitalOcean App Platform. Deploy directly from GitHub in minutes.
To follow this tutorial, you will need the following:
sudo
non-root user and a firewall.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:
Update your existing list of packages again for the addition to be recognized:
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.
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 version 20.10.14, 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 matches 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 dazzling_taussig
:
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.
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.
To install Docker with GPU support, you’ll need to follow a slightly different process than the standard Docker installation. This is because GPU support requires additional drivers and configurations to enable Docker to utilize the GPU resources. Here are the steps to help you install Docker with GPU support:
nvidia-docker2
package, which provides the necessary tools for Docker to interact with the NVIDIA GPU.nvidia-docker2
package:This should display the NVIDIA runtime as an available runtime.
To use GPU support with your Docker containers, you’ll need to specify the --gpus
flag when running your container. For example:
This command runs a container with the NVIDIA CUDA image and executes the nvidia-smi
command to verify GPU support.
For more information on installing and using Docker with GPU support, refer to the following resources:
By following these steps and using the provided resources, you should be able to successfully install and use Docker with GPU support on your system.
Docker Engine Security is crucial to ensure that your containers and the host system are protected from potential vulnerabilities. Here are some best practices to follow:
Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to define the services that make up your application and how they interact with each other. Here are some benefits of using Docker Compose:
Removing Docker packages is an essential part of maintaining your system and ensuring that you have the latest versions of Docker installed. Here are some steps to follow:
dpkg -l | grep docker
to list all installed Docker packages.sudo apt remove docker-ce
to remove the Docker package.sudo apt autoremove
to remove any dependencies that are no longer needed.sudo apt update
to update your package list after removing Docker packages.Images, containers, volumes, or custom configuration files on your host aren’t automatically removed. To delete all images, containers, and volumes:
Remove source list and keyrings:
If the Docker daemon fails to start, it can be due to various reasons such as configuration issues, conflicts with other services, or system resource constraints. To troubleshoot this issue, you may observe error messages like:
or
To resolve this issue:
sudo systemctl status docker
sudo journalctl -u docker
sudo systemctl enable docker
sudo systemctl restart docker
Permission errors occur when the user running Docker commands does not have sufficient privileges. To troubleshoot this issue, you may observe error messages like:
To resolve this issue:
docker
group: sudo usermod -aG docker ${USER}
sudo
before running Docker commands: sudo docker <command>
By following these steps, you should be able to resolve common errors related to the Docker daemon not starting and permission errors when running Docker commands.
To install Docker on Ubuntu, follow these steps:
sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
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
sudo apt update
sudo apt install docker-ce
To verify that Docker is installed on Ubuntu, run the following command: sudo systemctl status docker
This command will show you the status of the Docker service. If Docker is installed and running, you should see an output indicating that the service is active and running. Here’s an example of what you might see:
sudo
?To run Docker without using sudo
, you need to add your user to the docker
group. Here’s how:
docker
group: sudo usermod -aG docker ${USER}
After adding your user to the docker
group, you should be able to run Docker commands without using sudo
.
To uninstall Docker from Ubuntu, follow these steps:
sudo systemctl stop docker
sudo apt purge docker-ce
sudo apt autoremove
sudo rm /etc/apt/sources.list.d/docker.list
In this tutorial, you installed Docker, worked with images and containers, and pushed a modified image to Docker Hub. Now that you know the basics, explore the other Docker tutorials in the DigitalOcean Community. For more advanced Docker configurations, consider the following tutorials:
These tutorials will help you further expand your Docker knowledge and explore different use cases.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This curriculum introduces open-source cloud computing to a general audience along with the skills necessary to deploy applications and websites securely to the cloud.
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!
Thank you so much
but I see some differences from the steps on https://docs.docker.com/engine/install/ubuntu/
can you explain why?
Appreciate your patience and support for us and thanks to the entire team who is helping thousands and lakhs of readers with the best articles/blogs on DEVOPS, AWS Architect and all such tools. Here in this current article there is a mistake observed without executing container how come root changes with the container id, pls observe and update. THe command docker exec itself didnt run here but you mentioned
Your command prompt should change to reflect the fact that you’re now working inside the container and should take this form:
Output root@d9b100f2f636:/#
Thanks for this great tutorial! So basically you can jump in and use an environment created by someone. How would you ensure that the environment you use is secured? For example for a PHP webapp using LAMP stack, you would depend on linux, apache, php and mysql. How would you know all these components installed are not compromised? In the old days, I used to compiled everything from source and that would take the whole day!
I am unable to start the docker when I use command
sudo service docker start
it outputs starting docker but when I usesudo service docker status
it shows docker not running. I am using wls2 Ubuntu 22.04 lts. or when I usesudo docker run hello-world
I get output asdocker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?. See 'docker run --help'.
I have tried solutions at other forums but doesn’t work.For those having problems getting the docker service to run:
https://stackoverflow.com/questions/74186195/docker-service-wont-start-on-ubuntu-22-04-on-wsl2/74186196#74186196
¡Hi! At
Step 6
, help for Docker isdocker run --help
(double dash). Thanks for this tutorial, ¡very great! 👍🏽I could not run the install part to the end, kept bumping into this error when running ‘sudo apt update’
W: GPG error: https://download.docker.com/linux/ubuntu jammy InRelease: The following signatures couldn’t be verified because the public key is not available: NO_PUBKEY 7EA0A9C3F273FCD8 E: The repository ‘https://download.docker.com/linux/ubuntu jammy InRelease’ is not signed.
I had to install Docker following the official instructions … any idea why this fails ?
I love this tutorial SO MUCH. AND i love Digital Ocean so much !!! The quality of tutorials is always the best. Thank you!!
if you use
and want use docker from you default user, bit not loging to root, you need use this command:
Thank you for the article, very helpful. I used it to update docker on a machine where it was installed earlier from another source. However I think it misses the installation of
docker-ce-cli
because when I updated onlydocker-ce
it left the Docker Client in the previous version.