pull redis mirror
There are several ways to create a redis image. You can pull it directly from the warehouse, or you can compile and create it yourself using the dockerfile file.
Based on the existing redis image, docker can use run or create->start to create containers.
1.docker run
When building an image, to provide a port for accessing redis, -p is
created and started for port mapping 1), and set port mapping
[root@vm000949 ~]# docker run -p 127.0.0.1:6379:6379 -it --name="my-redis-server" -d redis
d66037100bddcd230e0c9955bdfb9b0dbae8ce4028a81534e1693ab95737c90a
[root@vm000949 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d66037100bdd redis "docker-entrypoint.sh" 6 seconds ago Up 5 seconds 127.0.0.1:6379->6379/tcp my-redis-server
docker run: Recreate a container and run the command, the syntax is as follows:
Docker run [OPTIONS] IMAGES [COMMAND][ARG]
The commonly used OPTIONS are:
-t assign a pseudo input terminal to the
container -I run the container in interactive mode
-name give the container a name
-d run in background mode
-p portmap
Check the port mapping situation, where e24b3e0a7df0 is the redis container.
[root@vm000949 ~]# docker port e24b3e0a7df0
6379/tcp -> 127.0.0.1:6379
2) Enter the redis container
docker exec -it e24b3e0a7df0 redis-cli
127.0.0.1:6379> set day newDay OK 127.0.0.1:6379> get day "newDay" 127.0.0.1:6379> exit
The docker exec syntax is as follows:
Docker exec [OPTIONS] CONTAINER COMMAND [ARG]
Where OPIONS includes:
-d : detached mode: run in the background
-i : keep STDIN open even if not attached
-t : allocate a pseudo terminal
This is a simple creation process, and direct run is a container that is created and started.
Check the port mapping on the current virtual machine:
[root@vm000949 ~]# netstat -apn|grep 6379
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 2270/docker-proxy-c
The above is to directly use the run method, and the more correct life cycle should be:
create->start->exec
If the container has been stopped, start directly, and then exec. Using run will create another docker according to the command. For
example, to start over, you need to delete the current container
[root@vm000949 ~]# docker rm 695d5f6afc27
Error response from daemon: You cannot remove a running container 695d5f6afc27415126a40384a868c751ba635df2d4d7fb578424bc1bd9167166. Stop the container before attempting removal or use -f
A running container cannot be deleted.
[root@vm000949 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 695d5f6afc27 redis "docker-entrypoint.sh" 14 minutes ago Up 8 minutes 127.0.0.1:6379->6379/tcp ecstatic_lamarr
Stop the container and delete the container
[root@vm000949 ~]# docker stop 695d5f6afc27
695d5f6afc27
2.docker create
The syntax for creating a container is similar to run
[root@vm000949 ~]# docker create -p 127.0.0.1:6379:6379 -it --name="my-redis-server" -d redis unknown shorthand flag: 'd' in -d See 'docker create --help'.
If you directly modify run to create, an error will be reported, and the -d parameter cannot be executed without starting the operation.
[root@vm000949 ~]# docker create -p 127.0.0.1:6379:6379 -it --name="my-redis-server" redis
Error response from daemon: Conflict. The name "/my-redis-server" is already in use by container d66037100bddcd230e0c9955bdfb9b0dbae8ce4028a81534e1693ab95737c90a. You have to remove (or rename) that container to be able to reuse that name.
Prompt that the container created by the last run was not deleted
[root@vm000949 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@vm000949 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d66037100bdd redis "docker-entrypoint.sh" 3 minutes ago Exited (0) About a minute ago my-redis-server [root@vm000949 ~]# docker rm d66037100bdd D66037100bdd
Delete and recreate:
[root@vm000949 ~]# docker create -p 127.0.0.1:6379:6379 -it --name="my-redis-server" redis
2596bd8886b4095dc80e23315a5e073addb50fc9aa959456e026e6ca31676d28
[root@vm000949 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2596bd8886b4 redis "docker-entrypoint.sh" 8 seconds ago Created my-redis-server
Start the container:
[root@vm000949 ~]# docker start my-redis-server
my-redis-server
[root@vm000949 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2596bd8886b4 redis "docker-entrypoint.sh" About a minute ago Up 3 seconds 127.0.0.1:6379->6379/tcp my-redis-server 进入容器: [root@vm000949 ~]# docker exec -it my-redis-server redis-cli 127.0.0.1:6379> exit
From the above two methods, it can be seen that the life cycle comparison of create->start->exec is intuitive. Among them, the Docker run command includes two life cycles, create and start.
Finally: docker's log files are placed under /var/lib/docker/containers/, each docker has a corresponding file, cat its -json.log file to get the log file. In the disk space, the log file may take up a lot of Large spaces need to be cleaned up.