[英]Connecting to local docker-compose container Windows 10
Very similar to this question, I cannot connect to my local docker-compose container from my browser (Firefox) on Windows 10 and have been troubleshooting for some time, but I cannot seem to find the issue.与此问题非常相似,我无法从 Windows 10 上的浏览器 (Firefox) 连接到本地 docker-compose 容器,并且已经进行了一段时间的故障排除,但我似乎找不到问题所在。
Here is my docker-compose.yml:这是我的 docker-compose.yml:
version: "3"
services:
frontend:
container_name: frontend
build: ./frontend
ports:
- "3000:3000"
working_dir: /home/node/app/
environment:
DEVELOPMENT: "yes"
stdin_open: true
volumes:
- ./frontend:/home/node/app/
command: bash -c "npm start & npm run build"
my_app_django:
container_name: my_app_django
build: ./backend/
environment:
SECRET_KEY: "... not included ..."
command: ["./rundjango.sh"]
volumes:
- ./backend:/code
- media_volume:/code/media
- static_volume:/code/static
expose:
- "443"
my_app_nginx:
container_name: my_app_nginx
image: nginx:1.17.2-alpine
volumes:
- ./nginx/nginx.dev.conf:/etc/nginx/conf.d/default.conf
- static_volume:/home/app/web/staticfiles
- media_volume:/home/app/web/mediafiles
- ./frontend:/home/app/frontend/
ports:
- "80:80"
depends_on:
- my_app_django
volumes:
static_volume:
media_volume:
I can start the containers with docker-compose -f docker-compose.yml up -d
and there are no errors when I check the logs with docker logs my_app_django
or docker logs my_app_nginx
. I can start the containers with
docker-compose -f docker-compose.yml up -d
and there are no errors when I check the logs with docker logs my_app_django
or docker logs my_app_nginx
. Additionally, doing docker ps
shows all the containers running as they should.此外,执行
docker ps
显示所有容器按应有的方式运行。
The odd part about this issue is that on Linux, everything runs without issue and I can find my app on localhost at port 80. The only thing I do differently when I am on Windows is that I run a dos2unix
on my.sh files to ensure that they run properly.这个问题的奇怪之处在于,在 Linux 上,一切运行都没有问题,我可以在端口 80 的 localhost 上找到我的应用程序。当我在 Windows 上时,唯一不同的是我在 my.sh 文件上运行
dos2unix
以确保它们正常运行。 If I omit this step, then I get many errors which leads me to believe that I have to do this.如果我省略这一步,那么我会得到很多错误,这让我相信我必须这样做。
If anyone could give guidance/advice as to what may I be doing incorrectly or missing altogether, I would be truly grateful.如果有人可以就我可能做错了什么或完全遗漏什么提供指导/建议,我将不胜感激。 I am also happy to provide more details, just let me know.
我也很乐意提供更多细节,请告诉我。 Thank you!
谢谢!
EDIT #1: As timur suggested, I did a docker run -p 80:80 -d nginx
and here was the output:编辑#1:正如timur建议的那样,我做了一个
docker run -p 80:80 -d nginx
,这里是 output:
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
bf5952930446: Pull complete
ba755a256dfe: Pull complete
c57dd87d0b93: Pull complete
d7fbf29df889: Pull complete
1f1070938ccd: Pull complete
Digest: sha256:36b74457bccb56fbf8b05f79c85569501b721d4db813b684391d63e02287c0b2
Status: Downloaded newer image for nginx:latest
19b56a66955145e4f59eefff57340b4affe5f7e0d82ad013742a60b479687c40
C:\Program Files\Docker Toolbox\docker.exe: Error response from daemon: driver failed programming external connectivity on endpoint naughty_hoover (8c7b2fa4aef964899c366e1897e38727bb7e4c38431875c5cb8456567005f368): Bind for 0.0.0.0:80 failed: port is already allocated.
This might be the cause of the error but I don't really understand what needs to be done at this point.这可能是错误的原因,但我真的不明白此时需要做什么。
EDIT #2: As requested, here are my Dockerfiles (one for backend, one for frontend)编辑#2:根据要求,这是我的 Dockerfiles(一个用于后端,一个用于前端)
Backend Dockerfile:后端 Dockerfile:
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN apt-get update && apt-get install -y imagemagick libxmlsec1-dev pkg-config
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code
Frontend Dockerfile:前端 Dockerfile:
FROM node
WORKDIR /home/node/app/
COPY . /home/node/app/
RUN npm install -g react-scripts
RUN npm install
EDIT #3: When I do docker ps
, this is what I get:编辑#3:当我做
docker ps
时,这就是我得到的:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0da02ad8d746 nginx:1.17.2-alpine "nginx -g 'daemon of…" About an hour ago Up About an hour 0.0.0.0:80->80/tcp my_app_nginx
070291de8362 my_app_frontend "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:3000->3000/tcp frontend
2fcf551ce3fa my_app_django "./rundjango.sh" 12 days ago Up About an hour 443/tcp my_app_django
As we established you use Docker Toolbox that is backed by VirtualBox rather than default Hyper-V
Docker for Windows.正如我们建立的那样,您使用由 VirtualBox 支持的Docker 工具箱,而不是 Windows 的默认
Hyper-V
Docker。 In this case you might think of it as a VBox VM that actually runs Docker - so all volume mounts and port mappings apply to docker machine VM , not your host.在这种情况下,您可能会将其视为实际运行 Docker 的 VBox VM - 因此所有卷安装和端口映射都适用于docker 机器 VM ,而不是您的主机。 And management tools (ie Docker terminal and
docker-compose
) actually run on your host OS through MinGW .而管理工具(即 Docker 终端和
docker-compose
)实际上通过MinGW在您的主机操作系统上运行。
Due to this, you don't get binding ports on localhost
by default (but you can achieve this by editing VM properties in VirtualBox manually if you so desire - I just googled the second link for some picture tutorials).因此,默认情况下,您不会在
localhost
上获得绑定端口(但如果您愿意,可以通过在VirtualBox 中手动编辑 VM 属性来实现这一点 - 我刚刚搜索了一些图片教程的第二个链接)。 Suprisingly, the official documentation on this particular topic is pretty scarce - you can get a hint by looking at their examples though.令人惊讶的是,关于这个特定主题的官方文档非常稀缺——你可以通过查看他们的示例来获得提示。
http://192.168.99.100
http://192.168.99.100
Another thing that is different between these two solutions is volume mounts.这两种解决方案之间的另一个不同之处是卷安装。 And again, documentation sorta hints at what it should be but I can't point you a more explicit source.
再一次, 文档有点暗示它应该是什么,但我不能为你指出一个更明确的来源。 As you have probably noticed the terminal you use for all your docker interactions encodes paths a bit differently (I presume because of that MinGW layer ) and converted paths get sent off to docker-machine - because it's Linux and would not handle windows-style paths anyway.
您可能已经注意到您用于所有 docker 交互的终端对路径的编码略有不同(我推测是因为MinGW 层)并且转换后的路径被发送到 docker-machine - 因为它是 Linux 并且不会处理 Windows 样式的路径反正。
From here I see a couple of avenues for you to explore:从这里我看到了一些可供您探索的途径:
C:\Users\...\MyProject
C:\Users\...\MyProject
运行您的项目As the documentation states , you get c:\Users
mounted into /c/Users
by default.如文档所述,默认情况下,您将
c:\Users
安装到/c/Users
中。 So theoretically, if you run your docker-compose
from your user home folder - paths should automagically align - but since you are having this issue - you are probably running it from somewhere else.所以理论上,如果您从用户主文件夹运行
docker-compose
- 路径应该自动对齐 - 但由于您遇到此问题 - 您可能是从其他地方运行它。
You also can create your own mounting mount in Virtual Box .您还可以在 Virtual Box 中创建自己的安装支架。 Run
pwd
in your terminal and note where project root is.在终端中运行
pwd
并注意项目根目录的位置。 Then use Virtual Vox UI and create a path that would make it align with your directory tree (for example, D:\MyProject\
should become /d/MyProject
.然后使用 Virtual Vox UI 并创建一个路径,使其与您的目录树对齐(例如,
D:\MyProject\
为/d/MyProject
。
Hopefully this will not require you to change your docker-compose.yml
either希望这也不需要您更改
docker-compose.yml
Bear in mind, that Hyper-V will not coexist with VirtualBox.请记住,Hyper-V 不会与 VirtualBox 共存。 So this option might not be available to you if you need VBox for something else.
因此,如果您需要 VBox 来做其他事情,这个选项可能对您不可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.