无法连接到Docker容器?

时间:2022-02-12 20:55:01

I have a Node.js service built with Restify and I'm trying to use Docker to wrap it all up. My Dockerfile seems to work fine and my DB and Service boots and using docker exec I can curl REST endpoints just fine. However, the ports don't seem to get exposed. I'm on a Mac if that matters. Here's my Dockerfile:

我有一个节点。用Restify构建的js服务,我试着用Docker把它包起来。我的Dockerfile似乎工作得很好,我的DB和服务引导以及使用docker exec,我可以很好地卷起来REST端点。然而,端口似乎没有被暴露。如果那有关系的话,我就用Mac电脑。这是我的Dockerfile:

FROM ubuntu:14.04
MAINTAINER Oscar Godson

RUN apt-get update
RUN apt-get install -y nodejs-legacy
RUN apt-get install -y npm
RUN apt-get install -y mysql-server
RUN apt-get install -y mysql-client

COPY . /src
WORKDIR /src

RUN cd /src;npm install

EXPOSE 4000

CMD ./bin/installer.sh

Here's installer.sh:

installer.sh:

service mysql start
mysql -u root < ./bin/demo-data.sql
node index.js

Note: The MySQL DB is purely for testing and local dev. We use AWS RDS for production so thats why its using root access.

注意:MySQL DB仅用于测试和本地开发,我们使用AWS RDS进行生产,所以它使用root权限。

Now if I do:

如果我做的事:

docker run -p 4000:4000 86d57dae4522

And then:

然后:

curl http://localhost:4000/blah/ECC4E1D9-0E3C-4CE4-9D5B-2F2649A8F2FD

I get:

我得到:

curl: (7) Failed to connect to localhost port 4000: Connection refused

If I do it within the container with exec like:

如果我在集装箱内做,比如:

docker exec -i 86d57dae4522 curl http://localhost:4000/blah/ECC4E1D9-0E3C-4CE4-9D5B-2F2649A8F2FD

I get (this is expected since I havent logged into my service):

我得到(这是预期的,因为我没有登录我的服务):

{"message":"You don't have permissions to do that"}

Within my Restify server I did 0.0.0.0 for the host too since I saw some comments about Rails apps needing to set that so I thought I'd try it in Node/Restify but no luck.:

在我的Restify服务器中,我也为主机做了0.0.0.0.0.0,因为我看到了一些关于Rails应用需要设置的评论,所以我想我应该在Node/Restify中尝试一下,但是运气不好。

server.listen(process.env.PORT || 4000, "0.0.0.0", function() {
  console.log('%s listening at %s', server.name, server.url);
});

I've also tried a friend suggestion of:

我还尝试了一个朋友的建议:

docker run --net="host" 7500cdb

And i read that --hostname puts the IP you pass it into the /etc/hosts so I tried (this is my computers IP)

然后我读到-hostname将IP传递给/etc/hosts然后我尝试(这是我的电脑IP)

docker run --hostname="192.168.2.6" 7500cdb

No luck with any of these. Any ideas?

这些都不走运。什么好主意吗?

1 个解决方案

#1


4  

Yes, it matters very much that you're on a Mac.

是的,使用Mac电脑非常重要。

Docker doesn't run natively on the Mac, so you're running boot2docker which is setting up a little VirtualBox-based VM. It is that VM that's running Docker. When you forward some host ports, you are forwarding ports on the VM to the container. When you mount a local path on a container, it's a local path on the VM.

Docker并不是在Mac上运行的,所以你运行的是boot2docker,它建立了一个基于虚拟机的虚拟机。运行Docker的是VM。当您转发一些主机端口时,您正在将VM上的端口转发给容器。在容器上挂载本地路径时,它是VM上的本地路径。

In your example, rather than

在你的例子中,而不是

 http://localhost:4000/blah/ECC4E1D9-0E3C-4CE4-9D5B-2F2649A8F2FD

you need to run

您需要运行

  http://$(boot2docker ip):4000/blah/ECC4E1D9-0E3C-4CE4-9D5B-2F2649A8F2FD

You can also run boot2docker ip and save that address in your /etc/hosts file under a friendly name, if you like. (This can be helpful if your container is going to serve SSL certs).

您还可以运行boot2docker ip,并将该地址保存在/etc/hosts文件中,如果您愿意,可以使用友好的名称。(如果您的容器要提供SSL证书,这可能会很有帮助)。

#1


4  

Yes, it matters very much that you're on a Mac.

是的,使用Mac电脑非常重要。

Docker doesn't run natively on the Mac, so you're running boot2docker which is setting up a little VirtualBox-based VM. It is that VM that's running Docker. When you forward some host ports, you are forwarding ports on the VM to the container. When you mount a local path on a container, it's a local path on the VM.

Docker并不是在Mac上运行的,所以你运行的是boot2docker,它建立了一个基于虚拟机的虚拟机。运行Docker的是VM。当您转发一些主机端口时,您正在将VM上的端口转发给容器。在容器上挂载本地路径时,它是VM上的本地路径。

In your example, rather than

在你的例子中,而不是

 http://localhost:4000/blah/ECC4E1D9-0E3C-4CE4-9D5B-2F2649A8F2FD

you need to run

您需要运行

  http://$(boot2docker ip):4000/blah/ECC4E1D9-0E3C-4CE4-9D5B-2F2649A8F2FD

You can also run boot2docker ip and save that address in your /etc/hosts file under a friendly name, if you like. (This can be helpful if your container is going to serve SSL certs).

您还可以运行boot2docker ip,并将该地址保存在/etc/hosts文件中,如果您愿意,可以使用友好的名称。(如果您的容器要提供SSL证书,这可能会很有帮助)。