Skip to content

Many ports exposed, constraints it from scaling the containers due to address conflict #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rajiff opened this issue Sep 22, 2017 · 6 comments

Comments

@rajiff
Copy link

rajiff commented Sep 22, 2017

If we use cassandra as parent docker image we cannot scale such containers more than one, because it gets into error saying ADDRESS IN USE, as base docker image exposes so many ports

Can we have a mechanism to not expose them, instead expose them at run time or in the extended Dockerfile?

OR can we comment this line in the Dockerfile EXPOSE 7000 7001 7199 9042 9160

@yosifkit
Copy link
Member

Expose does not control the ports that are published to the host. They only define the "contract" of ports that this container might use (which might not use all but it depends on configuration). The mapping to host ports is controlled at run time. Just map the ports to different ports for each container you want to run or let Docker choose random unused ports for you.

$ # manually picking ports:
$ docker run -d -p 7001:7001 -p 7199:7199 -p 9042:9042 --name cass1 cassandra
$ docker run -d -p 7002:7001 -p 7200:7199 -p 9043:9042 --name cass2 cassandra

$ # or let docker pick ports for all items defined by EXPOSE
$ docker run -d -P --name cass1 cassandra
$ docker run -d -P --name cass2 cassandra
$ docker run -d -P --name cass3 cassandra
...

$ # you can even map any port of the container, but that doesn't mean it will actually 
$ # respond there and doesn't even need an EXPOSE defined in the Dockerfile
$ docker run -d -p 8080:80 --name cass1 cassandra

@rajiff
Copy link
Author

rajiff commented Sep 25, 2017

I am using docker-compose to run the container, in which I have not mapped the ports at all

Still when I scale the instance to more than one, I get this error, hence I am of opinion the ports are getting mapped automatically since the image has suggested that it is exposing these ports

Am I missing something?

@tianon
Copy link
Member

tianon commented Sep 26, 2017

Can you share your docker-compose.yml contents? The image itself cannot automatically publish ports, so something else needs to be going on here. 😕

@rajiff
Copy link
Author

rajiff commented Sep 27, 2017

Hi

below find them

=== START OF DOCKERFILE =====

FROM cassandra:3.11.0

RUN apt-get update && apt-get install -y curl
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash -
RUN apt-get install -y nodejs

RUN apt-get update \
    && npm install yarn -g \
    && npm install mocha -g

RUN mkdir -p /usr/app

WORKDIR /usr/app

COPY package.json .

RUN yarn

COPY . .

ENV CASSANDRA_URL='cassandra://localhost:9042/'

CMD ["npm", "start"]

=== END OF DOCKERFILE =====

=== START of docker-compose.yml ====

version: '2'
services:

  nodejscassandra:
    image: nodejscassandra:0.1.0
    build: ./
    network_mode: host
    restart: always
    volumes:
      - ../data:/data

=== END of docker-compose.yml ====

@yosifkit
Copy link
Member

The root of the problem is network_mode: host; you cannot scale with that since any process in the container will just bind to its regular ports and then the second container would fail since the ports it wants to use are already taken by the first since they are using the same network namespace (host). You'll want to instead publish any needed ports to the host.

Not related to your port conflicts, but a second problem is that cassandra is not running in the container; the only process started in the container is npm start (so your node app is probably just hitting a cassandra that you are running locally on the host). The recommended fix is two run two containers, one for your node app, one for cassandra; that way each can be scaled as needed. Something like the following, though you'll need to do more to to make cassandra work in a cluster (some discussion here: #94).

version: '2'
services:

  nodejs:
    image: customnodejs:0.1.0
    build: ./
    restart: always
    environment:
      - 'CASSANDRA_URL=cassandra://cassandradb:9042/'
    volumes:
      - ../data:/data
  cassandradb:
    image: cassandra:3.11.0

@rajiff
Copy link
Author

rajiff commented Oct 4, 2017

Yes, network mode is a issue it can be solved by switching to Bridge, but I think docker should have got something to override the port expose in the extending image

However I solved it using a shell script, which I wrote, which starts cassandra and then runs the node code, I also switched to Network mode bridge though I wanted it as host

I am closing this issue as of no

@rajiff rajiff closed this as completed Oct 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants