从另一个容器调用docker容器

时间:2016-03-06 18:57:41

标签: docker jetty docker-compose dockerfile microservices

我已经部署了两个docker容器,它们托管了两个部署在Jetty中的REST服务。

Container 1 hosts service 1 and it Listens to 7070
Container 2 hosts service 2 and it  Listens to 9090

终点: -

service1:
/ping
/service1/{param}

service2:
/ping
/service2/callService1

curl -X GET http://localhost:7070/ping [Works]
curl -X GET http://localhost:7070/service1/hello [Works]
curl -X GET http://localhost:9090/ping [Works]

我已经按照以下方式配置容器:

http://localhost:9090/serivce2/callService1 
calls 
http://localhost:7070/service1/hello

这会抛出连接拒绝异常。这是我的配置。

docker-compose.yml
------------------
service1: 
  build: microservice/
  ports: 
    - "7070:7070"
  expose:
    - "7070"
service2:
 build: microservice_link/
 ports:
 - "9090:9090"
 expose: 
 - "9090"
 links:
 - service1

service1 Dockerfile
-------------------
FROM localhost:5000/java:7
COPY ./target/service1.jar /opt
WORKDIR /opt
ENTRYPOINT ["java", "-jar", "service1.jar","7070"]
CMD [""]

service2 Dockerfile
-------------------
FROM localhost:5000/java:7
COPY ./target/service2.jar /opt
WORKDIR /opt
ENTRYPOINT ["java", "-jar", "service2.jar","9090"]
CMD [""]

docker info
-----------
root@LT-NB-108U:~# docker info
Containers: 3
 Running: 2
 Paused: 0
 Stopped: 1
Images: 12
Server Version: 1.10.1
Storage Driver: aufs
 Root Dir: /var/lib/docker/aufs
 Backing Filesystem: extfs
 Dirs: 28
 Dirperm1 Supported: false
Execution Driver: native-0.2
Logging Driver: json-file
Plugins: 
 Volume: local
 Network: null host bridge
Kernel Version: 3.13.0-48-generic
Operating System: Ubuntu precise (12.04.5 LTS)
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 3.47 GiB
Name: LT-NB-108U
ID: BS52:XURM:3SD7:TC3R:7YVA:ZBZK:CCL2:7AVC:RNZV:RBGW:2X2T:7C46
WARNING: No swap limit support
root@LT-NB-108U:~# 

问题: -

我正在尝试从容器2访问容器1中部署的端点。但是,我得到连接拒绝异常。 我尝试在容器2中暴露端口7070.这没有用。

1 个答案:

答案 0 :(得分:1)

curl http://service1:7070/ 

使用 - host1_name:inner_port_of_host1

该主机在容器 2 中称为“service1”。将其用作主机名,端口是 service1 容器中的内部端口侦听器。

如果您在 service1 上有一个快速服务器,请侦听端口 7070。