下面是一个简单的例子,展示如何通过Docker Compose文件部署Nacos、MySQL和Redis。请确保您的机器上已经安装了Docker和Docker Compose。
1,准备好mysql、redis、nacos镜像
sudo docker pull mysql:8 && sudo docker pull redis:7.2 && sudo docker pull nacos/nacos-server:v2.4.3
2,如果因为网络问题,没有下载到,可以直接下载镜像包
- mysql:8镜像包
- redis:7.2镜像包
- nacos/nacos-server:v2.4.3镜像包
3,根据需求下载Docker镜像包,然后导入到Docker:
sudo docker load -i xxxx.tar
4,查看docker-compose 版本
sudo docker-compose -version
5,如果遇到以下情况,说明docker-compose没有安装:
6,docker-compose没有的话,则根据以下地址下载:
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
- docker-compose
8,设置权限:
sudo chmod +x /usr/local/bin/docker-compose
9,创建符号链接(如果需要的话,这一步是可选的,用于将 Docker Compose 添加到 PATH 中,以便可以在任何位置运行它):
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
10,验证安装:
sudo docker-compose -version
11,创建nacos数据库SQL导入Dockerfile
sudo vi Dockerfile
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的文件:
sudo vi docker-compose.yml
version: '3.8'services:ncc-mysql:container_name: ncc-mysqlrestart: unless-stoppedbuild:context: .dockerfile: ./Dockerfileimage: mysql:8privileged: truevolumes:- /data/ncc/mysql:/var/lib/mysqlports:- 3306:3306environment:- TZ=Asia/Shanghai- MYSQL_ROOT_PASSWORD=mysql@2024- MYSQL_DATABASE=nacos- MYSQL_USER=nacos- MYSQL_PASSWORD=nacos@2024- LANG=C.UTF-8healthcheck:test: [ "CMD", "mysqladmin" ,"ping", "-h", "localhost" ]interval: 5stimeout: 10sretries: 10networks:- ncc-dockerncc-nacos:container_name: ncc-nacosrestart: unless-stoppedimage: nacos/nacos-server:v2.4.3privileged: truevolumes:- /data/ncc/nacos:/home/nacosports:- 8848:8848- 9848:9848environment:- 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=SecretKey012345678901234567890123456789012345678901234567890123456789depends_on:ncc-mysql:condition: service_healthynetworks:- ncc-dockerncc-redis:container_name: ncc-redisrestart: unless-stoppedimage: redis:7.2privileged: truevolumes:- /data/ncc/redis:/dataports:- 6379:6379environment:- TZ=Asia/Shanghainetworks:- ncc-dockercommand: ["redis-server", "--requirepass", "redis@2024","--appendonly","yes"]networks:ncc-docker:driver: bridgeexternal: false
13,启动服务
在包含docker-compose.yml文件的目录中打开终端,运行以下命令来启动服务:
sudo docker-compose up -d
14,发现nacos起不了,查看一下日志:
sudo docker logs ncc-nacos
15,发现启动的脚步找不到,查看一下挂载路径:
16,确实是没有,解决办法,先启动一个不挂载的容器,把文件拷贝到宿主主机
sudo docker-compose down
17,修改docker-compose.yml,把volumes注释掉:
sudo vi docker-compose.yml
18,再启动服务:
sudo docker-compose up -d
确实是启动成功了,然后把容器/home/nacos拷贝到本地/data/ncc/nacos/
sudo docker cp ncc-nacos:/home/nacos /data/ncc/nacos
19,关闭服务,并且去掉刚才volumes注释
sudo docker-compose down
sudo vi docker-compose.yml
20,重新启动服务
sudo docker-compose up -d
21,检查mysql服务是否正常
sudo docker exec -it ncc-mysql mysql -uroot -pmysql@2024
22,检查redis服务是否正常
sudo docker exec -it ncc-redis redis-cli
23,检查nacos是否正常:
sudo docker logs ncc-nacos
查看报错是因为数据库没有建好
https://raw.githubusercontent.com/alibaba/nacos/develop/distribution/conf/mysql-schema.sql
将mysql-schema.sql下载,并上传到服务器上面
然后执行SQL运行,并查看数据库表是否存在
sudo docker exec -i ncc-mysql mysql -uroot -pmysql@2024 nacos < ./mysql-schema.sql
sudo docker exec -it ncc-mysql mysql -uroot -pmysql@2024
再查看nacos日志,说明已经启动成功
进行访问:http://47.108.195.246:8848/nacos/
账号:nacos
密码:nacos
OK,完成!