当前位置:网站首页>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
(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 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
边栏推荐
- Multiply LCA (nearest common ancestor)
- 分布式机器学习框架与高维实时推荐系统
- On data preprocessing in sklearn
- drools执行String规则或执行某个规则文件
- Sparkcontext: error initializing sparkcontext solution
- Pytorch builds LSTM to realize clothing classification (fashionmnist)
- Map and set
- (C语言)输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
- Embedded Software Engineer career planning
- Leetcode209 长度最小的子数组
猜你喜欢
drools决策表的简单使用
Less than three months after the programmer was hired, the boss wanted to launch the app within one month. If he was dissatisfied, he was dismissed immediately
Find the common ancestor of any two numbers in a binary tree
Deep understanding of NN in pytorch Embedding
Experiment of connecting mobile phone hotspot based on Arduino and esp8266 (successful)
Initial JDBC programming
分布式机器学习框架与高维实时推荐系统
Differences between nodes and sharding in ES cluster
自然语言处理系列(一)——RNN基础
drools动态增加、修改、删除规则
随机推荐
Lekao: contents of the provisions on the responsibility of units for fire safety in the fire protection law
(C language) input a line of characters and count the number of English letters, spaces, numbers and other characters.
Mysql database foundation
Record the range of data that MySQL update will lock
mysql索引和事务
CONDA common command summary
LeetCode—剑指 Offer 59 - I、59 - II
Writing method of then part in drools
浅谈sklearn中的数据预处理
Sort---
Use sqoop to export ads layer data to MySQL
上传文件时,服务器报错:IOFileUploadException: Processing of multipart/form-data request failed. 设备上没有空间
Simple understanding of ThreadLocal
JZ63 股票的最大利润
LeetCode—剑指 Offer 37、38
[QT] Qt development environment installation (QT version 5.14.2 | QT download | QT installation)
Drools terminates the execution of other rules after executing one rule
Leetcode739 每日温度
Full link voltage measurement
堆(優先級隊列)