You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker新手咨询:Java Socket音乐服务器双容器的Docker Compose文件配置问题

Hey there! Let's get your Docker Compose setup sorted out properly—you're close, but there are a few syntax and configuration issues to fix. Let's break this down step by step.

First, Let's Fix Your Current Compose File's Issues

Your existing YAML has several problems that will prevent it from working:

  • Syntax errors: Missing spaces after colons (e.g., version:'3' should be version: '3'), invalid port formatting (-25000:25000 needs a space and quotes like - "25000:25000"), and using network (singular) instead of networks (plural).
  • No build logic: You're using the raw java:8 image directly, but this won't include your compiled Client.java/Server.java files like your manual Dockerfiles did.
  • Unnecessary port mappings: Container-to-container communication doesn't require exposing ports to the host machine—you only need ports mapped if you want external devices to access the server.
  • Missing interactive mode: Your manual docker run uses -it to keep the terminal alive; Compose needs equivalent settings for your client container.

Correct Docker Compose Configuration

Assuming your project structure looks like this (matching your separate Dockerfiles):

your-project/
├── server/
│   ├── Dockerfile
│   └── Server.java
├── client/
│   ├── Dockerfile
│   └── Client.java
└── docker-compose.yml

Here's the fixed Compose file that mirrors your manual setup:

version: '3.8'  # Use a modern, well-supported version
services:
  server:
    build: ./server  # Tells Compose to build using the Dockerfile in the server directory
    environment:
      - SERVER_HOST_ENV=server  # Matches your manual env var setup
    networks:
      - client_server_network
    # No ports needed here unless you want external access to the server

  client:
    build: ./client  # Builds from the client directory's Dockerfile
    environment:
      - SERVER_HOST_ENV=server  # Make sure your Client.java reads this var to connect!
    networks:
      - client_server_network
    stdin_open: true  # Equivalent to `-i` in docker run
    tty: true         # Equivalent to `-t` in docker run—keeps the client terminal active

# Define the shared custom network (matches your manual network name)
networks:
  client_server_network:
    name: client_server_network
    driver: bridge  # Default network driver, explicitly set for clarity

Key Details Explained

  1. Shared Network: The top-level networks block creates (or reuses, if it already exists) your client_server_network. Both services are attached to this network, so they can communicate using their service names as hostnames—your client can connect to server:25000 directly.
  2. Build Configuration: Instead of referencing pre-built images, build: ./server and build: ./client tell Compose to use your existing Dockerfiles to build the images automatically when you run docker-compose up --build.
  3. Interactive Mode: stdin_open: true and tty: true ensure your client container doesn't immediately exit, just like the -it flag in your manual docker run command.

How to Run It

Just run this command from your project directory:

docker-compose up --build

This will build both images (if they don't exist), create the network (if it doesn't already exist), and start both containers.

Quick Check for Your Java Code

Make sure your Client.java is reading the SERVER_HOST_ENV environment variable to determine the server's hostname. For example:

String serverHost = System.getenv("SERVER_HOST_ENV");
Socket socket = new Socket(serverHost, 25000);

内容的提问来源于stack exchange,提问作者william12

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 04:22:51