docker-compos联合两个容器
阅读原文时间:2023年07月11日阅读:1

使用网络方式使容器互联

数据库使用两个别名 db mysql

docker network create -d bridge my-bridge-network # 创建一个网络
docker run -itd --name mariadb --env MYSQL_ROOT_PASSWORD=example \
--network=my-bridge-network --network-alias=db --network-alias=mysql mariadb # 启动数据库

docker run -itd --name MyWordPress --network=my-bridge-network --network-alias=lap -p 80:80 wordpress # 启动应用

docker run --name db --env MYSQL_ROOT_PASSWORD=example -d mariadb 启动数据
docker run --name MyWordPress --link db:mysql -p : -d wordpress 启动 LAP

使用网络

version: ''
services:
web:
image: http
networks:
- myappnet1
worker:
image: http
networks:
- myappnet2
db:
image: http
networks:
myappnet1:
aliases:
- database
myappnet2:
aliases:
- mysql
networks:
myappnet1:
myappnet2:

version: ''
services:
web:
image: http
container_name: web
depends_on:

  • db
    ports:
  • "9090:80"
    links:
  • db:mysql
  • db:databases
    db:
    image: mariadb
    container_name: db
    environment:
    MYSQL_ROOT_PASSWORD: somewordpress
    MYSQL_DATABASE: wordpress
    MYSQL_USER: wordpress
    MYSQL_PASSWORD: wordpress

version: ''
services:
web:
image: lap
container_name: web
restart: always
depends_on:
- db
ports:
- "80:80"
networks:
- mynet
volumes:
- /etc/localtime:/etc/localtime:ro
- /home/www:/var/www
db:
image: mariadb
container_name: db
restart: always
networks:
mynet:
aliases:
- mysql
- db
- database
environment:
MYSQL_ROOT_PASSWORD: example
volumes:
- /etc/localtime:/etc/localtime:ro
- /home/mysql/data:/var/lib/mysql
networks:
mynet: