Expose mongo port in other container Expose mongo port in other container docker docker

Expose mongo port in other container


You can build your custom image on top of mongodb official image, which gives you the flexibility to install additional required packages.

FROM mongo:latestRUN apt-get install ssh

Also try to use docker-compose to build and link your containers together, it will ease the process greatly.

version: '2'services:  mongo:    image: mongo:latest    ports:      - "27017"  custom_project:    build:      context: .  # Parent directory address of Dockerfile      dockerfile: Dockerfile-Custom  # Name of Dockerfile    command: /root/docker-entrypoint.sh

This is the image used for mongodb official image.


You have to make your container run on the same network. No need to ssh into your mongo or app container.

https://docs.docker.com/engine/userguide/networking/

First define a network

docker network create --driver bridge isolated_nw

Start you containers using that newly network

docker run -p 27017:27017 --network=isolated_nw -itd --name=mongo-cont mongodocker run --network=isolated_nw -itd --name=app your_image

The image of mongo includes EXPOSE 27017 so from your app container, you should be able to access to the mongo container using its name mongo-cont


You are trying to SSH into your container to gain access to it, but that isn't how you connect. Docker provides functionality to securely connect via the following methods.

Connect into a running container - Docs:

docker exec -it <container name> bash$ root@665b4a1e17b6:/#

Start a container from image, and connect to it - Docs:

docker run -it <image name> bash$ root@665b4a1e17b6:/#

Note: If it is an Alpine based image, it may not have Bash installed. In that case using sh instead of bash in your commands should work. Mongo's Dockerfile looks to use debian:jessie which will have bash support.