0

I am new to docker. When I tried to curl localhost inside container, I got Failed to connect to localhost port 9922: Connection refused.

I saw an answer for mac that is replace localhost with docker.for.mac.localhost which worked for my mac. But what about ubuntu?

Here is the details:

Docker file:

# 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"]

requirements.txt:

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

Inside my codes:

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

Docker command:

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

Above, if I use url = 'http://docker.for.mac.localhost:9922', it works perfectly. But url = 'http://localhost:9922' won't work. Outside docker container url = 'http://localhost:9922' will work. So here is my question: if i run this docker image on ubuntu what should i use to access localhost? Something like url = 'http://docker.for.ubuntu.localhost:9922'?

H.Li
  • 171
  • 2
  • 12
  • can you share the output of "docker ps" command? – Aaron_ab Dec 10 '18 at 09:33
  • Possible duplicate of [From inside of a Docker container, how do I connect to the localhost of the machine?](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) – David Maze Dec 10 '18 at 09:54
  • @Aaron_ab I used "docker run --rm" so "docker ps" gives nothing. I mentioned above **docker.for.mac.localhost** works fine on my macbook. Maybe i need to change the way I asked to "how to find the ip of my machine's localhost inside a docker container"? – H.Li Dec 10 '18 at 10:19
  • Show more info, Dockerfile, docker-compose.yml and if its a single container: how did you ran the container – Ntwobike Dec 10 '18 at 14:09

0 Answers0