jdog
jdog

Reputation: 10769

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'

Upvotes: 7

Views: 35968

Answers (3)

Juraj Bublinec
Juraj Bublinec

Reputation: 490

You are just running another container on the same port. You can view it by docker ps and stop it by docker stop [CONTAINER ID].

Upvotes: 8

Amit Tiwari
Amit Tiwari

Reputation: 483

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

Upvotes: 0

Efrat Levitan
Efrat Levitan

Reputation: 5632

  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.

Upvotes: 8

Related Questions