ubuntu20.04 安装docker,docker-compose

版本:Ubuntu Focal 20.04 (LTS)

一.Docker安装

1.如果存在旧版本,卸载旧版本

旧版本的 Docker 称为 docker 或者 docker-engine,使用以下命令卸载旧版本:

sudo apt-get remove docker \
               docker-engine \
               docker.io

2.使用 APT 安装

由于 apt 源使用 HTTPS 以确保软件下载过程中不被篡改。因此,我们首先需要添加使用 HTTPS 传输的软件包以及 CA 证书。

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

3.为了确认所下载软件包的合法性,需要添加软件源的 GPG 密钥,鉴于国内网络问题,这里我们使用阿里云的GPG 密钥:

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

4.我们需要向 sources.list 中添加 Docker 软件源

echo \
  "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

5.安装 Docker:更新 apt 软件包缓存,并安装 docker-ce

sudo apt-get update

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

6.启动 Docker

sudo systemctl enable docker
sudo systemctl start docker

7.建立 docker 用户组

默认情况下,docker 命令会使用 Unix socket 与 Docker 引擎通讯。而只有 root 用户和 docker 组的用户才可以访问 Docker 引擎的 Unix socket。出于安全考虑,一般 Linux 系统上不会直接使用 root 用户。因此,更好地做法是将需要使用 docker 的用户加入 docker 用户组

建立 docker 组:

sudo groupadd docker

将当前用户加入 docker 组:

usermod -aG docker $USER

退出当前终端并重新登录,进行如下测试

8.测试 Docker 是否安装正确

root@zph-IdeaCentre-GeekPro-14IOB:/home/zph# docker version
Client: Docker Engine - Community
 Version:           20.10.12
 API version:       1.41
 Go version:        go1.16.12
 Git commit:        e91ed57
 Built:             Mon Dec 13 11:45:33 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.12
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.16.12
  Git commit:       459d0df
  Built:            Mon Dec 13 11:43:42 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.13
  GitCommit:        9cc61520f4cd876b86e77edfeb88fbcd536d1f9d
 runc:
  Version:          1.0.3
  GitCommit:        v1.0.3-0-gf46b6ba
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

$ docker run --rm hello-world

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/

说明docker安装成功!

二.Docker-Compose 安装

PIP 安装方式

1.先安装pip 

apt install python3-pip

2.安装docker-compose

sudo pip install -U docker-compose

可以看到类似如下输出,说明安装成功:

Collecting docker-compose
  Downloading docker_compose-1.29.2-py2.py3-none-any.whl (114 kB)
...
Successfully installed attrs-21.4.0 distro-1.7.0 docker-5.0.3 docker-compose-1.29.2 dockerpty-0.4.1 docopt-0.6.2 jsonschema-3.2.0 pyrsistent-0.18.1 python-dotenv-0.19.2 texttable-1.6.4 websocket-client-0.59.0

bash 补全命令

curl -L https://raw.githubusercontent.com/docker/compose/1.27.4/contrib/completion/bash/docker-compose > /etc/bash_completion.d/docker-compose

3.卸载

sudo pip uninstall docker-compose

download the version of Docker Compos安装方式

1.curl下载docker compose

sudo curl -L https://github.com/docker/compose/releases/download/1.20.1/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose

2.对二进制文件应用可执行权限

sudo chmod +x /usr/local/bin/docker-compose

3.测试是否安装成功

docker-compose --version

出现以下说明安装成功

docker-compose version 1.20.1, build 5d8c71b

你可能感兴趣的:(docker,容器,ubuntu)