Quickly build various test environments (Mysql, Redis, ES, MongoDB) through Docker

The topic I will share with you today is how to quickly build various test environments through Docker. The ones listed in this article are also commonly used by Xiaoha in his work, including Mysql, Redis, Elasticsearch, MongoDB, and just a few lines of commands in seconds. It is easy to solve the problem of environment construction, and I believe it is also useful for friends.

Friendly reminder: Before building, you need to install Docker first. This article is based on the foundation that you have already installed Docker!

Stop talking nonsense, the text begins!

table of Contents

  • 1. Image acceleration

  • 2. Quick installation & build Mysql environment

  • Three, quickly install & build Redis environment

  • Fourth, quickly install & build MongDB environment

  • Five, quickly install & build Elasticsearch environment

  • Six, summary

1. Image acceleration

By default, Docker downloads images from the official image address Docker Hub. Because the server is abroad, the download speed is often very slow. In order to increase the download speed of the image, we can manually configure the domestic image accelerator to speed up the download speed.

There are many mirror accelerator options in China, such as Alibaba Cloud, DaoCloud, etc.

This article mainly talks about how to configure Alibaba Cloud's image accelerator.

2.1 Log in to Alibaba Cloud to get acceleration information

  1. First, you need to register an Alibaba Cloud account. If you don’t have one, use the following link to register:

https://dev.aliyun.com/

  1. Jump to the mirror acceleration page https://cr.console.aliyun.com/ to obtain acceleration configuration information:

2.2 Configure Docker

2.2.1 Determine the Docker Client version

Before configuration, you first need to determine the version of Docker Client, the recommended version is 1.10.0+ :

2.2.2 Configure Mirror Accelerator

PS: Here is the CentOS system as an example. If you are another system, you can refer to the official documentation of Alibaba Cloud Configuration Accelerator.

By modifying the daemon configuration file /etc/docker/daemon.jsonto use the accelerator:

Execute the following command:

 
  1. sudo mkdir -p /etc/docker

  2. sudo tee /etc/docker/daemon.json <<-'EOF'

  3. {

  4. "registry-mirrors": ["https://bjtzu1jb.mirror.aliyuncs.com"]

  5. }

  6. EOF

  7. sudo systemctl daemon-reload

  8. sudo systemctl restart docker

2.3 Verify the speed

Take downloading mongodb as an example to see the speed:

After configuring the accelerator, the speed has finally risen.

2. Quick installation & build Mysql environment

In this section, we will learn how to quickly install and build a Mysql environment through Docker.

2.1 Download Mysql image

Here is an example of Mysql 5.7:

 
  1. docker pull mysql:5.7

After the download is complete, docker imagescheck whether the image is downloaded successfully:

2.2 Start in the easiest way first

Start in a simple way first:

 
  1. docker run -d \

  2. --name mysql \

  3. -p 3306:3306 \

  4. -e MYSQL_ROOT_PASSWORD=123456 \

  5. mysql:5.7

  • -d: Run in the background;

  • --name mysql: Specify the name of the container as mysql;

  • -p3306:3306 Mount the 3306 port of the container to the 3306 port of the host;

  • -e MYSQL_ROOT_PASSWORD=123456: Specify the root password as 123456

After completion of the command, you can also docker psconfirm whether the vessel started successfully under command. If successful, we need to copy the directory files in the container to the host, including:

  • mysql configuration file;

  • Data storage directory for mounting (PS: If you don’t mount to the host, the data will be lost every time you start the container)

 
  1. # 将容器中的 mysql 配置文件复制到宿主机中指定路径下,路径你可以根据需要,自行修改

  2. docker cp mysql:/etc/mysql/mysql.conf.d/mysqld.cnf /usr/local/docker/mysql/config

  3. # 将容器中的 mysql 存储目录复制到宿主机中

  4. docker cp mysql:/var/lib/mysql/ /usr/local/docker/mysql/data

After all this is done, let's delete the container we just ran.

 
  1. docker rm -f mysql

PS: mysql is the name we specify when we run the container. Of course, you can also execute it first docker psand delete it by the container ID.

2.3 Officially run the Mysql container

