docker部署及常用命令

1 安装docker

1.1 yum方式安装 

1.1.1 下载依赖环境

yum -y install yum-utils device-mapper-persistent-data lvm2

1.1.2 指定Docker镜像源

yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

 1.1.3 安装docker

yum makecache fast
yum -y install docker-ce

yum install docker-ce -y  默认只开启stable仓库,因此安装的是最新稳定版。如想下载指定版本,则执行 yum install docker-ce-版本号.ce

查看所有仓库中所有docker版本可使用yum list docker-ce --showduplicates | sort -r命令查看。

1.1.4 启动docker并测试

# 启动Docker服务
 systemctl start docker # 关闭 systemctl stop docker 
 # 设置开机自动启动
 systemctl enable docker #关闭 systemctl disable docker
 # 测试    运行hello-world 镜像 根据这个镜像 创建容器
 docker run hello-world

1.1.5 常见问题

1.1.5.1 docker-ce-selinux >= 17.03.0.ce-1.el7.centos

通过下面地址查看安装docker版本依赖的rpm包
Index of linux/centos/7/x86_64/stable/Packages/

 执行: 

#yum install https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-selinux-17.03.0.ce-1.el7.centos.noarch.rpm

 安装成功重试yum install docker-ce-17.03.0.ce即可。

1.1.5.2 非root用户使用docker命令报错
1.1.5.2.1 现象
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.39/containers/json: dial unix /var/run/docker.sock: connect: permission denied

官方解释如下:

The docker daemon binds to a Unix socket instead of a TCP port. By default that Unix socket is owned by the user root and other users can access it with sudo. For this reason, docker daemon always runs as the root user. 
To avoid having to use sudo when you use the docker command, create a Unix group called docker and add users to it. When the docker daemon starts, it makes the ownership of the Unix socket read/writable by the docker group.
1.1.5.2.2 解决方案
1.1.5.2.2.1 创建docker用户组
#sudo groupadd docker

此步骤并非必须,默认情况下docker安装过程中会自动创建一个docker用户组

1.1.5.2.2.2 将当前用户加入docker用户组
sudo gpasswd -a ${USER} docker

注意以上命令只能用当前用户来操作,因为变量USER获取的是当前用户,如果要以root用户来操作,则需要指定的是具体的用户名,而非此变量

1.1.5.2.2.3 当前用户退出系统重新登陆

这里并不是批退出系统,只要切换一下用户即可,比如先切到root用户然后再切回来就可以了

1.1.5.3 安装docker时报 container-selinux >= 2.9错误

安装 docker 时出现了以下错误:

Error: Package: docker-ce-18.03.1.ce-1.el7.centos.x86_64 (docker-ce-edge)
Requires: container-selinux >= 2.9
You could try using --skip-broken to work around the problem
You could try running: rpm -Va --nofiles --nodigest

这个报错是 container-selinux 版本低或者是没安装的原因,yum 安装container-selinux 一般的yum源又找不到这个包,需要安装epel源,才能yum安装container-selinux

然后在安装 docker-ce 就可以了。

​$ wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
$ yum install epel-release  #阿里云上的epel源
$ yum makecache
​

然后,执行命令。

$ yum install container-selinux -y

1.2 二进制方式安装

由于是内网环境,安装docker不能yum安装,只能rmp或者二进制包安装,但是rmp安装又有许多的依赖包要安装,而且要解决依赖冲突问题;所以就选择了 二进制安装Docker 方式。网上查了很多资料,踩了很多坑,下面记录本人亲测有效的安装方式、步骤。

1.2.1 下载Docker CE二进制包

下载地址:Index of linux/static/stable/x86_64/

服务器下载命令:

$ wget -c https://download.docker.com/linux/static/stable/x86_64/docker-20.10.21.tgz

1.2.2 解压.tgz包

root用户操作:

tar -zxvf docker-20.10.21.tgz 

