duanba7653 2019-06-09 12:55
浏览 772
已采纳

无法从主机访问docker容器内的服务器

I am hosting a mysql server and a go http server in docker. I am unable to hit the http server from my host machine. My host machine is a mac.

I have tried using localhost:8080 and ipofserver:8080. I get the ip from the docker inspect. I am able to connect to my mysql server from my host, but i can't hit the server from the host.

Here is my docker ps output.

0.0.0.0:8080->8080/tcp
0.0.0.0:3306->3306/tcp, 33060/tcp

Below are my details:

Docker Desktop version 2.0.0.3.

docker-compose

version: '3.1'

services:

    mysql:
        image: mysql:latest
        restart: always
        environment:
            MYSQL_ROOT_PASSWORD: password
            MYSQL_DATABASE: mydb
        volumes:
            - mysql:/var/lib/mysql
        ports:
            - "3306:3306"
        networks:
            - mynetwork

    server:
        image: server:latest
        networks:
             - mynetwork
        ports:
             - "8080:8080"

volumes:
    mysql: ~

networks:
    mynetwork:
        driver: "bridge"

mysql dockerfile

FROM mysql:8.0.16

COPY ./scripts/mysql/dbgen-v1.sql /docker-entrypoint-initdb.d/

EXPOSE 3306

server dockerfile

FROM golang:1.12.5

WORKDIR a/go/path
COPY . .

ENV GOBIN=/usr/local/bin

RUN go get github.com/go-sql-driver/mysql
RUN go get github.com/iancoleman/strcase
RUN go get github.com/jmoiron/sqlx
RUN go get github.com/spf13/cobra
RUN go get github.com/gorilla/websocket
RUN go get github.com/spf13/viper

RUN go install -v cmd/project/main.go

EXPOSE 8080

CMD ["main"]
  • 写回答

1条回答

  • dongtongjian3127 2019-06-09 14:12
    关注

    (This answer is based on the chat we had in the comments)

    In order to expose the web server from inside the container to the host it needs to bind to 0.0.0.0 and not to 127.0.0.1. Using 0.0.0.0 ensures that the web server binds to the bridge interface that can be accessed from the host side.

    Relevant Docker docs: https://docs.docker.com/v17.09/engine/userguide/networking/default_network/binding/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看