1 what is Docker Compose
When we define the container, we use the command of Docker and build Docker, and then use the command of Docker. However, the application system of microservice architecture generally includes several microservices, and each microservice will generally deploy multiple instances. If each microservice needs to be started and stopped manually, it can be imagined that the efficiency is low and the amount of maintenance is large.
Docker Compose can easily and efficiently manage containers. It is an application tool for defining and running multi container dockers
2. Install Docker Compose
Installation command:
[root@iZ2ze4m2ri7irkf6h6n8zoZ ~]# 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@iZ2ze4m2ri7irkf6h6n8zoZ ~]# chmod +x /usr/local/bin/docker-compose
Check for successful installation:
[root@iZ2ze4m2ri7irkf6h6n8zoZ ~]# docker-compose -v
3. Brief introduction to docker compose file format
Docker Compose files are generally 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: project, service / reference tag and container
For example:
docker-compose.yml # A file represents a project serveices: # service container-name: # container build: - xxx:xxx network: # Reference label xxx:
The following is a standard docker compose YML file
version: "3" # Specified version services: # services proxy: # Custom container name build: ./proxy # The directory of Dockerfile, which is used to build containers networks: # Custom container network - frontend app: build: ./app networks: - frontend - backend db: image: postgres networks: - backend networks: frontend: driver: custom-driver-1 backend: driver: custom-driver-2 driver_opts: foo: "1" bar: "2"
4. Common commands of docker compose
- ps: list all running containers
docker-compose ps
- logs: view the service log output
docker-compose logs
- Port: print the bound public port. The following command can output the public port bound by eureka service port 8761
docker-compose port eureka 8761
- Build: build or rebuild services
docker-compose build
- start: starts the container where the specified service already exists
docker-compose start eureka
- stop: the container that stops the running service
docker-compose stop eureka
- rm: deletes the container for the specified service
docker-compose rm eureka
- up: build and start containers
docker-compose up
- kill: a container that stops a specified service by sending a SIGKILL signal
docker-compose kill eureka
- pull: download service image
docker-compose pull eureka
- scale: sets the number of service containers specified in the form of service=num
docker-compose scale user=3 movie=3
- run: execute a command on a service
docker-compose run web bash
5. Using Docker Compose to deploy Spring Boot+Redis in one click
Application construction
5.1.1 Spring Boot project
Dependency:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--redis--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
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:
@RestController public class HelloController { @Autowired private RedisTemplate<String, String> 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 file
Just redis that comes with redis Conf made some changes
#Comment out bind 127.0.0.1 # bind 127.0.0.1 -::1 #Modify protected mode yes - > no protected-mode no
5.2 package applications and build directories
5.2.1 packaging Spring Boot project
5.2.2 upload 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.jar
5.3 writing Dockerfile
5.3.1 Dockerfile of spring boot container
FROM java:8 MAINTAINER YMX "1712229564@qq.com" COPY sp_redis-0.0.1-SNAPSHOT.jar /root/sp_redis-0.0.1-SNAPSHOT.jar EXPOSE 8080 ENTRYPOINT ["java", "-jar","/root/sp_redis-0.0.1-SNAPSHOT.jar"]
5.3.2 Dockerfile of redis container
FROM redis MAINTAINER ymx 1712229564@qq.com COPY redis.conf /usr/local/etc/redis/redis.conf EXPOSE 6379 CMD ["redis-server","/usr/local/etc/redis/redis.conf" ]
5.4 write docker compose yml
version: "2.8" # Indicates that the docker compose file uses Version 2 file services: sp-demo: # Specify service name build: ./sp # Specify the path of Dockerfile ports: # Specify port mapping - "9001:8080" links: - re-demo:ymx.redis # Make container link re-demo: build: ./rd
5.5 run and test deployment results
function:
[root@iZ2ze4m2ri7i mycompose]# docker-compose up Creating network "mycompose_default" with the default driver Building re-demo Sending build context to Docker daemon 96.77kB Step 1/5 : FROM redis latest: Pulling from library/redis ......
Test:
[root@iZ2ze4m2ri7i mycompose]# curl http://localhost:9001/save/2/Ymx success [root@iZ2ze4m2ri7i mycompose]# curl http://localhost:9001/hello/2 Hello Ymx!
6 Summary
In the Spring Boot configuration file, the host of redis does not use localhost or 127.0.0.1, but uses the domain name YMX Redis, the domain name in docker compose The YML file is mapped, and then the container of Spring Boot can be linked to the redis container, but this situation depends on a default condition, that is, the docker network defaults to the bridge mode, and the two containers are in the same subnet, so they can access each other.
Therefore, links is not the only container network solution. When there are many containers, networks needs to be used for network management.
Reference article:
https://www.jianshu.com/p/658911a8cff3
https://www.jianshu.com/p/3004fbce4d37
https://blog.csdn.net/luo15242208310/article/details/88642187
https://blog.csdn.net/qq_36781505/article/details/86612988