如何配置 docker compose 以正确公开端口? [英] How do I configure docker compose to expose ports correctly?

查看:88
本文介绍了如何配置 docker compose 以正确公开端口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 docker 和 docker compose 与 postgres 一起运行 clojure 和 node 应用程序.

该项目包含在以下文件夹结构中.

project/-  应用程序/-- -- Dockerfile-  前端/-- --/Dockerfile-- docker-compose.yml

app/Dockerfile 看起来像这样......

FROM clojure:latest复制 ./usr/src/应用程序工作目录/usr/src/app曝光 9000CMD ["lein", "run", "migrate", "&&","lein", "run"]

frontend/Dockerfile 看起来像这样......

来自节点:5复制 ./usr/src/应用程序工作目录/usr/src/app运行 npm 安装曝光 8080CMD ["npm", "开始"]

最后 docker-compose.yml 看起来像...

前端:图片: Bradcypert/节点卷:- ./frontend:/usr/src/frontend端口:- 8080:8080"后端:图片: Bradcypert/Clojure卷:- ./app:/usr/src/backend端口:- 9000:9000"链接:- postgres邮局:图片:postgres端口:- 5432:5432"

后端由于单独的原因失败,但前端似乎运行成功,也就是说,我无法访问 localhost:8080 并查看应用程序.我需要做什么才能实现这一点?

提前致谢.

澄清一下,正在运行的命令是 docker-compose up

解决方案

使用 boot2docker(在 Mac 或 Windows 上),要从 localhost 访问任何端口,您必须配置 VirtualBox VM 以便port-forward 从 VM 到主机的端口.

您的端口映射是正确的,但您仍然需要让您的主机 (Mac) 可以看到您想从 localhost(您的 Mac)访问的一个端口.

例如参见

这样,您就不必知道您机器的 IP 是什么.
(您可以通过 docker-machine ls 后跟 docker-machine ip 看到)

I'm using docker and docker compose to run a clojure and a node app, alongside postgres.

The project is contained in the following folder structure.

project/
-- app/
-- -- Dockerfile
-- frontend/
-- -- /Dockerfile
-- docker-compose.yml

The app/Dockerfile looks like so...

FROM clojure:latest
COPY . /usr/src/app
WORKDIR /usr/src/app

EXPOSE 9000

CMD ["lein", "run", "migrate", "&&","lein", "run"]

The frontend/Dockerfile looks like so ...

FROM node:5
COPY . /usr/src/app
WORKDIR /usr/src/app
RUN npm install

EXPOSE 8080

CMD ["npm", "start"]

And lastly the docker-compose.yml looks like...

frontend:
  image: bradcypert/node
  volumes:
    - ./frontend:/usr/src/frontend
  ports:
    - "8080:8080"

backend:
  image: bradcypert/clojure
  volumes:
    - ./app:/usr/src/backend
  ports:
    - "9000:9000"
  links:
    - postgres

postgres:
  image: postgres
  ports:
    - "5432:5432"

backend is failing for a separate reason, but the frontend seems to be running successfully, that being said, I'm unable to hit localhost:8080 and see the app. What do I need to do make this happen?

Thanks in advance.

Just to clarify, the command being run is docker-compose up

解决方案

With boot2docker (on Mac or Windows), to access any port from localhost, you have to configure your VirtualBox VM in order to port-forward that port from the VM into the host.

Your port mappings are correct, but you still need to make visible to your host (Mac) the one port you want to access from localhost (your Mac).

See for instance "Using boot2docker to run Docker on a Mac or Windows" from Andrew Odewahn:

That way, you don't have to find out what the IP of your machine is.
(Which you can see with docker-machine ls followed by docker-machine ip <name>)

这篇关于如何配置 docker compose 以正确公开端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