Linux安装配置 docker docker-compose

一、安装docker

参考: docker官方文档 https://docs.docker.com/engine/install/

方式一 Install using the repository

能上外网的话,推荐此方法。
具体步骤请见官方文档链接

方式二 Install from a package

注意会出现依赖缺失的情况。
具体步骤请见官方文档链接

方式三 Install using the convenience script

具体步骤请见官方文档链接

方式四 Install from binaries

只能上内网,可用此方法

  1. 下载二进制包 docker-xx.xx.xx.tgz
    docker官方下载链接
    清华大学镜像源下载链接 国内推荐用此链接

  2. 解压二进制包

tar -zxvf /path/to/.tgz

Linux安装配置 docker docker-compose_第1张图片

  1. 复制解压后的文件到PATH中包含的路径,即可全局使用docker相关命令
sudo cp docker/* /usr/bin/
  1. 启动docker daemon
sudo dockerd &
  1. 检查docker是否正常启动
docker --version


二、将docker加入systemd管理

  1. 在/usr/lib/systemd/system/创建docker.service文件
sudo vim /usr/lib/systemd/system/docker.service
  1. 输入以下内容
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
# restart the docker process if it exits prematurely
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
[Install]
WantedBy=multi-user.target
  1. 设置开机启动
systemctl enable docker.service
  1. 刷新配置
systemctl daemon-reload
  1. 测试是否成功
查看开机自启状态:
systemctl list-unit-files |grep enabled |grep docker
systemctl is-enabled docker.service 


systemctl start docker.service     启动
systemctl status docker.service    查看状态
systemctl stop docker.service      停止
systemctl restart docker.service   重启
ps -ef |grep docker                查看docker进程


三、docker更换国内源

换中科大和网易镜像源,配置DNS

vim /etc/docker/daemon.json

更改为以下内容
{ 
 "registry-mirrors": [ "https://docker.mirrors.ustc.edu.cn", "http://hub-mirror.c.163.com" ],
 "dns": ["223.5.5.5", "223.6.6.6"]
 }

重启docker服务
systemctl restart docker


四、非root用户使用docker命令

解决非root用户使用docker命令提示权限受限

  1. 创建docker用户组
sudo groupadd docker
  1. 将当前用户加入docker组
sudo gpasswd -a ${USER} docker
  1. 更新组信息
newgrp docker
  1. 重启docker
sudo systemctl restart docker
  1. 测试执行结果
docker ps


五、安装docker-compose

参考: docker官方文档 https://docs.docker.com/compose/install/

  1. 下载docker-compose,若不能连外网,则可下载后通过ftp传到服务器 ,修改文件名称与存放路径
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  1. 添加可执行权限
sudo chmod +x /usr/local/bin/docker-compose
  1. 配置全局使用docker-compose命令
方法一 将/usr/local/bin添加至PATH
export PATH=$PATH:/usr/local/bin

方法二 创建软链接至PATH中包含的路径,如/usr/bin
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

ps: 查看PATH

echo $PAHT

在这里插入图片描述

  1. 测试docker-compose
docker-compose --version

你可能感兴趣的:(docker,docker,linux,centos,ubuntu,java)