Docker Community Forums

Share and learn in the Docker community.

Problems with Docker Compose sharing port on local machine

I am trying to run a docker-compose application with the following configuration:

version: '3'
services:
  server:
    build:
      context: .
      dockerfile: ./docker/server/Dockerfile
    network_mode: "host"
    ports:
      - "8071:8071"

So when I try to use the host network (local machine) and then make the ports accessible for my APIs to be tested, it doesn’t make them accessible. Why is this so? What should the ideal solution to this problem? Do I have to run in my local Kubernetes to achieve this or is there some other way I can do without Kubernetes?

Hey @yeshwanthshenoy
did you expose the port in dockerfile?
if not please do that
and please read below link for more info

Aparently, I am using a mac and seems there is no support for Mac when using host as network. Is there any workaround for this like creating a network myself and anyway to expose that network to my localhost? As for your question of expose, yes I am exposing it. Here is my dockerfile:

FROM ubuntu:latest
RUN apt-get -y update && apt-get -y upgrade
RUN apt-get -y install openjdk-8-jdk
RUN mkdir -p /app/build
COPY ./build /app/build
EXPOSE 8071
RUN chmod +x /app/build/install/boringssl/bin/hello-world-tls-server
CMD ["/app/build/install/boringssl/bin/hello-world-tls-server"]

You can create bridge network and expose the port to localhost

If you just need access to port 8071 delete the line network_mode... and let docker-compose manage the networking.

Doesnt work tried it.

It should work, and there are many possibilities why it doesn’t.

  • I assume you use Docker Desktop for Mac and not Toolbox, is this correct?
  • Did you try the nginx example from Networking Features?
  • Are you sure your container is running and doesn’t exit immediately?
  • Does your application listen to 0.0.0.0 or just to localhost?
  • I assume you use Docker Desktop for Mac and not Toolbox, is this correct?
  • Yes Docker Desktop
  • No
  • Are you sure your container is running and doesn’t exit immediately?
  • Yes, I verified
  • Does your application listen to 0.0.0.0 or just to localhost?
  • localhost

hey
is docker ps showing the port being mapped to host?
can you please text the docker ps result