Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

1.4K
Visualizações
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.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

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.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda