How do I run a Docker image and as soon as the process is over save the generated file inside the container to the host computer?

Asked

Viewed 18 times

-1

I have a code that should be run in a container. As soon as it runs it will generate a png file. Follow Dockerfile:

FROM python:3
COPY . /projeto
RUN apt update
RUN yes | apt install libeccodes-tools
RUN yes | apt install -y python3 && yes | apt install python3-pip
RUN pip3 install -r projeto/requirements.txt
WORKDIR /projeto
CMD python3 exemplo.py

example.py:

import pandas as pd
ex = pd.DataFrame({'teste': [1,2,3,4,5,6]})
ex.to_csv('exemplo.csv')

Image build:

docker build -t meu_codigo .

I wish I could give the command below and in the scheduled execution process already save the png images(or any file I generate) in some host computer folder.

docker run --rm meu_codigo

How can I do that? Thanks in advance.

  • 1

    Why not create a volume and use with the container? so all that save on volume will already be available on the host computer without needing commands to copy

  • This image will need to be rotated daily and restarted at the end of the process. These are the guidelines that the team below.

  • 1

    right, but you don’t need to delete the right volume? It’s that "folder" you want to save. If you just want to do a job that moves the volume content to another place

1 answer

0

To do this, Voce has to create a volume, a link between your directory and a directory inside the container

See this example, where the command will generate result inside a directory aux and this is linked to a directory output

Dockerfile

FROM alpine:3.13.5
RUN mkdir aux
CMD echo "my text" > aux/output.txt
mkdir output
docker build . -t my-image
docker run --volume=$(pwd)/output:/aux my-image
cat output/output.txt

in your case:

Dockerfile

FROM python:3
COPY . /projeto
RUN apt update
RUN yes | apt install libeccodes-tools
RUN yes | apt install -y python3 && yes | apt install python3-pip
RUN pip3 install -r projeto/requirements.txt
WORKDIR /projeto
CMD python3 exemplo.py

example.py

import pandas as pd
ex = pd.DataFrame({'teste': [1,2,3,4,5,6]})
ex.to_csv('aux/exemplo.csv')
mkdir output
docker build -t meu_codigo .
docker run --rm --volume=$(pwd)/output:/aux meu_codigo
cat output/exemplo.csv

Note that if the directory does not exist in the image, when using the volume option it is created without problems

Browser other questions tagged

You are not signed in. Login or sign up in order to post.