Docker containers standalone versus using docker-compose

Hello,
I have an application where the UI connects to the backend on udp port. When i run the backend process in a standalone docker container, the UI connects fine with the backend. However, if i run the same backend container using docker-compose, UI doesn’t seem to connect to the backend. when i see standalone the container using docker container ls, i don’t see any ports explicitly exposed but it works. However, i even tried to expose the ports in docker-compose, the container hosted doesn’t show connections. Any idea what could be different between running the container standalone versus with docker compose?

Thank you in advance!

Best Regards
SG

Please provide the exact commands you use to start your containers from the cli and your compose file. There must be a configuration difference.

I have yet to encounter a docker run command that is not expressable as a compose file and vice versa. They are just different representations of a “client-frontend” that drive the very same api endpoints of the docker engine.

Thanks for the prompt response. My standalone command is:

docker run -d --pid=host --network=host -u $(id -u):$(id -g) --rm test-container

whereas my docker-compose.yaml:

version: '3.7'
services:
  mycontainer:
    image: test-container
    command: test
    restart: unless-stopped
    ports:
      - "1114:1114/udp"

Not sure what difference is causing this:

Thanks in advance for your help!

SG

The most obvious differences between your docker run and compose file are:
equivalent of --pid=host missing in compose file
equivalent of --network=host missing in compose file (uses bridge instead and published ports)
equivalent of -u $(id -u):$(id -g) missing in compose file
your comose file has a command, you docke run does not.

Let me take a look at the compose specs and fill the voids. Which is the prefered network approach? host network OR brige + published ports?

update: I reformated your post, now the -u parameter makes sense.

Host network is fine. Thank you

The compose equivalent of docker run -d --name=mycontainer --pid=host --network=host -u $(id -u):$(id -g) --rm test-container

note: I added --name=mycontainer to reflect the service configuration in the compose file

is this compose file:

version: '2.4'
services:
  mycontainer:
    pid: host
    network: host
    user:  ${UID}:${GID}
    image: test-container

But, you can not run sub-commands in a compose file: so neither its $() or backtick syntax will work. As such the variables must exist in the shell before running docker-compose. Either by exporting them before or by prefixing your docker-compose command with it.

This should work(haven’t tested it though):
UID=$(id -u) GID=$(id -g) docker-compose up -d

Small correction: I think you wanted to write “network_mode: host”, not just network which would work in the build section according to the documentation. i don’t know why Docker uses two different names for the same purpose in two different place. I didn’t even know I could use network: host during build until today :smiley: so thanks.

good catch! indeed I ment network_mode: host.
Thanks @rimelek

Actualy this one should work without sub-process magic relying on internal bash variables:

version: '2.4'
services:
  mycontainer:
    pid: host
    network_mode: host
    user:  ${UID}:${GROUPS}
    image: test-container

Thank you for the prompt help and clarification!!!