For working professionals
For fresh graduates
More
5. Docker Hub
7. Docker Swarm
9. Docker Image
10. Docker Registry
11. Podman vs Docker
Docker has positively impacted the way we develop and deploy software. Since its inception in 2013, Docker has become the go-to platform for containerization, with millions of developers using it to build, share, and run applications consistently across various environments. In fact, Docker is downloaded 13 billion times per month from the Docker Hub.
Docker streamlines and automates the process of deploying software within small, portable containers. These containers are isolated environments that contain everything an application needs to run, from the code and runtime to system tools and libraries. This isolation is similar to virtual machines, but containers are much more efficient in terms of resource usage and startup time.
Thanks to Docker’s client-server architecture and the powerful Docker daemon, managing these containers is both straightforward and efficient. Docker is popularly used with Ubuntu because Docker works exceptionally well with this popular Linux distribution and is straightforward to install.
In this article, you will learn how to install Docker on an Ubuntu system. I will guide you through each step of the installation process, I will also show you how to run and manage containers. We will also touch on Docker images.
Docker is a significant tool that simplifies the management of application processes by running them in isolated containers, which are similar to virtual machines but are more lightweight and efficient, relying on the host operating system for many resources. This makes them highly portable and resource-friendly, ideal for modern software development.
To install Docker on Ubuntu in this tutorial, you will need the following:
First, you will need an Ubuntu 22.04 LTS server. It is important to perform some initial setup steps to enhance the security and usability of your server. Here is a quick rundown:
If you plan to create your own Docker images and drive them to the Docker Hub, you will need an account. Having a Docker Hub account allows you to store and share your Docker images easily. Setting up an account is straightforward:
There are two main ways to install Docker on Ubuntu: using the official Docker repository or using the default Ubuntu repositories. Let's go through both methods, starting with the preferred approach of using the official Docker repository.
sudo apt update
Install packages that are necessary to enable apt to access repositories over HTTPS:
sudo apt install -y curl ca-certificates apt-transport-https software-properties-common
Add Docker's GPG key to ensure the packages are authentic:
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 9DC858229FC7DD38854AE2D88D81803C0EBFCD88
sudo sh -c 'echo "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" > /etc/apt/sources.list.d/docker.list'
Refresh the package index to include the Docker packages:
sudo apt update
Install Docker CE (Community Edition):
sudo apt install docker-ce –y
Check that you have installed Docker and it is running:
sudo systemctl status docker
You should see the output indicates Docker is active and running.
If you prefer a simpler installation and do not need the latest version, you can install Docker from the default Ubuntu repositories.
Update your system's package index:
sudo apt update
Install Docker using the default repository:
sudo apt install docker.io -y
Launch Docker and make it boot-up
sudo systemctl start docker
sudo systemctl enable docker
Or you can also install Docker Compose Ubuntu for running and definingmulti-container Docker applications with a single YAML file configuration.
After running this command, you need to log out and back in for the changes to take effect.
sudo usermod -aG docker $USER
Check Docker's status to ensure it is running:
sudo systemctl status docker
You can also check the Docker version:
docker --version
You should see something like Docker version [latest_version_number], build [build_number].
After Docker is installed, start using Docker commands to manage containers.
To ensure Docker is set up correctly, you can run a simple test container:
docker run hello-world
If Docker is working properly, you will see something like this:
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1a7a86c55b9b: Pull complete
Digest: sha256:67c7c4f8ee7bb1ff729d7a5e9f6c30e828d7a14f7e0dd7a20e73c4818e6e4c4d
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
It looks from this message that everything is operating as it should with your installation.
Docker could not find the hello-world image locally, so it pulled it from Docker Hub, the default repository. Once downloaded, Docker created and ran a container from this image, displaying the welcome message.
docker ps
Once Docker is installed on your Ubuntu system, the next step is to work with Docker images. Docker containers are created from these images, and by default, Docker pulls them from Docker Hub, a popular repository for Docker images. Here is how you can manage Docker images effectively.
Search for images that are available on Docker Hub by using the docker search command. For instance, perform the following search for the Ubuntu image:
docker search ubuntu
You will get a list of available images:
Name | Description | Stars | Official | Automated |
---|---|---|---|---|
Ubuntu | A Debian-based Linux operating system. | 11000 | [OK] | |
dorowu/ubuntu-desktop-lxde-vnc | Provides HTML5 VNC interface for Ubuntu desktop. | 450 | [OK] | |
rastasheep/ubuntu-sshd | Dockerized SSH service built on top of official Ubuntu. | 250 | [OK] | |
... | ... | ... | ... | ... |
The OFFICIAL column indicates images that are officially maintained by the Docker team.
Once you have identified an image you want, download it using the docker pull command. Taking the official Ubuntu image as an example, download it:
docker pull ubuntu
You will see the following output:
Using default tag: latest
latest: Pulling from library/ubuntu
6e1bee0e3d6a: Pull complete
b1c53e5aeb0d: Pull complete
…
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest
To see which images are downloaded to your system, use the docker images command:
docker images
The output will list all downloaded images:
REPOSITORY | TAG | IMAGE ID | CREATED | SIZE |
---|---|---|---|---|
Ubuntu | latest | 6e1bee0e3d6a | 2 weeks ago | 72.9MB |
hello-world | latest | b1c53e5aeb0d | 5 months ago | 13.2kB |
With an image downloaded, you can run a container using the docker run command. For instance, to run a container with the Ubuntu image and get a bash shell:
docker run -it ubuntu bash
This command starts a new container from the Ubuntu image and opens an interactive terminal session.
Run an Interactive Ubuntu Container:
docker run -it ubuntu
Your prompt will change to indicate you are inside the container:
root@2c4b6f7c8a2d:/#
Update Package Database:
apt update
Install an Application (e.g., curl):
apt install curl
Verify Installation:
curl --version
Example Output:
curl 7.68.0 (x86_64-pc-linux-gnu) libcurl/7.68.0
Exit the Container:
Exit
List Active Containers:
docker ps
List All Containers (Including Stopped):
docker ps -a
Example Output:
CONTAINER ID | IMAGE | COMMAND | CREATED | STATUS | NAMES |
---|---|---|---|---|---|
2c4b6f7c8a2d | ubuntu | /bin/bash | 5 minutes ago | Exited (0) About a minute ago | focused_turing |
View Most Recent Container:
docker ps -l
Starting and Stopping Containers
Start a Container:
docker start 2c4b6f7c8a2d
Stop a Running Container:
docker stop focused_turing
Removing Containers
Remove a Container:
docker rm epic_morse
Running a Named Container
Run a Container with a Custom Name:
docker run --name my_ubuntu -it ubuntu
Automatically Remove Container After Exit:
docker run --rm -it ubuntu
When you start a Docker container, you have to create, modify, and delete files like you do in a virtual machine. These changes are specific to that container and will be lost once the container is removed with the docker rm command. To preserve your changes, you can commit the container state to a new Docker image.
Make Changes Inside a Container:
docker run -it ubuntu
Install Node.js as an example:
apt update && apt install -y nodejs
Verify the installation:
node -v
Example output:
v10.19.0
Commit the Container to a New Image:
docker commit -m "Added Node.js" -a "Your Name" container_id your_username/ubuntu-nodejs
Replace container_id with your container’s ID. Example:
docker commit -m "Added Node.js" -a "David" 3b5e72d1bb7e sammy/ubuntu-nodejs
Verify the New Image:
docker images
Example output:
REPOSITORY | TAG | IMAGE ID | CREATED | SIZE |
---|---|---|---|---|
David/ubuntu-nodejs | latest | c2c95d5c68a3 | 10 seconds ago | 150MB |
Pushing the Image to Docker Hub
Log in to Docker Hub:
docker login -u your_username
Enter your Docker Hub password when prompted.
Tag the Image (if necessary): If your Docker Hub username differs from the local username:
docker tag sammy/ubuntu-nodejs your_username/ubuntu-nodejs
Push the Image to Docker Hub:
docker push your_username/ubuntu-nodejs
Example Output:
The push refers to repository [docker.io/sammy/ubuntu-nodejs]
e3fbbfb44187: Pushed
5f70bf18a086: Pushed
...
Installing Docker on Ubuntu is a straightforward process that unlocks a powerful tool for modern software development and deployment. Docker containers provide isolated, efficient environments to run your applications, similar to virtual machines but with less overhead.
When you follow the steps outlined in this article to install Docker on Ubuntu, you can quickly set up Docker on your Ubuntu system. You can also run and manage containers, and you can even commit and share your custom container images.
There is no doubt that with Docker, you are well-equipped to handle the complexities of modern software development, and this will ensure your applications run smoothly and consistently no matter where they are deployed.
1. How do I install Docker on Ubuntu?
Use the official Docker repository to install Docker on Ubuntu. First, update your package list with sudo apt update, then install prerequisites with sudo apt install apt-transport-https ca-certificates curl software-properties-common. Add Docker’s GPG key and repository, then update your package list again and install Docker with sudo apt install docker-ce.
2. How to install an Ubuntu VM Docker?
To install Docker inside an Ubuntu VM, follow the same steps as you would on a regular Ubuntu system: update your package list, install prerequisites, add Docker’s GPG key and repository, update again, and finally install Docker with sudo apt install docker-ce.
3. How to run Docker in Docker Ubuntu?
Running Docker within Docker (Docker-in-Docker) can be achieved by starting a container with Docker privileges. Use the command docker run --privileged -d --name dind docker:dind to start a Docker container that can run Docker itself.
4. How to install and setup Docker?
First, update your system’s package list. Install the necessary prerequisites, add Docker’s official GPG key, and set up the Docker repository. After that, install Docker with sudo apt install docker-ce. Finally, start the Docker service and enable it to start on boot with sudo systemctl start docker and sudo systemctl enable docker.
5. Which command is used to install Docker?
The command to install Docker in Ubuntu is sudo apt install docker-ce.
6. How to use Docker step by step?
7. How to install Docker in Linux step by step?
8. What is Docker used for?
Docker is used for developing, shipping, and running applications in isolated environments called containers.
9. How to install and run Docker on Linux?
To install Docker, follow the steps to update your package list, install prerequisites, add Docker’s GPG key and repository, update again, and install Docker with sudo apt install docker-ce. Start and enable Docker with sudo systemctl start docker and sudo systemctl enable docker. Then, run a test container with docker run hello-world to ensure everything is set up correctly.
Author
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.