Next, officially run the Mysql container:

 
  1. docker run -d \

  2. --name mysql \

  3. -p 3306:3306 \

  4. -v /usr/local/docker/mysql/config/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf \

  5. -v /usr/local/docker/mysql/data/mysql:/var/lib/mysql \

  6. -e MYSQL_ROOT_PASSWORD=123456 \

  7. mysql:5.7

Others remain unchanged, and two additional mount subcommands are added:

  • -v/usr/local/docker/mysql/config/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf: Mount the /etc/mysql/mysql.conf.d/mysqld.cnf configuration file in the container to the host's /usr/local/docker/mysql/config/mysqld.cnf file;

  • -v/usr/local/docker/mysql/data:/var/lib/mysql: Mount the /var/lib/mysql data directory in the container to the /usr/local/docker/mysql/data directory of the host;

After executing the command, check whether the container is started:

As you can see, the container runs successfully

2.4 Try to connect via Mysql client

Connect to the just created mysql through the MySQL client to see if the connection is successful:

The connection is successful!

Three, quickly install & build Redis environment

In this section, we will learn how to use Docker to install & build a Redis environment.

3.1 Download the Redis image

First pull Redis mirror, where I chose redis:alpinea lightweight mirrored version:

 
  1. docker pull redis:alpine

After the download is complete, docker imagesconfirm whether the image has been downloaded to the local:

3.2 Run Redis container

 
  1. docker run -p 6379:6379 --name redis -v /usr/local/docker/redis/redis.conf:/etc/redis/redis.conf -v /usr/local/docker/redis/data:/data -d redis:alpine redis-server /etc/redis/redis.conf --appendonly yes

Command description:

  • -p6379:6379: Map port 6379 of the container to port 6379 of the host;

  • -v/usr/local/docker/redis/data:/data : Mount the /data data storage directory in the container to the /usr/local/docker/redis/data directory in the host;

  • -v/usr/local/docker/redis/redis.conf:/etc/redis/redis.conf : Mount the /etc/redis/redis.conf configuration file in the container to the /usr/local/docker/redis/redis.conf file of the host;

  • redis-server--appendonly yes: Execute the redis-server startup command in the container and open the redis persistence configuration;

After the command is completed, check whether the container has started successfully:

You can see that the redis container has been started successfully!

3.3 Connect the newly created container

Execute the following command to connect to redis:

 
  1. docker run -it redis:alpine redis-cli -h 172.17.0.1

Fourth, quickly install & build MongDB environment

In this section, we will learn how to quickly install and build a MongoDB environment through Docker.

4.1 Download MongoDB image

Here is the mongo 4 version as an example, download the mirror:

 
  1. docker pull mongo:4

After the download is complete, confirm whether the image is downloaded successfully:

4.2 Run MongoDB mirror

After the download is successful, run the mongoDB image:

 
  1. docker run -d \

  2. --name mongo \

  3. -v /usr/local/docker/mongo/configdb:/data/configdb \

  4. -v /usr/local/docker/mongo/data:/data/db \

  5. -p 27017:27017 \

  6. mongo:4 \

  7. --auth

  • -d: Run in the background;

  • --name mongo: Specify the container name as mongo;

  • -v/usr/local/docker/mongo/configdb:/data/configdb: Mount the /data/configdb directory in the container to the /usr/local/docker/mongo/configdb directory of the host;

  • -v/usr/local/docker/mongo/data:/data/db: Mount the /data/db data directory in the container to the /usr/local/docker/mongo/data directory of the host;

  • -p27017:27017: Map port 27017 of the container to port 27017 of the host;

After executing the command, check whether the container is started:

4.3 Add an administrator account

Excuting an order:

 
  1. docker exec -it mongo mongo admin

Then, create a root account with the highest authority:

 
  1. db.createUser({ user: 'admin', pwd: '123456', roles: [ { role: "root", db: "admin" } ] });

After successful creation, you will see Successfullyadded user:

4.4 Connect with the newly created root account and test it

 
  1. docker run -it --rm --link mongo:mongo mongo mongo -u admin -p 123456 --authenticationDatabase admin mongo/admin

After the connection is successful, we can execute related sql:

Show all databases:

 
  1. show dbs

Use a database:

 
  1. use admin

Enter the command exitto exit the connection!

Five, quickly install & build Elasticsearch environment

