Install and use Docker on ubuntu20

Docker is an application that simplifies the process of managing application processes in containers. Containers allow you to run applications in resource-isolated processes. They are similar to virtual machines, but containers are easier to transplant, take up less resources, and are more dependent on the resources of the host system.

In this article, Docker Community Edition (CE) will be installed and used on Ubuntu 20.04. Simultaneously simply use the container and the image, and finally push the image to the Docker repository.

This article assumes that you have a user account with sudo group privileges, but not root.

If not, create one under root 

adduser sammy

usermod -aG sudo sammy

This creates a user with sudo privileges

1: Install Docker

First update all installation packages:

sudo apt update

Then install some prerequisite packages to use the package via HTTPS:
sudo apt install apt-transport-https ca-certificates curl software-properties-common

Add the GPG key of the official Docker repository to the system:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add-

Add the Docker repository to the APT source:
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"

Update the package database with the Docker package in the newly added warehouse:
sudo apt update

Make sure you want to install from the Docker repository instead of the default Ubuntu repository:
apt-cache policy docker-ce

Finally, docker is actually installed

sudo apt install docker-ce

docker is now installed, the daemon is started, and the process will be started next time the system is enabled. Check if it is running:

sudo systemctl status docker

can be seen:

● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2021-02-20 18:05:52 UTC; 5min ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 11985 (dockerd)
      Tasks: 8
     Memory: 39.4M
     CGroup: /system.slice/docker.service
             └─11985 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
...


 

2: Execute Docker commands without sudo

By default, you can only run docker commands as the root user or users in the docker group, and the docker group is automatically created during the Docker installation process. In order not to use the sudo prefix, you need to join the doccker group:

sudo usermod -aG docker ${USER}
needs to log out and then log in to this group information to take effect. Or this is fine too. I logged out and then logged in. I didn't try the following methods.
su-${USER}

The command to check which group you are in is:
id -nG

The following information should be displayed

sammy sudo docker

If you add someone to the docker group, use the following command, where username is someone else’s username
sudo usermod -aG docker username

The docker command used later in this article assumes that you are in the docker group, otherwise you must add sudo
 

3: Use Docker commands

Using docker involves passing a series of options and commands to it, followed by parameters. The grammar takes the following form:

docker [option] [command] [arguments]

To view all available subcommands, type:
docker

To see the options available for a specific command, type:
docker docker-subcommand --help

The following command to view docker system information: 
docker info

4:Docker Image

Docker containers are built from Docker images. By default, Docker pulls these images from Docker Hub, which is a Docker registry managed by Docker, the company behind the Docker project. Anyone can host their Docker images on Docker Hub, so most of the applications and Linux distributions you need will host the images.

Let's test a simple hello-world:

docker run hello-world

Information similar to the following should be displayed:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:6a65f928fb91fcfbc963f7aa6d57c8eeb426ad9a20c7ee045538ef34847f44f1
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

Docker was initially unable to find the hello-world image locally, so it downloaded the image from the default repository Docker Hub. After downloading the image, Docker will create a container based on the image, execute the application in the container, and display a message.

You can use the docker command and search subcommand to search for images available on Docker Hub. For example, to search for Ubuntu images, enter:

docker search ubuntu

In the OFFICIAL column, OK represents an image built and supported by the company behind the project. After determining the Image you want to use, you can use the pull subcommand to download it to your computer.

Execute the following command to download the official ubuntu image to the local:

docker pull ubuntu

After downloading the image, you can use the downloaded image with the run subcommand to run the container. As seen in the hello-world example, if the image is not downloaded when executing docker with the run subcommand, the Docker client will first download the image and then use the image to run the container.

To view the image that has been downloaded to the computer, type:

docker images

The display information is as follows:

REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
ubuntu        latest    f63181f19b2f   4 weeks ago     72.9MB
hello-world   latest    bf756fb1ae65   13 months ago   13.3kB

5: Docker container

The hello-world container running in the previous step is an example of a container that runs and exits after issuing a test message. Containers can be much more useful than this, and they can be interactive. After all, they are similar to virtual machines, but are more resource-friendly.

For example, let's run a container using the latest image of Ubuntu. The combination of the -i and -t switches allows you to interactively access the shell:

docker run -it ubuntu

Then there is the following display, and enter the container's ubuntu 

leon@ubuntu-s-1vcpu-1gb-tor1-01:~$ docker run -it ubuntu

root@bd612e776819:/#

First update the package list:

root@bd612e776819:/# apt update
Get:1 http://security.ubuntu.com/ubuntu focal-security InRelease [109 kB]

For example, install nodejs

root@bd612e776819:/# apt install nodejs
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:

 Then, check the version of node:

root@bd612e776819:/# node -v
v10.19.0

All operations are only for this container.

To exit, type:

exit

root@bd612e776819:/# exit
exit
leon@ubuntu-s-1vcpu-1gb-tor1-01:~$

6: Manage Docker containers

After using Docker for a period of time, there will be many active (running) and inactive containers on your computer. To view the activity, use:

docker ps

I have no activity here, so it's displayed like this:

leon@ubuntu-s-1vcpu-1gb-tor1-01:~$ docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

To display all containers, do this:
docker ps -a

The display is like this:

leon@ubuntu-s-1vcpu-1gb-tor1-01:~$ docker ps -a
CONTAINER ID   IMAGE         COMMAND       CREATED             STATUS                         PORTS     NAMES
bd612e776819   ubuntu        "/bin/bash"   53 minutes ago      Exited (0) 22 minutes ago                gallant_visvesvaraya
bdbf06b1c3bc   hello-world   "/hello"      About an hour ago   Exited (0) About an hour ago             nifty_benz

