94

I'm new to Docker and trying to make a demo Rails app. I made a dockerfile that looks like this:

FROM ruby:2.2
MAINTAINER [email protected]

# Install apt based dependencies required to run Rails as 
# well as RubyGems. As the Ruby image itself is based on a 
# Debian image, we use apt-get to install those.
RUN apt-get update && apt-get install -y \
build-essential \
nodejs

    # Configure the main working directory. This is the base 
    # directory used in any further RUN, COPY, and ENTRYPOINT 
    # commands.
RUN mkdir -p /app
WORKDIR /app

    # Copy the Gemfile as well as the Gemfile.lock and install 
    # the RubyGems. This is a separate step so the dependencies 
    # will be cached unless changes to one of those two files 
    # are made.
COPY Gemfile Gemfile.lock ./
RUN gem install bundler && bundle install --jobs 20 --retry 5

# Copy the main application.
COPY . ./

# Expose port 8080 to the Docker host, so we can access it 
# from the outside.
EXPOSE 8080

# The main command to run when the container starts. Also 
# tell the Rails dev server to bind to all interfaces by 
# default.
CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0", "-p", "8080"]

I then built it like so:

docker build -t demo . 

And call a command to start the server which does start the server on port 8080:

Johns-MacBook-Pro:demo johnkealy$ docker run -it demo
=> Booting WEBrick
=> Rails 4.2.5 application starting in development on http://0.0.0.0:8080
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2016-04-23 16:50:34] INFO  WEBrick 1.3.1
[2016-04-23 16:50:34] INFO  ruby 2.2.4 (2015-12-16) [x86_64-linux]
[2016-04-23 16:50:34] INFO  WEBrick::HTTPServer#start: pid=1 port=8080

I then try to find the correct IP to navigate to:

Johns-MacBook-Pro:demo johnkealy$ docker-machine ip default
192.168.99.100

I navigate to http://192.168.99.100:8080 and get the error This site can’t be reached 192.168.99.100 refused to connect.

What could I be doing wrong ?

2
  • 1
    try to run container with publish option. docker run -it demo --publish 8080:8080 Commented Apr 23, 2016 at 17:03
  • 1
    thanks, @jozef, but I get "docker: Error response from daemon: Container command '--publish' not found or does not exist.."
    – jdkealy
    Commented Apr 23, 2016 at 17:11

12 Answers 12

88

You need to publish the exposed ports by using the following options:

-P (upper case) or --publish-all that will tell Docker to use random ports from your host and map them to the exposed container's ports.

-p (lower case) or --publish=[] that will tell Docker to use ports you manually set and map them to the exposed container's ports.

The second option is preferred because you already know which ports are mapped. If you use the first option then you will need to call docker inspect demo and check which random ports are being used from your host at the Ports section.

Just run the following command:

docker run -it -p 8080:8080 demo

After that your url will work.

1
  • 16
    Yes, and remember to put -p BEFORE image name, not after.
    – WesternGun
    Commented Aug 20, 2021 at 11:53
43

If you are using Docker toolkit on window 10 home you will need to access the webpage through docker-machine ip command. It is generally 192.168.99.100:

It is assumed that you are running with publish command like below.

docker run -it -p 8080:8080 demo

With Window 10 pro version you can access with localhost or corresponding loopback 127.0.0.1:8080 etc (Tomcat or whatever you wish). This is because you don't have a virtual box there and docker is running directly on Window Hyper V and loopback is directly accessible.

Verify the hosts file in window for any digression. It should have 127.0.0.1 mapped to localhost

1
  • This worked for me! I'm running Docker via minikube and the IP was different. Got it by running minikube ip
    – jiggy
    Commented Mar 15, 2018 at 18:17
17

I had the same problem. I was using Docker Toolbox on Windows Home. Instead of localhost I had to use http://192.168.99.100:8080/.

You can get the correct IP address using the command:

docker-machine ip

The above command returned 192.168.99.100 for me.

3
  • 11
    in macOS when I run this I get ` command not found: docker-machine`
    – Surya
    Commented Jan 5, 2021 at 4:44
  • You are a genius. This is was worked for me. I am using Spring Boot + OpenLDAP, both of them in separate containers. By using localhost it didn't work. By replacing localhost with my actual ip did the trick.
    – Linksx
    Commented Jun 11, 2021 at 14:51
  • Where did you get that docker-machine command from? It is not recognized neither on PowerShell nor WSL terminals. Commented Mar 25 at 11:45
8

Command EXPOSE in your Dockerfile lets you bind container's port to some port on the host machine but it doesn't do anything else. When running container, to bind ports specify -p option.

So let's say you expose port 5000. After building the image when you run the container, run docker run -p 5000:5000 name. This binds container's port 5000 to your laptop/computers port 5000 and that portforwarding lets container to receive outside requests.

