今天實(shí)驗(yàn)了下 docker 下的網(wǎng)絡(luò)設(shè)置,記錄一下過程,以免后面忘記。
(系統(tǒng):Centos 7.4 ,docker 版本:18.03.1-ce, docker-compose version 1.18.0)
cat docker-compose.yml
version: '3'
services:
test1:
image: busybox:latest # 鏡像為 busybox
entrypoint: # 容器啟動(dòng)后執(zhí)行 top 命令,使容器沒法立即退出
- top
networks:
backend: # 使用指定的網(wǎng)絡(luò) backend, 并且設(shè)置網(wǎng)絡(luò)別名為 test1,
aliases: # 設(shè)置網(wǎng)絡(luò)別名后,可以在其他容器中 ping test1 訪問到該容器
- test1
test2:
image: busybox:latest
entrypoint:
- top
networks:
backend:
aliases:
- test2
networks:
backend:
啟動(dòng)
docker-compose up -d
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4d05ceb2088d busybox:latest "top" 5 seconds ago Up 4 seconds ibaboss_test2_1
f4ccafa24664 busybox:latest "top" 5 seconds ago Up 4 seconds ibaboss_test1_1
docker exec -it 4d05ceb2088d /bin/sh
/ # ping test1
PING test1 (172.19.0.2): 56 data bytes
64 bytes from 172.19.0.2: seq=0 ttl=64 time=0.061 ms
64 bytes from 172.19.0.2: seq=1 ttl=64 time=0.062 ms
ping ibaboss_test1_1
PING ibaboss_test1_1 (172.19.0.2): 56 data bytes
64 bytes from 172.19.0.2: seq=0 ttl=64 time=0.045 ms
64 bytes from 172.19.0.2: seq=1 ttl=64 time=0.056 ms
64 bytes from 172.19.0.2: seq=2 ttl=64 time=0.061 ms
# 在網(wǎng)絡(luò)中可以通過 容器名字或者網(wǎng)絡(luò)的別名 進(jìn)行通信
Compose 的容器名稱格式是:<項(xiàng)目名稱><服務(wù)名稱><序號(hào)>
雖然可以自定義項(xiàng)目名稱、服務(wù)名稱,但是如果你想完全控制容器的命名,可以使用這個(gè)標(biāo)簽指定:
container_name: app
cat docker-compose_v1.yml
version: '3'
services:
test1:
image: busybox:latest
entrypoint:
- top
container_name: test1
networks:
- backend
test2:
image: busybox:latest
entrypoint:
- top
container_name: test2
networks:
- backend
networks:
backend:
啟動(dòng)
docker-compose -f docker-compose_v1.yml up -d
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
132859fc77c2 busybox:latest "top" About a minute ago Up About a minute test2
cd0a78dc9bd4 busybox:latest "top" About a minute ago Up About a minute test1
docker exec -it 132859fc77c2 ping test1
PING test1 (172.19.0.2): 56 data bytes
64 bytes from 172.19.0.2: seq=0 ttl=64 time=0.070 ms
64 bytes from 172.19.0.2: seq=1 ttl=64 time=0.068 ms
64 bytes from 172.19.0.2: seq=2 ttl=64 time=0.059 ms
補(bǔ)充:
docker compose 多容器共用網(wǎng)絡(luò)問題
前幾天計(jì)劃使用docker compose 編寫一個(gè)多容器共用一個(gè)容器網(wǎng)絡(luò),結(jié)果總是報(bào)錯(cuò),今天又試了一下才發(fā)現(xiàn)是端口綁定的問題造成的,端口只能綁定在產(chǎn)生網(wǎng)卡的容器上,綁定在其它窗口上就會(huì)報(bào)錯(cuò),如下代碼 mysql服務(wù)共用了nginx的網(wǎng)卡,在mysql里使用了port:3306時(shí)就會(huì)運(yùn)行失敗,將3306移到nginx上啟動(dòng)就成功了。
version: '3.3'
services:
nginx:
image: "lnp_php"
# container_name: "lnmp_nginx"
ports:
- "80:80"
- "443:443"
- "3306:3306"
expose:
- "3306"
volumes:
- /home/www/php:/home/www:rw
# depends_on:
# - mysql
# links:
# - mysql
mysql:
image: "mysql"
# container_name: "lnmp_mysql"
# ports:
# - "3306:3306"
# expose:
# - "3306"
volumes:
- /home/docker/conf/mysql_w:/etc/mysql:rw
environment:
- MYSQL_ROOT_PASSWORD=123456
depends_on:
- nginx
network_mode: "service:nginx"
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。