Skip to content
Advertisement

Docker-Compose Output File To Local Host

I have the below docker-compose.yaml file that sets up a database and runs a python script

version: '3.3'
services:
  db:
    image: mysql:8.0
    cap_add:
      - SYS_NICE
    restart: always
    environment:
      - MYSQL_DATABASE=test_db
      - MYSQL_ROOT_PASSWORD=xxx
    ports:
      - '3310:3310'
    volumes:
      - db:/var/lib/mysql
  py_service:
    container_name: test_py
    build: .
    command: ./main.py -r compute_init
    depends_on:
      - db
    ports:
      - 80:80
    environment:
      DB_HOST: db
      DB_PORT: 3306
      DB_USER: root
      DB_PASSWORD: xxx
      DB_NAME: test_db
    links:
      - db
    volumes:
      - py_output:/app/output
volumes:
  db:
    driver: local
  py_output:

To run it I perform the following

docker-compose build
docker-compose up
docker-compose run -v /home/ubuntu/docker_directory/output:/app/output/* py_service

Here is the Dockerfile

FROM python:3.7

RUN mkdir /app
WORKDIR /app
COPY env/requirements.txt requirements.txt

RUN pip install -r requirements.txt

COPY . .

CMD ["python3","main.py","-r","compute_init"]

Now this works fine I can see the data has been properly populated under the generated in the msql database. The python file at the end of the script should dump a csv file to /app/ouput/output.csv (via pandas library df.to_csv("output/output.csv")) My question is, how to recover that csv from the container to the local directory. The script seems to finish off without any errors, but can’t find the output file at the end.

Advertisement

Answer

it seems using docker-compose run -v $(pwd)/output:/app/output py_service did the job

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement