English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

docker-Compose Detailed Explanation and Example Code

docker-Compose usage example

Build a MySQL using docker + java service + nginx, a total of4A docker container, if using docker run to create containers one by one, it is very麻烦.-The compose tool, only needs to define a docker-The compose.yml file can quickly handle the creation of a group of containers,

mysql:
 image: daocloud.io/yjmyzz/mysql-osx:latest
 volumes:
   - ./mysql/db:/var/lib/mysql
 ports:
   - 3306:3306
 environment:
   - MYSQL_ROOT_PASSWORD=123456
service1:
 image: java:latest
 volumes:
   - ./java:/opt/app
 expose:
   - 8080
 #ports:
 #  - 9081:8080
 links:
   - mysql:default
 command: java -jar /opt/app/spring-boot-rest-framework-1.0.0.jar
service2:
 image: java:latest
 volumes:
   - ./java:/opt/app
 expose:
   - 8080
 #ports:
 #  - 9082:8080
 links:
   - mysql:default
 command: java -jar /opt/app/spring-boot-rest-framework-1.0.0.jar
nginx1:
  image: nginx:latest
  volumes:
   - ./nginx/html:/usr/share/nginx/html:ro
   - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
   - ./nginx/conf.d:/etc/nginx/conf.d:ro
  #expose:
  #  - 80
  ports:
   - "80:80"
  links:
   - service1:service1
   - service2:service2

The content is not complex, see the keywords in the reference article at the end of this article for details.

Question: Where is this file located?

See the directory structure below:

mysql-java-nginx
├── docker-compose.yml
├── java
│  └── spring-boot-rest-framework-1.0.0.jar
├── mysql
│  └── db
└── nginx
  ├── conf
  ├── conf.d
  │  └── default.conf
  ├── html
  │  └── index.html
  └── nginx.conf

Create container:

cd mysql-java-nginx
docker-compose up

It's that simple, one 'up' takes care of creating all containers, 'up' is usually used for the first creation, you can observe the real-time log output of the terminal to judge whether the container starts normally, if there is nothing wrong, directly Ctrl+C exit, then

docker-compose start

Start containers in background mode.  

Other commands include:

Commands:
 build       Build or rebuild services
 help        Get help on a command
 kill        Kill containers
 logs        View output from containers
 pause       Pause services
 port        Print the public port for a port binding
 ps         List containers
 pull        Pulls service images
 restart      Restart services
 rm         Remove stopped containers
 run        Run a one-off command
 scale       Set number of containers for a service
 start       Start services
 stop        Stop services
 unpause      Unpause services
 up         Create and start containers
 migrate-to-labels Recreate containers to add labels
 version      Show the Docker-Compose version information

As the name suggests, it's easy to understand the meaning.  

Reference articles:

https://docs.docker.com/compose/compose-file/

Thank you for reading, I hope it can help everyone. Thank you for your support of this site!

You May Also Like