Table of contents
background
Recently, I am developing a Django-based e-commerce website and using Redis to cache product information. Since I use Windows for development, Redis itself does not support Windows systems. There are only a few ways to check a lot of information. Win10 and above support the WSL subsystem. Considering that Github cannot use the latest version at the first time, WSL itself is unstable and other factors, use Docker to deploy the Rdis image to solve the problem of Windows using Redis.
2022.7.30 Update
Another usage of Docker was discovered during the development process. By creating a new Python image container in Docker, problems such as the lack of compilers in the Windows environment can be solved. Direct use of file mapping and port mapping can solve the problem of Python development environment. Deployment issues.
Known issues:
- Using Docker for environment deployment cannot directly jump to the code error location through Pycharm.
- The breakpoint debugging function cannot be used because the command line and Pycharm are separated.
Preparation
- latest version of docker
- Redis latest version compressed file
- Redis graphical control software RDM
- Docker mirror market
start deployment
Install Redis
Download and install Redis directly. After the installation is complete, open the command prompt and use the following code to install Redis. You can also use the graphical interface to install.
#在docker仓库搜索redis
docker search redis
#下载redis到本地仓库不加版本号默认是最新版
docker pull redis
#查看下载好的容器
docker images
If the download is successful, the effect displayed by using the command is as shown in the figure
label name | label meaning |
---|---|
REPOSITORY | The label indicates the name of the currently downloaded image |
TAG | Indicates the version of the current mirror |
IMAGE ID | It is the ID set by Docker itself for the image |
CREATED | Indicates when the image was created |
SIZE | Indicates the image size |
If the above content can be successfully displayed, it means that Redis has been successfully installed. Then we need to decompress the previously downloaded Redis compressed package, decompress the redis.config file and modify it.
Modify the Redis configuration file
In the default configuration file of Redis, only local connection is allowed, and at the same time, it runs in protected mode, and remote connection is not allowed. In the actual development, there are many inconveniences, and some contents of the configuration file need to be modified.
before fixing | after modification | effect |
---|---|---|
bind 127.0.0.1 | bind 0.0.0.0 | Allow external connections |
protected-mode yes | protected-mode no | Disable protected mode |
appendonly no | appendonly yes | Enable persistence |
requirepass " " | requirepass The password you need to set | set password |
Among these configurations, there is a # in front of some configurations, and the # can be deleted to make the configuration take effect
Start Redis
After modifying the configuration file, record the location of the configuration file, I saved it on the desktop, so the path is
C:\Users\TOM\Desktop\redis.conf
Next, use this path to start Redis. The specific startup code is as follows:
docker run -p 6379:6379 --name iredis -v C:\Users\TOM\Desktop\redis.conf:/etc/redis/redis.conf -v /home/docker/redis/data:/data -d redis:latest redis-server /etc/redis/redis.conf --appendonly yes
Parameter analysis:
parameter name | effect |
---|---|
docker | Call the docker command to start the container |
run | start a container |
-p 6379:6379 | Map port 6379 in the container with port 6379 of the host |
–name iredis | The container is named iredis |
-v Modified configuration file path: configuration path in Docker | Map and replace the local configuration file with the configuration file in Docker |
-v local persistent save file path: the default persistent path in Docker | Replace the persistent save path |
-d | Run in the background, you can delete this parameter if you don’t need it |
redis:latest | Run the image whose TAG is latest in the redis image |
redis-server /etc/redis/redis.conf | The default startup parameters, the configuration file here has been replaced by the configuration file on the desktop |
–appendonly yes | Turn on AOF to achieve data persistence |
The result after successful startup is as shown in the figure.
If the -d parameter is removed, the execution result is as shown in the figure:
Up to now, the work of deploying Redis in the Docker container under Windows has been completed, and the next step is to use the RDM software to connect with Django in the host.
Remote Connection
RDM connection database
First install our RDM software and connect after the installation is complete.
Parameter analysis
parameter name | effect |
---|---|
Name | Give the current link a name, just enter one you can understand |
Host | The IP where Redis is located, we use the local Docker deployment, so fill in the IP of the host machine, the local is 127.0.0.1 or you can fill in localhost, the two are equivalent |
Port | The port number mapped by the Docker container, that is, the last port number filled in the -p parameter in the command prompt. Since we use -p 6379:6379 for demonstration, fill in 6379. If you specify other ports yourself, fill in the port you specified |
auth | Fill in the password of the requirepass parameter in the configuration file |
After filling in, click Test Connection below to test the link, and click OK to save the connection after a successful page pops up.
On the left, you can see that the Redis database has been successfully connected.
Django connects to the database
After tossing for a long time, I can finally connect to the database. Django supports using Redis for caching. Here we need to download the corresponding third-party library to connect to Redis.
# 直接下载 django-redis
pip install django_redis
# 通过豆瓣源加速下载
pip install -i https://pypi.douban.com/simple django_redis
After the installation is complete, open the setting.py file in the project for configuration
# redis配置
CACHES = {
# 设置名为 default 的链接
"default": {
# 设置缓存功能
"BACKEND": "django_redis.cache.RedisCache",
# 设置 redis 的链接路径
"LOCATION": "redis://default:redis@localhost:6379/0", # 安装redis的主机的 IP 和 端口
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"CONNECTION_POOL_KWARGS": {
"max_connections": 1000,
"encoding": 'utf-8'
},
# 添加 redis 的密码
"PASSWORD": "redis"
}
}
}
After the configuration is complete, use the shell that comes with django to test
# 启动 Django 自带的命令行
python .\manage.py shell
Test after launch
# 导入 django-redis 库
from django_redis import get_redis_connection
# 使用 dafult 配置进行连接
con = get_redis_connection('default')
# 测试是否连接成功
con.ping()
# 测试成功结果
>>> con.ping()
True
Docker deploys Python images
Docker pulls the corresponding image version
# 拉取 Python38 版本镜像
docker pull rackspacedot/python38
# 查看下载结果
docker ps -a
|CONTAINER ID|IMAGE|COMMAND|CREATED|STATUS|PORTS|NAMES|
|e5cedc7e1e2d|python:3.8|"python3"|21 hours ago|Exited (137)|20 hours ago|
# 新建镜像容器,并配置文件目录映射
docker run -it -p 宿主机端口:容器端口 -v 宿主机目录:容器目录 --name 自定义的名字 镜像名称:镜像tag
docker run -it -p 8080:8080 -v c:\Destop\DjangoCode:/Code --name Python_Django python:3.8
# 启动镜像容器
docker start Python_Django
# 进入容器的交互命令终端
docker exec -it 容器id /bin/bash
docker exec -it e5cedc7e1e2d /bin/bash
Connections for Docker containers
two-way connection
Create a network through the network command of Docker, and add all containers to the network. Containers in the network can be directly accessed through the container ID or container name. The official document address
# 创建容器网络
docker network create [OPTIONS] NETWORK
docker netword create ContainNetwork
# 将容器加入网络
docker network connect [OPTIONS] NETWORK CONTAINER
dokcer network connect ContainNetwork reids
one-way connection
The single connection uses Docker's earlier configuration --link parameter, which may be discarded in the future, please pay attention to use
# 连接容器A和容器B,只能A访问B,B无法访问A,需在创建容器时就指定需要连接的关系
docker run 容器A --link 容器B
docker run Python_Django --link redis --link mysql # 可单向连接多个容器
So far, all the content that needs to be configured has been completed. Friends who need assistance can send an internal message or email to [email protected]. If you see it, you will reply. I am busy with work recently and may not be in time.