Docker Compose绑定失败:端口已分配 [英] Docker compose bind failed: port is already allocated

查看:45
本文介绍了Docker Compose绑定失败:端口已分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图将socketio服务器从EC2转移到Docker.

I have been trying to get a socketio server moved over from EC2 to Docker.

我已经能够通过Web(http)客户端连接到套接字,但是似乎不可能通过iOS或Android直接连接到套接字.

I have been able to connect to the socket via a web (http) client, but connecting directly to the socket via iOS or Android seems to be impossible.

我读到一个问题,可能是使用Docker时暴露的端口实际上并未发布.由于我们的移动应用程序当前在经典EC2实例上的端口8080上连接.我设置了一个docker-compose.yml文件来尝试打开所有端口和通信协议,但是我有两个问题:

I read one of the issues can be the ports exposed are not actually published when using Docker. Since our mobile apps currently connect on port 8080 on our classic EC2 instance. I setup a docker-compose.yml file to try and open all ports and communication protocals, but I am two issues:

1.我不确定应该调用什么服务,所以我选择了"src"(请参阅​​下面的DockerFile).但是由于服务器文件是app.js,想知道是否应该是app?

2.获取"0.0.0.0:8080绑定失败:端口已分配".

DockerFile

DockerFile

FROM ubuntu:14.04

ENV DEBIAN_FRONTEND noninteractive

RUN mkdir /src
ADD package.json /src

RUN apt-get update
RUN apt-get install --yes curl
RUN curl --silent --location https://deb.nodesource.com/setup_4.x | sudo bash -
RUN apt-get install --yes nodejs
RUN apt-get install --yes build-essential

RUN update-alternatives --install /usr/bin/node node /usr/bin/nodejs 10



RUN cd /src; npm install
RUN npm install --silent socket.io@0.9.14


WORKDIR /src




# Bundle app source
# Trouble with COPY http://stackoverflow.com/a/30405787/2926832
COPY . /src

ADD app.js /src/


EXPOSE 8080

CMD ["node", "/src/app.js"]

Docker-Compose.yml

Docker-Compose.yml

src:
  build: .
  volumes:
    - ./:/src
  expose:
    - 8080
  ports:
    - "8080"
    - "8080:8080/udp"
    - "8080:8080/tcp"
    - "0.0.0.0:8080:8080"
    - "0.0.0.0:8080:8080/tcp"
    - "0.0.0.0:8080:8080/udp"
  environment:
    - NODE_ENV=development
    - PORT=8080
  command:
    sh -c 'npm i && node server.js'
    echo 'ready'

推荐答案

  1. 获取"0.0.0.0:8080绑定失败:端口已分配".

您有重复的端口分配.

  1. ,如果未指定连接类型,则端口默认为 tcp :,表示"0.0.0.0:8080:8080&"; "0.0.0.0:8080:8080/tcp" 都试图绑定到相同的端口,因此也会出现错误.

  1. when not specifying a connection type, the port defaults to tcp: meaning "0.0.0.0:8080:8080" and "0.0.0.0:8080:8080/tcp" both trying to bind to the same port and hence your error.

因为 docker使用 0.0.0.0 用于默认绑定,同样适用于"8080:8080/tcp" "0.0.0.0:8080:8080/tcp" -两者都不需要.

since docker uses 0.0.0.0 for default binding, same applies to "8080:8080/tcp" and "0.0.0.0:8080:8080/tcp" - you have no need in both of them.

因此,您可以将 ports 部分缩小为:

therefore, you can shrink your ports section to:

   ports:
    - "8080:8080"
    - "8080:8080/udp"


我不确定应该调用什么服务

I am not sure what the service should be called

完全取决于您.通常,服务是根据服务的内容或在网络中的角色来命名的,例如 nginx_proxy laravel_backend 等.因此, node_app 对我来说听起来不错, app 在小型网络中也可以, src 似乎没有任何意义,但是再次提醒您-它只是您服务的一些标识符,没有任何其他作用.

it is completely up to you. usually services are named after their content, or role in the network, such as nginx_proxy laravel_backend etc. so node_app sounds good to me, app is also ok in small networks, src doesnt appear to have any meaning but again - it is just some identifier for your service, without any additional effect.

这篇关于Docker Compose绑定失败:端口已分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