This should do it.

7

In Docker Quickstart Terminal run following command:

$ docker-machine ip 192.168.99.100 
0
3

In Windows, you also normally need to run command line as administrator.

As standard-user:

docker build -t myimage -f Dockerfile .
Sending build context to Docker daemon  106.8MB
Step 1/1 : FROM mcr.microsoft.com/dotnet/core/runtime:3.0
Get https://mcr.microsoft.com/v2/: dial tcp: lookup mcr.microsoft.com on [::1]:53: read udp [::1]:45540->[::1]:53: read: 
>>>connection refused

But as an administrator.

docker build -t myimage -f Dockerfile .
Sending build context to Docker daemon  106.8MB
Step 1/1 : FROM mcr.microsoft.com/dotnet/core/runtime:3.0
3.0: Pulling from dotnet/core/runtime
68ced04f60ab: Pull complete                                                                                             e936bd534ffb: Pull complete                                                                                             caf64655bcbb: Pull complete                                                                                             d1927dbcbcab: Pull complete                                                                                             Digest: sha256:e0c67764f530a9cad29a09816614c0129af8fe3bd550eeb4e44cdaddf8f5aa40
Status: Downloaded newer image for mcr.microsoft.com/dotnet/core/runtime:3.0
 ---> f059cd71a22a
Successfully built f059cd71a22a
Successfully tagged myimage:latest
3

In some situations the internal process in the docker container is trying to bind to localhost (or 127.0.0.1 explicitly), but this can be a mistake. Consider trying binding to 0.0.0.0 instead. This has been a common problem for me in the past.

2
  • The Dockerfile in the question has a rails server -b 0.0.0.0 option already.
    – David Maze
    Commented Aug 12, 2023 at 23:03
  • 1
    this was the key to solving my issue with a gradio app running in Docker. Had to manually set it to 0.0.0.0 instead of default 127.0.0.1 and presto, it works. I have no clue why. Thanks!
    – vega
    Commented Feb 21 at 20:31
1

Make sure that you use the -p flag before the image name like this:

docker run -p 8080:8080 demo
0

If someone using docker compose make sure your containers are on the same network bridge like:

  container-1:
    networks:
     - app-network

  container-2:
    networks:
     - app-network
0

The most default approach using python with docker in ubuntu

in the root of the project run the command to create the requirements.txt file containing your virtual environament packages:

pip freeze > requirements.txt

create a new 'Dockerfile' in the project root:

FROM python:3.10.12
WORKDIR /app
COPY requirements.txt ./
RUN pip install -r requirements.txt --quiet
COPY . .
EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

execute bash command to build the docker image:

docker build -t my-app-img:0.1.0 .

execute bash command to run the image:

docker run --rm -d -p 8000:8000/tcp doospy-api:0.1.0

access it using the browser:

http://localhost:8000/

ubuntu 22.04,python 3.10.12, docker 24.0.7, venv

0

The most simple approach using python with docker in ubuntu

in the root of the project run the command to create the requirements.txt file containing your virtual environament packages:

pip freeze > requirements.txt

create a new 'Dockerfile' in the project root:

FROM python:3.10.12
WORKDIR /app
COPY requirements.txt ./
RUN pip install -r requirements.txt --quiet
COPY . .
EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

execute bash command to build the docker image:

docker build -t my-app-img:0.1.0 .

execute bash command to run the image:

docker run --rm -d -p 8000:8000/tcp doospy-api:0.1.0

access it using the browser:

http://localhost:8000/

ubuntu 22.04,python 3.10.12, docker 24.0.7, venv, django, rest_framework, restframework, image, container

0

tldr; Make sure your shell doesn't have unexpected environment variables set, such as the ones set by the install/setup of minikube:

$ minikube docker-env
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.49.2:2376"
export DOCKER_CERT_PATH="/home/jonathan/.minikube/certs"
export MINIKUBE_ACTIVE_DOCKERD="minikube"

These will cause Docker to use some minikube-oriented features that I don't understand yet (maybe namespacing is part of it?), which means Docker will run your container successfully, but it is intended to be controlled, and its ports exposed, using Kubernetes. The container's exposed port will not be visible to a browser unless you set up the Kubernetes service to do that.

I had this problem while playing around with tutorials such as the Kubernetes blog's post about running an example Python app, which incrementally leads you through:

  1. First, run the Python app on your host,
  2. Then runs the app as a Docker container,
  3. Finally, runs the Docker container using Kubernetes.

and step (2) wasn't working for me. The container ran, had no suspicious logs, the port was exposed in Dockerfile and "docker run" command, but I could not get a browser to connect to it.

Turns out that my earlier setup of kubectl and minikube had set the above variables. Starting a fresh shell and re-running the container, everything worked fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.