Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用预定义的Redis转储创建Docker容器_Docker_Redis - Fatal编程技术网

使用预定义的Redis转储创建Docker容器

使用预定义的Redis转储创建Docker容器,docker,redis,Docker,Redis,我尝试用数据创建Redis docker容器。这个问题启发了我的方法。但由于某种原因,它不起作用 这是我的Dockerfile: FROM redis EXPOSE 6379 COPY redis-dump.csv / RUN nohup bash -c "redis-server --appendonly yes" & sleep 5s \ && cat /redis-dump.csv | redis-cli --pipe \ &&

我尝试用数据创建Redis docker容器。这个问题启发了我的方法。但由于某种原因,它不起作用

这是我的Dockerfile:

FROM redis

EXPOSE 6379

COPY redis-dump.csv /

RUN nohup bash -c "redis-server --appendonly yes" & sleep 5s \
    && cat /redis-dump.csv | redis-cli --pipe \
    && redis-cli shutdown save
    && ls /data
和docker-compose.yml:

version: '3.3'

volumes:
  redisdata:

services:
  redis:
    build:
      context: docker/redis
    volumes:
      - redisdata:/data
    ports:
      - "6379:6379"
当我创建容器时,Redis是空的。当我连接到容器目录时,
/data
也是空的。但是当我看到docker创建的日志时,有
dump.rdb
appendonly.aof
文件。转储文件位于容器中。当我在容器中运行
cat/redis-dump.csv | redis cli--pipe
时,数据在redis中可用。所以,问题是为什么没有db文件

以下是创建容器的完整日志:

Creating network "restapi_default" with the default driver
Creating volume "restapi_redisdata" with default driver
Building redis
Step 1/4 : FROM redis
 ---> a55fbf438dfd
Step 2/4 : EXPOSE 6379
 ---> Using cache
 ---> 2e6e5609b5b3
Step 3/4 : COPY redis-dump.csv /
 ---> Using cache
 ---> 39330e43e72a
Step 4/4 : RUN nohup bash -c "redis-server --appendonly yes" & sleep 5s     && cat /redis-dump.csv | redis-cli --pipe     && redis-cli shutdown save     && ls /data
 ---> Running in 7e290e6a46ce
7:C 10 May 2019 19:45:32.509 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7:C 10 May 2019 19:45:32.509 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=7, just started
7:C 10 May 2019 19:45:32.509 # Configuration loaded
7:M 10 May 2019 19:45:32.510 * Running mode=standalone, port=6379.
7:M 10 May 2019 19:45:32.510 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
7:M 10 May 2019 19:45:32.510 # Server initialized
7:M 10 May 2019 19:45:32.510 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
7:M 10 May 2019 19:45:32.510 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
7:M 10 May 2019 19:45:32.511 * Ready to accept connections
All data transferred. Waiting for the last reply...
Last reply received from server.
errors: 0, replies: 67600
7:M 10 May 2019 19:45:37.750 # User requested shutdown...
7:M 10 May 2019 19:45:37.750 * Calling fsync() on the AOF file.
7:M 10 May 2019 19:45:37.920 * Saving the final RDB snapshot before exiting.
7:M 10 May 2019 19:45:37.987 * DB saved on disk
7:M 10 May 2019 19:45:37.987 # Redis is now ready to exit, bye bye...
appendonly.aof
dump.rdb
Removing intermediate container 7e290e6a46ce
 ---> 1f1cd024e68f

Successfully built 1f1cd024e68f
Successfully tagged restapi_redis:latest
Creating restapi_redis_1 ... done
docker volume rm redisdata
以下是数据示例:

SET user:id:35 85.214.132.117
SET user:id:66 85.214.132.117
SET user:id:28 85.214.132.117
SET user:id:40 85.214.132.117
SET user:id:17 85.214.132.117
SET user:id:63 85.214.132.117
SET user:id:67 85.214.132.117
SET user:id:45 85.214.132.117
SET user:id:23 85.214.132.117
SET user:id:79 85.214.132.117
SET user:id:26 85.214.132.117
SET user:id:94 85.214.132.117

您必须在启动容器之前删除卷:

Creating network "restapi_default" with the default driver
Creating volume "restapi_redisdata" with default driver
Building redis
Step 1/4 : FROM redis
 ---> a55fbf438dfd
Step 2/4 : EXPOSE 6379
 ---> Using cache
 ---> 2e6e5609b5b3
