Docker-compose does not expose port of both services (with example to reproduce)

I am confused why docker-compose exposes the ports in different ways. Maybe someone can explain me this behaviour?

  • On my machine the port 3306 from the mariadb container is available by 127.0.0.1:3306
    • This is as expected
  • On my machine the port 29000 is not available by 127.0.0.1:29000
    • This is not as expected. It is available by the ip of the docker-container: 172.17.0.32:80
  • I do not get any errors. As long as I am using the docker web container address everything works fine.
  • The Webserver is able to communicate with the mariadb by using mariadb.devlocal:3306

Here is my docker-compose YAML file:

version: '3'

services:
  db:
    image: mariadb:10.4
    restart: always
    networks:
      some-network:
        aliases:
          - mariadb.devlocal
    environment:
      MYSQL_ROOT_PASSWORD: example
    ports:
      - "3306:3306"

  web:
    image: nginx:1.18
    hostname: webserver.devlocal
    networks:
      - some-network
    ports:
      - "29000:80"
    depends_on:
      - db

networks:
  some-network:

You can try the example by executing docker-compose run web

Executing docker-compose ps shows this:

                  Name                               Command             State           Ports         
-------------------------------------------------------------------------------------------------------
docker-compose-test_db_1                   docker-entrypoint.sh mysqld   Up      0.0.0.0:3306->3306/tcp
docker-compose-test_web_run_23ef735f9a70   nginx -g daemon off;          Up      80/tcp

Executing netstat -tulpen | grep -i docker shows this:

tcp6       0      0 :::3306                 :::*                    LISTEN      0          131920      22696/docker-proxy

I am using this docker-compose version:

docker-compose version 1.23.2, build unknown
docker-py version: 3.7.3
CPython version: 2.7.12
OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016

I found the solution!!

add the --service-ports parameter to docker-compose run