gpt4 book ai didi

django - Docker 端口转发

转载 作者:IT老高 更新时间:2023-10-28 21:22:44 24 4
gpt4 key购买 nike

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

我遇到的问题是我无法在我希望看到的端口上看到应用程序。我正在公开端口 8080 并在 8080 上运行 uwsgi 进程,但在浏览器中的 ip 地址 $(boot2docker ip):8080 上找不到我的应用程序。我刚刚得到谷歌浏览器的“此网页不可用”。 (我使用的是 Mac,所以我需要获取 boot2docker 的 IP 地址)。容器显然正在运行,并报告我的 uwsgi 和 celery 进程也都成功运行。

当我运行 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"]

最佳答案

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

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

如果你使用 Fig.yml 文件中的 exposeports 指令,在 Fig 中也有类似的区别。

关于django - Docker 端口转发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27665100/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com