Converting docker-compose file to bash to auto run tests

Multi tool use
Multi tool use


Converting docker-compose file to bash to auto run tests



I'm currently working on an app and I want to auto test express before i push my code to production.



I have a docker-compose file which I can use to run my tests


version: '8'
services:
express:
build: services/comp/express
ports:
- "3000:3000"
database:
image: mongo:3.4.0
ports:
- "27017:27017"



However I want to be able to run these through a bash script instead (I don't want two docker-compose files).



I've tried the command


docker run -d -p 27017:27017 mongo:3.4.0



to run my database and to run my express server i've tried


docker build -t express_image ./comp/services/express/.
docker run -p 3000:3000 -t express_image



However, I can't get my tests running like this. I'm getting a cannot connect to database on first try error so I presume I'm using the wrong host for my database. When using a docker-compose file, I tried
const dbHost = 'mongodb://database/test-db';
to connect to the data base which works. While trying to run with the above commands, I've tried


const dbHost = 'mongodb://database/test-db';


const dbHost = 'mongodb://localhost:27017/';



Any advice on what I'm doing wrong to spin up the two containers through a terminal?




2 Answers
2



What i understand from above.. while using single docker-compose to auto test experess you have used const dbHost ='mongodb://database/test-db' and it worked fine.


const dbHost ='mongodb://database/test-db'



Now you have spin the container without docker-compose and trying to access db from express container using const dbHost = 'mongodb://localhost:27017/';


const dbHost = 'mongodb://localhost:27017/';



Reason for not working.. here local host is your express container not your Docker host machine where you have mapped mongodb container port.



Replace with -> const dbHost = 'mongodb://${docker_host_ip}:27017/';


const dbHost = 'mongodb://${docker_host_ip}:27017/';



Docker_host_ip will be the ip of docker_bridge for networking. Like 172.16.0.1(for Docker host machine).





Doesn't solve the error. Still getting can't connect on first attempt. Thanks for the answer though
– JNMN
2 days ago





if possible, can you please provide sample code(simple code making DB connection and express docekrfile) of what you are trying to do. so that i can produce issue on my system and check.
– Rohit Jindal
2 days ago



You need to do a couple of things to cause Docker to create and use an internal network, so its internal DNS resolution works. Docker Compose does this automatically for you, but if you're running Docker commands by hand you need to do it manually.



First, create the network, with totally default options:


docker network create mean



Then when you run the containers, attach them to this network, and explicitly give them names:


docker run -d --net mean --name database -p 27017:27017 mongo:3.6.2
docker build -t expressapp ./services/express
docker run -d --net mean --name express -p 3000:3000 expressapp



If the database location is in a constant in code as you've illustrated, you'll find this inflexible if you ever change the database location. You might set it from an environment variable, using process.env in your Node code and the docker run -e option to set the actual location when you run the container.


process.env


docker run -e





Doesn't solve the error. Still getting can't connect on first attempt. Thanks for the answer though
– JNMN
2 days ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

EbWxfP 8,FW2O,clAOEsUBEh4M628SIf EUhIgQukreK itwrpB,GD35qb8GJ3Qa ca OylN,4ft2 rwqdGAPI,4q,Jc21IoobBA6lRg
3,44kX,0mGSApkTEKRsqa

Popular posts from this blog

Delphi Android file open failure with API 26

.

Amasya