Record the process of installing docker-ce on ubuntu18.04

Basically follow the official tutorial step by step: https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository

1. Basic installation

1. Update the apt toolkit

sudo apt-get update
sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

2. Add docker official key

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

A warning will come out, don’t worry:
gpg: WARNING: unsafe ownership on homedir '/home/lz/.gnupg'

3. Configure the stable version of the warehouse (this needs to be selected according to the system architecture, but most of the hosts are x86_64/amd64)

echo \
  "deb [arch=amd64 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

4. Then update the apt toolkit, install the latest version of Docker Engine and containerd (you can also install the specified version, not mentioned here)

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io

5. You can check the docker version (note that sudo is now required to obtain root privileges):

sudo docker version

Return this piece of information:

Client: Docker Engine - Community
 Version:           20.10.5
 API version:       1.41
 Go version:        go1.13.15
 Git commit:        55c4c88
 Built:             Tue Mar  2 20:18:05 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.5
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       363e9a8
  Built:            Tue Mar  2 20:16:00 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.4
  GitCommit:        05f951a3781f4f2c1911b05e61c160e9c30eaa8e
 runc:
  Version:          1.0.0-rc93
  GitCommit:        12644e614e25b05da6fd08a38ffa0cfe1903fdec
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Second, post installation

1. In order to add it every time you run it sudo, you can add a user group:

sudo groupadd docker

return:groupadd: group 'docker' already exists

2. Add the user you want to give permission, $USER should be replaced with your own, for example mine is lz:

sudo usermod -aG docker $USER

3. Run the following statement to exit and re-enter:

newgrp docker 

Then you run it and you docker versionfind that sudoyou can return the version information as above without adding it. You can also use the following test;

docker run hello-world

If the return is as follows, it means that the docker-ce installation is successful:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete 
Digest: sha256:308866a43596e83578c7dfa15e27a73011bdd402185a84c5cd7f32a88b501a24
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

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/

More installation operations can be seen later (such as setting up automatic startup, http proxy, etc.): https://docs.docker.com/engine/install/linux-postinstall/

Three, common basic commands

First of all, you can use the help to view the command:

docker --help

Then put some commonly used commands here:

docker pull ${CONTAINER NAME}                    #拉取镜像
docker images                                    #查看本地所有镜像
docker ps                                        #查看所有正在运行的容器,加-q返回id
docker ps -a                                     #查看所有容器,加-q返回id
docker rmi ${IMAGE NAME/ID}                      #删除镜像
docker rm ${CONTAINER NAME/ID}                   #删除容器
docker save ${IMAGE NAME} > ${FILE NAME}.tar     #将镜像保存成文件
docker load < ${FILE NAME}.tar                   #从文件加载镜像
docker start ${CONTAINER NAME/ID}                #运行一个以前运行过的容器
docker stop ${CONTAINER NAME/ID}                 #停止一个正在运行的容器
docker logs ${CONTAINER NAME/ID}                 #显示运行容器的日志
docker run...                                    #运行一个容器
    --name ${container name}                          #设置容器名称
    -p ${host port}:${container port}                 #映射主机和容器内的端口
    -e ${env name}=${env value}                       #添加环境变量
    -d                                                #后台运行
    -v ${host folder path}:${container folder path}   #将主机目录挂在到容器内
docker ps -f "status=exited"                                   #显示所有退出的容器
docker ps -a -q                                                #显示所有容器id
docker ps -f "status=exited" -q                                #显示所有退出容器的id
docker restart $(docker ps -q)                                 #重启所有正在运行的容器
docker stop $(docker ps -a -q)                                 #停止所有容器
docker rm $(docker ps -a -q)                                   #删除所有容器
docker rm $(docker ps -f "status=exited" -q)                   #删除所有退出的容器
docker rm $(docker stop $(docker ps -a -q))                    #停止并删除所有容器
docker start $(docker ps -a -q)                                #启动所有容器
docker rmi $(docker images -a -q)                              #删除所有镜像
docker exec -it ${CONTAINER NAME/ID} /bin/bash                 #进入容器内
docker exec -it ${CONTAINER NAME/ID} ping ${CONTAINER NAME/ID} #一个容器ping另外一个容器
docker top ${CONTAINER NAME/ID}                                #显示一个容器的top信息
docker stats                                                   #显示容器统计信息(正在运行)
    docker stats -a                                            #显示所有容器的统计信息(包括没有运行的)
    docker stats -a --no-stream                                #显示所有容器的统计信息(包括没有运行的) ,只显示一次
    docker stats --no-stream | sort -k8 -h                     #统计容器信息并以使用流量作为倒序
docker system 
      docker system df           #显示硬盘占用
      docker system events       #显示容器的实时事件
      docker system info         #显示系统信息
      docker system prune        #清理文件

The following answer can also be referred to.
Update it when there are subsequent matches or running projects with docker~
In fact, the most important thing is to learn how to write Dockerfile.

Guess you like

Origin blog.csdn.net/laizi_laizi/article/details/114850326