简体   繁体   English

Docker 容器拒绝连接

[英]Connection refused by Docker container

I've struggled with this for quite some time.我已经为此苦苦挣扎了一段时间。

I have a Django application and I'm trying to package it into containers.我有一个 Django 应用程序,我正在尝试将它打包到容器中。

The problem is that when I publish to a certain port (8001) the host refuses my connection.问题是当我发布到某个端口 (8001) 时,主机拒绝了我的连接。

$ docker-machine ip default
192.168.99.100

When I try to curl or reach by browser 192.168.99.100:8001, the connection is refused.当我尝试通过浏览器 192.168.99.100:8001 卷曲或访问时,连接被拒绝。

C:\Users\meow>curl 192.168.99.100:8001
curl: (7) Failed to connect to 192.168.99.100 port 8001: Connection refused

First remark: I'm using Docker Toolbox.第一句话:我正在使用 Docker 工具箱。

Let's start from the docker-compose.yml file.让我们从 docker-compose.yml 文件开始。

version: '2'
services:
  db:
    build: ./MongoDocker
    image: ockidocky_mongo
  web:
    build: ./DjangoDocker
    image: ockidocky
    #volumes: .:/srv
    ports: 
    - 8001:8000
    links: 
    - db

Second remark: This file orginally gave me some trouble about permission building from scratch.第二点:这个文件原本给我带来了一些从头开始构建权限的麻烦。 To fix this, I built the images separately.为了解决这个问题,我单独构建了图像。

docker build -t ockidocky .
docker build -t ockidocky_mongo .

Here's the dockerfile for Mongo:这是 Mongo 的 dockerfile:

# Based on this tutorial. https://devops.profitbricks.com/tutorials/creating-a-mongodb-docker-container-with-an-attached-storage-volume/
# Removed some sudo here and there because they are useless in Docker for Windows

# Set the base image to use to Ubuntu
FROM ubuntu:latest
# Set the file mantainer
MAINTAINER meow

RUN apt-key adv --keyserver  hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10 && \
echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | tee /etc/apt/sources.list.d/mongodb.list && \
apt-get update && apt-get install -y mongodb-org


VOLUME ["/data/db"]
WORKDIR /data

EXPOSE 27017
#Edited with --smallfiles (Check this issue https://github.com/dockerfile/mongodb/issues/9)
CMD ["mongod", "--smallfiles"]

Dockerfile for Django is based on this other tutorial.用于 Django 的 Dockerfile 基于其他教程。

I won't include the code, but it works.我不会包括代码,但它有效。

It's important to say that the last row is:重要的是要说最后一行是:

ENTRYPOINT ["/docker-entrypoint.sh"]

I changed the docker-entrypoint.sh to run without Gunicorn.我将 docker-entrypoint.sh 更改为在没有 Gunicorn 的情况下运行。

echo Start Apache server.
python manage.py runserver

At this point docker ps tells me that everything is up:此时docker ps告诉我一切正常:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
ddfdb20c2d7c        ockidocky             "/docker-entrypoint.s"   9 minutes ago       Up 9 minutes        0.0.0.0:8001->8000/tcp   ockidocky_web_1
2e2c2e7a5563        ockidocky_mongo       "mongod --smallfiles"    2 hours ago         Up 9 minutes        27017/tcp                ockidocky_db_1

When I run a docker inspect ockidocky and about ports, it displays:当我运行 docker inspect ockidocky 和关于端口时,它显示:

"Ports": {
                "8000/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "8001"
                    }
                ]
            },

Is this dependant on mounting volumes?这取决于安装卷吗?

It is one of the things I really can't figure out and gives me a lot of errors with Docker Toolbox.这是我真的无法弄清楚的事情之一,并且在使用 Docker Toolbox 时给了我很多错误。

As far as I can see everything worked fine during the build, and as far as I know the connection that was refused shouldn't depend on that.据我所知,在构建过程中一切正常,据我所知,被拒绝的连接不应该依赖于此。

EDIT: After connectinc to the container and listing the processes with ps -aux , this is what I see:编辑:在连接到容器并使用ps -aux列出进程ps -aux ,这就是我所看到的:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.7  3.0 218232 31340 ?        Ssl  20:15   0:01 python manage.p
root         9 13.1  4.9 360788 50132 ?        Sl   20:15   0:26 /usr/bin/python
root        15  0.0  0.2  18024  2596 ?        Ss   20:15   0:00 /bin/bash
root        20  0.1  0.3  18216  3336 ?        Ss   20:17   0:00 /bin/bash
root        33  0.0  0.2  34424  2884 ?        R+   20:18   0:00 ps -aux

Ps Feel free to suggest how I can make this project easier for myself. Ps 随意建议我如何让这个项目更容易。

I solved the issue.我解决了这个问题。 I don't know why I had to specify the door on this line of docker-entrypoint.sh :我不知道为什么我必须在docker-entrypoint.sh这一行上指定门:

python manage.py runserver 0.0.0.0:8000

Now docker logs ockidocky_web_1 shows the usual django output messages.现在docker logs ockidocky_web_1显示了通常的 django 输出消息。

If someone could give a proper explanation, I would be happy to edit and upvote.如果有人可以给出适当的解释,我很乐意编辑和投票。

我遇到了同样的问题,此外,Django settings.py 中的 ALLOWED_HOSTS 需要包含 docker 机器 IP。

It's mostly because of failure on the service you are going to run on your desired port(In your case the desired port is 8001)!这主要是因为您要在所需端口上运行的服务失败(在您的情况下,所需端口是 8001)!

If any networking checks is OK and you don't have any problem with your network, just check your service which going to listen on your desired port!如果任何网络检查都正常并且您的网络没有任何问题,只需检查您的服务,该服务将在您想要的端口上侦听! With the high chance of probabilty, your service is not loaded or ran successfully!很有可能,您的服务没有加载或运行成功!

The check for your service depends on the service you are running, but sometimes(most of the times) docker logs YOUR_CONTAINER_ID could help to know more about your failure reason!对您的服务的检查取决于您正在运行的服务,但有时(大多数情况下) docker logs YOUR_CONTAINER_ID可以帮助您了解更多有关失败原因的信息!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM