docker / compose

Define and run multi-container applications with Docker
https://docs.docker.com/compose/
Apache License 2.0
33.54k stars 5.18k forks source link

[BUG] Communication in the same docker compose #12016

Open wisamidris7 opened 1 month ago

wisamidris7 commented 1 month ago

Description

How to make the communication for containers in the same dockers compose For example I have these containers

  sqlServer:
    container_name: "sqlServer"
    image: "mcr.microsoft.com/mssql/server:2022-latest"
    environment:
      ACCEPT_EULA: "Y"
      MSSQL_SA_PASSWORD: "{Strong Password Here}"
    ports:
    - target: 1433
      published: 1433
    networks:
    - network1
    restart: unless-stopped
  myApp:
    container_name: "myApp"
    image: "myApp:latest"
    environment:
      ConnectionString: "Server=101.24.59.12,1433;User ID=sa;Password=???;TrustServerCertificate=true;Database=myDb"
    ports:
    - target: 8080
      published: 8080
    networks:
    - network2
    restart: unless-stopped

And networks written like this

networks:
  network1:
    driver: bridge
  network2:
    driver: bridge

And this is in ubuntu server so ipaddress was successfully working in Microsoft Sql Server Management Studio Also in empty console application inside the ubuntu server

And when calling it from myApp it was not working and gives me this error

myapp      | Unhandled exception. Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 40 - Could not open a connection to SQL Server)

And it should work because every one is in different network

NOTE: This is ubuntu server server and its working in google cloud and sql server was accessible from my own computer

Steps To Reproduce

I did docker compose up Command to run it

Compose Version

Docker Compose version v2.27.0

Docker Environment

Client: Docker Engine - Community
 Version:    26.1.3
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.14.0
    Path:     /usr/libexec/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.27.0
    Path:     /usr/libexec/docker/cli-plugins/docker-compose

Server:
 Containers: 42
  Running: 25
  Paused: 0
  Stopped: 17
 Images: 52
 Server Version: 26.1.3
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Using metacopy: false
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: systemd
 Cgroup Version: 2
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local splunk syslog
 Swarm: inactive
 Runtimes: runc io.containerd.runc.v2
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 8b3b7ca2e5ce38e8f31a34f35b2b68ceb8470d89
 runc version: v1.1.12-0-g51d5e94
 init version: de40ad0
 Security Options:
  apparmor
  seccomp
   Profile: builtin
  cgroupns
 Kernel Version: 5.15.0-25-generic
 Operating System: Ubuntu 22.04.4 LTS
 OSType: linux
 Architecture: x86_64
 CPUs: 6
 Total Memory: 15.63GiB
 Name: {private data}
 ID: a8e10efe-dee4-42f4-b365-891e189bcd4d
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false

Anything else?

No response

ndeloof commented 1 month ago

For containers to communicate, those must be connected to a common network. Compose creates one by default for this purpose, if you have no reason to set network1/network2 just remote all networks declaration

wisamidris7 commented 1 month ago

I initially encountered issues not with SQL Server or myApp, but with gRPC communication between two applications within the same docker-compose setup.

My goal was to configure these applications for both HTTP/1 and HTTP/2 communication. However, I discovered that HTTP/1 and HTTP/2 require HTTPS or TLS for proper functionality.

In my ASP.NET Core project, which utilizes both gRPC and HTTP APIs, everything worked perfectly locally. But upon deploying to Docker, I encountered a problem: HTTP/2 is not supported, and HTTP/1 was required for gRPC calls from myapp1 to myapp2.

To address this, I configured myapp2 to use HTTPS with a domain name and certificate through Kestrel, setting HttpProtocol to Http1AndHttp2. While this configuration worked for requests from Chrome (using HTTP/1) and an empty gRPC console application (using HTTP/2), requests from myapp1 to myapp2 failed. They went through IP address-based communication and resulted in certificate validation errors due to the use of HTTPS.

I attempted to resolve this by changing the URL from https://myapp2:10000 to https://example.com:3912, but this led to timeout issues with gRPC requests. Additionally, I tried isolating each service in separate networks, but this did not resolve the issue.

I am also encountering similar problems when using reverse proxies like Nginx, as it does not seem to support HTTP/1 and HTTP/2 properly.

Given these challenges, I am concerned that this might be a Docker-related issue, especially since the problem persists even when services are in different networks. Any insights or suggestions would be greatly appreciated.

ndeloof commented 1 month ago

I honestly don't understand what you described here :'( I suggest you don't try using dedicated network, just rely on Compose to set a default network so all services can communicate. Also ConnectionString should not use an IP (which isn't predictable) but service name.