This curriculum is designed to eliminate the problems with local development by using docker-compose
instead of manually installing and configuring things like mySQL Pro, etc.
Readers/Students should be to:
- Explain why Docker exists and what problems it solves
- Identify and describe the ports each contain
- Compare and contrast Docker & Docker Compose
- Use Docker, Terminal & MongoExpress to teach basic mongoDB concepts
- Distinguish between the name of a docker image (mongo) and the container name (learn-mongo)
- Use NPM Scripts to store docker commands
- Explain and demonstrate C.R.U.D. methods
- Create a docker file for a simple HTTP node.js site (ToDo App)
- Connect the docker image to mongo
- Interview Questions
- Slides
- Dockerize Node Applications
- MongoDB CRUD Methods
- Explain C.R.U.D.
- db, use db
- Insert
- Find()
- update() $set
- update() multi
- update() $push
- remove()
- drop()
- dropDatabase()
- MongoDB Advanced Methods
- Relationships
- MongoDB Express
- Accessing the database
- Edit Documents
- Securing MongoExpress ?
- Publish Tutorial(s)?
- Slides
- Resources
Today we'll be learning how to use Docker Compose to learn MongoDB.
Students should be to:
- Use Docker Compose to start and stop mongoDB environment
- Use docker exec to access the mongoDB Shell
- Distinguish between the name of a docker image (mongo) and the container name (learn-mongo)
- Use NPM Scripts to store docker commands such as docker exec for mongo shell
Hans McMurdy is an experianced web developer and coding teacher.
Installing development software on your local machine can be problematic when you go to deploy to a production server, it may not work as expected. Using Docker helps solve this problem without having the same overhead as you would have with a full blown Virtual Machine.
Meme about it working on your machine.
Docker - use OS-level virtualization to deliver software in packages called containers. MongoDb - A no-sql database that uses JavaScript-like syntax. Mongo-Express - Dashboard for mongo.
Docker is used to create a docker image. Docker Compose is used to configure relationships between containers.
MongoDB is no-sql database with JSON-like syntax.
The docker-compose.yml file is used to define the containers that our project uses. In this case we have a basic mongo container and mongo-express which works as a dashboard to visually see the our database without needing to use shell commands.
Docker-Compose Commands
- Check whats running before your start a project
docker && docker ps
- Take note on what ports (if any are in use)
- if any are running, stop the them
docker stop $(docker ps -aq)
- Run the project
docker-compose up
- (Run in background)
docker-compose up -d
- Stop the Project (Every Time)
- Best practice is to run this
docker-compose down
- If you dont pull down the container, then it stays running which not only can slow down your computer but more importantly block the ports you may want to use for other projects.
- If you run into a problem, READ the console before asking for help.
If docker is running and we goto http://localhost:8081/ we should see the dashboard appear and look something like the following:
Note that admin, config and local are always present when you start up a new mongo database.
In order to access the mongo shell, we need to open up the terminal and run the following docker command.
docker exec -it learn-mongo mongo
It should look like the following:
What is the command doing?
Think-pair share - find a classmate and look up the official docker api docks for docker exec -it
and in your own words describe what this is doing.
learn-mongo
refers to the container name we gave for this specific mongo container. If we didn't provide a container name it would be a random name and that would be more problematic to access the specific container we wanted.
mongo
simply refers to the image.
Lets go ahead and create a new collection and use it.
Type the following into the terminal: use learnMongo
.
We can then show the database we are using by using the db
command.
Next we are going to insert some data into our new database. To do this we'll use the the insert command.
db.info.insert(
{
"country": "United States of America",
"state": "Arizona",
"cities": ["Phoenix", "Mesa", "Chandler"]
}
)
This will insert a new document into the info collection.
Exercise Next, you should come up with 2 other states, with atleast two cities in each.
Finally, we will use the .find() method to find the document with a state of Arizona.
db.info.find({state:'Arizona'}).pretty()
Note that the .pretty() command just formates the data more properly.
By the end of the exercise your terminal should look something like following but with different examples:
If we open mongo-express, we should see a new database appeared. We can also use the terminal command show dbs
to display them in terminal.
Students should be able to:
- Identify the package.json file and the script section
- Modify the package.json file to add the docker command
"mongo": "docker exec -it learn-mongo mongo",
-
Distinguish between the name of the docker image (mongo) and the container name (learn-mongo)
-
Demonstrate Mongo Terminal Commands
- Running with docker compose
docker exec -it learn-mongo mongo