Reputation: 1691
I have 3 microservices, and I run them with docker.
Dockerfile of each of them. Frontend:
FROM node:alpine
LABEL maintainer="[email protected]"
WORKDIR /usr/app/front
EXPOSE 3000
COPY ./ ./
RUN npm install
CMD ["npm", "start"]
Backend 1 (back):
FROM openjdk:8-jdk-alpine
LABEL maintainer="[email protected]"
VOLUME /tmp
EXPOSE 8099
ARG JAR_FILE=build/libs/auth-0.0.3.jar
ADD ${JAR_FILE} digital.jar
ENTRYPOINT ["java","-jar","/digital.jar"]
Backend 2 (message):
FROM openjdk:8-jdk-alpine
LABEL maintainer="[email protected]"
VOLUME /tmp
EXPOSE 8082
ARG JAR_FILE=build/libs/sender-0.0.1.jar
ADD ${JAR_FILE} sender.jar
ENTRYPOINT ["java","-jar","/sender.jar"]
Frontend send REST-request to backend1, than, backend1 send REST-request to backend2 (message).
I published it on hub & run on external server in docker-compose:
version: '3.7'
services:
web:
image: account/front:0.0.1
restart: on-failure
ports:
- 80:3000
back:
image: account/back:0.0.3
restart: on-failure
ports:
- 8099:8099
message:
image: account/message:0.0.1
restart: on-failure
ports:
- 8082:8082
Backend services run on ports:
message_1_e8eb3b2d2477 | 2019-09-24 09:34:00.882 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8082 (http) with context path ''
back_1_1982cc6e57f7 | 2019-09-24 09:34:07.403 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8099 (http) with context path ''
As we see, each service run on its own port.
Than, I try to send request to front -> back -> message. back send request to message & reseive answer:
java.net.ConnectException: Operation timed out (Connection timed out)
Than, requests to message service not reach it.
When I send request directly with Postman, it works.
What's wrong?
UPD. request from front to back:
http://81.100.122.90:8099/auth/register
body:
{"username":"ksgcf","password":"123","firstName":"John","lastName":"Doe","email":"[email protected]"}
request from back to message (IP changed):
String url = "http://81.100.122.90:8082/email";
EmailMessageDto request = new EmailMessageDto(
dto.getEmail(),
"[email protected]",
"Email confirmation",
"Press link: http://dig.lamb.ru/confirm?username="
+ registrationToken.getUsername() + "&token=" + registrationToken.getToken()
);
So, I see this message when docker-compose run for the first time:
Creating network "project_default" with the default driver
Upvotes: 0
Views: 751
Reputation: 133
First, when you use docker-compose all services are available via there names. So you can access message from back like this
$ docker-compose exec back ping message
PING message (172.24.0.3) 56(84) bytes of data.
64 bytes from message (172.24.0.3): icmp_seq=1 ttl=64 time=0.078 ms
64 bytes from message (172.24.0.3): icmp_seq=2 ttl=64 time=0.068 ms
Second, check port bindings. You have to bind 0.0.0.0 (not localhost which is default for most of the services and frameworks) to access to the service from other containers via network. It's same you get ordinary virtual machines. You can check port availability with telnet
As example I'm checking is postresql available on 5432 from container called superset
$ docker-compose exec superset telnet postgres 5432
Trying 172.24.0.3...
Connected to postgres.
Escape character is '^]'.
Upvotes: 1