Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
67 views
in Technique[技术] by (71.8m points)

java - Docker files to docker compose

Im new at docker. I am making a java sockets music server and i have 2 files. Client.java and Server.java. Both are in separate containers. To mention, i run both services in command line

Docker files

FROM java:8
COPY Server.java /
RUN javac Server.java
EXPOSE 25000
ENTRYPOINT ["java"]
CMD ["Server"]
FROM java:8
COPY Client.java /
RUN javac Client.java
EXPOSE 25000
ENTRYPOINT ["java"]
CMD ["Client"]

i create also a network for these two to communicate.

docker network create client_server_network

and i run the images as follows:

docker run --env SERVER_HOST_ENV=server --network-alias server --network client_server_network -it server

docker run --network client_server_network -it clientimage

Now i want to create a docker compose file with those two Dockerfiles and the network. This is what i have done so far:

version:'3'
services:
  client:
    image: java:8
    ports:
      -25000:25000
    network:
      default:
        name: client_server_network  
    

  server:
    image: java:8
    ports:
      -25000:25000
    environment:
      -SERVER_HOST_ENV=server 
    network:

My question is how to add the common network in both services. Also is the way i write my docker compose file correct?

question from:https://stackoverflow.com/questions/65641622/docker-files-to-docker-compose

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Below is the code, by default both container is added to network named net and it must provide in the last line also

version:'3'
services:
  client:
    image: java:8
    ports:
      - 25000:25000
    networks:
      - client_server_network  
    

  server:
    image: java:8
    ports:
      - 25000:25000
    environment:
      - "SERVER_HOST_ENV=server" 
    networks:
      - client_server_network
networks:
  client_server_network:

Note that the network should be specified commonly at last in the same level of services and version. As indenting is a main factor in docker-compose. if it is incorrectly indent may shows error.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...