Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I connect to my local docker-compose container on Windows 10?

I'm trying to dockerize a Python application, for which I've been following this tutorial. The tutorial is from April 2015 and still uses Docker Machine, which, judging from this answer, is no longer necessary to run Docker containers locally on Windows.

I got it working with Docker Machine before, and was able to see the web app and interact with it. But now I'm trying to get this working without Docker Machine, with Docker version 17.06.0-ce, build 02c1d87, on Windows 10.

Here's the docker-compose.yml:

web:
  restart: always
  build: ./web
  expose:
    - "8000"
  links:
    - postgres:postgres
  volumes:
    - /usr/src/app/static
  env_file: .env
  command: /usr/local/bin/gunicorn -w 2 -b :8000 app:app

nginx:
  restart: always
  build: ./nginx/
  ports:
    - "80:80"
  volumes:
    - /www/static
  volumes_from:
    - web
  links:
    - web:web

data:
  image: postgres:latest
  volumes:
    - /var/lib/postgresql
  command: "true"

postgres:
  restart: always
  image: postgres:latest
  volumes_from:
    - data
  ports:
    - "5432:5432"

I started the containers:

$ docker-compose up -d
Creating polly_data_1 ...
Creating polly_data_1 ... done
Creating polly_postgres_1 ...
Creating polly_postgres_1 ... done
Creating polly_web_1 ...
Creating polly_web_1 ... done
Creating polly_nginx_1 ...
Creating polly_nginx_1 ... done

Then, when I run docker ps, it shows the following three containers running:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
9b2c1048f3a5        polly_nginx         "/usr/sbin/nginx"        4 seconds ago       Up 3 seconds        0.0.0.0:80->80/tcp       polly_nginx_1
d561ac5b901a        polly_web           "/usr/local/bin/gu..."   5 seconds ago       Up 4 seconds        8000/tcp                 polly_web_1
ecb029d6ec3a        postgres:latest     "docker-entrypoint..."   7 seconds ago       Up 5 seconds        0.0.0.0:5432->5432/tcp   polly_postgres_1

(At this point, navigating to http://localhost:8000/ in Chrome already yields ERR_CONNECTION_REFUSED.)

I then ran the script to set up the database, as per the tutorial (extra //s because I'm using Git Bash on Windows 10):

$ docker-compose run web ///usr/local/bin/python create_db.py
Starting polly_data_1 ...
Starting polly_data_1 ... done
Starting polly_postgres_1 ... done

Now when I run docker ps, it shows the following four containers running:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                  PORTS                    NAMES
a129c12f5982        polly_web           "//usr/local/bin/p..."   5 seconds ago       Up Less than a second   8000/tcp                 polly_web_run_1
9b2c1048f3a5        polly_nginx         "/usr/sbin/nginx"        16 seconds ago      Up 15 seconds           0.0.0.0:80->80/tcp       polly_nginx_1
d561ac5b901a        polly_web           "/usr/local/bin/gu..."   17 seconds ago      Up 16 seconds           8000/tcp                 polly_web_1
ecb029d6ec3a        postgres:latest     "docker-entrypoint..."   19 seconds ago      Up 17 seconds           0.0.0.0:5432->5432/tcp   polly_postgres_1

And localhost:8000 is still refusing to connect. The web container exposes port 8000, so I don't get why I can't connect to it.

How can I get this working so I can access the web app in the web container locally?

like image 426
Leon Overweel Avatar asked Jan 30 '23 23:01

Leon Overweel


1 Answers

Just change:

expose:
  - "8000"

By

ports:
  - "8000:8000"

Btw http://localhost:80 is not working?

Regards

like image 73
Carlos Rafael Ramirez Avatar answered Feb 06 '23 07:02

Carlos Rafael Ramirez