I have a python script that I have placed inside a docker container named "grapher". The python script inside the "grapher" container generates a graph and saves it like so:
# CODE ABOVE THIS MAKES PLOT #
plt.draw()
filename = "digraph" + str(self.count) + ".png"
plt.savefig(filename)
I want to access these saved figures on my computer, so I am attempting to use "volumes" inside my docker-compose file. The problem is, all the tutorials I find say I need to include the "path in the container". And then the tutorials just magically know what file path to use.
How the heck do I figure out what filepath my container is using?? I've made a bunch of file location guesses based off the tutorials I've found, one of which caused Ubuntu 18.04 to black-screen-of-death (whoops...). I am totally lost. I've included a snippet of my docker-compose.yml file below.
version: '3.0'
services:
# OTHER CONTAINERS ABOVE THIS#
grapher:
build: ./Grapher
depends_on:
- hmi_pass_thru
volumes:
- graph-data:/home/vic/Documents/5ExtraExtraNodes/Grapher
network_mode: host
volumes:
graph-data:
networks:
test_net:
external: true
Please help.
Edit 1: My main confusion is that I don't have a file system inside my container. My container is just running the python script. So how do I tell what my "path in container" is?
Edit 2 : My DockerFile that the "grapher" container is built from:
FROM python:3
WORKDIR /home/vic/Documents/5ExtraExtraNodes/Grapher
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python","-u","pcap_grapher3.py"]
Edit 3 : Results of exec
docker exec -it 5fe4dc4 /bin/bash
root@vic-Capstone:/home/vic/Documents/5ExtraExtraNodes/Grapher# ls -a
. digraph14.png digraph21.png digraph29.png digraph36.png digraph43.png digraph8.png
.. digraph15.png digraph22.png digraph3.png digraph37.png digraph44.png digraph9.png
Dockerfile digraph16.png digraph23.png digraph30.png digraph38.png digraph45.png pcap_grapher3.py
digraph1.png digraph17.png digraph24.png digraph31.png digraph39.png digraph46.png requirements.txt
digraph10.png digraph18.png digraph25.png digraph32.png digraph4.png digraph47.png
digraph11.png digraph19.png digraph26.png digraph33.png digraph40.png digraph5.png
digraph12.png digraph2.png digraph27.png digraph34.png digraph41.png digraph6.png
digraph13.png digraph20.png digraph28.png digraph35.png digraph42.png digraph7.png
root@vic-Capstone:/home/vic/Documents/5ExtraExtraNodes/Grapher#
I believe you are getting the syntax for volumes backwards. If you are looking to save files from your container onto you host, bind the volumes as host:container
.
You can define whichever path you want inside your container's file system. Then, you save to that location.
Since you copy your files over with
COPY . .
Your script is copied to the container directory relative to the WORKDIR
. Mounting this directory will allow you view/edit/save files to your host machine.
From the docs:
volumes:
# Just specify a path and let the Engine create a volume
- /var/lib/mysql
# Specify an absolute path mapping
- /opt/data:/var/lib/mysql
# Path on the host, relative to the Compose file
- ./cache:/tmp/cache
# User-relative path
- ~/configs:/etc/configs/:ro
# Named volume
- datavolume:/var/lib/mysql
An example Dockerfile which may help explain it better than me:
FROM python:3.8-slim-buster
# all code will be inside this directory in the container
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
# copy all code in present working directory on HOST to /app/. on the CONTAINER
COPY . .
CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"]
References:
https://docs.docker.com/compose/compose-file/compose-file-v3/#volumes
https://docs.docker.com/language/python/build-images/
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.