Skip to content

Local mongo replicaset for development using docker-compose

License

Notifications You must be signed in to change notification settings

sajithneyo/localmongo

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Localmongo

Local mongo replicaset running mongo:4.2-bionic for development using docker-compose.

Setup

Simply run:

docker-compose up

This should bring up 3 containers of mongodb, running a replicaset called rs0.

Application

In your main application, you need to adjust your docker-compose in order for your application to be able to connect to the mongo containers. Use the below template of docker-compose.yml:

version: "3"
services:
  app:
    build: .
    networks:
      - default
      - localmongo
    external_links:
      - localmongo1:mongo1
      - localmongo2:mongo2
      - localmongo3:mongo3
networks:
  localmongo:
    external:
      name: localmongo_default

Troubleshooting

Assuming you clone this repo into a folder called localmongo, upon running docker-compose, docker will create a network called localmongo_default. You can view this by running docker network ls, and also by inspecting docker inspect localmongo1.

In the application's docker-compose.yml, we are specifying that the app must join 2 networks: the default one, as well as localmongo network, which is declared at the end of the file, pointing to an external network localmongo_default.

To troubleshoot connectivity issues, do docker exec -it <appContainerID> bash into your application container and look at cat /etc/hosts. You should see 2 entries for the container, one per network. You should be able to do ping mongo1.

If it does not work, do docker inspect <appContainerID> and proceed from there. To clean up networks, use with care: docker network prune

Checkout the documentation of docker-compose networking.

Credits

Kira Layto for their Stack Overflow code snippet.

About

Local mongo replicaset for development using docker-compose

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Shell 100.0%