In the previous article , a PHP runtime environment was created, and now a MySQL service is required to run directly:
root@thinkpad:~# docker run --name rocket-mysql -v /home/rocketfish/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7
It will automatically download and get a service image with mysql version 5.7.
At this time, to run a web service, we need to run docker run twice. What if there are more web containers or other service containers?
Docker officially advocates that a container only provides one service, and multiple services/containers can be managed using docker-compose.
docker-compose itself is a tool written in Python and can be installed directly through pip:
root@thinkpad:~# sudo pip install --upgrade pip
If you do not have a local Python environment, you can also use the docker image of docker-compose to run:
root@thinkpad:/home/compose-web# curl -L https://github.com/docker/compose/releases/download/1.8.0/run.sh > /usr/local/bin/docker-compose root@thinkpad:/home/compose-web# docker-compose --version #Start downloading the mirror, it is recommended to use the first one
View help
root@thinkpad:~# docker-compose -h Define and run multi-container applications with Docker. Usage: docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...] docker-compose -h|--help Options: -f, --file FILE Specify an alternate compose file (default: docker-compose.yml) -p, --project-name NAME Specify an alternate project name (default: directory name) --verbose Show more output -v, --version Print version and exit -H, --host HOST Daemon socket to connect to --tls Use TLS; implied by --tlsverify --tlscacert CA_PATH Trust certs signed only by this CA --tlscert CLIENT_CERT_PATH Path to TLS certificate file --tlskey TLS_KEY_PATH Path to TLS key file --tlsverify Use TLS and verify the remote --skip-hostname-check Don't check the daemon's hostname against the name specified in the client certificate (for example if your docker host is an IP address) Commands: build Build or rebuild services bundle Generate a Docker bundle from the Compose file config Validate and view the compose file create Create services down Stop and remove containers, networks, images, and volumes events Receive real time events from containers exec Execute a command in a running container 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 push Push 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 version Show the Docker-Compose version informationThe first thing to do is to create docker-compose.yml, which is a document in YAML format
mkdir docker cd docker mkdir web mkdir db vim docker-compose.yml
The content is as follows:
version: '2'
services:
db:
image: mysql:5.7
ports:
- "3306:3306"
restart: always
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: web
MYSQL_USER: root
MYSQL_PASSWORD: rootweb
volumes:
- ./db/data:/var/lib/mysql
web:
depends_on:
- db
image: nginx-php-fpm:phalcon
ports:
- "80:80"
restart: always
environment:
WEB_DB_HOST: db:3306
WEB_DB_PASSWORD: root
volumes:
- ./web/html:/var/www/html/
links:
- db
The environment here defines environment variables, such as the host's MAC, which can be obtained in the system variables of the docker container. Each programming language has a method for obtaining system environment variables. These variables can also be organized and loaded into files, see
here
.
The volumes here define the files or folders or data containers to be mapped into the container, you can refer to here . Note that if multiple containers share the same directory, there will be write conflicts, such as MySQL, multiple instances, and the data directory needs to be separated. So design your program, which part needs to be read-only (can be shared), and which part needs to be written; whether the written part can be placed in its own temporary directory, or other public services.
run and view
root@thinkpad:/home/compose-web# docker-compose up -d
Creating network "composeweb_default" with the default driver
Creating composeweb_db_1
Creating composeweb_web_1
root@thinkpad:/home/compose-web# docker-compose ps
Name Command State Ports
------------------------------------------------------------------------------------
composeweb_db_1 docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp
composeweb_web_1 /start.sh Up 443/tcp, 0.0.0.0:80->80/tcp
#也可以使用原来的命令
root@thinkpad:/home/compose-web# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
efbdaf257748 nginx-php-fpm:phalcon "/start.sh" 13 seconds ago Up 11 seconds 0.0.0.0:80->80/tcp, 443/tcp composeweb_web_1
a6935d20911e mysql:5.7 "docker-entrypoint.sh" 14 seconds ago Up 13 seconds 0.0.0.0:3306->3306/tcp composeweb_db_1
docker-compose up -d will start and run all containers in the background.
docker-compose only provides management of multiple docker services, and the original docker commands can still be run on these containers. Check the IP of the web container:
root@thinkpad:/home/compose-web# docker inspect composeweb_web_1 | grep IPAddress "SecondaryIPAddresses": null, "IPAddress": "", "IPAddress": "172.18.0.3",Then visit: http://172.18.0.3/ to see the web page.
If you want to stop the service, you can use docker-compose stop:
root@thinkpad:/home/compose-web# docker-compose stop db
Stopping composeweb_db_1 ... done
root@thinkpad:/home/compose-web# docker-compose stop web
Stopping composeweb_web_1 ... done
Start it again and get the service with the same name:
root@thinkpad:/home/compose-web# docker-compose up -d
Starting composeweb_db_1
Starting composeweb_web_1
各个容器的名称也可以在配置文件里面通过参数container_name来指定。
docker-compose的配置还有很多其他的参数,可以参考这里。比如刚才我们通过docker inspect来查找容器IP,也可以配置成静态IP:
version: '2'
services:
app:
image: nginx-php-fpm:phalcon
networks:
app_net:
ipv4_address: 172.18.0.10
ipv6_address: 2001:3984:3989::10
networks:
app_net:
driver: bridge
driver_opts:
com.docker.network.enable_ipv6: "true"
ipam:
driver: default
config:
- subnet: 172.18.0.0/24
gateway: 172.18.0.1
- subnet: 2001:3984:3989::/64
gateway: 2001:3984:3989::1
也可以
自定义网络。
刚才我们的配置文件里面直接指定了镜像,也可以指定Dockerfile镜像构建,假如Dockerfile在web目录下面:
build: context: ./webOr specify the Dockerfile directly:
build: context: . dockerfile: Dockerfile-alternateIn fact, the configuration of docker-compose has already covered the configuration items of Dockerfile , and can also be directly used to build multiple docker services, such as specifying running commands
command: [/bin/bash]
If the entry program here cannot run continuously, docker will exit after the operation is completed. So if you need a docker container that can run in the background, the entry program must keep a program running in the foreground without exiting, such as running a crontab image, the entry program can be:
cron && bash
The cron program runs in the background. If it does not cooperate with the foreground program bash, the container execution and cron will exit. Most other containers running crontab force a program not to exit in the foreground, such as this :
cron && tail -f /var/log/cron.log
Docker compose can also be combined with Docker Swarm .
参考链接:
Install Docker Compose
Quickstart: Docker Compose and WordPress
YAML 模板文件
Introduction to Docker Compose Tool for Multi-Container Applications
Dockerfile基本结构
How to Get Environment Variables Passed Through docker-compose to the Containers