Search code examples
dockerfigdocker-compose

Docker Compose does not bind ports


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": []
},

Solution

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