Reputation: 23302
I have an app written in NodeJS that I would like to regularly test, ideally in an isolated Docker container.
I've been following along at the tutorial here: Testing a Node.JS Application Within a Docker Container. One of the things it demonstrates, is how to run your tests inside a Docker container, and how to set up the Dockerfile. For instance:
# This official base image contains node.js and npm
FROM node:7
ARG VERSION=1.0.0
# Copy the application files
WORKDIR /usr/src/app
COPY package.json app.js LICENSE /usr/src/app/
COPY lib /usr/src/app/lib/
LABEL license=MIT \
version=$VERSION
# Set required environment variables
ENV NODE_ENV production
# Download the required packages for production
RUN npm update
# Make the application run when running the container
CMD ["node", "app.js"]
Since my app uses MongoDB, I need a running MongoDB instance to properly test my application. I would like to include MongoDB in the Docker container.
I cannot find any instructions on how to do this (or more generally, how to add any database to a container). Any tips would be greatly appreciated
Upvotes: 3
Views: 5818
Reputation: 463
I think having multiple purpose container (ie. business logic + db) is not really docker like, so it probably why you don't find anything about this. What you should propably do in this situation is use multiple containers, expose the correct port then call it from your application.
I suggest you to look into Docker-Compose to do such infrastructure, which will probably have two services : one for your node server and one for your mongoDB.
It will even be easier to maintain and configure than having all your service inside one containers, since you can split your logic easily.
EDIT: I did'nt test the following docker-compose.yml, so it'll probably need some fix, but it should help you enough if you read the documentation along side.
Starting from here, you could have a docker-compose.yml file looking like this :
version: "3"
services:
app:
container_name: app
restart: always
build: ./path/to/your/dockerfile/root
ports:
- "3000:3000"
links:
- mongo
mongo:
container_name: mongo
image: mongo
volumes:
- ./data:/data/db
ports:
- "27017:27017"
Then, inside your apps, you can access you db from this kind of url : mongodb://mongo:27017/db
Upvotes: 8