这里在另一台CentOS虚拟机(192.168.30.206)上进行,同样需要配置仓库的地址insecure-registries
并且登录 docker login
,在这不重复了。
创建docker网络
为了让不同容器间可以进行通讯,我们需要创建一个虚拟网络
docker network create my_network
默认是桥接方式
docker netwokr ls
可以查看已有的网络,包括刚创建的my_network
NETWORK ID NAME DRIVER SCOPE
8af18841665a bridge bridge local
d1e0ccc27f72 docker_gwbridge bridge local
b3fbea323302 host host local
bb48f115670d my_network bridge local
870a1a30994b none null local
docker inspect my_network
可以查看某个网络的详细配置
[
{
"Name": "my_network",
"Id": "bb48f115670d0dbbb28388023f1f9267d201a026a0f96e272baeeb5155a04a50",
"Created": "2018-12-20T08:00:12.7584478Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.19.0.0/16",
"Gateway": "172.19.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {},
"Options": {},
"Labels": {}
}
]
拉取镜像
docker pull 192.168.30.205:5000/discovery:0.0.1-SNAPSHOT
docker images
可以查看镜像列表
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.30.205:5000/discovery 0.0.1-SNAPSHOT 7f47d9c96ff8 33 minutes ago 129MB
创建并启动容器
docker run -d -p 8761:8761 --name discovery --network my_network 192.168.30.205:5000/discovery:0.0.1-SNAPSHOT
-d
后台启动
-p
指定端口
--name
指定镜像名称 这个名称要跟工程中的defaultZone: http://discovery:8761/eureka/
的discovery
一致
--network
指定网络
查看进程
docker ps
查看运行中的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6189fd4ad892 192.168.30.206:5000/discovery:0.0.1-SNAPSHOT "java -jar /discover…" 4 seconds ago Up 2 seconds 0.0.0.0:8761->8761/tcp discovery
docker logs -f discovery
可以查看某个容器的运行日志
编写docker-compose文件
如果每个服务都需要docker pull,docker run 那就太麻烦了,所以我们可以编写一个docker-compose.yml,统一配置。
docker-compose.yml
version: '3'
services:
discovery:
image: 192.168.30.205:5000/discovery:0.0.1-SNAPSHOT
ports:
- "8761:8761"
networks:
- my_network
provider-user:
image: 192.168.30.205:5000/provider-user:0.0.1-SNAPSHOT
ports:
- "8762:8762"
networks:
- my_network
provider-order:
image: 192.168.30.205:5000/provider-order:0.0.1-SNAPSHOT
ports:
- "8763:8763"
networks:
- my_network
#指定网络
networks:
my_network:
##使用已有的网路
external:
name: my_network
docker-compose -f docker-compose.yml up -d
启动后会自动拉取镜像并运行
Pulling discovery (192.168.30.205:5000/discovery:0.0.1-SNAPSHOT)...
0.0.1-SNAPSHOT: Pulling from discovery
4fe2ade4980c: Pull complete
6fc58a8d4ae4: Pull complete
819f4a45746c: Pull complete
b2fcdb18b297: Pull complete
Pulling provider-user (192.168.30.205:5000/provider-user:0.0.1-SNAPSHOT)...
0.0.1-SNAPSHOT: Pulling from provider-user
4fe2ade4980c: Already exists
6fc58a8d4ae4: Already exists
819f4a45746c: Already exists
0900a6bf8880: Pull complete
Pulling provider-order (192.168.30.205:5000/provider-order:0.0.1-SNAPSHOT)...
0.0.1-SNAPSHOT: Pulling from provider-order
cd784148e348: Pull complete
35920a071f91: Pull complete
92e4d568ad31: Pull complete
4bf5a484ac0b: Pull complete
Creating provider-user ... done
Creating provider-order ... done
Creating discovery ... done
等待一会,访问 http://192.168.30.206:8761/ 可以看到两个服务已经注册上来,因为启动顺序不确定,所以provider-user
和provider-order
可能会找不到注册中心,等discovery
起来了就好了
访问 http://192.168.30.206:8763/orders/users/2 可以看到两个微服务间也能成功调用