In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to use Docker to install Redis and Mysql". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Recently, I am going to write an article on how to implement distributed locks, which can be implemented with the help of database, cache, and Zoopkpeer. The above software needs to be installed when implementing the code. So I thought of using docker to simplify my installation, and I could just learn about Docker. This article describes how to install Mysql/Reids/Zookeeper using Docker.
There are many installation tutorials about docker, and different operating systems are different, so I won't repeat them here. If it is only personal research and study, it is recommended that you install on mac using docker for mac installation, https://store.docker.com/editions/community/docker-ce-desktop-mac
Basic concepts of Docker
Before using Docker, let's take a look at a few core concepts of Docker
Docker Daemon
The Docker engine is a daemon running in the background, and after we start it, we can send commands related to Docker through the Docker client.
Docker Images
Docker image, this is similar to the image when we installed Windows, except that the image of windows, in the past we usually stored on CD or USB disk, the image here we usually publish to Docker Registry
Docker Containers
Docker container. After obtaining the Docker image, we can run the image. At this point, we will launch a Docker container in which the programs in the image will be run. If the Docker image is understood as a class, then the Docker container is an instance
Docker Client
Docker client, after we install Docker, we open the terminal and use docker-related commands to operate, these commands are Docker client commands, in addition, there is a REST API-based Docker client, REST API is generally used in the development of some Docker-based operation and maintenance systems.
Docker Registry
The Docker image registry, and the registry of Docker's official website is http://hub.docker.com. We can also set up our own registration center.
These are the core concepts of Docker. Let's take a look at how to install Redis, Zookeeper and Mysql using Docker, and learn to use some common commands of Docker Client in the process.
Redis find Mirror
Use the following command to search for Redis images in Docker Hub
Docker search redis
Executing the above command will search for all images with the redis keyword (to reduce the length, only part of the results will be shown)
YanyanwangdeMacBook-Pro:~ yanyan.wang$ docker search redisNAME DESCRIPTION STARS OFFICIAL AUTOMATEDredis Redis is an open source key-value store th... 3788 [OK] bitnami/redis Bitnami Redis Docker Image 47 [OK] torusware/speedus-redis Always updated official Redis docker image... 32 [OK]
Where Name represents the name of the image repository, without / represents the official Docker repository, and with / represents the images made public by other users. OFFICIAL indicates whether it is an official warehouse. Generally speaking, we recommend using an official warehouse.
Pull the image
We use the following command to pull the image of Redis
Docker pull redis:latest
Redis:latest indicates that we download NAME as the image of redis, and latest represents TAG
After executing the above command, Docker Client will download the image of Redis from the Docker image registry. After downloading, we will check the local image.
List all mirrors
We use the following command to view the local redis image
Docker images
Execute the above command and all local mirrors will be displayed
REPOSITORY TAG IMAGE ID CREATED SIZEredis latest a858478874d1 10 days ago 184 MB
Where IMAGE ID represents mirrored ID and is unique
Create and start the container
Create and start the Redis container using the Reids image using the following command
Docker run-p 6379 redis:latest redis-server 6379-v $PWD/data:/data-d redis:latest redis-server-- appendonly yes
By executing the above command, we use the redis:latest image to create and start a Redis container, where
-p 6379VR 6379: map port 6379 of the container to port 6379 of the host
-v $PWD/data:/data: Mount the data under the current directory in the host to the container's / data
Redis-server-- appendonly yes: execute the redis-server startup command in the container and open the redis persistent configuration
Check the container startup status
Use the following command to view all containers
Docker ps
Execute the above command, and the output is as follows
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES07f0edc95fb3 redis:latest "docker-entrypoint..." 2 minutes ago Up 2 minutes 0.0.0.0 minutes ago Up 6379-> 6379/tcp quirky_davinci
Where Status indicates running status, Up indicates running, and PORTS indicates the port number that the container needs to expose to the public.
Connect, view containers
We use the Redis image to connect to the redis container using the redis-cli client
Docker run-it redis:latest redis-cli-h 192.168.0.101
By executing the above command, we connect to the redis container, and then we can use the redis command to operate
192.168.0.101 test 6379 > set "testKey"test" OK192.168.0.101:6379 > get "testKey"test" 192.168.0.101
At this point, we have completed the installation of Reids.
Zookeeper
Next we install zookeeper, and we use the same method as redis. Directly use the least number of steps to install without too much explanation
Find zookeeper image docker search zookeeper
The search results are as follows:
NAME DESCRIPTION STARS OFFICIAL AUTOMATEDjplock/zookeeper Builds a docker image for Zookeeper versio... 160 [OK] zookeeper Apache ZooKeeper is an open-source server... 157 [OK] mesoscloud/zookeeper ZooKeeper 70 [OK] digitalwonderland/zookeeper Latest Zookeeper-clusterable 14 [OK] springxd/zookeeper A Docker image that can run a ZooKeeper se... 6 [OK] pull mirror docker pull zookeeper to create and launch container docker run-p 2181-v $PWD/data:/data-d zookeeper:latest
Create and start the container using the zookeeprt image. We map 2181 of the container to 2181 of the machine, and mount the data of the current directory to the / data of the container.
View the current container list: yanyanwangdeMacBook-Pro:zookeeper yanyan.wang$ docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES44b43e486817 zookeeper:latest "/ docker-entrypoin..." 24 minutes ago Up 24 minutes 2888/tcp, 0.0.0.0 minutes ago Up 2181-> 2181/tcp 3888/tcp mystifying_wing2358c79358d6 redis:latest "docker-entrypoint..." About an hour ago Up About an hour 0.0.0.0 docker exec 6379-> 6379/tcp fervent_lamarr enters the container docker exec-it 44b43e486817 / bin/bash
After entering the container, ls looks at the operation directory and can see which directories are under.
Bash-4.3# lsLICENSE.txt bin dist-maven lib zookeeper-3.4.10.jar.ascNOTICE.txt build.xml docs recipes zookeeper-3.4.10.jar.md5README.txt Conf ivy.xml src zookeeper-3.4.10.jar.sha1README_packaging.txt contrib ivysettings.xml zookeeper-3.4.10.jarMysql
Next we install Mysql, which is different from pulling the image directly from the docker registry above. We first build our own mysql image, and then create and start the container
Build an image and write a Dockerfile file
We can use Dockerfile to build quickly. Dockerfile is actually a script for writing Docker images. Dockerfile scripts have a fixed format through which we can write Docker images of different needs. Here is the script to build the Mysql image and what the script means
# # setting basic image FROM mysql:latest## setting maintainer information MAINTAINER yywang # # setting description information LABEL Descripttion= "This image is build for MAC to use mysql" Vendor= "GitHub" Version= "latest" # # setting the command RUN apt-get updateRUN apt-get-y install vimRUN usermod-u 1000 mysqlRUN mkdir-p / var/run/mysqldRUN chmod-R 777 / var/run/mysqld to be executed during image creation
Where FROM means to set the basic image instruction, which has a fixed format, that is, "repository name: tag signature", and RUN represents the command to be executed during the image construction process.
Save the above script in a file called Dockerfile, and the script for the Dockfile of the mysql image is completed.
Build a mysql image
Enter the following command in the same directory as the Dockerfile file:
Docker build-t yywang/mysql-osx:latest.
Where-t specifies the name of the mirror,. This means reading the Dockerfile file in the current directory
View Mirror
After executing the above command, we look at the current image
YanyanwangdeMacBook-Pro:zookeeper yanyan.wang$ docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEyywang/mysql-osx latest 38c6579b6f70 4 minutes ago 446 MB
You can see that yywang/mysql-osx is the mirror we just built.
Create and start containers create and start mysql containers
Next we create and start the mysql container
Docker run-d-h mysql\-v $PWD/mysql:/var/lib/mysql\-p 0.0.0.0 PWD/mysql:/var/lib/mysql 3306 name mysql\-e MYSQL_ROOT_PASSWORD=123456 yywang/mysql-osx
Where-e means to set the password of the root account of mysql to 123456
Connect, view containers
We use the mysql client of the Mysql image to connect to the mysql container
Docker run-it yywang/mysql-osx mysql- h292.168.0.101-uroot-p123456
After logging in, we can use the command of mysql to operate mysql
Mysql > select GET_LOCK ("testKey", 0); +-- + | GET_LOCK ("testKey", 0) | +-- + | 1 | +-- + 1 row in set (0.01 sec) mysql > select RELEASE_LOCK ("testKey") +-+ | RELEASE_LOCK ("testKey") | +-+ | 1 | +-+ 1 row in set (0.00 sec)
At this point, the installation of Mysql has been completed.
This is the end of "how to install Redis and Mysql using Docker". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.