Docker/C2/Docker-Compose/English
Visual Cue | Narration |
Show slide:
Welcome |
Hello and welcome to the Spoken Tutorial on “Docker Compose”. |
Show slide:
Learning Objectives |
In this tutorial, we will learn about:
|
Show slide:
System Requirements |
To record this tutorial, I am using
|
Show slide:
Prerequisites |
To follow this tutorial:
|
Show slide:
Code files |
|
Show slide:
Docker Compose |
|
Show slide:
YAML |
|
Show slide:
Flowchart |
This flowchart explains the process of using Docker Compose.
We will run two images using Docker compose. |
Hover over Docker Hub block. | The images will be pulled from Docker Hub. |
Hover over node block. | The first is the node image. |
Hover over Express server application code block. | It will be used to run an Express web server.
Express is a nodejs framework. It will allow us to view the data in the mongodb database, through the browser. |
Hover over mongo block. | The second is the mongo image which will run a mongodb database.
The Express server will connect to the mongodb database. |
Hover over mongosh block. | We will add data to the database using the mongosh shell. |
Hover over browser block. | Then we will view the inserted data in the browser using the Express server. |
Hover over Host ports block. | We have shown the port mappings in this diagram as well.
We will go through them in detail in the code. |
Point to the files in the Downloads folder
Open the mongo-express directory. |
I have created the source files required for this demonstration.
Let us open the mongo hyphen express directory. |
Select and highlight the app.js file. | The app dot js file is a JavaScript file.
It contains code for a web server developed using the Express framework. The web server connects to the mongodb database. |
Select and highlight the mongo-express.yaml file.
Open the file mongo-express.yaml in the text editor. |
The mongo hyphen express dot yaml file configures Docker Compose.
Let us view the contents of this file. Open the file in the text editor. |
Highlight services: | The top element of the file is services.
Each service can hold a single or multiple containers of an image. It also defines various properties for the containers. |
Highlight mongodb-service. | The name of the first service is mongodb hyphen service.
Note that the name of the service need not be the same as the image or container. On the next indentation level, we will define the properties for the service. |
Highlight container_name | First we define the name of the container as mongodb hyphen container for the image.
If you do not specify the container name, a random name will be assigned. |
Highlight image | The name of the image is mongo. |
Highlight environment | The environment key sets the environment variables.
The variables are specified as a list. We set two environment variables here. |
Highlight MONGO_INITDB_ROOT_USERNAME | First is the name of the root user. |
Highlight MONGO_INITDB_ROOT_PASSWORD | The second is the root user password. |
Highlight ports | Finally, we specify port bindings for the container.
Multiple bindings can be specified using a list. |
Highlight 127.0.0.1:27017:27017 | Here, we are specifying only one port binding.
The port 27017 on localhost, is mapped to port 27017 of the container. |
Only narration. | Next we will go through the service description for the Express server. |
Highlight express-service | The name of the service is express hyphen service. |
Highlight container_name under node | The container name is express hyphen container. |
Highlight image | The image is node. |
Highlight user | The user key sets the user under which the container process runs.
This can be used to give different levels of permissions to the container. Here we are setting the user to node. |
zHighlight working_dir | Then we set the working directory using the working underscore dir key.
It sets the working directory inside the container for executing commands. |
Highlight environment | Next we specify the environment variables. |
Highlight MONGODB_USERNAME | MONGODB underscore USERNAME sets the username for accessing the database. |
Highlight MONGODB_PASSWORD | MONGODB underscore PASSWORD specifies the password of the user.
The username and password should be the same as set under mongodb service. |
Highlight MONGODB_CONTAINER_NAME | MONGODB underscore CONTAINER underscore NAME sets the mongodb container name.
This name is required to connect to the database from the Express server. |
Highlight MONGODB_CONTAINER_PORT | MONGODB underscore CONTAINER underscore PORT specifies mongodb container port.
The value must match the port specified under mongodb service. |
Highlight volumes | Volumes are used to persist data between the container runs.
We shall discuss volumes in detail in upcoming tutorials. |
Highlight ports | Next we set the port bindings. |
Highlight 127.0.0.1:3000:3000 | We shall access our web server at port 3000 on localhost. |
Highlight depends_on | The depends_on key specifies other services that this service depends on. |
Highlight mongodb-service | The express service will start after the mongodb service has started. |
Highlight command | The command key is used to set the container commands.
These are run everytime the container is started. |
Highlight bash -c "npm install && node app.js"
|
These commands set up a nodejs directory and install the required packages.
Then, the server is run using nodejs. |
Open a terminal window by pressing Ctrl, Alt and T keys simultaneously.
Cd Downloads/mongo-express |
Open a terminal window by pressing Ctrl, Alt and T keys simultaneously.
Go to the Downloads folder Switch to the mongo-express directory where the source code is saved. |
Switch to the mongo-express directory using cd command. | Now we will run the two images using Docker Compose. |
Type the command sudo docker compose -f mongo-express.yaml up -d | Type the command as shown here. |
Highlight -f mongo-express.yaml | The hyphen f flag is used to specify the yaml file for docker compose.
Here, the name of the file is mongo hyphen express dot yaml. |
Highlight up | The up command starts all the services mentioned in the file. |
Highlight -d | The hyphen d flag runs the services in the background. |
Press Enter to run the command. | Press Enter.
Please wait until the pull is complete. |
Highlight the output of the command. | The output shows that the containers were started. |
Type sudo docker ps and press Enter. | Type sudo space docker space ps and press Enter.
The output shows that our containers are running. |
Type sudo docker logs express-container and press Enter. | Let us check if the Express server is running.
Type sudo space docker space logs space express hyphen container. Here express hyphen container is the container running the Express server. Press Enter. |
In the output, highlight Server started at http://localhost:3000 | The output shows that our server is running at port 3000 on localhost. |
Highlight Connected to MongoDB. | It also shows that the server is connected to the mongodb database.
If you do not get this output, please wait for a few seconds. Then run the docker logs command again. |
Open Firefox web browser.
Type [1] and press Enter. |
Let us now access the server on the browser.
Open web browser. Type the address as shown. Press Enter. |
Highlight the text on the page. | The output shows that the Express server is running. |
Switch to the terminal. | Now we will add data to the mongodb database using mongosh.
Switch to the terminal. |
Type the command as shown here. | |
Highlight exec | We are using exec command to connect to the database using mongosh shell. |
Highlight –username | Make sure the username and password here are the same as set in yaml file. |
Press Enter to execute the command. | Now execute the command by pressing Enter. |
Highlight test> | You will be connected to the test database. |
To insert data into the database, type the following command. | |
Highlight db.users.insertMany | Db dot users dot insertMany inserts multiple documents into the users database. |
Highlight {name:"Karan", age:24}
Press Enter. |
We insert data of two users. It contains their name and age. Let us execute this command. Press Enter. |
Highlight insertedIds | The output shows the object ids of the inserted documents. |
Switch to the Firefox browser. | Now we shall view the list of users using our Express server.
Open the browser. |
Type the address http://localhost:3000/users in the search bar.
Press Enter. |
Type the following address in the search bar.
This navigates to the users API endpoint of the Express server. This API allows us to connect to the mongodb database. Press Enter. |
Highlight the text on the web page. | We get the list of users that we inserted in the mongosh shell.
So our express container successfully connected to the mongodb container. We did not create a Docker network for connecting the containers. Docker compose automatically creates a network for the containers. This allows them to communicate with each other. |
Show slide:
Summary |
This brings us to the end of this tutorial.
Let us summarise. In this tutorial, we have learnt about
|
Show slide:
Assignment (1) |
As an assignment, please do the following:
|
Show slide:
Assignment (2) |
|
Show slide:
Assignment (3) URL shown on slide:localhost:5000/users |
Follow the same steps as shown in this tutorial and do the following
|
Show slide:
Assignment Observation |
This is the output of the docker compose command when run with the hyphen d flag. |
Show slide:
Assignment Observation |
We inserted the following data using mongosh. |
Show slide:
Assignment Observation |
On the browser, we get names of the inserted users using the Flask web server. |
Show slide:
About Spoken Tutorial project |
The video at the following link summarises the Spoken Tutorial project.
Please download and watch it |
Show slide:
Spoken Tutorial Workshops |
The Spoken Tutorial Project conducts workshops and gives certificates.
For more details, please write to us. |
Show slide:
Answers for THIS Spoken Tutorial |
Please post your timed queries in this forum. |
Show slide:
FOSSEE Forum |
For any general or technical questions on Docker, visit the FOSSEE forum and post your question. |
Show slide:
Acknowledgement |
Spoken Tutorial Project was established by the Ministry of Education, Government of India |
Show slide:
Thank You |
This is Karthik Chandrasekhar, a FOSSEE Semester Long Intern 2024, IIT Bombay signing off.
Thanks for joining. |