Deepin系统中安装Docker和Docker Compose
Tofloor
poster avatar
leibris
deepin
2020-01-20 18:00
Author
本帖最后由 leibris 于 2020-1-20 17:22 编辑

系统版本:Deepin 15.11(其它版本未测试)

安装 Docker卸载老版本的docker,没安装过docker的可以直接跳过该步骤。
  1. $ sudo apt-get update
  2. $ sudo apt-get remove docker docker-ce docker-engine docker.io containerd.io runc
Copy the Code
下载最新版本Docker下载×https://download.docker.com/linux/static/stable/x86_64/

推荐选择19.x以上版本,允许非 root 用户运行守护程序,启用 Rootless 模式可以防止攻击者通过错误的Docker设置和漏洞夺取主机的 root 权限而破坏系统。

将下载包解压到系统目录

  1. $ tar xzvf /path/to/.tgz
  2. $ sudo mv docker/* /usr/bin/
Copy the Code
这种不受包管理系统管理的文件我一般是安置到/opt/目录下,想删除就直接删目录就行了,然后通过设置系统环境变量来指定运行path。但是我尝试了几次,在systemd服务启动时没能成功,原因是systemd找不到containerd的路径。所以暂且放置到官方推荐的/usr/bin目录下。以后没事再来找原因了。如果你想要方便以后卸载,你可以将你复制进去的文件列份备忘清单保存好,然后按清单删除文件就卸载了。详情看下面卸载部分。

添加系统用户组

这样可以允许非 root 用户运行守护程序

  1. $ sudo groupadd docker
  2. $ sudo usermod -aG docker $USER
  3. $ newgrp docker
Copy the Code
验证一下是否安装成功
  1. $ docker --version
  2. Docker version 19.03.5, build 633a0ea838
Copy the Code
配置Docker服务

Docker是C\S架构,我们需要配置一个服务来启动S端

添加国内镜像

新增一个配置文件/etc/docker/daemon.json ,编辑如下内容:

  1. {
  2. "registry-mirrors": ["https://registry.docker-cn.com"]
  3. }
Copy the Code
配置系统服务

deepin 基于systemd来管理服务,新建一个配置目录

  1. $ sudo mkdir /etc/systemd/system/docker.service.d
Copy the Code

在配置目录下新增/etc/systemd/system/docker.service.d/docker.socket文件,编辑如下内容:

  1. [Unit]
  2. Description=Docker Socket for the API
  3. PartOf=docker.service

  4. [Socket]
  5. ListenStream=/var/run/docker.sock
  6. SocketMode=0660
  7. SocketUser=root
  8. SocketGroup=docker

  9. [Install]
  10. WantedBy=sockets.target
Copy the Code

再新增一个/etc/systemd/system/docker.service.d/docker.service文件,编辑内容如下

  1. [Unit]
  2. Description=Docker Application Container Engine
  3. Documentation=https://docs.docker.com
  4. After=network-online.target docker.socket firewalld.service
  5. Wants=network-online.target
  6. Requires=docker.socket

  7. [Service]
  8. Type=notify
  9. # the default is not to use systemd for cgroups because the delegate issues still
  10. # exists and systemd currently does not support the cgroup feature set required
  11. # for containers run by docker
  12. ExecStart=/usr/bin/dockerd -H fd://
  13. ExecReload=/bin/kill -s HUP $MAINPID
  14. LimitNOFILE=1048576
  15. # Having non-zero Limit*s causes performance problems due to accounting overhead
  16. # in the kernel. We recommend using cgroups to do container-local accounting.
  17. LimitNPROC=infinity
  18. LimitCORE=infinity
  19. # Uncomment TasksMax if your systemd version supports it.
  20. # Only systemd 226 and above support this version.
  21. #TasksMax=infinity
  22. TimeoutStartSec=0
  23. # set delegate yes so that systemd does not reset the cgroups of docker containers
  24. Delegate=yes
  25. # kill only the docker process, not all processes in the cgroup
  26. KillMode=process
  27. # restart the docker process if it exits prematurely
  28. Restart=on-failure
  29. StartLimitBurst=3
  30. StartLimitInterval=60s

  31. [Install]
  32. WantedBy=multi-user.target
Copy the Code

注意分配好文件权限

  1. $ chmod 644 /etc/systemd/system/docker.service.d/*
Copy the Code

变动配置后,执行如下命令重新加载文件

  1. $ systemctl daemon-reload
Copy the Code

启动服务

  1. $ systemctl start docker
Copy the Code

验证服务是否正常

  1. $ docker run hello-world
Copy the Code

设置开机启动服务(可选)

  1. $ systemctl enable docker
Copy the Code
卸载(如果不想用了,可以完全卸载)

  1. $ systemctl disable docker
  2. $ systemctl stop docker
  3. $ sudo cd /usr/bin
  4. $ sudo rm containerd  containerd-shim  ctr  docker  dockerd  docker-init  docker-proxy  runc
  5. $ sudo rm -rf /etc/docker/
  6. $ sudo rm -rf /etc/systemd/system/docker.service.d/*
Copy the Code


安装Docker Compose
使用 Docker Compose 可以轻松、高效的管理容器,它是一个用于定义和运行多容器 Docker 的应用程序工具。
到官方仓库下载与已安装docker版本配套的最新版,到github下载时,一定要开启wget的断点续传功能下载。
  1. $ sudo wget -c -t 0 https://github.com/docker/compose/releases/download/1.25.1/docker-compose-`uname -s`-`uname -m` -O /usr/local/bin/docker-compose
Copy the Code
添加执行权限
  1. $ sudo chmod a+rx /usr/local/bin/docker-compose
Copy the Code
验证是否安装成功
  1. $ docker-compose -v
  2. docker-compose version 1.25.1, build a82fef07
Copy the Code
卸载
  1. $ sudo rm /usr/local/bin/docker-compose
Copy the Code


Reply Favorite View the author
All Replies
avatar
deepinuser17
deepin
2020-01-20 20:40
#1
感谢分享。 用户自己创建的启动文件应该放在/etc/systemd/system里面。/usr/lib/systemd/system里面是系统安装的启动文件。系统更新时会删除,更改启动文件。
Reply View the author
avatar
观摩
deepin
2020-01-20 21:43
#2
好,收藏备用
Reply View the author
avatar
leibris
deepin
2020-01-21 00:58
#3
https://bbs.deepin.org/post/188160
感谢分享。 用户自己创建的启动文件应该放在/etc/systemd/system里面。/usr/lib/systemd/system里面是系统 ...

非常好的批评,已经修改了错误部分。谢谢您的宝贵意见!
Reply View the author
avatar
豆浆包子
deepin
2020-01-23 03:12
#4
我这边是添加apt docker源,类似Ubuntu18.04系统,省掉了自己添加系统服务部分内容。

Reply View the author