Deploying Docker to AWS Elastic Beanstalk — how to forward port to host? (port binding)

旧街凉风 提交于 2021-02-16 14:56:46

问题


I have a project set up with CircleCI that I am using to auto-deploy to Elastic Beanstalk. My EBS environment is a single container, auto-scaling, web environment. I am trying to run a service that listens on raw socket port 8080.

My Dockerfile:

FROM golang:1.4.2

...

EXPOSE 8080

My Dockerrun.aws.json.template:

{
  "AWSEBDockerrunVersion": "1",
  "Authentication": {
    "Bucket": "<bucket>",
    "Key": "<key>"
  },
  "Image": {
    "Name": "project/hello:<TAG>",
    "Update": "true"
  },
  "Ports": [
    {
      "ContainerPort": "8080"
    }
  ]
}

I have made sure to expose port 8080 on the "role" assigned to my project environment.

I used the exact deployment script from the CircleCI tutorial linked above (except with changed names).

Within the EC2 instance that is running my EBS application, I can see that the Docker container has run successfully, except that Docker did not forward the exposed port to the host container. I have encountered this in the past when I ran docker run .... without the -P flag.

Here is an example session after SSH-ing into the machine:

[ec2-user@ip-xxx-xx-xx-xx ~]$ sudo docker ps
CONTAINER ID        IMAGE                              COMMAND                CREATED             STATUS              PORTS               NAMES
a036bb061aea        aws_beanstalk/staging-app:latest   "/bin/sh -c 'go run    3 days ago          Up 3 days           8080/tcp            boring_hoover
[ec2-user@ip-xxx-xx-xx-xx ~]$ curl localhost:8080
curl: (7) Failed to connect to localhost port 8080: Connection refused

What I expect to see is the ->8080 or whatever in the container that forwards it onto the host.

When I do docker inspect on my container, I also see that these two configurations are not what I want:

    "PortBindings": {},
    "PublishAllPorts": false,

How can I trigger a port binding in my application?

Thanks in advance.


回答1:


It turns out I made a misunderstanding in how Docker's networking stack works. When a port is exposed but not published, it is still available to the local network interface through the Docker container's private IP address. You can obtain this IP address by checking docker inspect <container>.

Rather than doing curl localhost:8080 I could do curl <containerIP>:8080.

In my EBS deploy, nginx was automatically setup to forward (HTTP) traffic from Port 80 to this internal private port as well.




回答2:


I had the same problem in a rails container (port 3000 using puma) by default rails server only binds localhost to the listening interface, I had to use -b option to bind 0.0.0.0 and that solved the problem.

In react I have no the same problem cause npm serve package binds all interfaces by default



来源:https://stackoverflow.com/questions/32317714/deploying-docker-to-aws-elastic-beanstalk-how-to-forward-port-to-host-port

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!