当前位置:网站首页>Docker compose configuration mysql, redis, mongodb
Docker compose configuration mysql, redis, mongodb
2022-07-02 12:21:00 【Ostrich5yw】
Docker-compose The configuration Mysql,Redis,MongoDB Detailed explanation
One 、docker-compose brief introduction
Compose Is used to define and run multiple containers Docker Application tools . adopt Compose, You can use YAML File to configure the application's services . then , Use a command , You can create and start all services from the configuration .
Use Compose It's basically a three-step process :
- Use Dockerfile Define your application environment , So that it can be copied anywhere .
- use docker-compose.yml Define the services that make up the application , So that they can run together in isolated environments .
docker-compose.yml Main structure :
①. version: # edition
②. services: # service
redis: # Service one
depends_on: # Depending on which service , It will start after the dependent service is started
image:
build:
network:
volumes:
redis: # Service two
web: # Service 3
③. Other settings ( Network volume 、 Global rules )
volumes:
networks:
configs:
- function docker compose up or docker compose up -d( Background operation ) Run your entire application .
Be careful : Every time you modify any configuration file , All use docker-compose up --build Rebuild
summary : With docker-compose, When we want to start multiple services , There is no need to do it one by one docker run operation , You just need to write docker-compose.yml The configuration file , You can run all your services at once .
Two 、 Build an instance project
The example given here ,Web Frame usage Spring-boot, And use at the same time Mysql,Redis,MongoDB These three most common databases nowadays , Simply count it .( That is, every time you visit a web page , The counters in the three libraries are increased by one )
1. Front end and back end implementation
// Achieve every click on the web , Accumulate all databases , And return the value recorded by the current counter .
@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";
}
<!-- The front end receives the parameters passed by the back end and displays -->
<span id="mysql"></span>
<hr>
<span id="redis"></span>
<hr>
<span id="mongo"></span>
</body>
<script> var message1 = "Mysql After the update :" +[[${
mysqlres}]]; var message2 = "Redis After the update :" +[[${
redisres}]]; var message3 = "Mongo After the update :" +[[${
mongores}]]; document.getElementById("mysql").textContent = message1; document.getElementById("redis").textContent = message2; document.getElementById("mongo").textContent = message3; </script>
2.Dockerfile And docker-compose.yml
Dockerfile Will we Springboot packaged jar package , Assemble as docker Mirror image , In the docker Run in .
# Docker image for springboot file run
# VERSION 0.0.1
# Author: eangulee
# The basic image uses java
FROM java:8
# author
MAINTAINER 5yw <[email protected]>
# take jar The package is added to the container and renamed app.jar
ADD dockerweb-0.0.1-SNAPSHOT.jar app.jar
# function jar package
ENTRYPOINT ["java","-jar","/app.jar"]
docker-compose.yml yes docker-compose Core profile for ,docker The image will be built and run according to this configuration file , In a class like ours, you need to enable multiple images (web,mysql,redis,mongo) Project ,docker-compose It reduces our workload a lot .( That is, we don't need to do each image in turn run operation )
version: '3.8'
services:
dockerweb:
build: .
depends_on: # send web The project will run after all databases are running
- mysql
- redis
- mongo
ports:
- 8000:8000 # Configure port mapping ( Host port : Container port )
mysql:
image: 'mysql'
environment:
MYSQL_ROOT_PASSWORD: 123456 # To configure Mysql password
MYSQL_USER: root
MYSQL_PASS: 123456
container_name: "mysql"
restart: always
ports:
- 3306:3306
volumes: # Volume mount
- /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 # Host path : Container path
- /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
Pay particular attention to the service name ( for example dockerweb), The name here ,docker-compose They will be mapped to the corresponding domain name . For example, if we want to visit mysql, It's usually 192.168.0.xxx:3306, And when we have domain names , Just write as mysql:3306 that will do
When there are multiple services , such as Mysql1 stay 192.168.0.1,Mysql2 stay 192.168.0.2, We just need to write as mysql:3306 Without specifying IP.
3. Create a mirror image
We're going to write docker-compose.yml,Dockerfile And exported jar Put the package in the same folder , And create the three mount folders described in the configuration file .
We run docker-compose up Running the mirror .
After project operation , We need to Mysql And MongoDB Import initial data .
docker exec -it mysql /bin/bash Get into Mysql Mirror image
- mysql -uroot -p 123456
- create database test;
- use test;
- CREATE TABLE
hellomysql(namevarchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,ageint(11) NULL DEFAULT NULL
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;- INSERT INTO
hellomysqlVALUES (‘5yw’, 0);
docker exec -it mongo /bin/bash Get into MongoDB Mirror image
- 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
Through another window , Input curl localhost:8000 View results .
3、 ... and 、 Example program source code
https://gitee.com/Ostrich5ywtt/dockerweb
边栏推荐
- Deep understanding of NN in pytorch Embedding
- CV2 in OpenCV VideoWriter_ Fourcc() function and cv2 Combined use of videowriter() function
- [old horse of industrial control] detailed explanation of Siemens PLC TCP protocol
- Leetcode122 买卖股票的最佳时机 II
- SSH automatically disconnects (pretends to be dead) after a period of no operation
- CDA data analysis -- common knowledge points induction of Excel data processing
- WSL 2 will not be installed yet? It's enough to read this article
- 【C语言】杨辉三角,自定义三角的行数
- Leetcode122 the best time to buy and sell stocks II
- 排序---
猜你喜欢

arcgis js 4. Add pictures to x map

Differences between nodes and sharding in ES cluster

Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement

Go学习笔记—多线程

基于Arduino和ESP8266的Blink代码运行成功(包含错误分析)

HR wonderful dividing line

记录一下MySql update会锁定哪些范围的数据

PyTorch nn. Full analysis of RNN parameters

arcgis js 4.x 地图中加入图片

Map和Set
随机推荐
Go学习笔记—基于Go的进程间通信
【C语言】杨辉三角,自定义三角的行数
The blink code based on Arduino and esp8266 runs successfully (including error analysis)
Sweetheart leader: Wang Xinling
Simple use of drools decision table
寻找二叉树中任意两个数的公共祖先
Sub thread get request
Sparkcontext: error initializing sparkcontext solution
mysql索引和事务
ES集群中节点与分片的区别
MySQL and PostgreSQL methods to grab slow SQL
String palindrome hash template question o (1) judge whether the string is palindrome
Discrimination of the interval of dichotomy question brushing record (Luogu question sheet)
Lekao: contents of the provisions on the responsibility of units for fire safety in the fire protection law
CDA数据分析——Excel数据处理的常见知识点归纳
Jenkins user rights management
Leetcode topic [array] -540- single element in an ordered array
LeetCode—剑指 Offer 59 - I、59 - II
Lombok common annotations
Leetcode922 按奇偶排序数组 II