Retrieve Docker application results before exiting

Viewed 44

I need to run an external application using docker. I use the following code to run the application.

docker run --mount type=bind,source="$(pwd)"/PSORTb,target=/home/tdi/PSORTb/ --rm -ti "brinkmanlab/psortb_commandline:1.0.2" /usr/local/psortb/bin/psort -i /home/tdi/PSORTb/all_validated_effectors.fasta --negative

I use mount to point towards my local input file and --negative is a flag used in the application. However, my question is how to retrieve the output. If I understand correctly, the docker container exits after the job is completed and simultaneously deletes the output file. After running the above code, I get the following message.

Saving results to /20210910084030_psortb_gramneg.txt

How can I read this file or save locally?

3 Answers

You need a Docker Volume to persist the data to the Host.

Follow these steps:

Create a directory on the Host next to the Dockerfile, or where you start the docker command from

mkdir outputfiles

Write the output in the Application to a directory

Make sure your application is writing the outputfile to a certain directory. Let's say /output

Connect both directories with a 'Volume'

See the "-v" line below:

docker run 
    --mount type=bind,source="$(pwd)"/PSORTb,target=/home/tdi/PSORTb/ 
    --rm 
    -v ./outputfiles:/output
    -ti "brinkmanlab/psortb_commandline:1.0.2" /usr/local/psortb/bin/psort 
    -i /home/tdi/PSORTb/all_validated_effectors.fasta 
    --negative

After the container get removed, the file will still be there.

See these Docs for more info

Mount a host directory as a volume and write the file there instead of in the / of the container. Once the container exits, the files will remain inside the directory on the host.

In the example below, the host's "/root" directory is mapped to the "/opt/root" directory inside the container, which is a strange directory to map, but it should work just as almost any other dir. The container would write its files to "/opt/root" internally and then the host would retrieve them from "/root" externally once the work has completed.

The "-v" parameter is what you want to look at here:

The following example mounts the host's /root directory into the Docker container at /opt/root:
docker run -t -i -v /root:/opt/root debian /bin/bash

https://docs.windriver.com/bundle/Wind_River_Linux_Tutorial_Using_Docker_Containers_LTS_1/page/cdv1540580083374.html

If you can choose the directory where the file is saved then you can probably use another bind mount to get the file saved in the host filesystem:

docker run -v "$(pwd)/output:/output/dir"

But if the file is saved in the container's root directory (/) I don't think this is the best solution.

Another way would be to keep the container after it stopped by removing the --rm option from your command. Then you can copy the file from the container to the host filesystem using docker cp:

docker cp CONTAINER_ID:/20210910084030_psortb_gramneg.txt ./your/host/dir
Related