Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build redis stand-alone Container in Docker Environment

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to build a redis stand-alone container in Docker environment". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to build a redis stand-alone container in Docker environment.

1 background introduction

Using traditional deployment to build a redis environment will consume too much repetitive work, resulting in very low deployment efficiency. For example: apply for a virtual machine, install the operating system, the package depends on installation and redis application deployment, this process takes a lot of time, and problems in the installation process also take some time to deal with. Then the portability of the traditional deployment is quite poor. It is not so easy to migrate the existing redis to the new host, perhaps the easiest way is to use the tar package, but the new host still needs to install redis-related dependency packages and take some time to debug redis, which is a waste of time.

In order to break the inefficiency caused by the traditional deployment model, we adopt the docker container deployment model to simplify the repetitive work, and to facilitate operation and maintenance engineers to build a set of redis environment more quickly, redis containerization can solve the repetitive and tedious work in the installation process. Finally, it is mirrored and stored in the image warehouse. As long as there is a need for redis services, you can run the redis container on any host with docker software installed. The image after Docker containerization can achieve the ready-to-use effect.

2 Image packaging process

0.Linux compiler environment: prepare a linux environment that can be accessed on the extranet, and install the docker runtime environment.

Basic image: before building an application image, you need to prepare a clean set of centos basic image, and then deploy the redis software on this basic image.

Configuration standardization: write the redis software installation and redis configuration steps into the dockerfile file according to the command format of dockerfile. Then prepare the configuration files needed by the running environment and the corresponding runtime startup scripts to facilitate the packaging of these files into the image when building the application image.

Build an application image: deploy the dependent software package and redis software you want to run into the image by writing a dockerfile file in the basic operating system image, and finally use the docker command to make a new complete image package. Next time, download the image deployment directly with redis.

Startup container: the docker command starts the redis service during the process of starting the container.

In Docker, there are two ways to build a custom image, one is through the commit instruction, and the other is through the Dockerfile file. Here we recommend the second way to build a redis image using a dockerfile file.

3 basic framework of Dockerfile

Dockfile is a script interpreted by a Docker program. Dockerfile consists of one instruction, each corresponding to a command under the Linux. The Docker program translates these Dockerfile instructions into real Linux commands. Dockerfile has its own writing format and supported commands, and the Docker program solves the dependencies between these commands, similar to Makefile. The Docker program will read the Dockerfile and generate a custom image according to the instructions. An obvious script like Dockerfile is more acceptable to users than a black box like image, which clearly shows how image is created. With Dockerfile, when we need to customize our own additional requirements, we just need to add or modify instructions on Dockerfile to regenerate image, saving us the trouble of typing commands.

The content of Dockerfile is divided into four parts: basic image information, maintainer information, mirror operation instructions and container startup instructions.

Operating system

FROM

Select different types of basic operating system images according to your needs

Common software

ADD

Select the common software needed to run, such as PHP and NGINX, and automatically set the corresponding running variables.

Designated user

USER

Set the user who launches the container. The default is root.

Perform installation

RUN

Execute the installation script for the demand package

Switch directories

WORKDIR

Can be switched multiple times (equivalent to cd command)

Environment variable

ENV

Setting environment variables in the mirror

Port mapping

EXPOSE

This directive maps a port in the container to a port in the host machine

Storage usage

VOLUMN

Define plug-in directories on demand (high IO requirements, persistence, host sharing, etc.)

Start up and run

ENTRYPOINT

Setting instruction, which specifies the command to be executed when the container starts, which can be set multiple times, but only the last one is valid

Start up and run

CMD

Scripts that need to be executed when the container starts

4 Redis containerization process 4.1 Linux compilation environment

Prepare a centos7 virtual machine and install Docker software on this host. Here we take docker version 1.13.1 as an example to show how to install Docker software.

# download docker-engine

Wget https://yum.dockerproject.org/repo/main/centos/7/Packages/docker-engine-1.13.1-1.el7.centos.x86_64.rpm

# download docker-engine-selinux

Wget https://yum.dockerproject.org/repo/main/centos/7/Packages/docker-engine-selinux-1.13.1-1.el7.centos.noarch.rpm

# install docker software dependency files

Yum install-y libtool-ltdl policycoreutils-python

# install docker-engine-selinux first, then install docker-engine-selinux

Yum localinstall docker-engine-selinux-1.13.1-1.el7.centos.noarch.rpm-y

Yum localinstall docker-engine-1.13.1-1.el7.centos.x86_64.rpm-y

# restart docker service

Systemctl start docker

Systemctl enable docker

The following represents that the docker software has been installed successfully

4.2 obtain the basic image

