Docker compose port mapping

后端 未结 4 1314
别跟我提以往
别跟我提以往 2020-12-13 01:56

I have a docker-compose yml file as in below

version: \'2\'
services:
  nodejs:
    build:
      context: .
      doc         


        
4条回答
  •  甜味超标
    2020-12-13 02:17

    It's important to point out that all of the above solutions map the port to every interface on your machine. This is less than desirable if you have a public IP address, or your machine has an IP on a large network. Your application may be exposed to a much wider audience than you'd hoped.

    redis:
      build:
        context:
        dockerfile: Dockerfile-redis
        ports:
        - "127.0.0.1:3901:3901"
    

    127.0.0.1 is the ip address that maps to the hostname localhost on your machine. So now your application is only exposed over that interface and since 127.0.0.1 is only accessible via your machine, you're not exposing your containers to the entire world.

    The documentation explains this further and can be found here: https://docs.docker.com/compose/compose-file/#ports


    Note: If you're using Docker for mac this will make the container listen on 127.0.0.1 on the Docker for Mac VM and will not be accessible from your localhost. If I recall correctly.

提交回复
热议问题