https://docs.docker.com/compose/compose-file/compose-file-v3/
#查看
docker secret ls
#创建一个密码
echo abc123 | docker secret create my_pass -
#创建一个文件密码
echo abc123 > pass1.txt
docker secret create my_pass1 pass1.txt
#使用
docker service create --name mysql-demo --secret mysql_pass mysql:5.7
默认放在容器的/run/secrets/目录下
version: "3.9"
services:
db:
image: mysql:latest
volumes:
- db_data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD_FILE: /run/secrets/db_root_password
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD_FILE: /run/secrets/my_pass
secrets: # 容器使用secrets
- db_root_password
- my_pass
secrets: # 声明secrets
db_root_password:# 数据库密码
file: ./db_root_password.txt #密码的文件地址
my_pass:
external: true
name: my_pass1
方法二:
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD_FILE=/run/secrets/mysql-root --secret my-pass