简体   繁体   English

如何将文件复制到停止的docker容器

[英]How to Copy file to stopped docker container

I am running elasticsearch from within a docker container. 我正在docker容器中运行elasticsearch。

While configuring elasticsearch for ssl and shield my elasticsearch.yml file got illegal entry ie TAB instead of a space . 在为ssl和shield配置elasticsearch时,我的elasticsearch.yml文件被非法输入,即TAB而不是space
Now my docker container is not starting up and giving the following error: 现在我的docker容器没有启动并出现以下错误:

{1.4.4}: Setup Failed ...
- SettingsException[Failed to load settings from [file:/elasticsearch/config/elasticsearch.yml]]
         IOException[Tabs are illegal in YAML.  Did you mean to use whitespace character instead?]
 org.elasticsearch.common.settings.SettingsException: Failed to load
 settings from [file:/elasticsearch/config/elasticsearch.yml]
         at org.elasticsearch.common.settings.ImmutableSettings$Builder.loadFromStream(ImmutableSettings.java:947)
         at org.elasticsearch.common.settings.ImmutableSettings$Builder.loadFromUrl(ImmutableSettings.java:931)
         at org.elasticsearch.node.internal.InternalSettingsPreparer.prepareSettings(InternalSettingsPreparer.java:77)
         at org.elasticsearch.bootstrap.Bootstrap.initialSettings(Bootstrap.java:106)
         at org.elasticsearch.bootstrap.Bootstrap.main(Bootstrap.java:177)
         at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:32)
 Caused by: java.io.IOException: Tabs are illegal in YAML.  Did you
 mean to use whitespace character instead?
         at org.elasticsearch.common.settings.loader.YamlSettingsLoader.load(YamlSettingsLoader.java:44)
         at org.elasticsearch.common.settings.ImmutableSettings$Builder.loadFromStream(ImmutableSettings.java:944)
         ... 5 more

How can I edit elasticsearch.yml or replace it without loosing data or replace the elasticsearch.yml file in my existing container? 如何编辑elasticsearch.yml或替换它而不丢失数据或替换现有容器中的elasticsearch.yml文件?

您可以将文件复制出来然后返回容器( 即使容器已停止 ),并使用docker cp $cont_name:/path/in/container /path/on/host复制出来,然后将docker cp /path/on/host $cont_name:/path/in/container

There are several cases: 有几种情况:

  1. In the container, the elasticsearch.yml file resides in a volume data directory 在容器中, elasticsearch.yml文件驻留在卷数据目录中

Volume data directory is a special data storage backend for Docker containers, which is called vfs backend. 卷数据目录是Docker容器的特殊数据存储后端 ,称为vfs后端。 The directories are essentially normal directories mapped in the host file system, thus provide no copy-on-write capability. 这些目录基本上是在主机文件系统中映射的普通目录,因此不提供写时复制功能。 Mainly the mapped directories locate at /var/lib/dockers/vfs/dir/{container_id} , but this is configurable. 主要是映射目录位于/var/lib/dockers/vfs/dir/{container_id} ,但这是可配置的。 To be sure, you can use docker inspect {container_name} to check the location: 可以肯定的是,您可以使用docker inspect {container_name}来检查位置:

$> docker inspect my_container

..... (omitted output)

"Volumes": {
"/datadir": "/var/lib/docker/vfs/dir/b2479214c25cd39c901c3211ed14cb9668eef822a125ca85de81425d53c9ccee"
},

As you can see, /datadir , which is a volume data directory in the container, is mapped to /var/lib/docker/vfs/dir/b2479214c25cd39c901c3211ed14cb9668eef822a125ca85de81425d53c9ccee of the host file system. 如您所见, /datadir (容器中的卷数据目录)映射到主机文件系统的/var/lib/docker/vfs/dir/b2479214c25cd39c901c3211ed14cb9668eef822a125ca85de81425d53c9ccee Under such circumstances, the answer to your question is quite easy: just copy them as normal files into the mapped host directory. 在这种情况下,您的问题的答案很简单:只需将它们作为普通文件复制到映射的主机目录中。

  1. The directory in container is not volume data directory. 容器中的目录不是卷数据目录。

Since Docker can use multiple storage backend for non-volume directories, there is no simple answer for you question. 由于Docker可以为非卷目录使用多个存储后端,因此您的问题没有简单的答案。

If you happened to use AUFS as the backend, the container file system is mounted onto the host file system, which is somehow similar to the vfs case. 如果您碰巧使用AUFS作为后端,则容器文件系统将安装到主机文件系统上,这与vfs情况类似。 You can locate the mapped directory in the host file system, and access files there. 您可以在主机文件系统中找到映射的目录,并在那里访问文件。 For detailed information about AUFS in Docker, please refer to Docker and AUFS in practice . 有关Docker中AUFS的详细信息,请参阅Docker和AUFS

If you use other backends, eg devicemapper, or btrfs, I guess there's no simple way to access container files from the host. 如果您使用其他后端, 例如 devicemapper或btrfs,我想没有简单的方法可以从主机访问容器文件。 Maybe you can try @VonC 's method. 也许你可以试试@VonC的方法。

replace it without losing data 替换它而不会丢失数据

Ideally, those data should be stored in path mounted from separate data volume containers ( which do not run, they are just created ). 理想情况下,这些数据应存储在从单独的数据卷容器( 不运行,只是创建它们 )的路径中。 That way, your main service container (the elasticsearch one) can crash and be replaced at will. 这样,您的主服务容器( elasticsearch容器)可能会崩溃并随意更换。
In that configuration (mounting data from volume containers), you could rebuild your elasticsearch image with the new config file, and resume from there. 在该配置中(从卷容器安装数据),您可以使用新的配置文件重建elasticsearch图像,然后从那里继续。

In your current config, if those data are not in a VOLUME declared by your Dockerfile, what you can do is: 在您当前的配置中,如果这些数据不在您的Dockerfile声明的VOLUME中,您可以做的是:

  • [docker commit <stoppped_container_id>][1] newimage
  • make a Dockerfile using that newimage, and COPY the fixed config file 使用该newimage创建一个Dockerfile,并复制固定的配置文件
  • running your container from that new image. 从新图像运行容器。

Tabs are not allowed in YML file. YML文件中不允许使用选项卡。 You can edit it with any editor nano or vim or vi. 您可以使用任何编辑器nano或vim或vi对其进行编辑。

Replacing or editing the elasticsearch.yml file wont leads to data loss. 替换或编辑elasticsearch.yml文件不会导致数据丢失。

Docker images are delivered trimmed to bare minimum - so no editor is installed with the shipped container. Docker镜像被修剪到最小 - 因此没有安装编辑器与装运容器。 That's why there's a need to install it manually. 这就是为什么需要手动安装它的原因。

docker exec -it <container> bash

and run: 并运行:

apt-get update
apt-get install vim

or use the following Dockerfile: 或使用以下Dockerfile:

FROM confluent/postgres-bw:0.1 来自汇合/ postgres-bw:0.1

RUN ["apt-get", "update"]
RUN ["apt-get", "install", "-y", "vim"]

For more How to edit file after I shell to a docker container? 有关更多如何在shell到docker容器后编辑文件?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM