-1

Good afternoon

I'm a new user of Docker and I have a question please:

I created a container named "container_worker" which running a Python script to create some data. The data created is stored in the container in a file named "data".

I want to copy this "data" file to my host to be able to use it for another purpose. I saw it's possible to do it with the "docker cp" command but I want to do it directly in my Dockerfile or my Docker-compose file.

Here are my files:

Dockerfile:

FROM archlinux

RUN pacman-db-upgrade \
&& pacman -Syyu --noconfirm \
&& pacman -S python --noconfirm \
&& pacman -S python-pip --noconfirm \
&& pip install requests

COPY /worker/script.py .

CMD python3 vmtracer.py >> data

docker-compose.yml

version: "3"
services:
  worker:
    image: image_worker
    container_name: container_worker
    build:
      dockerfile: ./worker/Dockerfile

Thank you very much.

2
  • A Docker container has an isolated filesystem, and can't usually read or write files from the host. Would it make more sense to run this program in a Python virtual environment, without Docker?
    – David Maze
    Feb 9, 2022 at 15:05
  • Hello, thank you for the advice. But I want to use Docker. I found a way with volumes between host and container. Thanks Feb 24, 2022 at 11:55

1 Answer 1

1

You can bind/mount volume from your local machine to container and put the output to that shared location.
Use command docker volume create my-vol , to create volume.
Use command docker volume ls ,to list your volumes.
Use parameter volumes: {your-volume-name} in docker-compose file to use the created volume.

Refer the link for more : https://docs.docker.com/storage/volumes/

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.