Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to quickly deploy multiple Container Services using Docker Compose

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Xiaobian shares with you how to use Docker Compose to quickly deploy multiple Kubernetes Engine. I hope you will gain something after reading this article. Let's discuss it together!

1 What is Docker Compose?

When we used Docker earlier, we defined Dockerfile files, and then used docker build, docker run and other commands to operate containers. However, the application system of microservice architecture generally contains several microservices, and each microservice generally deploys multiple instances. If each microservice has to be manually started and stopped, the efficiency is low and the maintenance amount is large.

Easy and efficient container management with Docker Compose, an application tool for defining and running multi-container Dockers

2 Installing Docker Compose

Installation command:

[root@iZ2ze4m2ri7irkf6h7n8zoZ ~]# curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose[root@iZ2ze4m2ri7irkf6h7n8zoZ ~]# chmod +x /usr/local/bin/docker-compose

Check if installation is successful:

[root@iZ2ze4m2ri7irkf6h7n8zoZ~]#docker-compose-v3 Docker Compose file format

Docker compose files are typically named docker-compose. yml, and the Docker-compose command is executed in the directory where the file is located.

Docker Compose is divided into three layers, namely project, service/reference label and container.

For example:

docker-compose.yml #A file represents a project services: #Services container-name: #Container build: - xxx:xxx network: #quote label xxx:

Here is a standard docker-compose. yml file

version: "3"#Specify version services: # services proxy: #Custom container name build: ./ proxy #Dockerfile directory, used to build containers networks: #custom container networks - frontend app: build: ./ app networks: - frontend - backend db: image: postgres networks: - backendnetworks: frontend: driver: custom-driver-1 backend: driver: custom-driver-2 driver_opts: foo: "1" bar: "2" 4 Docker Compose

ps: List all running containers

docker-compose ps

logs: View service log output

docker-compose logs

port: Print the public port bound. The following command can output the public port bound to port 8761 of eureka service.

docker-compose port eureka 8761

Build: Build or rebuild services

docker-compose build

start: Starts an existing container for the specified service

docker-compose start eureka

stop: Stop the container of a running service

docker-compose stop eureka

rm: Delete container for specified service

docker-compose rm eureka

up: Build, launch container

docker-compose up

kill: Stop the container of the specified service by sending the SIGKILL signal

docker-compose kill eureka

pull: download service image

docker-compose pull eureka

scale: Set the number of containers for the specified service luck, specified in the form of service = num

docker-compose scale user=3 movie=3

Run: Executes a command on a service

docker-compose run web bash 5 Using Docker Compose One-click deployment Spring Boot + Redis 5.1 Build applications 5.1.1 Spring Boot project

Dependency:

org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-data-redis

Profile:

spring: redis: #host: 127.0.0.1 host: ymx.redis port: 6379 password: jedis: pool: max-active: 8 max-wait: -1 max-idle: 500 min-idle: 0 lettuce: shutdown-timeout: 0

Controller Code:

@RestControllerpublic class HelloController { @Autowired private RedisTemplate redisTemplate; @RequestMapping("/hello/{id}") public String hello(@PathVariable("id") Integer id) { return redisTemplate.opsForValue().get(String.valueOf(id)); } @RequestMapping("/save/{id}/{name}") public String save(@PathVariable("id") Integer id, @PathVariable("name") String name) { try { redisTemplate.opsForValue().set(String.valueOf(id), "Hello " + name + "! "); } catch (Exception e) { return "false"; } return "success";} 5.1.2 Redis Configuration Files

Just a little modification of redis. conf that comes with redis

#comment out bind www.example.com bind 127.0.0.1-:: 1#modify protected-mode yes-> noprotected-mode no5.2 package apps and build directories 5.2.1 package Spring Boot projects

5.2.2 Upload the redis. conf configuration file 5.2.3 Directory structure-mycompose-docker-compose. yml - rd - Dockerfile - redis.conf - sp - Dockerfile - sp_redis-0.0.1-SNAPSHOT.jar5.3 Writing Dockerfile5.3.1 DockerfileFROM java for Spring Boot container: 8 MAINTAINER YMX "1712229564@qq.com" COPY sp_redis-0.0.1-SNAPSHOT.jar/root/sp_redis-0.0.1-SN@SHOT. jarEXPOSE 8080 ENTRYPOINT ["java","-jar","/root/sp_redis-0.0.1-SNAPSHOT.jar "] 5.3.2 DockerfileFROM redis MAINTAINER ymx 171redis container 2229564@qq.comCOPY redis. conf/usr/local/etc/redis/redis. confEXPOSE 6379 CMD [" redis-server ","/usr/local/etc/redis/redis. conf"] 5.4 Writing Docker-Compose. ymlversion: "2.8"#indicates that the Docker-Compose file uses Version 2 fileservices: sp-demo: #Specify service name build: ./ sp #Specify the path to Dockerfile ports: #Specify port mapping - "9001:8080" links: - re-demo: ymx. redis #container linking re-demo: build: ./ rd5.5 Run and test deployment results

Run:

[root@iZ2ze4m2ri7i mycompose]# docker-compose upCreating network "mycompose_default" with the default driverBuilding re-demoSending build context to Docker daemon 96.77kBStep 1/5 : FROM redislatest: Pulling from library/redis......

Testing:

[root@iZ2ze4m2ri7i mycompose]# curl http://localhost:9001/save/2/Ymxsuccess[root@iZ2ze4m2ri7i mycompose]# curl http://localhost:9001/hello/2Hello Ymx! After reading this article, I believe you have a certain understanding of "How to use Docker Compose to rapidly deploy multiple Kubernetes Engine." If you want to know more about this knowledge, please pay attention to the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report