Docker Compose 不绑定端口

Docker Compose does not bind ports

我的容器有以下 Docker 文件:

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

我构建这个容器并用 docker run -i -t -p 8080:8080 -v /srv/source:/usr/src/app -w /usr/src/app --rm iojs-dev bash 启动镜像。 Docker 将端口 8080 绑定到主机端口 8080,这样我就可以从我的客户端访问 iojs 应用程序。一切正常。

现在我想用 docker-compose 启动我的容器,使用下面的 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

当我现在 运行 docker-compose run webfrontend bash 我无法访问主机上的端口 8080。没有端口被绑定。 docker ports的结果为空,docker inspect的结果也为空,端口设置:

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

这是 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 可能是您正在寻找的命令,它将根据您的 fig.yml(重新)创建所有容器并启动它们。

根据 documentation

,这是 docker-compose run 的故意行为

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.

克服这个问题的一种方法是使用 up 而不是 run,其中:

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

如果您使用的是 1.1.0 或更高版本,另一种方法是传递 --service-ports 选项:

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

P.S。尝试编辑原始答案,两次被拒绝。保持优雅,SO。