可以从其他容器访问docker容器,但不能从本地主机访问

时间:2019-02-05 20:00:44

标签: docker

我有一个使用MySQL,Nest.js和数据库Angular的应用程序。我设法使Nest与Nest的MySQL容器nad(我想是Angular)进行通信,但是当我尝试从浏览器访问前端时,它说无法访问该站点,或者服务器没有响应如果我从邮递员向后端发出请求。我已经在公开和分配端口,但是它不起作用。

我正在运行docker-compose up --build,浏览localhost:4200或向localhost:3000发送请求,而我从浏览器中得到的是Chrome上的localhost didn’t send any data. ERR_EMPTY_RESPONSE

这是mi docker-compose.yml文件:

version: '3'

services:

# MySQL
  db:
    image: mysql:5.7
    command: --default-authentication-plugin=mysql_native_password
    ports:
      - "3306:3306"
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}

# Nest.js Backend
  server:
    build: ./backend
    expose:
      - 3000
    ports:
      - "3000:3000"
    depends_on:
      - db
    links:
      - db
    volumes:
      - ./backend/src:/srv/app/bk-god/src

# Angular 6 Frontend
  client:
    build: ./frontend
    expose:
      - 4200
    ports:
      - "4200:4200"
    volumes:
      - ./frontend/src:/srv/app/fr-god/src
      - ./frontend/public:/srv/app/fr-god/public
    links:
      - server

这是我在每个文件夹上的Dockerfile [唯一的区别是后端运行Yarn运行start:dev]:

FROM node:8.15.0-alpine

RUN mkdir -p /srv/app/fr-god
WORKDIR /srv/app/fr-god

COPY package.json /srv/app/fr-god
COPY yarn.lock /srv/app/fr-god

RUN yarn

COPY . /srv/app/fr-god
CMD ["yarn", "start"]

PS:可通过控制台和GUI指向localhost:3306来访问MySQL。

这是docker ps的输出:

CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                    PORTS                               NAMES
435bfc1aa66d        docker_client       "yarn start"             About a minute ago   Up About a minute         0.0.0.0:4200->4200/tcp              docker_client_1
c96a84289d12        docker_server       "yarn run start:dev"     About a minute ago   Up About a minute         0.0.0.0:3000->3000/tcp              docker_server_1
069f8d61b770        mysql:5.7           "docker-entrypoint.s…"   About a minute ago   Up About a minute         0.0.0.0:3306->3306/tcp, 33060/tcp   docker_db_1

/ etc / hosts内容:

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1               localhost
255.255.255.255         broadcasthost
::1                     localhost
来自docker logs docker_client_1

日志:

yarn run v1.12.3
$ ng serve
** Angular Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **

Date: 2019-02-06T23:57:54.079Z
Hash: c0fd0f6da579440bf752
Time: 20737ms
chunk {main} main.js, main.js.map (main) 85.7 kB [initial] [rendered]
chunk {polyfills} polyfills.js, polyfills.js.map (polyfills) 240 kB [initial] [rendered]
chunk {runtime} runtime.js, runtime.js.map (runtime) 6.22 kB [entry] [rendered]
chunk {styles} styles.js, styles.js.map (styles) 16.7 kB [initial] [rendered]
chunk {vendor} vendor.js, vendor.js.map (vendor) 4.83 MB [initial] [rendered]
ℹ 「wdm」: Compiled successfully.
来自docker logs docker_server_1

日志:

yarn run v1.12.3
$ nodemon
[nodemon] 1.18.9
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: /srv/app/bk-god/src/**/*
[nodemon] starting `ts-node -r tsconfig-paths/register src/main.ts`
[Nest] 38   - 2019-2-6 23:57:33   [NestFactory] Starting Nest application...
[Nest] 38   - 2019-2-6 23:57:33   [InstanceLoader] TypeOrmModule dependencies initialized +201ms
[Nest] 38   - 2019-2-6 23:57:40   [InstanceLoader] AppModule dependencies initialized +2ms
[Nest] 38   - 2019-2-6 23:57:40   [InstanceLoader] PlayerModule dependencies initialized +1ms
[Nest] 38   - 2019-2-6 23:57:40   [InstanceLoader] MatchModule dependencies initialized +0ms
[Nest] 38   - 2019-2-6 23:57:40   [RoutesResolver] PlayerController {/player}: +67ms
[Nest] 38   - 2019-2-6 23:57:40   [RouterExplorer] Mapped {/, GET} route +11ms
[Nest] 38   - 2019-2-6 23:57:40   [RouterExplorer] Mapped {/, POST} route +2ms
[Nest] 38   - 2019-2-6 23:57:40   [RoutesResolver] MatchController {/match}: +1ms
[Nest] 38   - 2019-2-6 23:57:40   [RouterExplorer] Mapped {/, GET} route +1ms
[Nest] 38   - 2019-2-6 23:57:40   [RouterExplorer] Mapped {/, POST} route +8ms
[Nest] 38   - 2019-2-6 23:57:40   [RouterExplorer] Mapped {/detail, GET} route +2ms
[Nest] 38   - 2019-2-6 23:57:40   [NestApplication] Nest application successfully started +8ms

1 个答案:

答案 0 :(得分:0)

阅读this文档使我认为您的“暴露”声明可能正在破坏您的“端口”声明。尝试删除“暴露”声明并重新加载容器。这应该可以解决您的问题。