文章目录
Docker环境搭建(linux)
准备 更新的相关的yum包
yum -y update
或者:
yum makecache fast
安装相关的依赖,并 更换设置YUM源,更换为阿里云:
yum install -y yum-utils device-mapper-persistent-data lvm2
删除旧版的数据(没安装过可忽略)
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-selinux \
docker-engine-selinux \
docker-engine
安装加速依赖,这个不安装之前的会报错 (可忽略)
yum makecache fast
或
yum clean metadata
补充:查看docker版本
yum list docker-ce --showduplicates | sort -r
设置Docker的镜像仓库-使用阿里源
# 使用阿里源
sudo yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 清华大学源
sudo yum-config-manager \
--add-repo \
https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
执行官方的安装脚本(安装的社区版滴)
yum install -y docker-ce docker-ce-cli containerd.io
或
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
验证安装
docker --version
配置开机启动
systemctl enable docker
启动docker,最关键,默认安装完成之后是没有启动的,会报错
systemctl start docker
启动docker: systemctl start docker
停止docker: systemctl stop docker
重启docker: systemctl restart docker
查看docker状态:systemctl status docker
开机自动启动docker:systemctl enable docker
Docker帮助命令:docker --help
卸载Docker服务
卸载依赖 yum remove docker-ce docker-ce-cli containerd.io
删除资源 rm -rf /var/lib/docker
/var/lib/docker 是docker的默认工作路径!
运行一个简单的示例容器:docker run hello-world
docker run hello-world
查看所有的容器:
docker ps -a
配置国内镜像加速器
登录阿里云找到容器服务,搜索镜像服务,找到镜像加速器
使用阿里云镜像加速https://cr.console.aliyun.com/cn-hangzhou/instances
#重载此配置文件 systemctl daemon-reload
#重启 docker systemctl restart docker
查看本地的镜像
docker images
删除全部镜像
docker rmi -f `docker images -q`
后台的模式启动的容器实例,新建容器并启动
docker run -it -d 名字:标签 /bin/sh
docker run -it -d ubuntu:15.10 /bin/sh
登录容器
# 登录已经启动的容器方式
方式1 docker exec -it 容器id bashshell
方式2 docker exec -it 容器名称|容器id /bin/bash
停止容器:
docker stop f1b40b91bb48
常用其他命令
# 查看容器中进程信息 ps
docker top 容器id
# 查看日志
docker logs -t --tail n 容器id #查看n行日志
docker logs -ft 容器id #跟着日志
# 查看镜像的元数据
docker inspect 容器id
# 查看容器IP地址
docker inspect --format='{{.NetworkSettings.IPAddress}}' 容器名
# 查看当前docker运行状态 (查看CPU状态)
docker stats
开启远程访问(非必要,注意安全问题)
编辑docker的宿主机文件/lib/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
BindsTo=containerd.service
After=network-online.target firewalld.service containerd.service
Wants=network-online.target
Requires=docker.socket
[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 -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3
# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s
# 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
# Comment TasksMax if your systemd version does not support it.
# Only systemd 226 and above support this option.
TasksMax=infinity
# 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
[Install]
WantedBy=multi-user.target
修改内容如下,并重启
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
systemctl daemon-reload
systemctl restart docker
测试访问情况
curl http://localhost:2375/version
docker-compose 安装(linux)
推荐):
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
对二进制文件应用可执行权限
sudo chmod +x /usr/local/bin/docker-compose
验证是否成功:
docker-compose -v
方法一,推荐
2.1 docker-compose的安装(如果使用yum安装会重置python3的版本)
首先去git官网查看 在 Compose 仓库的 releases 页面 下载你的目标平台最新的 docker-compose 软件包,我们这里下载的是
docker-compose-linux-x86_64
2.2 注意这里的版本选择
写这篇文章时最新的版本是 2.26.1,你可以通过下面命令一键下载并将其安装到系统目录:
sudo curl -L "https://github.com/docker/compose/releases/download/v2.26.1/docker-compose-linux-$(uname -m)" -o /usr/local/bin/docker-compose
2.3 直接给可执行权限
sudo chmod +x /usr/local/bin/docker-compose
2.4 检查其版本信息,确认安装是否成功
docker-compose --version
方法二,不推荐,安装后会重置python3的版本
3.1 步骤如下
3.1.1 添加epel源
yum install -y epel-release
3.1.2 安装docker-compose
yum install -y docker-compose
3.1.3 查看版本
docker-compose --version
3.1.4 配置Docker加速器
cat <<END > /etc/docker/daemon.json
{
"registry-mirrors": [
"https://1nj0zren.mirror.aliyuncs.com",
"https://docker.mirrors.ustc.edu.cn",
"http://f1361db2.m.daocloud.io",
"https://registry.docker-cn.com"
]
}
END
3.1.5 从新加载
systemctl daemon-reload
systemctl restart docker
3.1.6 启动容器时如果遇到错误
此linux的内核中的selinux不支持 OVERLAY2 GRAPH DRIVER,禁用SELINUX,设置–SELINUX-ENABLED=FALSE
修改设置,标红的地方加=false
vi /etc/sysconfig/docker
3.1. 7 再执行启动容器,并查看版本
systemctl start docker
docker versin
docker-compose version
docker ps
docker-compose help