To view recently created containers, use -l
docker ps -l

To start a stopped container, use docker start followed by the container ID or the name of the container. Let's start the Ubuntu-based container with ID bdbf06b1c3bc:
docker start bdbf06b1c3bc

This ID is different, as you can see in the docker ps -a view, the following is after startup, and then use docker ps to view:

 docker start bd612e776819
bd612e776819
leon@ubuntu-s-1vcpu-1gb-tor1-01:~$ docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED             STATUS         PORTS     NAMES
bd612e776819   ubuntu    "/bin/bash"   About an hour ago   Up 7 seconds             gallant_visvesvaraya

To stop a running container, use docker stop followed by the container ID or name. This time, we will use the container name gallant_visvesvaraya assigned by Docker:

docker stop gallant_visvesvaraya

After confirming that the container is no longer needed, use the docker rm command to delete it again using the container ID or name. Use the docker ps -a command to find the container ID or name of the container associated with the hello-world image and delete it.

docker rm nifty_benz

The following is the process of checking with ps -a first, and checking after rm. nifty_benz is the name ps sees, each is different.

leon@ubuntu-s-1vcpu-1gb-tor1-01:~$ docker ps -a
CONTAINER ID   IMAGE         COMMAND       CREATED       STATUS                          PORTS     NAMES
bd612e776819   ubuntu        "/bin/bash"   6 hours ago   Exited (0) About a minute ago             gallant_visvesvaraya
bdbf06b1c3bc   hello-world   "/hello"      6 hours ago   Exited (0) 5 hours ago                    nifty_benz
leon@ubuntu-s-1vcpu-1gb-tor1-01:~$ docker rm nifty_benz
nifty_benz
leon@ubuntu-s-1vcpu-1gb-tor1-01:~$ docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED       STATUS                     PORTS     NAMES
bd612e776819   ubuntu    "/bin/bash"   6 hours ago   Exited (0) 2 minutes ago             gallant_visvesvaraya
leon@ubuntu-s-1vcpu-1gb-tor1-01:~$

You can start a new container and name it with the --name switch. You can also use the --rm switch to create a container, which will be deleted when it stops. For more information about these and other options, see the docker run help command.

Containers can be converted to images, and they can be used to build new containers. Let us see how it works.

7: Commit changes to the container

When starting a Docker image, you can create, modify, and delete files just like using a virtual machine. The changes made will only be applied to that container. It can be started and stopped, but once it is destroyed with the docker rm command, the changes will be lost forever.

This section explains how to save the container state as a new Docker image.

After installing Node.js in the Ubuntu container, there is now a container to run the image, but the container is different from the image used to create it. But in the future, you may want to reuse this Node.js container as the basis for a new image.

Then use the following command to commit the changes to the new Docker image instance.

docker commit -m "What you did to the image" -a "Author Name" container_id repository/new_image_name

The -m switch is used to submit a message, which helps you and others know the changes you made, and -a is used to specify the author. The container_id is the one you noted when you started the interactive Docker session earlier in this tutorial. Unless another repository is created on Docker Hub, the repository is usually the Docker Hub username.

For example, for the sammy user and the container ID is d9b100f2f636, the command will be:

docker commit -m "added Node.js" -a "sammy" d9b100f2f636 sammy/ubuntu-nodejs

When the image is submitted, the new image will be saved on the local computer. Later in this tutorial, you will learn how to push an image to a Docker repository such as Docker Hub so that others can access it.

Listing the Docker image again will show the new image, as well as the old image:

docker images

The following is the operation process:

leon@ubuntu-s-1vcpu-1gb-tor1-01:~$ docker commit -m "added Node.js" -a "leon" bd612e776819 leon/ubuntu-nodejs
sha256:e5120a19cee69adfa4ea83f9e97934aa0ba33624ce4230b92a999c4cb4d2f75d
leon@ubuntu-s-1vcpu-1gb-tor1-01:~$ docker images
REPOSITORY           TAG       IMAGE ID       CREATED              SIZE
leon/ubuntu-nodejs   latest    e5120a19cee6   About a minute ago   167MB
ubuntu               latest    f63181f19b2f   4 weeks ago          72.9MB
hello-world          latest    bf756fb1ae65   13 months ago        13.3kB
leon@ubuntu-s-1vcpu-1gb-tor1-01:~$

8: Push Docker Image to Docker repository

After creating a new image from an existing image, the next step is to share that image with friends, the entire world on Docker Hub, or other Docker libraries that you have access to. To push an image to Docker Hub or any other Docker registry, you must have an account in it.

The first step is to log in to your account:

docker login -u docker-registry-username

If the user name and login account are inconsistent:

docker tag sammy/ubuntu-nodejs docker-registry-username/ubuntu-nodejs

Then push:

docker push docker-registry-username/docker-image-name
If the username is sammy and the image is ubuntu-nodejs, use the following command:

docker push sammy/ubuntu-nodejs

9: Some commands for Ccontainer and Images

List all container IDs

docker ps -aq

Stop all containers

docker stop $(docker ps -aq)

Delete all containers

docker rm $(docker ps -aq)

Delete all mirrors
docker rmi $(docker images -q)

This article is completed by learning  How To Install and Use Docker on Ubuntu 20.04  .

That's it for the introduction.

Guess you like

Origin blog.csdn.net/leon_zeng0/article/details/113881191