- An example docker-compose.yml
services: elasticsearch: image: elasticsearch:7.14.2 container_name: es environment: discovery.type: single-node ES_JAVA_OPTS: "-Xms512m -Xmx512m" ports: - "9200:9200" - "9300:9300" healthcheck: test: ["CMD-SHELL", "curl --silent --fail localhost:9200/_cluster/health || exit 1"] interval: 10s timeout: 10s retries: 3 networks: - elastic logstash: image: logstash:7.14.2 container_name: log environment: discovery.seed_hosts: logstash LS_JAVA_OPTS: "-Xms512m -Xmx512m" volumes: - ./logstash/pipeline/logstash-nginx.config:/usr/share/logstash/pipeline/logstash-nginx.config - ./logstash/nginx.log:/home/nginx.log ports: - "5000:5000/tcp" - "5000:5000/udp" - "5044:5044" - "9600:9600" depends_on: - elasticsearch networks: - elastic command: logstash -f /usr/share/logstash/pipeline/logstash-nginx.config kibana: image: kibana:7.14.2 container_name: kib ports: - "5601:5601" depends_on: - elasticsearch networks: - elastic networks: elastic: driver: bridge
2.1 image
- web user defined service name
- Image can be specified as the image name or image ID. if it does not exist locally, it will automatically try to pull
services: web: image: redis
2.2 build
- The service startup can be based on 2.1 image or Dockerfile. The build task will be executed when the service is started up.
- The sub tag context sets the context root directory
- The sub tag Dockerfile specifies the Dockerfile file
- The child tag args specifies the environment variables used during the build
services: web: build: context: ../ dockerfile: Dockerfile args: - buildno=1 - password=123456
- Specifying both the image and build tags will name the built image after the image
services: web: build: . image: webapp:tag
2.3 command
- Using command overrides the command to be executed by default after the image is started
services: web: command: redis-server /etc/redis/redis.conf --appendonly yes
services: web: command: ["redis-server", "/etc/redis/redis.conf", "--appendonly", "yes"]
2.4 container_name
- The format of docker compose startup container name is: < project name > < service name > < serial number >
- You can also use container_name custom container name
services: web: container_name: redis-6379
2.5 depends_on
- Generally, in a project, the container needs to have a specific start sequence, otherwise it will fail to start due to dependency problems.
- Example: if you start the application container without starting the database, the application container will exit because the database cannot be found
- Solution: dependencies_ on
services: web: build: . depends_on: - db - redis redis: image: redis:latest db: image: mysql:5.7
2.6 dns
- The same purpose as the -- dns parameter in docker run
services: web: dns: 8.8.8.8
services: web: dns: - 8.8.8.8 - 114.114.114.114
2.7 entrypoint
- There is an instruction entrypoint in Dockerfile to specify the access point. The definition of entrypoint in docker-compose.yml will overwrite the definition in Dockerfile
services: web: entrypoint: java -jar app.jar --spring.profiles.active=release
services: web: entrypoint: - java - -jar - app.jar - --spring.profiles.active=release
2.8 environment
- It is equivalent to the function of docker run -e
services: mysql: image: mysql environment: - MYSQL_ROOT_PASSWORD=123456 - MYSQL_ALLOW_EMPTY_PASSWORD=yes
2.9 extra_hosts
- Adding the host name mapping is equivalent to adding records to / etc/hosts, and is equivalent to the function of docker run -- add host
services: web: extra_hosts: - webnode1:192.168.171.100 - webnode2:192.168.171.101
- This is what / etc/hosts in the container can see
192.168.171.100 webnode1 192.168.171.101 webnode2
2.10 ports
- The port mapping label is equivalent to the docker run -p function
services: web: ports: - 80:80 - 1180:1180
2.11 volumes
- Mounting a directory or an existing data volume container is equivalent to the docker run -v function
- HOST:CONTAINER HOST:CONTAINER:ro,: ro means read-only, and the default is read-write
services: web: volumes: - /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime:ro - ./webdist:/home/webdist
2.12 networks
- Join the specified network
services: web: networks: - web-net - backend-net
2.13 cap_add,cap_drop
- Add or remove the kernel functionality of the container
2.14 cgroup_parent
- Specifies the parent cgroup of the container
2.15 others
- There are other single value Tags: user working_dir hostname privileged restart
services: web: user: mysql working_dir: /home/webdist hostname: webnode privileged: true restart: always