chown root:root docker/*

1.2.3 复制二进制文件到/usr/bin/目录

cp -p docker/* /usr/bin/

1.2.4 创建用户组

groupadd docker

1.2.5 配置相关服务配置文件

有三个配置文件:

  • docker.service
  • docker.socket、
  • containerd.service。

docker.service配置文件:

$ touch docker.service
$ vim docker.service
[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

docker.socket配置文件:

$ touch docker.socket
$ vim docker.socket
[Unit]
Description=Docker Socket for the API
PartOf=docker.service
[Socket]
ListenStream=/var/run/docker.sock
SocketMode=0660
SocketUser=root
SocketGroup=docker
[Install]
WantedBy=sockets.target

containerd.service配置文件:

$ touch containerd.service
$ vim containerd.service
[Unit]
Description=containerd container runtime
Documentation=https://containerd.io
After=network.target local-fs.target
[Service]
ExecStartPre=-/sbin/modprobe overlay
ExecStart=/usr/bin/containerd
Type=notify
Delegate=yes
KillMode=process
Restart=always
RestartSec=5
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNPROC=infinity
LimitCORE=infinity
LimitNOFILE=infinity
# Comment TasksMax if your systemd version does not supports it.
# Only systemd 226 and above support this version.
TasksMax=infinity
OOMScoreAdjust=-999
[Install]
WantedBy=multi-user.target

1.2.6 拷贝配置文件到指定目录

$ sudo cp docker.socket /etc/systemd/system
$ sudo cp docker.service /etc/systemd/system
$ sudo cp containerd.service /etc/systemd/system

添加可执行权限:

$ chmod a+x /etc/systemd/system/docker.service
$ chmod a+x /etc/systemd/system/docker.socket
$ chmod a+x /etc/systemd/system/containerd.service

1.2.7 启动 dockerd 服务进程

$ systemctl enable docker.service

1.2.8 启动docker服务

$ systemctl start docker.service

1.2.9 检验是否启动成功

通过运行镜像“hello-world”来验证 Docker Engine 是否正确安装:

$ sudo docker run hello-world

1.3 配置docker仓库加速

1.3.1 配置阿里镜像加速

# 复制粘贴即可
 sudo mkdir -p /etc/docker
 sudo tee /etc/docker/daemon.json <<-'EOF'
 {
   "registry-mirrors": ["https://tp01urzb.mirror.aliyuncs.com"]
 }
 EOF
 sudo systemctl daemon-reload
 sudo systemctl restart docker

1.3.2 配置多个镜像仓库加速

 vi  /etc/docker/daemon.json
 # 内容如下,一开始只有第一个加速镜像,拉取mysql时拉不下来,就多加了几个
 {
   "registry-mirrors": [
         "https://tp01urzb.mirror.aliyuncs.com",
         "https://hub-mirror.c.163.com",
         "https://mirror.baidubce.com",
         "https://docker.mirrors.ustc.edu.cn",
         "https://reg-mirror.qiniu.com"
         ]
 }
 ​
 # 重启两个服务 注意上边镜像地址用"",每个镜像之间有逗号,写错重启会报错
 systemctl daemon-reload
 systemctl restart docker

1.4 配置镜像仓库

$ cat /etc/docker/daemon.json 
{
  "insecure-registries": ["10.12.5.62"]
}

可配置多个镜像仓库,配置方式如下:

{
  "insecure-registries": [
            "10.12.5.62",
            "10.12.5.60"
            ]
}

2 镜像/容器操作

2.1 拉取镜像

从中央仓库拉取镜像到本地
docker pull 镜像名 # 默认拉取最新版本
 ​
 # 拉取 Tomcat 以及指定版本 Tomcat
 docker pull tomcat
 docker pull tomcat:8 # tomcat 8

镜像不带版本,默认拉取最新版本 

2.2 本地镜像查看

#docker images
REPOSITORY                                                                               TAG                                IMAGE ID            CREATED             SIZE

2.3 删除本地镜像

docker rmi 镜像标识 
 # 当利用该镜像创建了容器时,要先删除容器,再删除镜像

2.4 运行容器

# 简单操作
 docker run 镜像的标识|镜像名称[:tag]
 # 常用的一些参数,以上边Tomcat 为例
 docker run -d -p 8080:8080 --name tomcat8 64fbf6b1021d
 # -d 后台运行容器
 # -p 宿主机端口:容器端口  为了映射当前Linux的端口和容器的端口
 # --name 容器名称:指定容器名称 
 # 最后一个参数是 镜像名 或者 镜像标识

2.5 查看容器

# 查看正在运行的容器
 docker ps
 # 查看所有的容器
 docker ps -a
 # 查看容器日志
 # -f:可以滚动查看日志的最后几行
 docker losg -f 容器id

2.6 进入容器内部

 docker exec -it 容器id bash
 # 退回到宿主机 control+p+q

2.7 重启&启动&停止&删除容器

# 重新启动容器
 docker restart 容器id
 ​
 # 启动停止运行的容器
 docker start 容器id
 ​
 # 停止指定的容器(删除容器前,需要先停止容器)
 docker stop 容器id
 ​
 # 停止全部容器
 docker stop $(docker ps -qa)
 ​
 # 删除指定容器
 docker rm 容器id
 ​
 # 删除全部容器
 docker rm $(docker ps -qa)
 # 查看容器元信息
 docker inspect 容器id
 ​
 # 设置容器自动启动
 docker update --restart=always 容器id
 

3 docker安装 Tomcat

docker search tomcat
docker pull tomcat # 自动拉取最新镜像
docker pull tomcat:8 # 拉去指定版本
docker images # 查看本地镜像
 ​
# 创建容器 -d 后台运行, -p 宿主机端口号:容器端口号, --name 容器名 镜像标识 | 镜像名
docker run -d -p 8080:8080 --name tomcat1 tomcat:8

4 docker部署mysql

4.1 拉取镜像,查看镜像标识

docker pull mysql:8
 # 查看标识
docker images

4.2 创建容器

docker run -d -p 3306:3306 --name mysql8 -e MYSQL_ROOT_PASSWORD=root 2a6a0d93ee45
 # -e MYSQL_ROOT_PASSWORD=root 为数据库设置密码

4.3 进入容器测试

# 进入容器
 docker exec -it mysql8 bash

5 docker配置加速器

5.1 注册并获取镜像加速器地址

以阿里云为例。

登录阿里云账号—>阿里云的镜像中心–>镜像加速器----> 得到一个进行加速取地址。

5.2  配置docker daemon文件

$ vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://zkscx4sa.mirror.aliyuncs.com"]
}
$ systemctl daemon-reload 
$ systemctl restart docker
$ docker search nginx										##查找官方nginx镜像
$ docker images												##查看本地是否存在,如果存在,由于镜像分层,就会出现拉取其中一部分,本地存在镜像就不再拉取了
$ docker pull nginx											##拉取镜像
$ docker images												##验证是否拉取成功

6 常见问题

6.1 Docker run 执行报错提示可能检测到恶意路径,被拒绝

docker: Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: error mounting "/var/lib/docker/containers/be10d59d7d74ae9f54198f59df42b227d785953ec2231b4ed307248170f71dbf/resolv.conf" to rootfs at "/etc/resolv.conf": possibly malicious path detected -- refusing to operate on /etc/resolv.conf: unknown.
ERRO[0016] error waiting for container: context canceled  

此问题一般是docker版本和系统版本不匹配导致,建议降低docker版本重新安装。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

alden_ygq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值