This is a study note from a tutorial video in YouTube: https://www.youtube.com/watch?v=fqMOX6JJhGo&t=4723s
Docker compose is used to run several differences services in one .yml file.
docker run WeibotopHistory
docker run mariadb
docker-compose.yml
services:
web:
image: "WeibotopHistory"
database:
image: "mariadb"
Use "docker-compose" command to bring up all the application stacks in yml files.
docker-compose up
How to compose yml files?
To explain how to compose the yml files, we use the voting app example. The structure of voting app is like below:
These connections between difference services can be described using "run --link" command like below:
docker run -d --name=redis redis
docker run -d --name=db postgres:9.4
docker run -d --name=vote -p 5000:80 --link redis:redis voting-app
docker run -d --name=result -p 5001:80 --link db:db result-app
docker run -d --name=worker --link db:db --link redis:redis worker
Above commands can be described in yml file like below:
docker-compose.yml
redis: # This is the name of container, it is
# corresponding to "--name" in "run" command
image: redis # This is the image name
db:
image: postgres:9.4
vote:
image: voting-app
ports: # This is corresponding to "-p" in "run" command
- 5000:80
links: # This is corresponding to "--link" in "run" command
- redis
result:
image: result
ports:
- 5001:80
links:
- db
worker:
image: worker
links:
- redis
- db
However if some of the images have not been built yet, you can use "build" keywords instead of "image" to build and run. For example, since the voting-app, result app, and worker app are written by developer, they may not be built into images yet. Then we can modify above yml file to build the images.
redis:
image: redis
db:
image: postgres:9.4
vote:
build: ./vote # Here will build the image with directory "vote"
ports:
- 5000:80
links:
- redis
result:
build: ./result
ports:
- 5001:80
links:
- db
worker:
build: ./worker
links:
- redis
- db
Above format is for yml version 1.
In version 2, there are some differences:
- All the containers need to be defined in "services" section;
- If you need dependencies, you can use "depends_on" attribute to specify the services that need to be run before this service;
For example, the above yml can be modified in version 2 like below:
version: '2' # Here needs to specify the version using string
services: # All services need to be put under "services"
redis:
image: redis
db:
image: postgres:9.4
vote:
image: voting-app
ports:
- 5000:80
depends_on: # Here to tell to run redis before run this
- redis
result:
image: result
ports:
- 5001:80
depends_on:
- db
worker:
image: worker
depends_on:
- redis
- db
Version 3 is similar to version 2. However some tags are removed and simplified.
Networking in docker compose
Let's use an example directly.
If you want to create a network structure like above, you need to add"networks" tag in the yml files.
version: 2
services:
redis:
image: redis
networks: # Here needs to specify the network you want
- back-end # to use from the networks defined
db:
image: postgres:9.4
vote:
image: voting-app
ports:
- 5000:80
depends_on:
- redis
networks:
- front-end
- back-end
result:
image: result
ports:
- 5001:80
depends_on:
- db
networks:
- front-end
- back-end
worker:
image: worker
depends_on:
- redis
- db
networks:
- back-end
networks: # Here to define the networks
front-end:
back-end:
Reference: https://www.youtube.com/watch?v=fqMOX6JJhGo&t=4723s
Comments
Post a Comment