Docker deployment redis combat

"  In other words, when the computer do not want to set up a development environment -! Programming three minutes ."

What is docker

Docker is an application used to manage containers, and the container is a service that simulates the environment on which the application depends. Generally, only one program is installed in a container, and the application in the container will think that it is the only process running on the machine, and a machine can run multiple containers independently.

The advantage of using containers is that there is no need for cumbersome configuration of the operating environment, which is convenient for construction, testing, and continuous integration; it can dynamically expand and shrink, make full use of a server to run multiple services, so the microservice architecture can be simulated on the local machine.

Of course, I personally think that the coolest thing is that you can develop and test at will in the container, but you don't have to worry about messing up your beloved computer.

ready

  • A machine with docker installed

  • Can connect to the Internet and access the latest version of Docker Engine (server)

  • The Docker Client (Docker command line tool) is installed on the local development machine and can be accessed through the command line.

Run the container

You can search for ready-made images on the official website, or you can find the container in the following way, the command format:

docker search <name>

Actual execution

$ docker search redis
NAME               DESCRIPTION                                             STARS OFFICIAL AUTOMATED
redis              Redis is an open source key-value store that… 7023            [OK]
bitnami/redis      Bitnami Redis Docker Image 114                                          [OK]   
省略……

We found the OFFICIAL (official) docker image name of redis is redis. In order to make it provide services, our next task is to let him run as a background service, the command format:

$ docker run <options> <image-name>

By default, Docker will run commands in the foreground. To run in the background, you need to specify the option -d.

$ docker run -d redis
570315fb8a0596ff5581653a77f8ef406f86a0ab4b1a2061de7a423335812ed3

By default, Docker will run the latest version. If you need a specific version, you need to specify it explicitly.
For example, version 3.2

$ docker run -d redis:3.2

Since this is the first time we use Redis image, so it will be downloaded to the Docker host, if it exists locally, it will not.

View running containers

You can use docker pscommands to list all running containers, including information such as the image used to start the container and the normal running time.

$ docker ps
CONTAINER ID IMAGE   COMMAND                 CREATED       STATUS PORTS NAMES
570315fb8a05 redis   "docker-entrypoint.s…" 5 minutes ago Up 5 minutes 6379/tcp elastic_mclean

To list more detailed information of the running container, such as IP address, the command format is as follows:

docker inspect <friendly-name|container-id>

The log of the container can be viewed through the docker logs command, the command format is as follows:

docker logs <friendly-name|container-id>

Visit redis

We have successfully deployed a redis with a container, but it is not the same as installing a redis directly. The container is like a virtual machine. If you want to access the service, you must enter it, or use the exposed port like remote access. To visit.

-p <host-port>:<container-port>Option can bind the port when starting the container.

At this time, it is used -name <name>to define a name when starting the container. It will be more convenient to query and view logs later.

Because redis occupies port 6379 by default, we can map port 6379 to the local 6379

$ docker run -d  --name redisHostPort -p 6379:6379 redis:latest
694384d739307f3c5bc59fa

tips: By default, the port on the host is mapped to 0.0.0.0, you can specify a specific IP when defining the port mapping, for example -p 127.0.0.1:6379:6379

Assuming that we want to deploy multiple redis, it is impossible to find a port to map by ourselves every time. Docker supports automatic binding to an available port, just use -p 6379this parameter, like the following.

$ docker run -d --name redisDynamic -p 6379 redis:latest
dcd3dabe51c3f0a8ddbdfd

Which port is bound to, you can use the following command to query (this time the custom name redisDynamic is used), of course, it docker psis also possible to use it directly .

$ docker port redisDynamic 6379
0.0.0.0:32768

Storing data

When using a container, we will find that once the container is created, deleted, and upgraded, the data stored in it will go away with the wind, so we must find a way to store the data.

We query the redis mirror storage data directory on the official website in the /datadirectory, and then we store the /opt/docker/data/redis
parameters locally .-v 本机位置:容器内位置

docker run -d --name redisMapped -v /opt/docker/data/redis:/data redis
c9c154695500260407d99d

Of course, the configuration file can also be redirected to the local, and all container configurations, logs, and data can be managed uniformly, which is very convenient.

Interact with the platform

docker run ubuntu ps Start the Ubuntu container and execute the command ps to see all the processes running in the container.
docker run -it ubuntu bashDirectly access the bash shell in the container.

$ docker run ubuntu ps
  PID TTY TIME CMD
    1 ?  00:00:00 ps
$ docker run -it ubuntu bash
root@6db973337059:/# exit


docker attach <container> The container to be attached to access the running docker must be running.
docker exec -it <container> bash|shDirectly access the bash shell in the container.

$ docker attach c9c154695500 /bin/bash
root@c9c154695500:/data# exit
$ docker exec -it c9c154695500 /bin/bash
root@c9c154695500:/data# exit

The container name can also be used directly here

  • attach directly into the container without starting a new process

  • exec opens a new terminal in the container and starts a new process


那什么时候用attach,什么时候用exec呢?


Guess you like

Origin blog.51cto.com/15076235/2608330