8

阿里云Ubuntu云服务器上安装docker和docker-compose的详细步骤

 3 years ago
source link: https://xmanyou.com/a-li-yun-ubuntu-ecs-install-dockerhe-docker-compose-manually/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
7 February 2021 / docker

阿里云Ubuntu云服务器上安装docker和docker-compose的详细步骤

在阿里云的Ubuntu云服务器上安装docker/docker-compose的详细步骤如下:

安装docker

  1. 更新apt源

apt-get update

  1. 添加https传输的软件包以及CA证书

apt-get install \

   apt-transport-https \

   ca-certificates \

   curl \

   gnupg-agent \

   software-properties-common

  1. 检查软件包的合法性

$ curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -





# 官方源

# $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

  1. 添加docker软件源

$ sudo add-apt-repository \

   "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu \

   $(lsb_release -cs) \

   stable"

# 官方源

# $ sudo add-apt-repository \

#    "deb [arch=amd64] https://download.docker.com/linux/ubuntu \

#    $(lsb_release -cs) \

#    stable"

  1. 安装docker

sudo apt-get update

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

  1. 管理docker服务

sudo systemctl status docker

sudo systemctl enable docker

sudo systemctl start docker

sudo systemctl stop docker

  1. 管理docker用户组

只有root和docker用户组才能正常使用docker。所以,如果当前用户不是root的话,需要将当前用户加入docker组。


sudo groupadd docker

sudo usermod -aG docker $USER

  1. 测试docker

用hello-world测试一下docker服务是否正常


docker run hello-world

  1. 配置docker hub镜像

阿里云提供镜像加速服务
aliyun-docker-hub-mirrior

检查当前镜像设置


systemctl cat docker | grep '\-\-registry\-mirror'

如果该命令有输出,那么请执行 $ systemctl cat docker 查看 ExecStart= 出现的位置,修改对应的文件内容去掉 --registry-mirror 参数及其值,并按接下来的步骤进行配置。

如果以上命令没有任何输出,那么就可以在 /etc/docker/daemon.json 中写入如下内容(如果文件不存在请新建该文件):

{
 "registry-mirrors": [
   "<你的阿里云容器镜像地址>"
   "https://hub-mirror.c.163.com",
   "https://mirror.baidubce.com"
 ]
}

安装docker-compose

  1. 下载最新版本

github查看最新release https://github.com/docker/compose/releases

例如 linux的最新版本是1.28.2

https://github.com/docker/compose/releases/download/1.28.2/docker-compose-Linux-x86_64

注意

如果的云服务器是国内,github上有些资源是没法下载的,需要曲线救国,比如先下载到本地,然后再上传到服务器,等等。

  1. 上传到/usr/local/bin

  2. 添加可执行权限


chmod +x

  1. 安装docker-compose 命令补全

curl -L https://raw.githubusercontent.com/docker/compose/1.28.2/contrib/completion/bash/docker-compose -o docker-compose.1.28.2

保存到/etc/bash_completion.d

sudo mv ./docker-compose.1.28.2 /etc/bash_completion.d/docker-compose

  1. 设置默认日志限制,避免磁盘被占满

https://xmanyou.com/docker-logging-file-limitation/

阿斌

Read more posts by this author.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK