Docker is a free, open-source and cross-platform containerization tool that helps you to deploy and run the application in an isolated environment. Docker has become one of the most important parts of modern software development and DevOps pipelines.
In this tutorial, you will learn –
If you want to host Docker on Cloud and don’t know how to choose the best Cloud hosting provider. You can read my guide on The 8 Best Docker Hosting Platforms in 2023.
Prerequisites
- A server running Ubuntu 20.04/Ubuntu 22.04.
- A root password is configured on your server.
Install Docker on Ubuntu
There are two ways to install Docker on Ubuntu 20.04/22.04. You can install Docker from the Ubuntu default repository or from the Docker’s official repository. The latest version of Docker is not included in the Ubuntu default repository. So it is always recommended to install Docker from Docker’s official repository.
In this section, we will show you how to install Docker on Ubuntu from Docker’s official repository.
Step 1 – Install Required Dependencies
First, you will need to install some prerequisite packages in your system. You can install them by running the following command:
apt-get install apt-transport-https ca-certificates curl software-properties-common gnupg2 -y
Step 2 – Add Docker Official Repository
Next, you will need to import the GPG key and add the Docker repository to your system.
First, import the GPG key with the following command:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Next, add the Docker official repository using the following command:
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
Once the repository is added, update the repository cache with the following command:
apt-get update -y
Step 3 – Install Docker Ubuntu
Once the repository is updated, you can install the Docker on Ubuntu by running the following command:
apt-get install docker-ce -y
This command will install the latest version of Docker from the Docker official repository.
Step 4 – Verify Docker Version
Once the Docker has been installed, you can verify the installed version of Docker with the following command:
docker --version
You should get the following output:
Docker version 20.10.2, build 2291f61
Step 5 – Install a Specific Docker Version
If you want to install a specific Docker version, first list all available Docker versions using the following command:
apt list -a docker-ce
You should see all the Docker version in the following output:
docker-ce/focal,now 5:20.10.2~3-0~ubuntu-focal amd64 [installed] docker-ce/focal 5:20.10.1~3-0~ubuntu-focal amd64 docker-ce/focal 5:20.10.0~3-0~ubuntu-focal amd64 docker-ce/focal 5:19.03.14~3-0~ubuntu-focal amd64 docker-ce/focal 5:19.03.13~3-0~ubuntu-focal amd64 docker-ce/focal 5:19.03.12~3-0~ubuntu-focal amd64 docker-ce/focal 5:19.03.11~3-0~ubuntu-focal amd64 docker-ce/focal 5:19.03.10~3-0~ubuntu-focal amd64 docker-ce/focal 5:19.03.9~3-0~ubuntu-focal amd64
Now, install the specific version of Docker by adding =version after package name as shown below:
apt-get install docker-ce=5:19.03.11~3-0~ubuntu-focal
Step 6 – Manage Docker Services
To start the Docker service, run the following command:
systemctl start docker
To enable the Docker service at system reboot, run the following command:
systemctl enable docker
To check the status of Docker service, run the following command:
systemctl status docker
Run Docker Commands Without Sudo
By default, Docker can be run as a root user or with Sudo privileges. If you want to run Docker commands as a non-root user then you will need to add your user to the docker group.
You can add your user to the docker group using the following command:
usermod -aG docker [user]
Now, your user can run Docker commands without specifying sudo.
Docker Basic Commands
You can list all commands available in Docker with the following command:
docker
You should get a list of all commands in the following output:
Usage: docker [OPTIONS] COMMAND A self-sufficient runtime for containers Options: --config string Location of client config files (default "/root/.docker") -c, --context string Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker context use") -D, --debug Enable debug mode -H, --host list Daemon socket(s) to connect to -l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info") --tls Use TLS; implied by --tlsverify --tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem") --tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem") --tlskey string Path to TLS key file (default "/root/.docker/key.pem") --tlsverify Use TLS and verify the remote -v, --version Print version information and quit Management Commands: app* Docker App (Docker Inc., v0.9.1-beta3) builder Manage builds buildx* Build with BuildKit (Docker Inc., v0.5.1-docker) config Manage Docker configs container Manage containers context Manage contexts image Manage images manifest Manage Docker image manifests and manifest lists network Manage networks node Manage Swarm nodes plugin Manage plugins secret Manage Docker secrets service Manage services stack Manage Docker stacks swarm Manage Swarm system Manage Docker trust Manage trust on Docker images volume Manage volumes Commands: attach Attach local standard input, output, and error streams to a running container build Build an image from a Dockerfile commit Create a new image from a container's changes cp Copy files/folders between a container and the local filesystem create Create a new container diff Inspect changes to files or directories on a container's filesystem events Get real time events from the server exec Run a command in a running container export Export a container's filesystem as a tar archive history Show the history of an image images List images import Import the contents from a tarball to create a filesystem image info Display system-wide information inspect Return low-level information on Docker objects kill Kill one or more running containers load Load an image from a tar archive or STDIN login Log in to a Docker registry logout Log out from a Docker registry logs Fetch the logs of a container pause Pause all processes within one or more containers port List port mappings or a specific mapping for the container ps List containers pull Pull an image or a repository from a registry push Push an image or a repository to a registry rename Rename a container restart Restart one or more containers rm Remove one or more containers rmi Remove one or more images run Run a command in a new container save Save one or more images to a tar archive (streamed to STDOUT by default) search Search the Docker Hub for images start Start one or more stopped containers stats Display a live stream of container(s) resource usage statistics stop Stop one or more running containers tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE top Display the running processes of a container unpause Unpause all processes within one or more containers update Update configuration of one or more containers version Show the Docker version information wait Block until one or more containers stop, then print their exit codes Run 'docker COMMAND --help' for more information on a command.
To see system-wide information about Docker, run the following command:
docker info
Output:
Client: Context: default Debug Mode: false Plugins: app: Docker App (Docker Inc., v0.9.1-beta3) buildx: Build with BuildKit (Docker Inc., v0.5.1-docker) Server: Containers: 0 Running: 0 Paused: 0 Stopped: 0 Images: 0 Server Version: 20.10.2 Storage Driver: overlay2 Backing Filesystem: extfs Supports d_type: true Native Overlay Diff: true Logging Driver: json-file Cgroup Driver: cgroupfs Cgroup Version: 1 Plugins: Volume: local Network: bridge host ipvlan macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog Swarm: inactive Runtimes: runc io.containerd.runc.v2 io.containerd.runtime.v1.linux Default Runtime: runc Init Binary: docker-init containerd version: 269548fa27e0089a8b8278fc4fc781d7f65a939b runc version: ff819c7e9184c13b7c2607fe6c30ae19403a7aff init version: de40ad0 Security Options: apparmor seccomp Profile: default Kernel Version: 5.4.0-29-generic Operating System: Ubuntu 20.04 LTS OSType: linux Architecture: x86_64 CPUs: 2 Total Memory: 1.941GiB Name: ubuntu2004 ID: VD6X:YW65:VO3V:L7O6:TS4I:FOMR:QKYG:CCYG:6IBJ:ISSU:QDS7:57AI Docker Root Dir: /var/lib/docker Debug Mode: false Registry: https://index.docker.io/v1/ Labels: Experimental: false Insecure Registries: 127.0.0.0/8 Live Restore Enabled: false
How to Use Docker
At this point, Docker is installed and running in your system. Next, you will need to check whether you can access and download the image from Docker Hub.
Let’s download the hello-world image from the Docker Hub using the docker pull command:
docker pull hello-world
This will download or pull the hello-world image from the Docker Hub repository as shown below:
Using default tag: latest latest: Pulling from library/hello-world 0e03bdcc26d7: Pull complete Digest: sha256:31b9c7d48790f0d8c50ab433d9c3b7e17666d6993084c002c2ff1ca09b96391d Status: Downloaded newer image for hello-world:latest docker.io/library/hello-world:latest
Next, verify the downloaded image with the following command :
docker images
You should see your downloaded image in the following output:
REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest bf756fb1ae65 12 months ago 13.3kB
Next, run a container from the hello-world image with the following command:
docker container run hello-world
You should see the following output:
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 Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/
How to Uninstall Docker
If you want to uninstall Docker from your system. It is recommended to remove all Docker containers, images and volumes from your system.
First, stop all running containers with the following command:
docker container stop $(docker container ls -aq)
Once all containers are stopped, remove them with the following command:
docker container rm $(docker container ls -aq)
Next, remove all Docker images with the following command:
docker image prune -a
Next, remove all docker objects with the following command:
docker system prune -a --volumes
Finally, remove the Docker package with all dependencies by running the following command:
apt-get purge docker-ce -y apt-get autoremove apt-get clean
Conclusion
In the above post, you learned how to install and use Docker on Ubuntu 20.04/22.04. To learn more about Docker, check out my all Docker tutorial.
Recommended Reading
- How to Stop / Remove all Docker Containers and Images
- The Ultimate Docker Cheat Sheet – Quick Reference
- How to Install and Use Docker Compose on Ubuntu 20.04 | 22.04
- Docker Exec – How to Connect to a Docker Container
- How To Remove Docker Images, Containers & Volumes
- How to Use Docker Push & Pull Command with Examples
- How To List / Start / Stop Docker Containers
- How to Use Docker Run Command with Examples