• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

1.3K
Vistas
Docker - Writing python output to a csv file in the current working directory

I want to learn how to deploy applications using Docker and I'm working with this simple python program that writes some data into a csv file in the current working directory. I can see the output.csv file in the current working directory on my local machine but having trouble with it when I run the docker image.

After reading various articles and posts on stackoverflow, mounting the local directory path using "-v" seems to be the way to achieve this but I am unable get the right command. I tried creating a new directory on my local machine named "output_docker" ( I also replaced the get.cwd() option in the program with this path) and use that with the -v option.

 docker container run -v "/Users/Desktop/output_docker" docker_image_name

Python code (scraper.py)

import pandas as pd
import os

data = [['tom', 10], ['nick', 15], ['juli', 14]]
df = pd.DataFrame(data, columns = ['Name', 'Age'])

dirpath = os.getcwd()
print("dirpath = ", dirpath, "\n")

output_path = os.path.join(dirpath,'output.csv')
print(output_path,"\n")

df.to_csv(output_path)

DOCKERFILE

FROM python:3
ADD scraper.py /
RUN pip install pandas
CMD ["python3","./scraper.py"]

Output on local machine

$python3 scraper.py
   Name  Age
0   tom   10
1  nick   15
2  juli   14

dirpath =  /Users/Prathusha/Desktop/topos_docker 

/Users/Prathusha/Desktop/topos_docker/output.csv 

Output when I run the docker image

$docker build -t ex_scraper .
$docker run ex_scraper
   Name  Age
0   tom   10
1  nick   15
2  juli   14
dirpath =  / 

/output.csv 

I understand that the "output.csv" file will be located within the subdirectories of the docker container, but I want it to be visible in the current working directory(or desktop) when the docker image is run on a different machine. Would appreciate if anyone can point out where I am going wrong.

about 3 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can bind your host directory, I would suggest using a WORKDIR & replace ADD with COPY -

DOCKERFILE

FROM python:3
WORKDIR /mydata
COPY scraper.py ./
RUN pip install pandas
CMD ["python3","./scraper.py"]

Run it -

docker run -v ${PWD}:/data ex_scraper

You should now be able to see the CSV in your current directory on host.

about 3 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda