I want to communicate two docker container. I am new on Docker and I tried many different ways.
First I make 2 different Dockerfile
myJavaAPI Dockerfile:
FROM java:8
WORKDIR /app
COPY . .
EXPOSE 4016:23503
CMD java -jar myJavaServerApi.jar
The java API listen on localhost:4016 and serve the data from 127.0.0.1:23503
Other Dockerfile:
FROM python:3.8
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY . .
EXPOSE 23503:23503
CMD ["python","./myDaemonServer.py"]
myDaemonServer get a connection from 23503 and doing some job.
I tried to run these 2 file in my machine and everythings work fine. But when I create Docker These two container can not communicate the myDaemonServer can not see my Java API.
I built my images with these commands:
docker build -t dockerjava .
docker build -t dockerdaemon .
and run it by:
docker run -d <ImageID>
First I run dockerjava afterthat I run dockerdaemon
Also, I tried this command and nothing change:
docker run -p --net=host <DockerImageIDs>
How can I fix this problem?
EDIT: The daemon(python file) listen on 127.0.0.1:23503 and serve it from 5002 port.
CodePudding user response:
You can solve this problem in two ways,
- one is to create a docker-compose.yml file in which you can specify the
networks
Provided the sample docker-compose file below for your app.
version: "3.3"
services:
# Java API
myJavaAPI:
image: "dockerjava"
ports:
- "4016:23503"
restart: "no"
networks:
- localnetwork
# Python Daemon
myDaemonServer:
image: "dockerdaemon"
ports:
- "23503:23503"
restart: "no"
depends_on:
- myJavaAPI
networks:
- localnetwork
networks:
localnetwork:
- Second approach is to use
http://host.docker.internal:23503
in place oflocalhost:23503
in your python code which makes a call to the java api.
But I would recommend you to use the first approach, as that is the neater way of making the containers connect each other.
CodePudding user response:
Two Docker containers can communicate via TCP.
For an easy configuration you can use a single docker-compose.yml:
version: '3.6'
services:
myJavaServer:
container_name: myJavaServer
restart: always
image: ...
ports:
- 80:80
- 443:443
myDaemonServer:
container_name: myDaemonServer
restart: always
image: ...
networks:
default:
driver: bridge
name: mynetwork
If you want to use seperate docker-compose.yml files you must use an external network:
version: '3.6'
services:
myJavaServer:
container_name: myJavaServer
restart: always
image: ...
ports:
- 80:80
- 443:443
networks:
- mynetwork
networks:
mynetwork:
external: true
And for the deamon:
version: '3.6'
services:
myDaemonServer:
container_name: myDaemonServer
restart: always
image: ...
networks:
- mynetwork
networks:
mynetwork:
external: true
If you use two docker-compose.yml files the external network can be created with the command docker network create ...
: https://docs.docker.com/engine/reference/commandline/network_create/
Inside your containers you can now simply communicate by using the name: ping myJavaServer
. Or http://myJavaServer:1234
It is not necessary to map the ports for the communication within the same network (port 1234).
Port mapping is only necessary to expose you server to the outside word (e.g. port 80/443).
In order to start your two containers you can run the command docker-compose up
in the directory of your docker-compose.yml
file.