compose: docker-compose up -d doesn't expose ports when defined with build directive
docker-compose up -d
is supposed to expose the ports and supposedly be able to publish the ports according to the yml, however, it is not working for the services build from build:
configuration.
docker-compose.yml
version: '3.1'
services:
nginx:
build:
context: "."
ports:
- "9999:80"
network_mode: 'host'
service_cassandra:
image: 'cassandra:3.0'
ports:
- "19042:9042"
Dockerfile for nginx
FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
These files above end up like this:
$ docker-compose up -d --build
Building nginx
Step 1/4 : FROM nginx
---> 46102226f2fd
Step 2/4 : COPY nginx.conf /etc/nginx/nginx.conf
---> Using cache
---> f36baa7f9388
Step 3/4 : EXPOSE 80
---> Running in 8b4a5621a0b3
---> 75fbceea828b
Removing intermediate container 8b4a5621a0b3
Step 4/4 : CMD nginx -g daemon off;
---> Running in 50f1efaf665d
---> e1cd7d703925
Removing intermediate container 50f1efaf665d
Successfully built e1cd7d703925
Successfully tagged tmp_nginx:latest
Recreating tmp_nginx_1
tmp_service_cassandra_1 is up-to-date
$ docker-compose ps
Name Command State Ports
----------------------------------------------------------------------------------------------------------------------------------
tmp_nginx_1 nginx -g daemon off; Up
tmp_service_cassandra_1 /docker-entrypoint.sh cass ... Up 7000/tcp, 7001/tcp, 7199/tcp, 0.0.0.0:19042->9042/tcp, 9160/tcp
$ netstat -lna |grep 19042
tcp6 0 0 ::1.19042 *.* LISTEN
tcp4 0 0 *.19042 *.* LISTEN
$ netstat -lna |grep 9999
(no result)
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
43d7167e3671 tmp_nginx "nginx -g 'daemon ..." About a minute ago Up About a minute tmp_nginx_1
a49374c38de3 cassandra:3.0 "/docker-entrypoin..." 6 minutes ago Up 6 minutes 7000-7001/tcp, 7199/tcp, 9160/tcp, 0.0.0.0:19042->9042/tcp tmp_service_cassandra_1
so…ports configuration seems working for containers that is built from image pull, but not for containers built from a local dockerfile.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 11
- Comments: 39
Links to this issue
Commits related to this issue
- Added note to 'ports' section: Port mapping is incompatible with `network_mode: host` Took a while to figure this one out. Source: https://github.com/docker/compose/issues/4799#issuecomment-29960503... — committed to jgentes/docker.github.io by jgentes 6 years ago
- Added note to 'ports' section: Port mapping is incompatible with `network_mode: host` Took a while to figure this one out. Source: https://github.com/docker/compose/issues/4799#issuecomment-29960503... — committed to docker/docs by jgentes 6 years ago
- ARUHA-280 Update docker-compose.yaml version to 3.6 * `network_mode` and `ports` are incompatible. * https://github.com/docker/docker.github.io/pull/6448 * https://github.com/docker/compose/issue... — committed to john-coleman/nakadi by john-coleman 6 years ago
- ARUHA-280 Update travis.yml DOCKER_COMPOSE_VERSION to 1.21.1 * `network_mode` and `ports` are incompatible. * https://github.com/docker/docker.github.io/pull/6448 * https://github.com/docker/comp... — committed to john-coleman/nakadi by john-coleman 6 years ago
- add default network https://github.com/docker/compose/issues/4799#issuecomment-580082713 — committed to ggogel/seafile-containerized by ggogel 3 years ago
- Defining default net re: https://github.com/docker/compose/issues/4799 — committed to BetterCorp/cloudflarewarp by mrinc 2 years ago
oh, you didn’t specify, but I’m assuming you’re using
run
instead ofup
? If so, you need--service-ports
to publish the ports from the service definition. See our docsFor me the problem was that my service, that I was running in the docker container, was hosted on 127.0.0.1 instead of 0.0.0.0 which makes the service inaccessible from the host system even if the port is published.
Port mapping is incompatible with
network_mode: host
:https://docs.docker.com/engine/userguide/networking/#default-networks
I’m having the same issue, and I’m not using the
network_mode: host
Are you using the
--service-ports
flag?so what was the solution exactly? I’m having the same problem and can’t manage to make it working yet
try running
docker-machine inspect default | grep IPAddress
and then use this address instead of localhost.I’ve found that
docker-compose
doesn’t do port mapping if custom nework only is used. For example,This service is unavailable from the host machine. But add network used by all containers, started with
docker run
, and port 8080 will be mapped. I use docker-compose version 1.25.2 for windows and this network is named default, though there’s no network with such name indocker network ls
. I’ve found it withinspect
to typical (i mean started bydocker run
) container. So make such changeThen
docker ps
looks better STATUS PORTS NAMES Up 14 seconds 0.0.0.0:8080->8080/tcp some_my_service_nameI am facing the same problem my
docker-compose
is not exposing ports.The command I am using is
docker-compose up -d --build
Can any one help me? Thank You@gooney47 if I could give you more emojis I would, thank you!!!
NOTE : The host networking driver only works on Linux hosts, and is not supported on Docker for Mac, Docker for Windows, or Docker EE for Windows Server.
@kmursk That was it for me, thanks 🎉
Shouldn’t docker at least print a
WARNING:
whenever it ignores a field? Implicit behaviors are rather time consuming to hunt down.@gooney47 was right
use
@akadoya Thank you for you quick answer and sorry if my issue was not clear enough. My problem is pretty simple, I have the configuration above in my
Dockerfile
anddocker-compose.yml
which basically map the port80
of my host machine to the port3000
of my container. But when I rundocker-compose run ...
, the mapping is not set between the two ports, and I cannot access my app through port80
?For the love of code everyone read the answer by @gooney47. Hours wasted…
@b4dnewz you cannot use port-mapping when using host network_mode because it’s designed to expose all ports as written in Dockerfile. (see details in the link @shin- mentioned above) = use other network mode.
@gaillota not sure about your issue but I suspect that it was run by a regular user? (port 80 needs system admins’ privilege to open)
tried with
run --service-ports
but no luck.This flag is invalid on compose up. You can only use it on compose run.
the port 3001 is not exposed, why ? And the command
docker-compose
up only start the node container not the postgresl containerI’m having a problem with the ports on the .yml file it seems to be not working, or maybe I’m doing something wrong.
Dockerfile
docker-compose.yml
And when I inspect the container the Network output is the following:
The only work around for this is running:
docker-compose run -p 1234:443 backend
With that I got:
Any idea?