Functions of docker-compose
Docker-comopse can help us quickly build a development environment. For example, you can deploy redis, MongoDB, rabbitmq, MySQL, Eureka, and configserver on the local machine once, and then let them be the basis of other projects, which can be implemented.
Communication between containers-links
Since each docker instance is a closed environment, it cannot be shared by default, that is, your rabbit container cannot connect to your redis container, your configserver cannot connect to your Eureka container. If you want to allow data communication between them, you need to setlinks
When connecting to the Local Machine (host machine), you can use localhost and port to access these containers.
Startup sequence-depends_on
For the startup sequence, for example, if your configserver depends on Eureka and you want to start the dependent container before running yourself, you can use the depends_on attribute to implement this, of course, it is only the starting order, and it cannot be guaranteed that the service will start the other one when it comes. The solution is to useRetry upon failure
Mechanismrestart: on-failure
When the configserver fails, you can restart until it is successful (the master is until Eureka is started ).
Container and container must communicate with each other by service name
If we want to deploy a specific project in docker-compose and want to access other services, we need to use the service name defined in docker-compose instead of localhost, because when your container is up, its localhost is its own container instead of the host machine. If you want to access the container, you can uselocalhost
This is already mentioned in the previous article.
The following is an example of how to deploy the development environment.
Version: "3.3" services: # configurations related to public components MongoDB: Image: Mongo: 3.4.10 ports:-"27017: 27017" networks:-dev volumes:-pai_data: /data/DB redis: Image: redis: 3.2-Alpine networks:-dev ports:-"6379: 6379" volumes:-redis_data:/Data Rabbit: Image: rabbitmq: 3.6.10-management-Alpine hostname: Rabbit ports:-"5672: 5672"-"15672: 15672"-"61613: 61613" networks:-dev environment: rabbitmq_default_vhost: pilipa volumes: -rabbitmq_data:/var/lib/rabbitmq eurekaserver: Build :. /springcloud/Eureka-server restart: On-failure ports:-"8761: 8761"-"8762: 8762" networks:-dev configserver: Build :. /springcloud/config-server restart: On-failure ports:-"8888: 8888"-"8889: 8889" networks:-dev depends_on:-eurekaserver # dependent service links: -eurekaserver environment: spring_profiles_active: devops volumes:-/users/Lind. zhang/project/config-repo:/config_repo # The front is the local path, and the back is the path in the container. In configserver, the following container path is configured.
Start and update
- Image first
docker-compose build
- Restart the service
Docker-Compose up-D #-D is run in the background
- Stop Service
docker-compose down
- View container logs
Docker logs-F container ID
I hope this article will help you!
Use docker-compose to deploy the development environment