首页 > 解决方案 > docker 容器 curl localhost 连接被拒绝 ubuntu

问题描述

我是码头工人的新手。当我尝试在容器内卷曲 localhost 时,出现Failed to connect to localhost port 9922: Connection refused

我看到了一个 mac 的答案,用 docker.for.mac.localhost 替换 localhost这适用于我的 mac。但是ubuntu呢?

这是详细信息:

码头工人文件:

# Use an official Python runtime as a parent image
FROM python:3.7-slim

# Set up working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# Make port 5000 available to the world outside this container
EXPOSE 5000

#Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

要求.txt:

numpy
pandas
setuptools
pytest
apscheduler
flask
bokeh
google-api-python-client
google-auth
google-auth-httplib2
oauth2client
tqdm
bs4
tabulate
seaborn
matplotlib
requests

在我的代码里面:

url = 'http://docker.for.mac.localhost:9922'
# url = 'http://localhost:9922'

码头工人命令:

$ docker run --rm -it -p 5000:5000 --name wallet <name-of-image>

上面,如果我使用url = ' http://docker.for.mac.localhost:9922 ',它工作得很好。但是url = ' http://localhost:9922 '不起作用。外部 docker 容器url = ' http://localhost:9922 '将起作用。所以这是我的问题:如果我在 ubuntu 上运行这个 docker 映像,我应该使用什么来访问 localhost?像url = ' http://docker.for.ubuntu.localhost:9922 ' 之类的东西?

标签: pythondocker

解决方案


推荐阅读