一、准备工作:
Docker支持64位版本的CentOS 7和CentOS 8及更高版本,它要求Linux内核版本不低于3.10
- 查看Linux版本的命令:
lsb_release -a
或cat /etc/redhat-release
- 查看内核版本有三种方式:
cat /proc/version uname -a uname -r
二、安装Docker方式
方式一:自动化安装Docker
1.安装Docker:
Docker官方和国内daocloud都提供了一键安装的脚本,使得Docker的安装更加便捷。
官方的一键安装方式:
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
国内 daocloud一键安装命令:
curl -sSL https://get.daocloud.io/docker | sh
执行上述任一条命令,耐心等待即可完成Docker的安装。
安装成功后,使用docker version命令查看是否安装成功,安装成功后,如下
2.启动Docker:
启动命令:systemctl start docker 或 systemctl start docker.service
验证docker启动成功:
或
3.删除Docker:
删除安装包:
yum remove docker-ce
删除镜像、容器、配置文件等内容:
rm -rf /var/lib/docker
方式二:离线安装Docker
1.Docker安装脚本
即install_docker.sh如下:
#!/bin/bash
####################
# docker24安装脚本 #
####################
WORK_PATH=/home/data/install_pkg
TMP_PATH=$WORK_PATH/tmp
PKG_PATH=$WORK_PATH/docker
CONF_PATH=$WORK_PATH/docker
#检查安装目录
if [ ! -d $PKG_PATH ];
then
echo -e "${PKG_PATH} 缺失,结束进程"
exit 1
fi
if [ ! -d $TMP_PATH ];
then
mkdir -p $TMP_PATH
fi
#从共享目录复制到本地
cd $TMP_PATH || exit
cp $PKG_PATH/docker-24.0.7.tgz ./
#解压安装
tar -xvf docker-24.0.7.tgz
#将解压出来的docker内容移动至/home/data/docker下,同时复制到/usr/bin下
cp docker/* /usr/bin/
mkdir -p /home/data/docker/data
mv docker/* /home/data/docker
#注册编辑docker服务 并修改docker数据存放位置
cp $CONF_PATH/docker.service /etc/systemd/system/
#赋予可执行权限 启动 设置自启
chmod +x /etc/systemd/system/docker.service
systemctl daemon-reload && systemctl enable docker.service && systemctl start docker
#安装docker-compose
cp $PKG_PATH/docker-compose-v2.16.0-linux-x86_64 /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
#检查启动状态
systemctl status docker
docker -v
docker-compose -v
ps:确保/home/data/install_pkg/docker 目录下有如下内容:
2.docker.service内容:
作用:注册docker为service服务,在/etc/systemd/system/目录下新增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=
ExecStart=/usr/bin/dockerd --graph=/home/data/docker/data/ #安装docker的data路径
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
3.Docker启动脚本(op_docker.sh):
#!/bin/sh
# ./op_docker.sh start 启动 stop 停止 restart 重启 status 状态 monitor 监控(未启动->自动重启)
# JVM参数
AppName=docker
opUser=root
RE_LOG_HOME=/home/data/monitorlog/crontab
RE_LOG_FILE=$RE_LOG_HOME/$(date +%F)/$(date +%F)_restart.txt
STATE_HOME=/home/data/monitorlog/component-status
STATE_FILE=$STATE_HOME/$AppName
if [ `whoami` != "$opUser" ];then
echo -e " only $opUser can run me"
exit 1
fi
if [ "$1" = "" ];
then
echo -e "\033[0;31m 未输入操作名 \033[0m \033[0;34m {start|stop|restart|status|monitor} \033[0m"
exit 1
fi
if [ "$AppName" = "" ];
then
echo -e "\033[0;31m 未输入应用名 \033[0m"
exit 1
fi
function start()
{
PID=`ps -ef|grep "/usr/bin/dockerd" |grep -v 'grep' | awk '{print $2}'`
if [ x"$PID" != x"" ]; then
echo "$AppName is running..."
else
systemctl start docker
echo "Start $AppName success..."
fi
}
function stop()
{
echo "Stop $AppName"
PID=""
query(){
PID=`ps -ef|grep "/usr/bin/dockerd" |grep -v 'grep' | awk '{print $2}'`
}
query
if [ x"$PID" != x"" ]; then
systemctl stop docker
echo "$AppName (pid:$PID) exiting..."
while [ x"$PID" != x"" ]
do
sleep 1
query
done
echo "$AppName exited."
else
echo "$AppName already stopped."
fi
}
function restart()
{
stop
sleep 2
start
}
function status()
{
PID=`ps -ef|grep "/usr/bin/dockerd" |grep -v 'grep' | awk '{print $2}' |wc -l`
if [ $PID != 0 ];then
echo "$AppName is running..."
else
echo "$AppName is not running..."
fi
}
function monitor()
{
PID=`ps -ef|grep "/usr/bin/dockerd" |grep -v 'grep' | awk '{print $2}' |wc -l`
if [ $PID != 0 ];then
echo "$AppName is running..."
echo $(date +%Y-%m-%d%n%H:%M:%S)_running > $STATE_FILE
else
start
echo "$AppName is not running,and restarting..."
echo $(date +%Y-%m-%d%n%H:%M:%S)_notrunning > $STATE_FILE
echo $(date +%Y-%m-%d%n%H:%M:%S) >> $RE_LOG_FILE
echo "$AppName is not running,and restarting..." >> $RE_LOG_FILE
fi
}
case $1 in
start)
start;;
stop)
stop;;
restart)
restart;;
status)
status;;
monitor)
monitor;;
*)
echo "操作名"$1"错误";;
esac
三、Docker相关命令:
汇总列一下常见的Docker操作命令:
- 重启Docker服务:systemctl restart docker / service docker restart
- 关闭Docker服务:docker service docker stop / docker systemctl stop docker
- 停止容器:docker stop 容器ID
- 重启容器:docker restart 容器ID
- 删除容器:docker rm container_id
- 搜索仓库镜像:docker search 镜像名
- 查看镜像:docker images
- 拉取镜像:docker pull 镜像名(或docker pull nginx:xxx ,xxx指版本号)
- 删除镜像:docker rmi image_id
- 启动(停止的)容器:docker start 容器ID
- 启动(新)容器:docker run -it ubuntu /bin/bash(或docker run -p 80:80 -d --name nginx nginx:1.24.0,第一个80宿主机端口,第二个80容器端口)
- 进入容器:
docker attach 容器ID
或docker exec -it 容器ID /bin/bash
,推荐使用后者。 - 先启动再设置自启动:docker update --restart=always 容器ID
- 查看正在运行的容器:docker ps
- 查看所有容器:docker ps -a
- 查看docker 日志:docker logs -t -f rabbitmq (docker 容器的默认日志路径/var)
- 镜像打包:docker save 镜像名称:镜像版本 >打包后的文件名字.tar
- 将镜像加载到Docker:docker load -i 打包后的文件名字.tar(例子:docker load -i minio.tar)
- 查看docker内存情况:docker stats -a
更多的命令可以通过docker help
命令来查看。
- 查看所有自启动项目:systemctl list-unit-files |grep enabled
- 设置开机自启:systemctl enable docker
- 守护进程重启:systemctl daemon-reload(重载配置文件以生效)
- 服务器重启:reboot -f
端口映射示意图