docker compose details and sample code

  • 2020-05-17 07:17:43
  • OfStack

docker-compose use examples

docker is used to build 1 mysql + java service + nginx, 4 docker containers in total. If docker run is used to create 1 container by 1 container, it is troublesome to create 10 points. In order to create containers in batches more efficiently, docker has launched the docker-compose tool. You only need to define one docker-compose. yml file to quickly create a set 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, the specific keyword explanation see the reference article link at the end of this article.

Q: where is the file?

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 a container:


cd mysql-java-nginx
docker-compose up

up is usually used for the first time. You can observe the log output of the terminal in real time to determine whether the container starts normally. If there is no problem, just exit Ctrl+C, and then

docker-compose start

Start the container in backchannel mode.

Other orders 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

You can basically tell by the name.

Reference article:

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

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: