Docker Community Forums

Share and learn in the Docker community.

How to Extract file from Image?

Pls forgive me all ignorance here; complete Docker novice.

I am sure I’m not the first to encounter this, but cannot think of the correct search keywords apparently, so I’m coming up blanks.

I want to extend an image for myself, specifically the official Docker Wordpress image as it doesn’t offer quite what I need. I want to copy the raw (parameterised) files from the image, make my changes and then COPY them back in a Dockerfile. But can’t figure out how to get to the raw files.

Specifically; wp-config.php and .htaccess.

If you could just point me to the correct resource where I can figure this out; or alternatively a tutorial that doesn’t require me going back to “my first docker file” or the like, I would be grateful.

Many thanks.

There are severall ways to achive what you want…
– export the image, extract it and the archives inside it and get your file: docker save repo:tag > image.tar then tar xf image.tar to extract files of the tar. Repeat the tar step for each tar in the subfolders.

– copy the file from a running container: docker cp imageid:/path/to/file localpath

– use sed in your Dockerfile to inline replace content in the files: sed -i ... filename

Thanks that reply.

Had a look at the running image and saw what I expected. i.e. a fully filled file without the obvious ‘calling / assigning’ of the environment parameters.

I then did as you suggested and un-tarred every layer.tar file only to find the actual file absent in the structure. So it would appear they build it up somehow, so I guess I will have to figure out how they are doing that.

As an aside, I have seen hundreds of Docker tutorials about how to assign an environment variable in the run call, etc. But a variable is only good if it can be called.

NOWHERE, have I found any tutorial show me how to use that variable in a filesystem file to call it. If someone could point out to me where I can find that and where I have been a dope, I will gratefully continue on.

That’s impossible. Check the entrypoint script and follow what it does. The file MUST exist in one of the layers. Remember: a container is self contained. There are no magic hidden parts.

Probably a better option is to take the file from the github repo of the image? Sometimes the github repos are linked in the docker hub description or you can try a simple google search to find it.

It appears that all the magic happens in the file, which will require some advanced understanding of bash.

very much appreciated @meyay.

I will dig away at that for a bit. I’ve never bash scripted, but have a sufficient programming background to pick up what’s being done, and google if not.

Actualy, It might be sufficient to focus on set_config 'KEY' "$YOUR_NEW_ENV", of course you will need to add it to wp-config.php like define( 'KEY', 'some random string' ); as well to make it work. The function set_config encapsulates the replacment of the parameter in the config.

Though, I might be wrong. The above details are from a 5 minute analysis…

1 Like