当前位置:网站首页>Docker-compose配置Mysql,Redis,MongoDB
Docker-compose配置Mysql,Redis,MongoDB
2022-07-02 09:43:00 【Ostrich5yw】
Docker-compose下配置Mysql,Redis,MongoDB详解
一、docker-compose简介
Compose是用于定义和运行多容器Docker应用程序的工具。通过Compose,您可以使用YAML文件来配置应用程序的服务。然后,使用一个命令,就可以从配置中创建并启动所有服务。
使用Compose基本上是一个三步过程:
- 使用Dockerfile定义你的应用环境,以便可以在任何地方复制它。
- 用docker-compose.yml定义组成应用程序的服务, 以便它们可以在隔离的环境中一起运行。
docker-compose.yml主要结构:
①. version: #版本
②. services: #服务
redis: #服务一
depends_on: #依赖于哪个服务,它会在依赖的服务启动之后再启动
image:
build:
network:
volumes:
redis: #服务二
web: #服务三
③. 其他设置(网络卷、全局规则)
volumes:
networks:
configs:
- 运行docker compose up或docker compose up -d(后台运行)运行您的整个应用程序。
注意:每次修改任一配置文件后,都要使用 docker-compose up --build 重新构建
总结:有了docker-compose,当我们想启动多个服务时,无需再一个一个进行docker run操作,而只需要编写docker-compose.yml配置文件,即可一次运行你的全部服务。
二、构建一个实例项目
这里给出的实例,Web框架使用Spring-boot,并同时使用Mysql,Redis,MongoDB这三个现在最常见的数据库,对其进行简单的计数操作。(即每访问一次网页,三个库中的计数器分别加一)
1.前后端实现
//实现每次点击网页,对各个数据库进行累加,并返回当前的计数器记录的数值。
@RequestMapping("/")
public String home(Model model){
int bef1 = mysqlMapper.selectMysql();
mysqlMapper.changeMysql();
int res1 = mysqlMapper.selectMysql();
model.addAttribute("mysqlbef", bef1);
model.addAttribute("mysqlres", res1);
model.addAttribute("redisres", redisTemplate.opsForValue().increment("age"));
Query query = new Query(Criteria.where("name").is("5yw"));
List<Map> list = mongoTemplate.find(query, Map.class, "hellomongodb");
int bef3 = (int) list.get(0).get("age");
//****************************************************************************************************
Update update = new Update();
update.set("age", bef3 + 1);
mongoTemplate.updateFirst(query, update, "hellomongodb");
//****************************************************************************************************
List<Map> list1 = mongoTemplate.find(query, Map.class, "hellomongodb");
int res3 = (int) list1.get(0).get("age");
model.addAttribute("mongobef", bef3);
model.addAttribute("mongores", res3);
return "Page/home.html";
}
<!-- 前端接收后端传递的参数并显示 -->
<span id="mysql"></span>
<hr>
<span id="redis"></span>
<hr>
<span id="mongo"></span>
</body>
<script> var message1 = "Mysql更新后:" +[[${
mysqlres}]]; var message2 = "Redis更新后:" +[[${
redisres}]]; var message3 = "Mongo更新后:" +[[${
mongores}]]; document.getElementById("mysql").textContent = message1; document.getElementById("redis").textContent = message2; document.getElementById("mongo").textContent = message3; </script>
2.Dockerfile及docker-compose.yml
Dockerfile将我们Springboot打包的jar包,装配成为docker的镜像,以在docker中运行。
# Docker image for springboot file run
# VERSION 0.0.1
# Author: eangulee
# 基础镜像使用java
FROM java:8
# 作者
MAINTAINER 5yw <[email protected]>
# 将jar包添加到容器中并更名为app.jar
ADD dockerweb-0.0.1-SNAPSHOT.jar app.jar
# 运行jar包
ENTRYPOINT ["java","-jar","/app.jar"]
docker-compose.yml是docker-compose的核心配置文件,docker将会根据这个配置文件进行镜像的构建以及运行,在类似于我们这类需要启用多个镜像(web,mysql,redis,mongo)的项目,docker-compose减少了我们很多的工作量。(即我们无需对每个镜像依次进行run操作)
version: '3.8'
services:
dockerweb:
build: .
depends_on: # 使web项目在数据库均运行之后再运行
- mysql
- redis
- mongo
ports:
- 8000:8000 # 配置端口映射(主机端口:容器端口)
mysql:
image: 'mysql'
environment:
MYSQL_ROOT_PASSWORD: 123456 # 配置Mysql密码
MYSQL_USER: root
MYSQL_PASS: 123456
container_name: "mysql"
restart: always
ports:
- 3306:3306
volumes: # 卷挂载
- /home/ostrich5yw/Desktop/DockerCompose/mysql/db:/var/lib/mysql
- /home/ostrich5yw/Desktop/DockerCompose/mysql/log:/var/log/mysql
- /home/ostrich5yw/Desktop/DockerCompose/mysql/conf/my.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf
redis:
image: 'redis'
container_name: "redis"
restart: always
volumes:
- /home/ostrich5yw/Desktop/DockerCompose/redis/data:/data # 主机路径:容器路径
- /home/ostrich5yw/Desktop/DockerCompose/redis/redis.conf:/etc/redis/redis.conf
mongo:
image: 'mongo'
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: 123456
container_name: "mongo"
restart: always
volumes:
- /home/ostrich5yw/Desktop/DockerCompose/mongo/db:/data/db
- /home/ostrich5yw/Desktop/DockerCompose/mongo/log:/data/logs
ports:
- 27017:27017
尤其要注意这里的service名称(例如dockerweb),这里的名称,docker-compose会将他们对应成相应的域名。例如我们如果要访问mysql,一般是192.168.0.xxx:3306,而当我们有域名时,只需要写为mysql:3306即可
在有多个服务时,比如Mysql1在192.168.0.1,Mysql2在192.168.0.2,我们只需要写为mysql:3306而无需指定IP。
3. 生成镜像
我们将编写的docker-compose.yml,Dockerfile以及导出的jar包放入同一文件夹,并建立配置文件中描述的三个挂载文件夹。
我们运行docker-compose up运行镜像。
项目运行后,我们需要为Mysql与MongoDB导入初始数据。
docker exec -it mysql /bin/bash 进入Mysql镜像
- mysql -uroot -p 123456
- create database test;
- use test;
- CREATE TABLE
hellomysql
(name
varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,age
int(11) NULL DEFAULT NULL
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;- INSERT INTO
hellomysql
VALUES (‘5yw’, 0);
docker exec -it mongo /bin/bash 进入MongoDB镜像
- mongo 127.0.0.1:27017/admin -u root -p 123456
- use test;
- db.createCollection(“hellomongodb”)
- db.getCollection(“hellomongodb”).insert( {
_id: ObjectId(“5feac4fba4de87481cd2139b”),
name: “5yw”,
age: NumberInt(“0”)
} );- exit
通过另一个窗口,输入curl localhost:8000查看结果。
三、实例程序源码
https://gitee.com/Ostrich5ywtt/dockerweb
边栏推荐
- Mish shake the new successor of the deep learning relu activation function
- Codeforces 771-div2 C (trouble, permutation is not very good)
- HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE
- Log4j2
- Depth filter of SvO2 series
- [C language] convert decimal numbers to binary numbers
- Jenkins用户权限管理
- 记录一下MySql update会锁定哪些范围的数据
- Log4j2
- 寻找二叉树中任意两个数的公共祖先
猜你喜欢
drools决策表的简单使用
jenkins 凭证管理
二分刷题记录(洛谷题单)区间的甄别
K-Means Clustering Visualization in R: Step By Step Guide
Dynamic debugging of multi file program x32dbg
初始JDBC 编程
HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE
刷题---二叉树--2
PyTorch nn. Full analysis of RNN parameters
Depth filter of SvO2 series
随机推荐
HOW TO ADD P-VALUES TO GGPLOT FACETS
drools中then部分的写法
Gaode map test case
初始JDBC 编程
Jenkins用户权限管理
[C language] convert decimal numbers to binary numbers
PyTorch nn.RNN 参数全解析
Natural language processing series (I) -- RNN Foundation
This article takes you to understand the operation of vim
输入一个三位的数字,输出它的个位数,十位数、百位数。
arcgis js 4.x 地图中加入图片
GGPUBR: HOW TO ADD ADJUSTED P-VALUES TO A MULTI-PANEL GGPLOT
全链路压测
Jenkins user rights management
How does Premiere (PR) import the preset mogrt template?
B high and beautiful code snippet sharing image generation
drools动态增加、修改、删除规则
Codeforces 771-div2 C (trouble, permutation is not very good)
自然语言处理系列(三)——LSTM
XSS labs master shooting range environment construction and 1-6 problem solving ideas