Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker compose bind failed: port is already allocated

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 [email protected]


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'
like image 246
jdog Avatar asked May 24 '19 04:05

jdog


1 Answers

  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.

like image 85
Efrat Levitan Avatar answered Sep 21 '22 03:09

Efrat Levitan