Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker compose port forwarding not working properly

Tags:

When I use docker with the very simple command:

docker run -p 80:80 nginx

Port forwarding works properly and I can get nginx 'welcome page' when I go to localhost:80 using browser/curl.

At the same time when I use very similar but docker-compose specific config:

version: '3'
services:
  nginx:
    image: nginx
    ports:
     - "80:80"

And when I do docker-compose up and go to the browser - I see infinite loading, so looks like port forwarding is not configured properly, but I can't understand what is wrong in the config. I tried using different browsers and curl, I'm getting the same result - infinite loading.

Nginx here is just an example because of it's simplicity, in fact I have the same issue with redis/mysql/java images, so the issue isn't related to nginx.

I've also tried the following ways to start container via docker-compose:

docker-compose run -p 80:80 nginx

docker-compose run --service-ports nginx

but no luck, I've got the same result.

In both cases (docker run and docker-compose up) I have the same network driver type - bridge.

I've compared results of docker inspect <container id> for both cases: http://i.prntscr.com/obvxi0yESEa92znLDEu_PA.png

And results of docker inspect <network id>: http://i.prntscr.com/yyTpetvJSXa-dz4o9Pcl3w.png

ifconfig docker0 results:

docker0   Link encap:Ethernet  HWaddr 02:42:f1:9a:b6:72  
          inet addr:172.17.0.1  Bcast:0.0.0.0  Mask:255.255.0.0
          inet6 addr: fe80::42:f1ff:fe9a:b672/64 Scope:Link
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:174 errors:0 dropped:0 overruns:0 frame:0
          TX packets:837 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:47434 (47.4 KB)  TX bytes:107712 (107.7 KB)

brctl show results:

bridge name     bridge id               STP enabled     interfaces
br-f7adc3956101         8000.02427f870e7f       no
docker0         8000.0242f19ab672       no

ifconfig on the host machine results: https://pastebin.com/6ufWeYTE

route on the host machine results:

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    600    0        0 wlp4s0
link-local      0.0.0.0         255.255.0.0     U     1000   0        0 docker0
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
192.168.0.0     0.0.0.0         255.255.255.0   U     600    0        0 wlp4s0

Both docker and docker-compose were installed using official sites instructions for Linux.

Host OS: Ubuntu 17.04

UPDATE: I've tried to set 'attachable' network property in compose config and the issue was fixed. Though still unclear why is that happening.

networks:
  default:
  attachable: true
like image 273
XZen Avatar asked Oct 31 '17 19:10

XZen


2 Answers

In my case, I was using docker-compose run without the --service-ports argument, so port mappings were ignored.

Example:

docker-compose.yml

version: "3"

services:
    app-host:
        image: nginx:1.19.0-alpine
        working_dir: /app
        volumes:
            - ./:/app/
        ports:
            - "80:3000"

command

    docker-compose run --service-ports app-host

References: discussion forum docker-compose documentation

like image 181
Zymotik Avatar answered Sep 23 '22 02:09

Zymotik


The solution for me was to add the line network_mode: 'host'

here is the full file

version: '3'

services:
  nginx:
    image: nginx
    ports:
      - "80:80"
    volumes: ['./:/app']
    network_mode: 'host'
like image 25
Elia Weiss Avatar answered Sep 24 '22 02:09

Elia Weiss