Step 3/4 : COPY redis-dump.csv /
 ---> Using cache
 ---> 39330e43e72a
Step 4/4 : RUN nohup bash -c "redis-server --appendonly yes" & sleep 5s     && cat /redis-dump.csv | redis-cli --pipe     && redis-cli shutdown save     && ls /data
 ---> Running in 7e290e6a46ce
7:C 10 May 2019 19:45:32.509 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7:C 10 May 2019 19:45:32.509 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=7, just started
7:C 10 May 2019 19:45:32.509 # Configuration loaded
7:M 10 May 2019 19:45:32.510 * Running mode=standalone, port=6379.
7:M 10 May 2019 19:45:32.510 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
7:M 10 May 2019 19:45:32.510 # Server initialized
7:M 10 May 2019 19:45:32.510 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
7:M 10 May 2019 19:45:32.510 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
7:M 10 May 2019 19:45:32.511 * Ready to accept connections
All data transferred. Waiting for the last reply...
Last reply received from server.
errors: 0, replies: 67600
7:M 10 May 2019 19:45:37.750 # User requested shutdown...
7:M 10 May 2019 19:45:37.750 * Calling fsync() on the AOF file.
7:M 10 May 2019 19:45:37.920 * Saving the final RDB snapshot before exiting.
7:M 10 May 2019 19:45:37.987 * DB saved on disk
7:M 10 May 2019 19:45:37.987 # Redis is now ready to exit, bye bye...
appendonly.aof
dump.rdb
Removing intermediate container 7e290e6a46ce
 ---> 1f1cd024e68f

Successfully built 1f1cd024e68f
Successfully tagged restapi_redis:latest
Creating restapi_redis_1 ... done
docker volume rm redisdata
然后将Dockerfile更改为以下内容:

FROM redis

EXPOSE 6379

COPY redis-dump.csv /

ENTRYPOINT nohup bash -c "redis-server --appendonly yes" & sleep 5s \
    && cat /redis-dump.csv | redis-cli --pipe \
    && redis-cli save \
    && redis-cli shutdown \
    && ls /data
为了更快地获得结果,我建议将卷映射到本地文件夹:

version: '3.3'

services:
  redis:
    build:
      context: .
    volumes:
      - ./redisdata:/data
    ports:
      - "6379:6379"
看到它运行后,可以切换回正常的docker卷

现在运行:

docker-compose build
docker-compose up -d
容器将启动,也将正常停止,因为没有进程继续运行。但数据将显示在数据文件夹中

通常,在使用数据库时,填充应该在正在运行的容器而不是映像上完成

经过讨论,我们决定使用多阶段构建:

FROM redis as import 

EXPOSE 6379 

COPY redis-dump.csv / 

RUN mkdir /mydata 

