简体   繁体   中英

Docker python output csv file

I have a script python which should output a file csv. I'm trying to have this file in the current working directory but without success.

This is my Dockerfile

FROM python:3.6.4

RUN apt-get update && apt-get install -y libaio1 wget unzip

WORKDIR /opt/oracle 
RUN wget https://download.oracle.com/otn_software/linux/instantclient/instantclient- 
basiclite-linuxx64.zip && \ unzip instantclient-basiclite-linuxx64.zip && rm 
-f instantclient-basiclite-linuxx64.zip && \ cd /opt/oracle/instantclient* 
&& rm -f jdbc occi mysql *README jar uidrvci genezi adrci && \ echo 
/opt/oracle/instantclient > /etc/ld.so.conf.d/oracle-instantclient.conf && 
ldconfig 
RUN pip install --upgrade pip

COPY . /app 
WORKDIR /app

RUN pip install --upgrade pip 
RUN pip install pystan 
RUN apt-get -y update && python3 -m pip install cx_Oracle --upgrade 
RUN pip install -r requirements.txt

CMD [ "python", "Main.py" ]

And run the container with the following command

docker container run -v $pwd:/home/learn/rstudio_script/output image

Your script Main.py is probably not trying to write to /home/learn/rstudio_script/output . The working directory in the container is /app because of the last WORKDIR directive in the Dockerfile. You can override that at runtime with --workdir but then the CMD would have to be changed as well.

One solution is to have your script write files to /output/ and then run it like this:

docker container run -v $PWD:/output/ image

This is bad practice to bind a volume just to have 1 file on your container be saved onto your host.

Instead, what you should leverage is the copy command:

docker cp <containerId>:/file/path/within/container /host/path/target

You can set this command to auto execute with bash, after your docker run.

So something like:

#!/bin/bash

# this stores the container id
CONTAINER_ID=$(docker run -dit img)

docker cp $CONTAINER_ID:/some_path host_path

If you are adamant on using a bind volume, then as the others have pointed out, the issue is most likely your python script isn't outputting the csv to the correct path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM