Amit Jamwal
Amit Jamwal

Reputation: 649

How to Copy file to stopped docker container

I am running elasticsearch from within a docker container.

While configuring elasticsearch for ssl and shield my elasticsearch.yml file got illegal entry i.e. TAB instead of a space.
Now my docker container is not starting up and giving the following error:

{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?

Upvotes: 37

Views: 33143

Answers (3)

BMitch
BMitch

Reputation: 264026

You can copy files out and then back into a container (even when the container is stopped) with the docker cp $cont_name:/path/in/container /path/on/host to copy out and then docker cp /path/on/host $cont_name:/path/in/container.

Upvotes: 73

VonC
VonC

Reputation: 1325337

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.
In that configuration (mounting data from volume containers), you could rebuild your elasticsearch image with the new config file, and resume from there.

In your current config, if those data are not in a VOLUME declared by your Dockerfile, what you can do is:

  • [docker commit <stoppped_container_id>][1] newimage
  • make a Dockerfile using that newimage, and COPY the fixed config file
  • running your container from that new image.

Upvotes: 0

Haizi
Haizi

Reputation: 708

There are several cases:

  1. In the container, the elasticsearch.yml file resides in a volume data directory

Volume data directory is a special data storage backend for Docker containers, which is called vfs backend. 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. To be sure, you can use docker inspect {container_name} to check the location:

$> 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. 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.

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. 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.

If you use other backends, e.g. devicemapper, or btrfs, I guess there's no simple way to access container files from the host. Maybe you can try @VonC 's method.

Upvotes: 2

Related Questions