RUN nohup bash -c "redis-server --appendonly yes" & sleep 5s \ 
&& cat /redis-dump.csv | redis-cli --pipe \ 
&& redis-cli save \ 
&& redis-cli shutdown \ 
&& cp /data/* /mydata/ 

RUN ls /mydata 

FROM redis 

COPY --from=import /mydata /data 
COPY --from=import /mydata /mydata 

RUN ls /data 

CMD ["redis-server", "--appendonly", "yes"]
第一阶段(导入)与最初发布的几乎相同。因为我们注意到在上次运行命令后,/data中的文件被删除,所以我们在另一个名为/mydata的文件夹中创建了一个副本


第二个阶段使用与基本阶段相同的映像,但它只复制上一个阶段所需的内容:来自/mydata的数据。它将此数据放置在/data文件夹中,然后启动redis服务器。

在启动容器之前,您必须删除卷:

Creating network "restapi_default" with the default driver
Creating volume "restapi_redisdata" with default driver
Building redis
Step 1/4 : FROM redis
 ---> a55fbf438dfd
Step 2/4 : EXPOSE 6379
 ---> Using cache
 ---> 2e6e5609b5b3
Step 3/4 : COPY redis-dump.csv /
 ---> Using cache
 ---> 39330e43e72a
Step 4/4 : RUN nohup bash -c "redis-server --appendonly yes" & sleep 5s     && cat /redis-dump.csv | redis-cli --pipe     && redis-cli shutdown save     && ls /data
 ---> Running in 7e290e6a46ce
7:C 10 May 2019 19:45:32.509 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7:C 10 May 2019 19:45:32.509 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=7, just started
7:C 10 May 2019 19:45:32.509 # Configuration loaded
7:M 10 May 2019 19:45:32.510 * Running mode=standalone, port=6379.
7:M 10 May 2019 19:45:32.510 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
7:M 10 May 2019 19:45:32.510 # Server initialized
7:M 10 May 2019 19:45:32.510 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
7:M 10 May 2019 19:45:32.510 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
7:M 10 May 2019 19:45:32.511 * Ready to accept connections
All data transferred. Waiting for the last reply...
Last reply received from server.
errors: 0, replies: 67600
7:M 10 May 2019 19:45:37.750 # User requested shutdown...
7:M 10 May 2019 19:45:37.750 * Calling fsync() on the AOF file.
7:M 10 May 2019 19:45:37.920 * Saving the final RDB snapshot before exiting.
7:M 10 May 2019 19:45:37.987 * DB saved on disk
7:M 10 May 2019 19:45:37.987 # Redis is now ready to exit, bye bye...
appendonly.aof
dump.rdb
Removing intermediate container 7e290e6a46ce
 ---> 1f1cd024e68f

Successfully built 1f1cd024e68f
Successfully tagged restapi_redis:latest
Creating restapi_redis_1 ... done
docker volume rm redisdata
然后将Dockerfile更改为以下内容:

FROM redis

EXPOSE 6379

COPY redis-dump.csv /

ENTRYPOINT nohup bash -c "redis-server --appendonly yes" & sleep 5s \
    && cat /redis-dump.csv | redis-cli --pipe \
    && redis-cli save \
    && redis-cli shutdown \
    && ls /data
为了更快地获得结果,我建议将卷映射到本地文件夹:

version: '3.3'

services:
  redis:
    build:
      context: .
    volumes:
      - ./redisdata:/data
    ports:
      - "6379:6379"
看到它运行后,可以切换回正常的docker卷

现在运行:

docker-compose build
docker-compose up -d
容器将启动,也将正常停止,因为没有进程继续运行。但数据将显示在数据文件夹中

通常,在使用数据库时,填充应该在正在运行的容器而不是映像上完成

经过讨论,我们决定使用多阶段构建:

FROM redis as import 

EXPOSE 6379 

COPY redis-dump.csv / 

RUN mkdir /mydata 

RUN nohup bash -c "redis-server --appendonly yes" & sleep 5s \ 
&& cat /redis-dump.csv | redis-cli --pipe \ 
&& redis-cli save \ 
&& redis-cli shutdown \ 
&& cp /data/* /mydata/ 

RUN ls /mydata 

FROM redis 

COPY --from=import /mydata /data 
COPY --from=import /mydata /mydata 

RUN ls /data 

CMD ["redis-server", "--appendonly", "yes"]
第一阶段(导入)与最初发布的几乎相同。因为我们注意到在上次运行命令后,/data中的文件被删除,所以我们在另一个名为/mydata的文件夹中创建了一个副本


第二个阶段使用与基本阶段相同的映像,但它只复制上一个阶段所需的内容:来自/mydata的数据。它将此数据放置在/data文件夹中,然后启动redis服务器。

我删除了所有内容,不仅是卷:docker compose down-v--rmi local,没有任何帮助。您可以发布“docker volume ls”的输出吗?还可以删除卷映射,运行容器,然后在容器中执行shell,并直接在运行的容器中检查/数据的内容。docker volume ls | grep redis不显示任何内容,redis没有卷。我已经尝试连接到container和check/data dir,由于某种原因,它是空的,这实际上是我的问题。但这并不是我读到的docker或volumes问题。这一定是你开始的方式。如果您共享CSV文件中我可以使用的几行代码,我可以自己启动它并给您答案我删除所有内容不仅仅是卷:docker compose down-v--rmi local,它没有帮助。您可以发布“docker volume ls”的输出吗?您还可以删除卷映射,运行容器,然后在容器中执行shell,并直接在运行的容器中检查/数据的内容。docker volume ls | grep redis不显示任何内容,redis没有卷。我已经尝试连接到container和check/data dir,由于某种原因,它是空的,这实际上是我的问题。但这并不是我读到的docker或volumes问题。这一定是你开始的方式。如果你分享CSV文件中我可以使用的几行代码,我可以自己开始并给你答案