In this section, we will learn how to quickly install and build an Elasticsearch environment through Docker.

5.1 Download the Elasticsearch image

Here is an example of quickly installing & building Elasticsearch environment with Elasticsearch 6.5.0:

 
  1. docker pull elasticsearch:6.5.0

After the download is complete, docker imagescheck whether the image is downloaded successfully:

5.2 Simply run the Elasticsearch image first

After the download is successful, simply run the Elasticsearch image:

 
  1. docker run -d \

  2. --name es \

  3. -p 9200:9200 -p 9300:9300 \

  4. -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms200m -Xmx200m" \

  5. elasticsearch:6.5.0

  • -d: Run in the background;

  • --name es: Specify the name of the container as es;

  • -p9200:9200-p9300:9300 Mount the 9200 and 9300 ports of the container to the 9200 and 9300 ports of the host;

  • -e"discovery.type=single-node"-e ES_JAVA_OPTS="-Xms200m -Xmx200m": Specify single node mode, JVM memory occupies 200m

After completion of the command, you can also docker psconfirm whether the vessel started successfully under command.

You can see that the es container has run successfully. Next, enter the container:

 
  1. docker exec -it es /bin/bash

Install the analysis-ik Chinese word segmentation plugin:

 
  1. ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.5.0/elasticsearch-analysis-ik-6.5.0.zip

PS: es supports the built-in es plug-in command to install from v5.5.1. If the version you install is not 6.5.0, you need to modify the version number in the command. For details, refer to https://github.com/medcl /elasticsearch-analysis-ik

After the installation is successful, exit the container:

 
  1. exit

Delete the container you just ran:

 
  1. docker rm -f es

PS: Of course, you can also delete it by the ID of the container.

5.3 Copy related files

 
  1. # 复制 es 配置文件目录到宿主机指定目录,目标目录你可以根据需要,自行修改

  2. docker cp es:/usr/share/elasticsearch/config /usr/local/docker/es

  3. # 复制 es 持久化数据目录到宿主机指定目录

  4. docker cp es:/usr/share/elasticsearch/data /usr/local/docker/es

  5. # 复制 es 插件目录到宿主机指定目录

  6. docker cp es:/usr/share/elasticsearch/plugins /usr/local/docker/es

5.4 Modify es related configuration

We have just entered the specified config configuration directory, modify jvm.optionsthe file:

 
  1. -Xms300m

  2. -Xmx300m

PS: Because the Xiaoha test server has 2G memory, here I changed it to JVM memory which occupies 300m. If you have enough memory, you don't need to change it.

Modify the elasticsearch.ymlfile, add the following configuration:

 
  1. node.name: master

  2. http.cors.enabled: true

  3. http.cors.allow-origin: "*"

Explain the added configuration, set the node as the master node, and allow cross-domain access, so that you can use the head plug-in graphical interface to access later.

5.5 Run Elasticsearch container

 
  1. docker run -d \

  2. --name es \

  3. -p 9200:9200 -p 9300:9300 \

  4. -v /usr/local/docker/es/config:/usr/share/elasticsearch/config \

  5. -v /usr/local/docker/es/data:/usr/share/elasticsearch/data \

  6. -v /usr/local/docker/es/plugins:/usr/share/elasticsearch/plugins \

  7. elasticsearch:6.5.0

This time, we added additional related mount commands:

  • -v/usr/local/docker/es/config:/usr/share/elasticsearch/config: Mount the /usr/share/elasticsearch/config configuration directory in the container to the /usr/local/docker/es/config directory of the host;

  • -v/usr/local/docker/es/data:/usr/share/elasticsearch/data: Mount the /usr/share/elasticsearch/data data directory in the container to the /usr/local/docker/es/data directory of the host;

  • -v/usr/local/docker/es/plugins:/usr/share/elasticsearch/plugins: Mount the /usr/share/elasticsearch/plugins plugin directory in the container to the /usr/local/docker/es/plugins directory of the host;

5.6 Test it and see if es can be accessed normally

Test it to see if es starts successfully:

 
  1. curl http://localhost:9200

OK, the single-node environment of es is now set up!

Guess you like

Origin blog.csdn.net/baidu_39322753/article/details/107612785