djangodockernginxdocker-composedocker-toolbox

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.

Here is my 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. Additionally, doing docker ps shows all the containers running as they should.

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. 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:

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)

Backend 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:

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:

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

Solution

  • As we established you use Docker Toolbox that is backed by VirtualBox rather than default Hyper-V Docker for Windows. 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. And management tools (i.e. Docker terminal and docker-compose) actually run on your host OS through 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). Suprisingly, the official documentation on this particular topic is pretty scarce - you can get a hint by looking at their examples though.

    So in your case, the correct url should be 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.

    From here I see a couple of avenues for you to explore:

    Run your project from C:\Users\...\MyProject

    As the documentation states, you get c:\Users mounted into /c/Users by default. 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.

    Create another share

    You also can create your own mounting mount in Virtual Box. Run pwd in your terminal and note where project root is. 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.

    Hopefully this will not require you to change your docker-compose.yml either

    Alternatively, switch to Hyper-V Docker Desktop - and these particular issues will go away.

    Bear in mind, that Hyper-V will not coexist with VirtualBox. So this option might not be available to you if you need VBox for something else.