Create a Mongodb/NodeJS app with data persistence.
This covers docker networking, volumes, Docker registries, Image/Container management and Docker compose.
- Clone the repository to your localmachine
- Build the frontend image with below command
$ docker build -t mongoapp . - Tag the image and upload in Docker hub. (optional step)
$ docker tag mongoapp <<-your-dockerhub-username->>/mongoapp:1.0
$ docker push <<-your-dockerhub-username->>/mongoapp:1.0 - Run docker compose to create Mongodb and Mongoexpress
$ docker-compose up -d - Goto Mongo express : http://localhost:8080
- Create a database in Mongodb as "my-db"
- Create a collection "users"
- Create the frontend container with below command using the image created in 1st step. (Please debug minor issues if there are any & start the container. You can also pull the image from docker hub where you uploaded the image earlier)
$ docker run --name mongoapp --network docker_default -dp 3000:3000 mongoapp - Visit the local webpage at http://localhost:3000
You can update the data and verify that data is persisting even after container restarts/deletion (You may delete the mongoapp container and create a new one to verify data persistence)
- Clear everything
$ docker rm -f mongoapp
$ docker-compose down
$ docker-compose down --volume
Exercise
- In this demo we are creating the frontend with a Dokerfile and the backend with a docker compose file (Mongodb + Mongoexpress). However we can put everything together in docker compose file. We can then create & destroy everything with single command (docker-compose down).
- Please create an end to end docker compose file by the weekend and share with me.