Learn MongoDB 4.x
上QQ阅读APP看书,第一时间看更新

Creating the demo environment using Docker and Docker Compose

Docker is a virtualization technology that has significant advantages over others (for example, VirtualBox or VMware) in that it directly uses resources from the host computer and is able to share those resources between virtual machines (VMs) (referred to as containers in the Docker reference guide). In this section, you learn how to configure a Docker container in which you can practice installing MongoDB following the discussion in this chapter. Following that, you learn how to configure a Docker container that can be used to practice using the source code and MongoDB query examples found in Chapter 3, Essential MongoDB Administration Techniques, to Chapter 12, Developing in a Secured Environment, of this book.

As with many software environments available these days, Docker is free to install and use for your own personal purposes (for example, learning MongoDB!). Please see the Docker Terms of Service ( https://www.docker.com/legal/docker-terms-service) if you plan to include Docker in a project for hire. The other virtual environments mentioned are VirtualBox ( https://www.virtualbox.org/) and VMware ( https://www.vmware.com/).
Installing Docker and Docker Compose

Before you can recreate the demo environment used to follow examples from this book, you will need to install Docker. In this section, we will not cover the actual installation of Docker as it's already well documented. Please note that in all cases, you will need to make sure that hardware virtualization support has been enabled for your computer's basic input/output system (BIOS). Also, for Windows, you will install Docker Desktop on Windows rather than the straight binary executable, as is the case for Linux- or Mac-based operating systems. In addition, for Windows, you will need to enable the Windows hypervisor. The Windows installation steps will give you more information on this.

In addition, to recreate the demo environment, you will need to install Docker Compose. Docker Compose is an open source tool provided by Docker that allows you to install a Docker container defined by a configuration file. As with Docker itself, installation instructions vary slightly between Linux, Windows, and macOS computer hosts. 

Docker installation instructions can be found here: https://docs.docker.com/engine/install/. The primary installation guide for Docker Compose can be found here: https://docs.docker.com/compose/install/. For information about hardware virtualization, consult this article from Intel: https://www.intel.com/content/www/us/en/virtualization/virtualization-technology/intel-virtualization-technology.html. For more information on the Windows hypervisor, have a look at this article: https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/quick-start/enable-hyper-v.
Configuring Docker for Chapter 2

A special Docker Compose configuration has been created that allows you to test your skills installing MongoDB. This container is drawn from the latest long-term support (LTS) version of the Ubuntu Linux image on Docker Hub. It also has the necessary prerequisites for installing MongoDB. The files to create and run this container are located in the source code repository for the book, at /path/to/repo/chapters/02

Let's first have a look at the docker-compose.yml file first. It is in YAML file format (as is the mongod.conf file), so you must pay careful attention to indents. The first line indicates the version of Docker Compose. After that, we define services.  The only service defined creates a container named learn-mongo-installation-1 and an image named learn-mongodb/installation-1. Two volumes are mounted to allow data to be retained even if the container is restarted.

Also, a file in the common_data directory is mounted as /etc/hosts, as illustrated in the following code block:

version: "3"
services:
learn-mongodb-installation:
container_name: learn-mongo-installation-1
hostname: installation1
image: learn-mongodb/installation-1
volumes:
- db_data_installation:/data/db
- ./common_data:/data/common
- ./common_data/hosts:/etc/hosts

Continuing with the services definition, we also need to map the MongoDB port 27017 to something else so that there will be no port conflicts on your host computer. We indicate the directory containing the Docker build information (for example, the location of the Dockerfile), and tell the container to stay open by setting stdin_open and tty both to true. Finally, still in the services block, we assign an IP address of 171.16.2.11 to the container, as illustrated in the following code block:

    ports:
- 27111:27017
build: ./installation_1
stdin_open: true
tty: true
networks:
app_net:
ipv4_address: 172.16.2.11

In the last two definition blocks, we define a Docker virtual network, allowing the container to route through your local host computer. We also instruct Docker to create a volume called db_data_installation that resides outside the container. In this manner, data is retained on your host computer between container restarts. The code for this can be seen in the following snippet:

networks:
app_net:
ipam:
driver: default
config:
- subnet: "172.16.2.0/24"

volumes:
db_data_installation: {}

Next, we look at the main build file, /path/to/repo/chapters/02/installation_1/Dockerfile. As you can see from the following code block, this file draws from the latest stable version of Ubuntu and installs tools needed for the MongoDB installation:

FROM ubuntu:latest
RUN \
apt-get update && \
apt-get -y upgrade && \
apt-get -y install vim && \
apt-get -y install inetutils-ping && \
apt-get -y install net-tools && \
apt-get -y install wget && \
apt-get -y install gnupg

To build the container, use this command:

docker-compose build

To run the container, use this command (the -d option causes the container to run in the background):

docker-compose up -d

To access the container, use this command:

docker exec -it learn-mongo-installation-1 /bin/bash

Once the command shell into the installation container is established, you can then practice installing MongoDB following the instructions given in this chapter. When you are done practicing MongoDB installation, be sure to stop the container, using this command:

docker-compose down

Let's now have a look at creating the demo environment used in Chapter 3, Essential MongoDB Administration Techniques, to Chapter 12, Developing in a Secured Environment, of this book. 

You will need to stop the container described here before you start the container described in the next sub-section; otherwise, you might encounter an error.

For more information about the directives in the docker-compose.yml file, please review the Overview of Docker Compose documentation article (https://docs.docker.com/compose/). Here is a useful list of Docker commands: https://docs.docker.com/engine/reference/commandline/container/.

Configuring Docker for Chapters 3 to 12

In the root directory of the repository source code, you will see a docker-compose.yml file. This file contains instructions to create the infrastructure for the demo server you can use to test files discussed in Chapter 3Essential MongoDB Administration Techniques, to Chapter 12Developing in a Secured Environment, of the book. The virtual environment created through this docker-compose file contains not only MongoDB fully installed, but also, Python, pip, and the PyMongo driver have all been installed as well. The heart of this Docker container is based upon the official MongoDB image hosted on Docker Hub.

Chapter 13Deploying a Replica Set, to Chapter 16Sharded Cluster Management and Development, have their own demo configuration, as you will discover when reading those chapters.

After the version identification, the next several lines contain services definitions, as follows: 

version: 3
services:
learn-mongodb-server-1:
container_name: learn-mongo-server-1
hostname: server1
image: learn-mongodb/member-server-1
volumes:
- db_data_server_1:/data/db
- .:/repo
- ./docker/hosts:/etc/hosts
- ./docker/mongod.conf:/etc/mongod.conf
ports:
- 8888:80
- 27111:27017
build: ./docker
restart: always
command: -f /etc/mongod.conf
networks:
app_net:
ipv4_address: 172.16.0.11

In this example, there is only one definition, learn-mongodb-server-1, which is used for most of the examples in this book. In this definition is the name of the Docker container, its hostname, the name of the image, volumes to be mounted, and additional configuration information. You will also notice that an IP address of 172.16.0.11 has been assigned to the container. Once it is up and running, you can access it using this IP address.

The remaining two directives define the overall Docker virtual network and an external disk allocation where the Docker container will store MongoDB data files, as illustrated in the following code snippet:

networks:
app_net:
ipam:
driver: default
config:
- subnet: "172.16.0.0/24"

volumes:
db_data_server_1: {}

Docker Compose uses a /path/to/repo/docker/Dockerfile file for instructions on how to build the new image. Here are the contents of that file:

FROM unlikelysource/learning_mongodb
COPY ./init.sh /tmp/init.sh
RUN chmod +x /tmp/init.sh
RUN /tmp/init.sh
COPY ./run_services.sh /tmp/run_services.sh
RUN chmod +x /tmp/run_services.sh
CMD /tmp/run_services.sh

The first directive tells Docker to pull the base image from the image created expressly for this book. After that, an initialization script is copied and executed. Here are the contents of the initialization script:

#!/bin/bash
echo '***************************************************'
echo ' '
echo 'To shell into the Docker container, do this:'
echo ' docker exec -it learn-mongo-server-1 /bin/bash'
echo ' '
echo '(1) To restore sample data run this script:'
echo ' /path/to/repo/restore_data_inside.sh'
echo '(2) To restart Apache run this script:'
echo ' /path/to/repo/restart_apache_inside.sh'
echo ' '
echo '***************************************************'

The second script referenced starts MongoDB and Apache and then loops. This ensures both services remain running. Here is the other script, found at /path/to/repo/docker/run_services.sh. Since this script is long, let's take it block by block. The first block of scripting starts a mongod (that is, a MongoDB daemon) running in the background. If it fails to start, the script exits, stopping the Docker container from running. The code for this can be seen in the following snippet:

#!/bin/bash
/usr/bin/mongod -f /etc/mongod.conf --bind_ip_all &
status=$?
if [ $status -ne 0 ]; then
echo "Failed to start mongod: $status"
exit $status
fi

In a similar vein, the second block of code starts Apache running, as follows:

/usr/sbin/apachectl -k start &
status=$?
if [ $status -ne 0 ]; then
echo "Failed to start apache: $status"
exit $status
fi

The remainder of the script simply loops, and checks the process status for both mongod and Apache 2. If either of the process checks fail to return results, the script ends, causing the container to stop. The code can be seen in the following snippet:

while sleep 60; do
ps aux |grep httpd |grep -v grep
PROCESS_1_STATUS=$?
ps aux |grep apache2 |grep -v grep
PROCESS_2_STATUS=$?
# If the greps above find anything, they exit with 0 status
# If they are not both 0, then something is wrong
if [ -f $PROCESS_1_STATUS -o -f $PROCESS_2_STATUS ]; then
echo "One of the processes has already exited."
exit 1
fi
done
For more information on the official MongoDB Docker image, have a look here: https://hub.docker.com/_/ubuntu.

Now, let's have a look at getting the demo environment up and running.

Running the demo environment

To bring the demo environment online, open a Terminal window, and change to the directory where you restored the source code for the book. To build the new Docker image to be used to test examples from Chapter 3, Essential MongoDB Administration Techniques, to Chapter 12Developing in a Secured Environment, run this command:

docker-compose build

To run the image, simply issue this command (the -d option causes the container to run in the background):

docker-compose up -d

After that, you can issue this command to open up a command shell inside the running container:

docker exec -it learn-mongo-server-1 /bin/bash

You should see a /repo directory that contains all the source code from the repository. If you then change to the /repo/chapters/02 directory, you can run the Python test program that confirms whether or not a connection to MongoDB is successful. Here is a screenshot of what you might see:

If you need to restart either MongoDB or Apache, simply stop the running container and restart it. The startup script will then restart both MongoDB and Apache.

Finally, we will have a look at establishing communications between your local computer that is hosting the Docker container, and the container's Apache web server.

Connecting to the container from your host computer

In a production environment (that is, on a server that is directly accessible via the internet), you would normally add a Domain Name Service (DNS) entry using the services of your Internet Service Provider (ISP). For development purposes, however, you can simulate a DNS address by adding an entry to the hosts file on your own local computer. For Linux and macOS computers, the file is located at /etc/hosts. For Windows computers, the file is located at  C:\Windows\System32\drivers\etc\hosts. For our fictitious company Book Someplace, we add the following entry to the local hosts file:

172.16.0.11    learning.mongodb.local

At this point, from your host computer, in a Terminal window or Command Prompt from outside the Docker container, test the connection using ping learning.mongodb.local, as shown here:

And that concludes the setup instructions for the book.