Docker 及常用环境安装
操作系统要求:要安装 Docker Engine,您需要一个 CentOS 7 或 8 的维护版本,不支持或未测试存档版本。(本文基于 CentOS)
卸载旧版本
较旧的 Docker 版本称为 docker 或 docker-engine。如果已安装这些程序,请卸载它们以及相关的依赖项。
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
安装方法
安装所需软件包并设置稳定的存储库
# 安装所需软件
sudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
# 设置稳定仓库(这里我使用的是阿里云的仓库)
sudo yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
安装 Docker 引擎
sudo yum install docker-ce docker-ce-cli containerd.io
启动 Docker
sudo systemctl start docker
开机自启
systemctl enable docker
卸载
# 卸载Docker Engine,CLI和Containerd软件包
sudo yum remove docker-ce docker-ce-cli containerd.io
# 主机上的映像,容器,卷或自定义配置文件不会自动删除,要删除所有图像,容器和卷
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd
加速
在阿里云控制台搜索 容器镜像服务
然后选择 镜像加速器
,在操作文档中选择 CentOS
,按照文档要求操作即可完成镜像加速。具体操作为:
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["你的加速器地址,自行在阿里云上看"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
Docker 安装 Redis
安装
docker pull redis
mkdir -p /mydata/redis/conf
touch /mydata/redis/conf/redis.conf
docker run -p 6379:6379 --name redis -v /mydata/redis/data:/data -v /mydata/redis/conf/redis.conf:/etc/redis/redis.conf -d redis redis-server /etc/redis/redis.conf
# 启动 redis-cli
docker exec -it redis redis-cli
修改配置文件
vi /mydata/redis/conf/redis.conf
配置文件内容为:
appendonly yes
开机自启
# mysql、nginx等也可如此修改
docker update redis --restart=always
Docker 安装 MySQL
我安装的是 5.7,习惯了 MySQL5,觉得 MySQL8屁事太多了(个人意见,哈哈)。
docker pull mysql:5.7
docker run --restart=always --privileged=true -d -v /data/mysql/data:/var/lib/mysql -v /data/mysql/log:/var/log/mysql -v /data/mysql/conf:/etc/mysql -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=root mysql:5.7
修改配置文件
vi /data/mysql/conf/my.cnf
配置文件内容:
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
init_connect='SET collation_connection=utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
# skip-grant-tables
# secure_file_priv=/var/lib/mysql
Docker 安装 Nginx
docker pull nginx
mkdir -p /data/nginx/{conf,conf.d,html,log}
touch nginx.conf
vi nginx.conf
docker run --name nginx -d -p 80:80 -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/log:/var/log/nginx -v /data/nginx/html:/usr/share/nginx/html nginx
nginx 配置文件 nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
server{
listen 80;
server_name your_server_name; #你的serverName
root /usr/share/nginx/html;
index index.html;
}
}