在使用Docker的过程中会遇到各种各样的问题,下面是CentOS下使用Docker部分常见问题及处理方法的简单汇总,供参考。
1、CentOS下安装Docker
一般不建议使用系统yum源来安装,因为CentOS软件仓库里的docker版本太旧;
源码安装步骤稍显复杂,不适合初学者;
如果没有特殊需求,通常建议安装最新的Docker稳定版本,在线安装时需要先安装docker官方的yum配置包:
SET UP THE REPOSITORY
Install the yum-utils
package (which provides the yum-config-manager
utility) and set up the stable repository.
$ sudo yum install -y yum-utils$ sudo yum-config-manager \--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
详见https://docs.docker.com/engine/install/centos/
如果是离线安装,可以参考:
(1)CentOS7离线安装docker最新稳定版
(2)CentOS8上离线安装Docker最新稳定版
2、容器开机自启动的实现
首先需要保证docker服务本身是自启动的;
其次是创建容器时加上参数--restart=always(这个最常用),例如:
docker run -d --name Redis --restart=always -p 6379:6379 redis
如果创建时未指定 --restart=always,可通过update命令追加:
docker update --restart=always xxx
使用--restart=always要注意:务必在验证容器能正常启动后再添加此参数,不然创建容器后不断重启可能影响问题排查。
官方说明:
Use a restart policy
To configure the restart policy for a container, use the --restart flag when using the docker run command. The value of the --restart flag can be any of the following:
Flag Description
no --Do not automatically restart the container. (the default)
on-failure --Restart the container if it exit