You can download the Centos7 basic image directly in the Linux compilation environment. It is required that the compilation environment can connect to the public network and connect to the hub.docker.com public network image repository to download the image. Enter docker search centos below to see that there are many centos images in the public network docker image repository. You can download the centos base image through the docker pull openshift/base-centos7 command.

Use docker pull to download the image to the local linux host, and then check the integrity of the openshift/base-centos7 image. As long as the docker run command starts the image, we can see that the container has been started normally. Verification shows that the mirror image is normal.

Here are some simple commands that docker uses every day:

1. Download centos image

Docker pull openshift/base-centos7

2. View the image

Docker images

3. Delete the image

Docker rmi

4. Run the docker container

Docker run-itd openshift/base-centos7 echo "hello word"

5. Start and stop docker container

Docker start

Docker stop

Docker kill

6. Enter the docker container

Docker exec-it bash

Docker attach

7. Delete the container

Docker rm

4.3 create dockerfile to build redis environment

1. Write the dockerfile steps of redis packaged image

2. Dockerfile programming

# reference centos basic image

FROM openshift/base-centos7

MAINTAINER jaymarco

# install redis dependency package and redis software

RUN yum install gcc gcc-c++ make cmake tar python-setuptools-y & &\

Curl-fL http://download.redis.io/releases/redis-3.2.3.tar.gz | tar xzf-C / tmp & &\

Cd / tmp/redis-3.2.3 & &\

Make PREFIX=/usr/local/redis install & &\

Mkdir-p / usr/local/redis/ {bin,etc,var,logs} & &\

Cp-af src/ {redis-benchmark,redis-check-aof,redis-check-rdb,redis-cli,redis-sentinel,redis-server} / usr/local/redis/bin/ & &\

Cp-a redis.conf / usr/local/redis/etc/ & &\

Cp-a sentinel.conf / usr/local/redis/etc/ & &\

Echo "export PATH=/usr/local/redis/bin:\ $PATH" > / etc/profile.d/redis.sh & &\

Source / etc/profile.d/redis.sh & &\

Groupadd-r redis & &\

Useradd-g redis redis & &\

Chmod-R 755 / usr/local/redis/etc & &

Yum clean all & &\

Rm-rf / tmp/redis-3.2.3 & &\

/ bin/cp / usr/share/zoneinfo/Asia/Shanghai / etc/localtime & &\

Echo 'Asia/Shanghai' > / etc/timezone

# Optimization of redis parameters

RUN sed-e's / ^ bind 127.0.0.1/#bind 127.0.0.1 Universe'\

-e's save # "/ save" /'\

-e 's/save 900 1/#save 900 1DB'\

-e 's/save 300 10/#save 300 10 Universe'\

-e 's/save 60 10000/#save 60 10000 bot'\

-e 's/appendonly no/appendonly yes/'\

-e's big # maxmemory-policy noeviction/maxmemory-policy noeviction/'\

-e '481i\ requirepass Redis@2017'\

-e '539i\ maxmemory 2147483648'\

-I / usr/local/redis/etc/redis.conf & &\

Sed "s | dir. / | dir / usr/local/redis/dumpdb/ |"-I / usr/local/redis/etc/redis.conf & &\ "

Sed "s | logfile"| logfile" / usr/local/redis/logs/redis.log "|"-I / usr/local/redis/etc/redis.conf

# Container port mapping

EXPOSE 6379

# start redis

ENTRYPOINT ["/ usr/local/redis/bin/redis-server", "/ usr/local/redis/etc/redis.conf"]

4.4 build a redis image

Create a redis image through docker build, and the-t parameter specifies the name of the image.

Docker build-t redis:v3.2.3.

There will be similar output during execution:

After the execution is completed, enter docker images to view the current local image, as shown in the following figure, you can see the new image

1.1 start the redis container

Redis is stateful data. We only need to mount the local directory to the container as a volume when we start the redis container by storing the redis data file to the local host.

Create a redis storage directory on the local host

Mkdir-p / home/redisdump

Then start the redis container

Docker run-itd-- name redis

-v / home/redisdump:/usr/local/redis/dumpdb\

-p 6379 6379 redis:v3.2.3

We see that the container has started normally and can see the information that it is running.

1 verify redis container

When starting the redis container, port 6379 of the redis container is mapped to the external host port 6379, indicating that we can access the data in the redis container by adding 6379 to the host address.

Now let's test it.

Now the host simulates the insertion of a piece of data, and then returns to the container to see if the data exists.

When we enter the container, we can check the data, which means that our image is successful.

At this point, I believe you have a deeper understanding of "how to build a redis stand-alone container in Docker environment". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report