Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Copy file to stopped docker container

Tags:

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?

like image 290
Amit Jamwal Avatar asked Dec 23 '15 06:12

Amit Jamwal


People also ask

How do I resume a stopped docker container?

Enter the docker start command with the Container ID in the command line to resume the Container. Note: This preserves environment variables set during the initial docker run statement.

Can we copy a file into running docker container?

Another way to copy files to and from Docker containers is to use a volume mount. This means we make a directory from the host system available inside the container. The command above runs a grafana container and mounts the /tmp directory from the host machine as a new directory inside the container named /transfer.


2 Answers

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.

like image 82
BMitch Avatar answered Sep 25 '22 21:09

BMitch


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.

like image 27
Haizi Avatar answered Sep 23 '22 21:09

Haizi