table of Contents
Introduction
Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a Compose file to configure your application's services. Then, using a single command, you create and start all the services from your configuration.
Compose is a tool for defining and running multi-container Docker applications. Using combination, you can use the combination file to configure the application's services. Then, use a single command to create and start all services from the configuration.
We logo
can see from the above that, to put it bluntly, this thing is a tool (zhang) for managing containers (yu), we can conveniently use it to manage our docker
containers, which can greatly simplify the complex operations of the command line.
1. Get started quickly
If you are Mac
or the Windows
user uses the desktop version of the Docker
application, it will be installed for you by default docker-compose
(replaced with dc below), if it is centos
, you can use the command to yum -y install docker-compose
be too simple and not to explain too much.
Check the version, it can be displayed to prove that the installation is successful
lianying@lianyingdeiMac ~ % docker-compose version
docker-compose version 1.27.2, build 18f557f9
docker-py version: 4.3.1
CPython version: 3.7.7
OpenSSL version: OpenSSL 1.1.1g 21 Apr 2020
Next, we have a need, run a service-a
mount to /usr/local
, map port as 8082
and add servicehost
domain name mapping intranet ip
, many people can think that it should be a lengthy docker
command
docker run -idt -p 8082:8082 -v /Users/objcat/jar/service-a.jar:/usr/local/service-a.jar --name service-a java sh -c "echo 192.168.1.126 servicehost >> /etc/hosts && java -jar /usr/local/service-a.jar"
So a big lump, it seems very inconvenient, then we are used docker-compose
to optimize the look
First create a docker-compose.yml
file named
lianying@lianyingdeiMac SpringBoot01 % touch docker-compose.yml
lianying@lianyingdeiMac SpringBoot01 % ls
docker-compose.yml
Then just open it with a text editor and write the following
version: '2'
services:
service-a:
image: java
volumes:
- /Users/objcat/jar/service-a.jar:/usr/local/service-a.jar
ports:
- 8082:8082
command:
- /bin/sh
- -c
- |
echo 192.168.1.126 servicehost >> /etc/hosts
java -jar /usr/local/service-a.jar
Does this seem to be much clearer, let's run it next
lianying@lianyingdeiMac SpringBoot01 % docker-compose up -d
Starting springboot01_service-a_1 ... done
-d
Run in the background, otherwise the run log will appear on your screen. . .
Then check the running status
zhangyideMacBook-Pro:jar objcat$ docker-compose ps
Name Command State Ports
--------------------------------------------------------------------------------
jar_service- /bin/sh -c echo Up 0.0.0.0:8082->8082/tcp
a_1_5827fa3204e8 192.168.1. ...
We can see that the service is up and running, let's try to visit
http://localhost:8082/hello
Success, this part comes to an end.
2. Run multiple services
Some people may ask, how do multiple services work? It's very simple, let's do it here.
Now I have to do is open the three services service-a
, service-b
, service-eureka
, respectively, two and a registry service.
Let's improve itdocker-compose.yml
version: '2'
services:
service-eureka:
image: java
volumes:
- /Users/objcat/jar/service-eureka.jar:/usr/local/service-eureka.jar
ports:
- 8081:8081
command:
- /bin/sh
- -c
- |
echo 192.168.1.126 servicehost >> /etc/hosts
java -jar /usr/local/service-eureka.jar
service-a:
image: java
volumes:
- /Users/objcat/jar/service-a.jar:/usr/local/service-a.jar
ports:
- 8082:8082
command:
- /bin/sh
- -c
- |
echo 192.168.1.126 servicehost >> /etc/hosts
java -jar /usr/local/service-a.jar
service-b:
image: java
volumes:
- /Users/objcat/jar/service-b.jar:/usr/local/service-b.jar
ports:
- 8083:8083
command:
- /bin/sh
- -c
- |
echo 192.168.1.126 servicehost >> /etc/hosts
java -jar /usr/local/service-b.jar
Let's run it
zhangyideMacBook-Pro:jar objcat$ docker-compose up -d
jar_service-a_1_5827fa3204e8 is up-to-date
Creating jar_service-b_1_f7d01c317fab ... done
Creating jar_service-eureka_1_57bb1a079e9b ... done
zhangyideMacBook-Pro:jar objcat$ docker-compose ps
Name Command State Ports
---------------------------------------------------------------------------------------------------
jar_service-a_1_5827fa3204e8 /bin/sh -c echo 192.168.1. ... Up 0.0.0.0:8082->8082/tcp
jar_service-b_1_be87c0458e53 /bin/sh -c echo 192.168.1. ... Up 0.0.0.0:8083->8083/tcp
jar_service-eureka_1_933d5a60af31 /bin/sh -c echo 192.168.1. ... Up 0.0.0.0:8081->8081/tcp
zhangyideMacBook-Pro:jar objcat$
Registry
Visit service-a
Visit service-b
At this point we can already run multiple services! ! ! ! End of class--
Command expansion
Out of service
zhangyideMacBook-Pro:jar objcat$ docker-compose stop
Stopping jar_service-eureka_1_933d5a60af31 ... done
Stopping jar_service-b_1_be87c0458e53 ... done
Stopping jar_service-a_1_5827fa3204e8 ... done
Stop a single service
zhangyideMacBook-Pro:jar objcat$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
94c3472638c8 java "/bin/sh -c 'echo 19…" 16 minutes ago Up 6 seconds 0.0.0.0:8081->8081/tcp jar_service-eureka_1_933d5a60af31
8d1dfa0ec642 java "/bin/sh -c 'echo 19…" 16 minutes ago Up 6 seconds 0.0.0.0:8083->8083/tcp jar_service-b_1_be87c0458e53
6be5f9d8f423 java "/bin/sh -c 'echo 19…" 33 minutes ago Up 6 seconds 0.0.0.0:8082->8082/tcp jar_service-a_1_5827fa3204e8
zhangyideMacBook-Pro:jar objcat$ docker stop 6be5f9d8f423
6be5f9d8f423
Re-run the service
zhangyideMacBook-Pro:jar objcat$ docker-compose up -d
jar_service-b_1_be87c0458e53 is up-to-date
Starting jar_service-a_1_5827fa3204e8 ...
Starting jar_service-a_1_5827fa3204e8 ... done
zhangyideMacBook-Pro:jar objcat$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
94c3472638c8 java "/bin/sh -c 'echo 19…" 19 minutes ago Up 3 minutes 0.0.0.0:8081->8081/tcp jar_service-eureka_1_933d5a60af31
8d1dfa0ec642 java "/bin/sh -c 'echo 19…" 19 minutes ago Up 3 minutes 0.0.0.0:8083->8083/tcp jar_service-b_1_be87c0458e53
6be5f9d8f423 java "/bin/sh -c 'echo 19…" 36 minutes ago Up 13 seconds 0.0.0.0:8082->8082/tcp jar_service-a_1_5827fa3204e8
We can see that it docker-compose
will automatically recognize the open state of the container and open the one that needs to be opened for us
3. Content supplement
1. Maybe many people do echo 192.168.1.126 servicehost >> /etc/hosts
n't understand me very well
Let me talk about it here. The meaning of this command is to add the servicehost
domain name to the hosts
file. The purpose is to let me eureka
discover my service, not to write the address in the configuration file. The service corresponding to this command is configured as
server:
#服务端口号
port: 8082
spring:
application:
#服务名称 - 服务之间使用名称进行通讯
name: service-objcat-a
eureka:
client:
service-url:
#填写注册中心服务器地址
defaultZone: http://servicehost:8081/eureka
#是否需要将自己注册到注册中心
register-with-eureka: true
#是否需要搜索服务信息
fetch-registry: true
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${server.port}
management:
endpoints:
web:
exposure:
include: "*"