Docker端口转发

时间:2014-12-27 06:52:59

标签: django http ubuntu docker

我正在尝试在docker容器中运行我的django应用程序。我正在使用uWSGI来为django应用程序提供服务,并且还有一个芹菜工作者在后台运行。这些过程由supervisord启动。

我遇到的问题是我无法在端口上看到我希望看到它的应用程序。我在8080上暴露端口8080并运行uwsgi进程,但无法在ip地址$(boot2docker ip):8080的浏览器中找到我的应用程序。我刚收到谷歌Chrome浏览器'这个网页不可用'。 (我使用的是Mac,所以我需要获取boot2docker的ip地址)。容器显然正在运行,并报告我的uwsgi和芹菜进程也都成功运行。

当我运行docker exec CONTAINER_ID curl localhost:8080时,我收到了像

这样的回复
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    21    0    21    0     0   3150      0 --:--:-- --:--:-- --:--:--  3500

...所以看起来容器正在接受端口8080上的连接。

当我运行docker exec CONTAINER_ID netstat -lpn |grep :8080时,我得到tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 11/uwsgi

当我运行docker inspect CONTAINER_ID时,我得到以下内容:

[{
    "AppArmorProfile": "",
    "Args": [
        "-c",
        "/home/docker/code/supervisor-app.conf"
    ],
    "Config": {
        "AttachStderr": true,
        "AttachStdin": false,
        "AttachStdout": true,
        "Cmd": [
            "supervisord",
            "-c",
            "/home/docker/code/supervisor-app.conf"
        ],
        "CpuShares": 0,
        "Cpuset": "",
        "Domainname": "",
        "Entrypoint": null,
        "Env": [
            "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
            "env=staging"
        ],
        "ExposedPorts": {
            "8080/tcp": {}
        },
        "Hostname": "21443d8a16df",
        "Image": "vitru",
        "Memory": 0,
        "MemorySwap": 0,
        "NetworkDisabled": false,
        "OnBuild": null,
        "OpenStdin": false,
        "PortSpecs": null,
        "StdinOnce": false,
        "Tty": false,
        "User": "",
        "Volumes": null,
        "WorkingDir": ""
    },
"Created": "2014-12-27T01:00:22.390065668Z",
    "Driver": "aufs",
    "ExecDriver": "native-0.2",
    "HostConfig": {
        "Binds": null,
        "CapAdd": null,
        "CapDrop": null,
        "ContainerIDFile": "",
        "Devices": [],
        "Dns": null,
        "DnsSearch": null,
        "ExtraHosts": null,
        "Links": null,
        "LxcConf": [],
        "NetworkMode": "bridge",
        "PortBindings": {},
        "Privileged": false,
        "PublishAllPorts": false,
        "RestartPolicy": {
            "MaximumRetryCount": 0,
            "Name": ""
        },
        "SecurityOpt": null,
        "VolumesFrom": null
    },
    "HostnamePath": "/mnt/sda1/var/lib/docker/containers/21443d8a16df8e2911ae66d5d31341728d76ae080e068a5bb1dd48863febb607/hostname",
    "HostsPath": "/mnt/sda1/var/lib/docker/containers/21443d8a16df8e2911ae66d5d31341728d76ae080e068a5bb1dd48863febb607/hosts",
    "Id": "21443d8a16df8e2911ae66d5d31341728d76ae080e068a5bb1dd48863febb607",
    "Image": "de52fbada520519793e348b60b608f7db514eef7fd436df4542710184c1ecb7f",
    "MountLabel": "",
    "Name": "/suspicious_fermat",
    "NetworkSettings": {
        "Bridge": "docker0",
        "Gateway": "172.17.42.1",
        "IPAddress": "172.17.0.87",
        "IPPrefixLen": 16,
        "MacAddress": "02:42:ac:11:00:57",
        "PortMapping": null,
        "Ports": {
            "8080/tcp": null
        }
    },
"Path": "supervisord",
    "ProcessLabel": "",
    "ResolvConfPath": "/mnt/sda1/var/lib/docker/containers/21443d8a16df8e2911ae66d5d31341728d76ae080e068a5bb1dd48863febb607/resolv.conf",
    "State": {
        "ExitCode": 0,
        "FinishedAt": "0001-01-01T00:00:00Z",
        "Paused": false,
        "Pid": 16230,
        "Restarting": false,
        "Running": true,
        "StartedAt": "2014-12-27T01:00:22.661588847Z"
    },
    "Volumes": {},
    "VolumesRW": {}
}
]

作为一个不太熟悉Docker的人,我不太确定这一切意味着什么。也许有一个线索,为什么我无法连接到我的服务器?

这是我的Dockerfile,所以你可以看看我是否在做那些明显错误的事情。

FROM ubuntu:14.04

# Get most recent apt-get
RUN apt-get -y update

# Install python and other tools
RUN apt-get install -y tar git curl nano wget dialog net-tools build-essential
RUN apt-get install -y python3 python3-dev python-distribute
RUN apt-get install -y nginx supervisor
# Get Python3 version of pip
RUN apt-get -y install python3-setuptools
RUN easy_install3 pip

RUN pip install uwsgi

RUN apt-get install -y python-software-properties

# Install GEOS
RUN apt-get -y install binutils libproj-dev gdal-bin

# Install node.js
RUN apt-get install -y nodejs npm

# Install postgresql dependencies 
RUN apt-get update && \
    apt-get install -y postgresql libpq-dev && \
    rm -rf /var/lib/apt/lists

# Install pylibmc dependencies
RUN apt-get update
RUN apt-get install -y libmemcached-dev zlib1g-dev libssl-dev

ADD . /home/docker/code

# Setup config files
RUN ln -s /home/docker/code/supervisor-app.conf /etc/supervisor/conf.d/

# Create virtualenv and run pip install

RUN pip install -r /home/docker/code/vitru/requirements.txt

# Create directory for logs
RUN mkdir -p /var/logs

# Set environment as staging
ENV env staging

EXPOSE 8080

# The supervisor conf file starts uwsgi on port 8080 and starts a celeryd worker
CMD ["supervisord", "-c", "/home/docker/code/supervisor-app.conf"]

1 个答案:

答案 0 :(得分:8)

我相信你遇到的问题是EXPOSE只能使容器之间的端口可用...而不是主机系统。请参阅此处的文档:
https://docs.docker.com/reference/builder/#expose

您需要通过-p标志为docker run标志“发布”该端口:
https://docs.docker.com/reference/run/#expose-incoming-ports

如果你使用它,在fig.yml文件中的exposeports指令之间有一个类似的区别。