Docker Compose does not bind ports
Asked Answered
B

2

7

I have the following Dockerfile for my container:

FROM        centos:centos7

# Install software
RUN         yum -y update && yum clean all
RUN         yum install -y tar gzip wget && yum clean all

# Install io.js
RUN         mkdir /root/iojs
RUN         wget https://iojs.org/dist/v1.1.0/iojs-v1.1.0-linux-x64.tar.gz
RUN         tar -zxvf iojs-v1.1.0-linux-x64.tar.gz -C /root/iojs
RUN         rm -f iojs-v1.1.0-linux-x64.tar.gz

# add io.js to path
RUN         echo "PATH=$PATH:/root/iojs/iojs-v1.1.0-linux-x64/bin" >> /root/.bashrc

# go to /src
WORKDIR     /src

CMD         /bin/bash

I build this container and start the image with docker run -i -t -p 8080:8080 -v /srv/source:/usr/src/app -w /usr/src/app --rm iojs-dev bash. Docker binds the port 8080 to the host port 8080, so that I can access the iojs-application from my client. Everything works fine.

Now I want to start my container with docker-compose, using the following docker-compose.yml

webfrontend:
    image: iojs-dev
    links:
        - db
    command: bash -c "iojs test.js"
    ports:
        - "127.0.0.1:8080:8080"
    volumes:
        - /srv/source:/usr/src/app
        - /logs:/logs
db:
    image: mariadb
    environment:
        MYSQL_ROOT_PASSWORD: 12345

When I now run docker-compose run webfrontend bash I can not access the port 8080 on my host. No port was binded. The result of docker ports is empty and also the result of docker inspect is empty at the port settings:

"NetworkSettings": {
    "Bridge": "docker0",
    "Gateway": "172.17.42.1",
    "IPAddress": "172.17.0.51",
    "IPPrefixLen": 16,
    "MacAddress": "02:42:ac:11:00:33",
    "PortMapping": null,
    "Ports": {
        "8080/tcp": null
    }
},
"HostConfig": {
    "Binds": [
        "/srv/source:/usr/src/app:rw",
        "/logs:/logs:rw"
    ],
    "CapAdd": null,
    "CapDrop": null,
    "ContainerIDFile": "",
    "Devices": null,
    "Dns": null,
    "DnsSearch": null,
    "ExtraHosts": null,
    "Links": [
        "/docker_db_1:/docker_webfrontend_run_34/db",
        "/docker_db_1:/docker_webfrontend_run_34/db_1",
        "/docker_db_1:/docker_webfrontend_run_34/docker_db_1"
    ],
    "LxcConf": null,
    "NetworkMode": "bridge",
    "PortBindings": null,
    "Privileged": false,
    "PublishAllPorts": false,
    "RestartPolicy": {
        "MaximumRetryCount": 0,
        "Name": ""
    },
    "SecurityOpt": null,
    "VolumesFrom": []
},
Benitabenites answered 15/2, 2015 at 14:16 Comment(2)
The latest RC release of 1.1.0-rc2 (it's now called docker-compose) supports the --service-ports flag, which will bind the ports during fig run.Sensuous
@Sensuous Thanks, I was about to give up on my custom run script. Added to the answer.Transudate
H
4

This is intentional behavior for fig run.

Run a one-off command on a service.

One-off commands are started in new containers with the same config as a normal container for that service, so volumes, links, etc will all be created as expected. The only thing different to a normal container is the command will be overridden with the one specified and no ports will be created in case they collide.

source.

fig up is probably the command you're looking for, it will (re)create all containers based on your fig.yml and start them.

Heriot answered 16/2, 2015 at 8:6 Comment(1)
You are right and I should better RTFM ;-) It works now, thank you.Benitabenites
T
10

This is intentional behavior for docker-compose run, as per documentation:

When using run, there are two differences from bringing up a container normally:

  1. ...

  2. by default no ports will be created in case they collide with already opened ports.

One way to overcome this is to use up instead of run, which:

Builds, (re)creates, starts, and attaches to containers for a service.

Another way, if you're using version 1.1.0 or newer, is to pass the --service-ports option:

Run command with the service's ports enabled and mapped to the host.

P.S. Tried editing the original answer, got rejected, twice. Stay classy, SO.

Transudate answered 30/4, 2015 at 8:55 Comment(0)
H
4

This is intentional behavior for fig run.

Run a one-off command on a service.

One-off commands are started in new containers with the same config as a normal container for that service, so volumes, links, etc will all be created as expected. The only thing different to a normal container is the command will be overridden with the one specified and no ports will be created in case they collide.

source.

fig up is probably the command you're looking for, it will (re)create all containers based on your fig.yml and start them.

Heriot answered 16/2, 2015 at 8:6 Comment(1)
You are right and I should better RTFM ;-) It works now, thank you.Benitabenites

© 2022 - 2024 — McMap. All rights reserved.