下面是一个简单的例子,展示如何通过Docker Compose文件部署Nacos、MySQL和Redis。请确保您的机器上已经安装了Docker和Docker Compose。
1,准备好mysql、redis、nacos镜像
bash
sudo docker pull mysql:8 && sudo docker pull redis:7.2 && sudo docker pull nacos/nacos-server:v2.4.3
2,如果因为网络问题,没有下载到,可以直接下载镜像包
3,根据需求下载Docker镜像包,然后导入到Docker:
bash
sudo docker load -i xxxx.tar
4,查看docker-compose 版本
bash
sudo docker-compose -version
5,如果遇到以下情况,说明docker-compose没有安装:
6,docker-compose没有的话,则根据以下地址下载:
bash
sudo curl -L "https://github.com/docker/compose/releases/download/$(curl -s https://api.github.com/repos/docker/compose/releases/latest | grep tag_name | cut -d '"' -f 4)/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
7,如果因为网络或者下载慢,可根据以下连接进行下载,然后放到服务器 /usr/local/bin下,以下是centos x86_64
8,设置权限:
bash
sudo chmod +x /usr/local/bin/docker-compose
9,创建符号链接(如果需要的话,这一步是可选的,用于将 Docker Compose 添加到 PATH 中,以便可以在任何位置运行它):
bash
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
10,验证安装:
bash
sudo docker-compose -version
11,创建nacos数据库SQL导入Dockerfile
bash
sudo vi Dockerfile
bash
FROM mysql:8
ADD https://raw.githubusercontent.com/alibaba/nacos/develop/distribution/conf/mysql-schema.sql /docker-entrypoint-initdb.d/nacos-mysql.sql
RUN chown -R mysql:mysql /docker-entrypoint-initdb.d/nacos-mysql.sql
EXPOSE 3306
CMD ["mysqld", "--character-set-server=utf8mb4", "--collation-server=utf8mb4_unicode_ci"]
12,创建Docker Compose文件
在工作目录下创建一个名为docker-compose.yml的文件:
bash
sudo vi docker-compose.yml
bash
version: '3.8'
services:
ncc-mysql:
container_name: ncc-mysql
restart: unless-stopped
build:
context: .
dockerfile: ./Dockerfile
image: mysql:8
privileged: true
volumes:
- /data/ncc/mysql:/var/lib/mysql
ports:
- 3306:3306
environment:
- TZ=Asia/Shanghai
- MYSQL_ROOT_PASSWORD=mysql@2024
- MYSQL_DATABASE=nacos
- MYSQL_USER=nacos
- MYSQL_PASSWORD=nacos@2024
- LANG=C.UTF-8
healthcheck:
test: [ "CMD", "mysqladmin" ,"ping", "-h", "localhost" ]
interval: 5s
timeout: 10s
retries: 10
networks:
- ncc-docker
ncc-nacos:
container_name: ncc-nacos
restart: unless-stopped
image: nacos/nacos-server:v2.4.3
privileged: true
volumes:
- /data/ncc/nacos:/home/nacos
ports:
- 8848:8848
- 9848:9848
environment:
- PREFER_HOST_MODE=hostname
- MODE=standalone
- SPRING_DATASOURCE_PLATFORM=mysql
- MYSQL_SERVICE_HOST=ncc-mysql
- MYSQL_SERVICE_DB_NAME=nacos
- MYSQL_SERVICE_PORT=3306
- MYSQL_SERVICE_USER=nacos
- MYSQL_SERVICE_PASSWORD=nacos@2024
- MYSQL_SERVICE_DB_PARAM=characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
- NACOS_AUTH_ENABLE=true
- NACOS_AUTH_IDENTITY_KEY=2222
- NACOS_AUTH_IDENTITY_VALUE=2xxx
- NACOS_AUTH_TOKEN=SecretKey012345678901234567890123456789012345678901234567890123456789
depends_on:
ncc-mysql:
condition: service_healthy
networks:
- ncc-docker
ncc-redis:
container_name: ncc-redis
restart: unless-stopped
image: redis:7.2
privileged: true
volumes:
- /data/ncc/redis:/data
ports:
- 6379:6379
environment:
- TZ=Asia/Shanghai
networks:
- ncc-docker
command: ["redis-server", "--requirepass", "redis@2024","--appendonly","yes"]
networks:
ncc-docker:
driver: bridge
external: false
13,启动服务
在包含docker-compose.yml文件的目录中打开终端,运行以下命令来启动服务:
bash
sudo docker-compose up -d
14,发现nacos起不了,查看一下日志:
bash
sudo docker logs ncc-nacos
15,发现启动的脚本找不到,查看一下挂载路径:
16,确实是没有,解决办法,先启动一个临时不挂载的容器,把文件拷贝到宿主主机
bash
sudo docker-compose down
17,修改docker-compose.yml,把volumes注释掉:
bash
sudo vi docker-compose.yml
18,再启动服务:
bash
sudo docker-compose up -d
确实是启动成功了,然后把容器/home/nacos拷贝到本地/data/ncc/nacos/
bash
sudo docker cp ncc-nacos:/home/nacos /data/ncc/nacos
19,关闭服务,并且去掉刚才volumes注释
bash
sudo docker-compose down
sudo vi docker-compose.yml
20,重新启动服务
bash
sudo docker-compose up -d
21,检查mysql服务是否正常
bash
sudo docker exec -it ncc-mysql mysql -uroot -pmysql@2024
22,检查redis服务是否正常
bash
sudo docker exec -it ncc-redis redis-cli
23,检查nacos是否正常:
bash
sudo docker logs ncc-nacos
查看报错是因为数据库没有建好
https://raw.githubusercontent.com/alibaba/nacos/develop/distribution/conf/mysql-schema.sql
将mysql-schema.sql下载,并上传到服务器上面
然后执行SQL运行,并查看数据库表是否存在
bash
sudo docker exec -i ncc-mysql mysql -uroot -pmysql@2024 nacos < ./mysql-schema.sql
bash
sudo docker exec -it ncc-mysql mysql -uroot -pmysql@2024
再查看nacos日志,说明已经启动成功
进行访问:http://47.108.195.246:8848/nacos/
账号:nacos
密码:nacos
OK,完成!