Steve Lorimer
Steve Lorimer

Reputation: 28689

docker: show open ports from linked container

If I inspect the official mongo docker image, I can see that it exposes port 27017

$ docker inspect mongo
...
"ExposedPorts": {
    "27017/tcp": {}
},
...

I have run the image, binding the internal port to the same on my host:

$ docker run -p 27017:27017 -d --name db mongo

I now run my own image in interactive mode, launching bash

$ docker run -i -t --link db:db_1 cd9b5953b633 /bin/bash

In my dockerized container, if I try to show open ports, nothing is listening.

$ netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   Path

What am I doing wrong here? How can I connect from my dockerized container to the mongo container?

If it is of some use, here is my Dockerfile:

# https://registry.hub.docker.com/u/dockerfile/nodejs/ (builds on ubuntu:14.04)
FROM dockerfile/nodejs

MAINTAINER My Name, [email protected]

ENV HOME /home/web
WORKDIR /home/web/site

RUN useradd web -d /home/web -s /bin/bash -m

RUN npm install -g grunt-cli
RUN npm install -g bower

RUN chown -R web:web /home/web
USER web

RUN git clone https://github.com/repo/site /home/web/site

RUN npm install
RUN bower install --config.interactive=false --allow-root

ENV NODE_ENV development

# Port 9000 for server
# Port 35729 for livereload
EXPOSE 9000 35729
CMD ["grunt"]

Upvotes: 1

Views: 4961

Answers (1)

creack
creack

Reputation: 121752

Docker create a Network namespace, so within your container, you will not see the exposed port of the host.

In your usecase, you do not need to run mongo with -p if you just need to access it from an other container. The --link will simply "inject" the linked container info as environement variable.

From your new container, you can do env to see the list, and you will have something like DB_1_PORT_27027_TCP_ADDR with the private IP of the mongo container where you can connect.

Upvotes: 2

Related Questions