使用DockerCompose启动容器
1.Docker Compose 启动 MySQL
编写docker-compose.yml
yaml
version: '3.8'
services:
mysql:
container_name: mysql
image: mysql
restart: always
ports:
- "3306:3306"
volumes:
- ./mysql/conf:/etc/mysql/conf.d
- ./mysql/logs:/logs
- ./mysql/data:/var/lib/mysql
- ./mysql/db:/docker-entrypoint-initdb.d/
environment:
- "MYSQL_ROOT_PASSWORD=root"
- "TZ=Asia/Shanghai"
command:
--default-time-zone='+8:00'
--default-authentication-plugin=mysql_native_password
--innodb-buffer-pool-size=64M
--character-set-server=utf8mb4
--collation-server=utf8mb4_general_ci
--max_connections=1000
--innodb_lock_wait_timeout=500
一些初始化配置
mysql容器脚本初始化
有时候,我们在容器启动时需要初始化一下sql脚本,如下
- 有需要初始化的sql脚本,可以放到
./mysql/db
下,容器启动会自动执行 - 连接MySQL:
ip(127.0.0.1):3306 root/root
txt
.
├── docker-compose.yml
└── mysql
└── db
└── ufs.sql # 数据库脚本,创建ufs库,和各种表,以及初始化数据
启动容器
shell
[root@localhost ufs]# docker compose up -d mysql
[+] Running 13/13
✔ mysql 12 layers [⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿] 0B/0B Pulled 26.6s
✔ 72a69066d2fe Pull complete 5.5s
✔ 93619dbc5b36 Pull complete 5.5s
✔ 99da31dd6142 Pull complete 5.7s
✔ 626033c43d70 Pull complete 8.1s
✔ 37d5d7efb64e Pull complete 8.2s
✔ ac563158d721 Pull complete 10.1s
✔ d2ba16033dad Pull complete 10.2s
✔ 688ba7d5c01a Pull complete 10.3s
✔ 00e060b6d11d Pull complete 23.6s
✔ 1c04857f594f Pull complete 23.6s
✔ 4d7cfa90e6ea Pull complete 23.7s
✔ e0431212d27d Pull complete 23.7s
[+] Running 2/2
✔ Network ufs_default Created 0.0s
✔ Container mysql Started 0.5s
[root@localhost ufs]#
[root@localhost ufs]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5d79934bded3 mysql "docker-entrypoint.s…" 5 seconds ago Up 5 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
[root@localhost ufs]#
验证是否部署完成
ufs数据库已经初始化完成
shell
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| ufs |
+--------------------+
5 rows in set (0.00 sec)
2.Docker Compose 启动 Redis
编写docker-compose.yml
yaml
version: '3.8'
services:
redis:
container_name: redis
image: redis
restart: always
ports:
- "6379:6379"
volumes:
- ./redis/conf/redis.conf:/etc/redis/redis.conf
- ./redis/data:/data
command: redis-server /etc/redis/redis.conf
启动容器
shell
[root@localhost ufs]# docker compose up -d redis
[+] Running 7/7
✔ redis 6 layers [⣿⣿⣿⣿⣿⣿] 0B/0B Pulled 4.0s
✔ a2abf6c4d29d Already exists 0.0s
✔ c7a4e4382001 Already exists 0.0s
✔ 4044b9ba67c9 Already exists 0.0s
✔ c8388a79482f Already exists 0.0s
✔ 413c8bb60be2 Already exists 0.0s
✔ 1abfd3011519 Already exists 0.0s
[+] Running 1/1
✔ Container redis Started
测试连接
shell
[root@localhost ufs]# docker exec -it redis bash
root@5f059715a1ca:/data# redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379>
2.Docker Compose 启动 Mongo
自己构建镜像
txt
mongo/
├── conf
│ └── mongod.conf
└── Dockerfile
mongod.conf
yaml
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: /data/db
journal:
enabled: true
directoryPerDB: true
engine: wiredTiger
wiredTiger:
engineConfig:
cacheSizeGB: 8
directoryForIndexes: true
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
# how the process runs
processManagement:
timeZoneInfo: /usr/share/zoneinfo
#replication:
replication:
oplogSizeMB: 51200
replSetName: rs0
Dockerfile
shell
FROM mongo:4.4.0
ADD ./config/mongod.conf /etc/mongo/mongod.conf
编写docker-compose.yml
yaml
version: '3.8'
services:
mongo:
container_name: mongo
#image: mongo:4.4.0
build:
context: ./mongo
restart: always
ports:
- "27017:27017"
volumes:
- ./mongo/db:/data/db
- ./mongo/log:/var/log/mongodb
- ./mongo/config:/etc/mongo
启动容器
shell
[root@localhost ufs]# docker compose up -d mongo
[+] Building 15.6s (7/7) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 160B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/mongo:4.4.0 15.6s
=> [internal] load build context 0.0s
=> => transferring context: 858B 0.0s
=> CACHED [1/2] FROM docker.io/library/mongo:4.4.0@sha256:77f3d623d475c56ab43d61864bc3aa610953869724dbbea325989a0718741d1c 0.0s
=> [2/2] ADD ./config/mongod.conf /etc/mongo/mongod.conf 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:6ee7dfe78541d28c4f4195ca5a2964d615499dad0bc761d3a83b7c018c6d769b 0.0s
=> => naming to docker.io/library/ufs-mongo 0.0s
[+] Running 1/1
✔ Container mongo Started 1.7s
[root@localhost ufs]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
42e81872c719 ufs-mongo "docker-entrypoint.s…" 12 seconds ago Up 9 seconds 0.0.0.0:27017->27017/tcp, :::27017->27017/tcp mongo