duanba7653 2019-06-09 04: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 06: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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部