简体   繁体   中英

How to make docker container on mac accessible by other PC on same network

I currently have several containers running inside docker which is handled by docker-compose file.

The roughly content of the docker compose file looks like below

app1:
    container_name: app1
    depends_on:
      - app2
    image: someimagepath
    build:
        context: ./app1
        dockerfile: Dockerfile
    ports:
      - 127.0.0.1:81:80
    expose:
      - "80"
    networks:
      - datanet   

app2:
 ....

Question: How can I make it so that the app1 can be accessible by my other PC so that I can hit the URL like http://<LAN-IP>: 80 and connect to it?

Thanks

Your PC needs to access the Mac, so it needs the IP address of the Mac (suppose 192.168.1.3).

Suppose your Mac has Internal Network IP 192.168.1.3 From the PC you do: http://192.168.1.3 But then it enter the Mac on port 80.

Somehow you need to forward requests on the Mac to port 80 to your App1 Container. You need a ProxyServer for that, like Nginx.

  1. create another Docker Container running Nginx, listening to port 80 on the Host (the Mac) This will make sure that requests to port 80 on the Mac are picked up by the Nginx Docker Container
  2. The Nginx config file needs to forward requests on port 80 to your App1 Container This is a matter of proxy forwarding. App1 can listen to any port, Nginx forwarding takes the right port. Many example of this on Nginx sites.

Basically is comes down to realising that your PC cannot access a Docker Container running on another system directly. You something on the Host (where the container is running) to forward requests to that container.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM