Roustabout It is an open source platform that enables software developers to build, deploy, and manage containerized applications. This means packaging the application into a container, which is a combination of source code, libraries, and other dependencies required to run on any operating system.
In addition, when using containers, docker makes it simpler, smoother, and safer.
In this tutorial, I will introduce all the steps to install Docker on Ubuntu 20.04 and learn the basic concepts of using Docker containers/images.
In the official software repository of Ubuntu 20.04, docker can be used to install software packages by default. However, it is not the latest version. It is recommended to install the latest version of docker from the official docker repository.
First, you need to update the current package list and install the prerequisite packages to add the new repository to the system by running the following command:
$ sudo apt update $ sudo apt install apt-transport-https ca-certificates curl software-properties-common gnupg-agent
Next, you must import the GPG key of the Docker repository:
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Then, Add docker repository To the APT source on the system:
$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
Run the following command to update the newly added repository:
$ sudo apt update
Make sure to install docker from its repository and not the Ubuntu repository:
$ apt-cache policy docker-ce
You will see the output looks like this:
docker-ce: Installed: (none) Candidate: 5:19.03.11~3-0~ubuntu-focal Version table: 5:19.03.11~3-0~ubuntu-focal 500 500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages 5:19.03.10~3-0~ubuntu-focal 500 500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages 5:19.03.9~3-0~ubuntu-focal 500 500 https://download.docker.com/linux/ubuntu focal/stable amd64 Packages
The above output indicates that “docker-ce” from the Docker repository from Ubuntu 20.04 will be installed on your system.
Last install Roustabout (Community Edition):
$ sudo apt install docker-ce
In order to verify that docker has been successfully installed and running correctly, please run the following command:
$ sudo systemctl status docker
The output will show that the docker service is active (running):
● docker.service - Docker Application Container Engine Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled) Active: active (running) since Wed 2020-06-17 15:29:48 +07; 8min ago TriggeredBy: ● docker.socket Docs: https://docs.docker.com Main PID: 136640 (dockerd) Tasks: 17 Memory: 50.8M CGroup: /system.slice/docker.service └─136640 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock ...
You have just successfully installed docker on your Ubuntu computer. In the following part of this tutorial, we will discuss in depth how to use the docker command.
Execute docker without sudo privileges
By default, you must run docker commands with sudo privileges or by users in the docker group. If you try to run the docker command without sudo or without the user rights of the docker group, you will get the following error:
docker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?. See 'docker run --help'.
The solution is to add the user to the “docker” group:
$ sudo usermod -aG docker
Log out of the current session and log in again as an application member. Verify that the user is in the “docker” group by running the following command:
$ groups ubuntu sudo docker
From now on, you can run the docker command without “sudo”.
This section will explain how to use the docker command. Before going into details, let’s take a look at the syntax of the “docker” command:
$ docker [options] [sub-commands] [arguments]
To list all available subcommands of docker, run:
There are many options, subcommands and parameters to remember. Let’s delve into some of them in the following sections of this tutorial.
Management Commands: builder Manage builds config Manage Docker configs container Manage containers context Manage contexts engine Manage the docker engine image Manage images 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.
The Docker image is composed of source code, libraries, and applications as all necessary dependencies for the container to run. It is possible to build Docker images from scratch, but you can extract these images from the container registry. You can store images in the registry and download them with unique names. By default, Docker’s container registry is Docker Hub.
In order to check whether the image can be pulled from Docker Center, You can run the following command:
$ docker run hello-world
As you can see in the following output: docker cannot find the “hello-world” image locally, it extracted the image from Docker Hub. After downloading the image, docker creates a container from the image. The application in the container will be executed with the following message:
Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 0e03bdcc26d7: Pull complete Digest: sha256:d58e752213a51785838f9eed2b7a498ffa1cb3aa7f946dda11af39286c3db9a9 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 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 ...
To search for images on Docker Hub, you can use the’docker’ command with the’search’ subcommand. Suppose you want to search for “mountain” images, run:
$ docker search alpine
As you can see, the output returns a list of all images named “alpine”.
In addition, you can run the command “Docker picture pull” Then enter the picture name. E.g:
$ docker image pull alpine
Depending on the speed of the Internet, the download may take a minute.
Using default tag: latest latest: Pulling from library/alpine df20fa9351a1: Pull complete Digest: sha256:185518070891758909c9f839cf4ca393ee977ac378609f700f60a771a2dfe321 Status: Downloaded newer image for alpine:latest docker.io/library/alpine:latest
In addition, if you want to list all downloaded images, run the following command:
$ docker image ls
The output will display the detailed information of the image, including: repository, label, image ID, creation time and file size.
REPOSITORY TAG IMAGE ID CREATED SIZE alpine latest a24bb4013296 2 weeks ago 5.57MB hello-world latest bf756fb1ae65 5 months ago 13.3kB
Sometimes you need Delete docker Picture, you can run the following command:
$ docker image rm
$ docker image rm
Containers are running instances of docker images. The container executes the runtime of a single application or process. You can interact with the container and start/stop or delete it by running the “docker container” subcommand.
For example, to start the docker container based on the “Ubuntu” image, run:
[email protected]:~$ docker container run ubuntu
After downloading the newer image of Ubuntu from Docker Hub and starting the Ubuntu container, it will return to the system command prompt because the Ubuntu container has stopped immediately after starting.
Unable to find image 'ubuntu:latest' locally latest: Pulling from library/ubuntu a4a2a29f9ba4: Pull complete 127c9761dcba: Pull complete d13bf203e905: Pull complete 4039240d2e0b: Pull complete Digest: sha256:35c4a2c15539c6c1e4e5fa4e554dac323ad0107d8eb5c582d6ff386b383b7dce Status: Downloaded newer image for ubuntu:latest [email protected]:~$
In order to interact with the Ubuntu container through the command line, you can use the following command to start the container
$ docker container run -it ubuntu
You have now entered the container and accessed its interactive shell.
So far, you have run many containers on the Ubuntu system. To list all active (running) containers, run the following command:
$ docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
The result is an empty list, so there are no containers running. If you want to list all containers: active and inactive, run:
$ docker container ls -a
The output is similar to:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 45cc68d4395e ubuntu "/bin/bash" 14 minutes ago Exited (0) 11 minutes ago stoic_dhawan fc91e50d223e ubuntu "/bin/bash" 26 minutes ago Exited (0) 26 minutes ago sleepy_ritchie ce0d81682913 alpine "/bin/bash" 26 minutes ago Created trusting_kowalevski 4291477f5ac2 alpine "/bin/sh" 27 minutes ago Exited (130) 27 minutes ago priceless_wozniak 86c0fd44a4f4 alpine "/bin/sh" 28 minutes ago Exited (0) 28 minutes ago objective_agnesi 3fdcaa196fca hello-world "/hello" 31 minutes ago Exited (0) 31 minutes ago eloquent_rhodes
Once you want to delete/delete the container, run:
$ docker container rm
In this tutorial, we learned how to install Docker on Ubuntu 20.04 and how to use images and containers through examples. They are the foundation of the Docker concept and are usually used by developers.
Thanks for reading, please leave your suggestions in the comment section below.