Docker compose bind failed: port is already allocated

懵懂的女人 提交于 2021-02-18 12:09:19

问题


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

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.

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. I am not sure what the service should be called so I went with "src" (see DockerFile below). But wondering if it should be app since server file is app.js?

2. Getting "Bind for 0.0.0.0:8080 failed: port is already allocated".

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

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:


  1. Getting "Bind for 0.0.0.0:8080 failed: port is already allocated".

you have duplicated port allocations.

  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.

  2. 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.

therefore, you can shrink your ports section to:

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

I am not sure what the service should be called

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.




回答2:


you just need to open docker.yml file and change your port address.....this happen for me because already that container was used by my company another member

example from 0.0.0.0:80==>0.0.0.0:8000 and also port from ports:- 80/80 to ports:- 8000:8000



来源:https://stackoverflow.com/questions/56285989/docker-compose-bind-failed-port-is-already-allocated

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