0

I have a Dockerfile inside which I call a python code that generates and creates an XML report. When I build the Dockerfile, the python code runs but the problem is that I am not able to persist the the generated XML report. The Dockerfile is as below:

FROM python:3.7.5-alpine3.10 as base
FROM base as builder
RUN mkdir /install
WORKDIR /install
pip install flask

COPY *.py /app/

WORKDIR /app
RUN python main.py

main.py has a code to generate an XML report. When I just call python main.py on cmd prompt, the report gets generated and saved on the same directory where the code is . But I am not able to locate the report when I call build Docker (which in turns calls RUN main.py). The way I build docker is as follows:

docker build -f ./Dockerfile

Only on the success based on the report output, I decide whether to get create the docker image or not.

Jeevika
  • 155
  • 1
  • 4
  • 18
  • I think that what you want is to access locally the `.xml` file generated by your script, am I right? If that's the case, then you have to use [volumes](https://docs.docker.com/storage/volumes/). By creating a volume, you will be able to access all the generated files inside the container in your localhost. – vpalmerini Jan 07 '21 at 21:21

1 Answers1

0

given the following files

# Dockerfile

FROM python:alpine
RUN mkdir /app
WORKDIR /app
COPY *.py /app/
RUN python main.py
# main.py

import random
import sys

if __name__ == "__main__":
  num = random.randint(1, 10)
  print(num)
  with open("output.txt", "w") as f: f.write(str(num))
  sys.exit(0 if num % 2 == 0 else 1)

a docker image named foo will be created if the random drawn integer is even and fail otherwise

docker build -f ./Dockerfile -t foo .

if the image is created, you can read the output of script by

docker run --rm foo cat /app/output.txt

if you wish to copy the output file an image - create a temporary container from the image, copy the file, then delete the container

id=$(docker create foo)
docker cp $id:/app/output.txt ./
docker rm -v $id
Mr.
  • 9,429
  • 13
  • 58
  • 82
  • I am not running something in a container. I am just building a dockerfile. The image hasn't created yet – Jeevika Jan 07 '21 at 17:51
  • Thanks, but I do not want to 'cat' the file, I want to persist in my local folder (not container) – Jeevika Jan 08 '21 at 04:14