Extract files from Docker Image

wrap up

Since it can not be extracted directly from the image, start the container as follows and copy it with docker cp

CONTAINER_ID=`docker create $IMAGE_NAME`
docker cp $CONTAINER_ID:$FILEPATH .

background

You may want to retrieve the source code or files in an Image that you have deployed for bugs or spec investigation. I have summarized the procedure of what to do at that time.

Find the target file path

All you need to know is the path to the target file or directory, but if not, you'll need to look inside to find what you're looking for.

Search using the shell

It should contain / bin / bash and / bin / sh, so use it to find files interactively. docker run --rm -it $IMAGE_NAME /bin/bash

If it is not included, you can use it as the base image of the Dockerfile and create an installed image.

Look for the difference for each layer with dive

You can see the file tree for each layer by using dive. If you know that it was added in a particular layer, you may find it faster. dive $IMAGE_NAME

The following article will be helpful for how to use dive. https://qiita.com/t_o_d/items/625ad7a274ca34be3312

Copy the target file

It seems that files cannot be extracted directly from the image. Therefore, you can copy the files and folders by starting the container and copying with docker cp as shown below.

# IMAGE_Set the target image name in NAME and the file path in the Docker Image of the target file in FILEPATH.
IMAGE_NAME=me/my_application:1.0.0
FILEPATH=/app/scripts/woker/

CONTAINER_ID=`docker create $IMAGE_NAME`
docker cp $CONTAINER_ID:$FILEPATH .
docker rm $CONTAINER_ID

Recommended Posts

Extract files from Docker Image
[Docker] Copy files from docker container to host
Learn more about docker image and Dockerfile FROM
Copy files from docker container to host (docker cp)
Delete unused docker image
How to deploy to Heroku from a local docker image
docker review image creation-container start
Migrating from vargrant to docker
Using Docker from Java Gradle
Install openjdk8 on Docker image (Debian)
Run lambda with custom docker image
Create a lightweight STNS Docker image
Migrate Docker image to another server
Deploy Flask's Docker image on Heroku
Extract the required information from pom.xml
2. Create Docker image and register Registry
Restart apache with docker php-apache image
Get gcr image from local kubernetes
Update MySQL from 5.7 to 8.0 with Docker
[Splunk] Build Splunk Enterprise (use docker image)