Sample application showing how mongodb and docker can be run in Docker containers
-
Install Docker for Windows or Docker for Mac (If you're on Windows 7 install Docker Toolbox: http://docker.com/toolbox).
-
Rename
.env.sample
to.env
file in your root directory. -
Open a command prompt at the root of the application's folder.
-
Run
docker-compose up
-
Navigate to http://localhost:3000 (http://192.168.99.100:3000 if using Docker Toolbox) with any app to interact with the API. This assumes that's the IP assigned to VirtualBox - change if needed.
A few things to note about environment varibales.
ENV | Default | Note |
---|---|---|
DB_URL |
mongodb://dockernode:password@mongo:27017/dockerNode | Database URL Where data will be stored and retrieved |
MONGODB_ADMIN_USERNAME and MONGODB_ADMIN_PASSWORD |
root and root respectively |
Database authentication details |
MONGODB_APP_USERNAME and MONGODB_APP_PASSWORD |
dockernode and root respectively |
Used for setting the credentials for the application database |
MONGODB_APP_DB |
dockerNode | Application Database name |
The API only has one endpoint which is the /users
endpoint for saving users to the database. The endpoint works with the HTTP verbs: POST
, GET
, PUT
, DELETE
.
POST
/users- INPUT:
name: John Doe
email: john.doe@gmail.com
password: johndoe
- HTTP Status:
201: created
- JSON data
{
"_id": "59071791b0lkscm2325794",
"name": "John Doe",
"email": "john.doe@gmail.com",
"password": "johndoe",
"__v": 0
}
GET
/users
[
{
"_id": "59071791b0lkscm2325794",
"name": "John Doe",
"email": "john.doe@gmail.com",
"password": "johndoe",
"__v": 0
}
]
GET
/users/:id
{
"_id": "59071791b0lkscm2325794",
"name": "John Doe",
"email": "john.doe@gmail.com",
"password": "johndoe",
"__v": 0
}
DELETE
/users/:id
User John Doe was deleted
PUT
/users/:id- INPUT:
name: Jane Doe
email: jane.doe@gmail.com
password: janedoe
- HTTP Status:
200: OK
- JSON data
{
"_id": "59071791b0lkscm2325794",
"name": "Jane Doe",
"email": "jane.doe@gmail.com",
"password": "janedoe",
"__v": 0
}
Olajide Bolaji 'Nuel - Software Developer at Andela