i have a sharelatex container running. This docker compose file contains a mongo and redis container.
Here is the sharelatex docker compose:
version: '2'
services:
sharelatex:
restart: always
image: rigon/sharelatex-full
#image: sharelatex/sharelatex
container_name: sharelatex
networks:
- test-network
depends_on:
- mongo
- redis
privileged: true
links:
- mongo
- redis
volumes:
- ./sharelatex_data:/var/lib/sharelatex
- /var/run/docker.sock:/var/run/docker.sock
environment:
SHARELATEX_MONGO_URL: mongodb://mongo/sharelatex
SHARELATEX_REDIS_HOST: redis
SHARELATEX_APP_NAME: ShareLaTeX
SHARELATEX_SITE_URL: https://latex.tkwant.de
mongo:
restart: always
image: mongo
container_name: mongo
expose:
- 27017
volumes:
- ./mongo_data:/data/db
networks:
- test-network
redis:
restart: always
image: redis
container_name: redis
networks:
- test-network
expose:
- 6379
volumes:
- ./redis_data:/data
networks:
test-network:
external: true
I want to create a node application which needs mongodb, too. How can i connect these two container? I read about network and tried out docker network but without success.
This is my node docker compose:
version: '3.5'
services:
app:
container_name: app
restart: always
build: .
ports:
- '3001:3000'
networks:
- test-network
networks:
test-network:
driver: external
and here my index.js:
// Connect to MongoDB
mongoose
.connect(
'mongodb://mongo:27017/test2',
{ useNewUrlParser: true }
)
.then(() => console.log('MongoDB Connected'))
.catch(err => console.log("errorErrorError"));
I am open for all answers... running mongo in an own container or create a docker network. But I dont know what is the best or the easiest.
Update 1:
First Problem: Sharelatex does not work anymore --> sharelatex is now in another network. My nginx reverse proxy does not find sharelatex container anymore. Second Problem is: when i want to start node docker i get this error (dont know why but it want to create a new network): Creating network "dockernodemongo_test-network" with driver "external" ERROR: plugin "external" not found
For connecting to your local MongoDB instance from a Container you must first allow to accept connections from the Docker bridge gateway. To do so, simply add the respective gateway IP in the MongoDB config file /etc/mongod. conf under bindIp in the network interface section.
To connect a Node. js application to MongoDB, we have to use a library called Mongoose. mongoose. connect("mongodb://localhost:27017/collectionName", { useNewUrlParser: true, useUnifiedTopology: true });
For containers to communicate with other, they need to be part of the same “network”. Docker creates a virtual network called bridge by default, and connects your containers to it. In the network, containers are assigned an IP address, which they can use to address each other.
You can try something like this.
Create a docker network as follows.
docker network create <NETWORK_NAME>
In your sharelatex docker compose, you can add this network (test-network is the name of the network) like this
services:
mongo:
restart: always
image: mongo
container_name: mongo
networks:
- test-network
expose:
- 27017
volumes:
- ./mongo_data:/data/db
networks:
test-network:
external: true
Similarly, you can do the same (use same network name) in the docker compose file for your node application.
Once both the containers are in the same network, you can use the container name as the host name to connect to it.
Additionally, you can verify if the containers are running in the same network using the following command
docker network <NETWORK_NAME> inspect
P.S.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With