You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
182 lines
4.6 KiB
182 lines
4.6 KiB
8 months ago
|
## 安装Docker
|
||
|
|
||
|
```bash
|
||
|
sudo apt-get install docker docker-compose
|
||
|
|
||
|
#查看Docker可用用户
|
||
|
sudo cat /etc/group |grep docker
|
||
|
# docker:x:998:表示存在
|
||
|
sudo groupadd docker
|
||
|
sudo usermod -aG docker $USER
|
||
|
newgrp docker # 激活组更改,无效的话尝试重连ssh,退出登录,重启计算机等
|
||
|
|
||
|
# 查看版本
|
||
|
docker version
|
||
|
```
|
||
|
## docker-compose操作
|
||
|
|
||
|
```bash
|
||
|
# 启动当前目录下的docker-compose的yaml文件
|
||
|
docker-compose up
|
||
|
# 后台运行
|
||
|
docker-compose up -d
|
||
|
# 启动文件中的指定服务
|
||
|
docker-compose up -d 服务名称
|
||
|
|
||
|
# 停止所有服务 这个命令会停止并移除所有容器,网络,但不包括数据卷
|
||
|
docker-compose down
|
||
|
# 仅停止服务不移除容器
|
||
|
docker-compose stop
|
||
|
# 停止特定服务
|
||
|
docker-compose stop 服务名称
|
||
|
# 重新启动服务
|
||
|
docker-compose restart
|
||
|
```
|
||
|
|
||
|
## docker卷操作
|
||
|
|
||
|
```bash
|
||
|
# 创建卷
|
||
|
# docker volume create [OPTIONS] [VOLUME]
|
||
|
# 这个命令用于创建一个新的卷。你可以指定卷的名称,如果不指定,Docker 会为你生成一个随机名称。此外,还可以通过选项设置卷的各种属性。
|
||
|
docker volume create my_volume
|
||
|
|
||
|
|
||
|
# 列出所有现有的卷
|
||
|
docker volume ls
|
||
|
|
||
|
# 检查卷
|
||
|
# docker volume inspect [VOLUME_NAME]
|
||
|
# 显示某个卷的详细信息,包括其名称、驱动、挂载点等
|
||
|
docker volume inspect my_volume
|
||
|
|
||
|
# 删除卷
|
||
|
# docker volume rm [VOLUME_NAME]
|
||
|
# 此命令用于删除指定的卷。如果某个卷当前被一个或多个容器使用,那么这个卷不能被删除,除非使用 -f 或 --force 选项
|
||
|
docker volume rm my_volume
|
||
|
|
||
|
# 清理未使用的卷
|
||
|
# 此命令会删除所有未被任何容器使用的卷。执行此命令前,Docker 通常会请求确认
|
||
|
docker volume prune
|
||
|
```
|
||
|
|
||
|
## 文件示例
|
||
|
```yaml
|
||
|
version: '3.8'
|
||
|
|
||
|
services:
|
||
|
mysql:
|
||
|
image: mysql:8
|
||
|
restart: always
|
||
|
environment:
|
||
|
MYSQL_ROOT_PASSWORD: Hxl1314521
|
||
|
MYSQL_DATABASE: hutao
|
||
|
MYSQL_USER: nie
|
||
|
MYSQL_PASSWORD: Hxl1314521
|
||
|
ports:
|
||
|
- "3306:3306"
|
||
|
volumes:
|
||
|
- mysql8:/var/lib/mysql
|
||
|
|
||
|
redis:
|
||
|
image: redis:latest
|
||
|
restart: always
|
||
|
ports:
|
||
|
- "6379:6379"
|
||
|
volumes:
|
||
|
- ./redis.conf:/usr/local/etc/redis/redis.conf
|
||
|
- redis_data:/data
|
||
|
- redis_log:/var/log/redis
|
||
|
|
||
|
redisinsight:
|
||
|
image: redislabs/redisinsight:latest
|
||
|
ports:
|
||
|
- "8001:8001"
|
||
|
|
||
|
volumes:
|
||
|
mysql8:
|
||
|
redis_data:
|
||
|
redis_log:
|
||
|
```
|
||
|
|
||
|
|
||
|
## redis配置
|
||
|
```editorconfig
|
||
|
# Redis持久化选项
|
||
|
# 使用RDB持久化
|
||
|
#
|
||
|
# save 900 1
|
||
|
# save 300 10
|
||
|
# save 60 10000
|
||
|
|
||
|
# AOF持久化开关
|
||
|
#appendonly yes
|
||
|
# AOF文件写入方式
|
||
|
#appendfsync everysec
|
||
|
# AOF重写时,是否减少写操作的阻塞
|
||
|
#no-appendfsync-on-rewrite no
|
||
|
|
||
|
# Redis安全设置
|
||
|
# 设置密码(这里设置的密码为yourpassword,请替换为你自己的密码)
|
||
|
requirepass Hxl1314521
|
||
|
|
||
|
# 网络设置
|
||
|
# 绑定的IP,如果需要远程连接,请注释掉这行
|
||
|
# bind 127.0.0.1
|
||
|
# 监听的端口,默认为6379
|
||
|
#port 6379
|
||
|
|
||
|
# 日志文件位置
|
||
|
logfile "/var/log/redis/redis-server.log"
|
||
|
|
||
|
# 最大内存使用量,超出则根据淘汰策略删除键,示例为无限制
|
||
|
# maxmemory <bytes>
|
||
|
# 内存淘汰策略
|
||
|
# maxmemory-policy noeviction
|
||
|
```
|
||
|
|
||
|
|
||
|
## Docker 安装
|
||
|
|
||
|
```bash
|
||
|
# 更新软件包索引
|
||
|
sudo apt-get update
|
||
|
# 安装所需的包
|
||
|
sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common
|
||
|
# 添加Docker的官方GPG密钥
|
||
|
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
|
||
|
# 设置稳定版仓库
|
||
|
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
|
||
|
# 再次更新软件包索引(此时包括Docker仓库)
|
||
|
sudo apt-get update
|
||
|
# 安装Docker Engine
|
||
|
sudo apt-get install -y docker-ce docker-ce-cli containerd.io
|
||
|
# 验证Docker是否安装成功
|
||
|
sudo docker version
|
||
|
# Docker作为非root用户
|
||
|
sudo usermod -aG docker $USER
|
||
|
newgrp docker
|
||
|
# 开启远程访问
|
||
|
sudo systemctl edit docker.service
|
||
|
#[Service]
|
||
|
#ExecStart=
|
||
|
#ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:2375
|
||
|
sudo systemctl daemon-reload
|
||
|
sudo systemctl restart docker.service
|
||
|
sudo netstat -lntp | grep dockerd
|
||
|
docker -H tcp://127.0.0.1:2375 version
|
||
|
```
|
||
|
|
||
|
## 卸载docker
|
||
|
|
||
|
```bash
|
||
|
# 删除软件包及其配置文件
|
||
|
sudo apt-get purge docker-ce docker-ce-cli containerd.io docker-compose
|
||
|
# 删除资源
|
||
|
sudo rm -rf /var/lib/docker
|
||
|
sudo rm -rf /var/lib/containerd
|
||
|
# 删除Docker的GPG密钥和源列表文件
|
||
|
sudo rm /usr/share/keyrings/docker-archive-keyring.gpg
|
||
|
sudo rm /etc/apt/sources.list.d/docker.list
|
||
|
```
|