How-to
How two Docker containers share one CSV file on an edge PC
Short answer
The vendor Node-RED container stores its CSV through a relative-path bind mount, so no other container can reach it. Declare a Docker named volume, mount it in both the Node-RED and Python services, point the write file node at /mydata, and mount the volume read-only on the Python side.
On this page
The CSV your Node-RED logger writes under home/edge/sim-edge-data/sim-edge-platform-node-red/files cannot be opened from a second Docker container, because the vendor stack stores it through a bind mount declared with a relative path. A relative bind mount belongs to one compose project only, so your Python app can never mount it by name. The fix is to move the logging onto a Docker named volume: run a Node-RED instance you own, point its write file node at /mydata, and mount that volume read-only in the Python container. The same two-runtime pairing on one machine is covered in the article on running Python and Node-RED beside PLC code on an edge PC (article:run-python-and-node-red-beside-plc-code-on-an-edge-pc).
Why the vendor bind mount cannot be shared #
A bind mount maps a host directory into one container, and a relative path in a compose file resolves against that application's own project directory. No other compose project knows the location, and there is no name to reference it by. A named volume is the opposite: Docker manages the storage and gives it a name, and any container that mounts that name sees the same files, whether it sits in the same compose file or a different one.
The vendor Flow Creator container uses the bind-mount form, so its files are reachable only from within Flow Creator itself. Do not try to point your Python compose file at the vendor project's host path by absolute location; that couples your application to the vendor's directory layout and breaks on the next stack update.
Check before you start: run docker inspect <flow-creator-container> and read the Mounts array. The entry for the files directory shows "Type": "bind" with a host source path. That confirms the data cannot be shared by name and that a new logging path is required.
Declare one named volume both compose files can see #
Declare the volume once under the top-level volumes: key and mount it in both services. The image names below are examples standing in for your own Node-RED image and your own Python image; the structure is what matters:
services:
node-red:
image: your-node-red-image
volumes:
- mydata:/mydata
python-app:
image: your-python-image
volumes:
- mydata:/mydata:ro
volumes:
mydata:
name: mydata
The name: line matters. Compose prefixes named volumes with the project name by default, so a project directory called edgelogs would create a volume actually named edgelogs_mydata. Setting an explicit name gives both compose files one exact name to resolve. If you prefer the default, declare the volume in the second project with external: true and spell out the prefixed name exactly; a mismatched name does not raise an error, it silently creates a second, empty volume.
If compose rejects the edited file with a must-be-a-mapping error, a volumes entry is indented as a string instead of a mapping. The same failure class is walked through in the fix for the Docker Compose logging options must be a mapping error (article:fixing-the-docker-compose-logging-options-must-be-a-mapping-error).
Check: after starting the stack, run docker volume ls and docker volume inspect <volume-name>. Exactly one volume with the expected name should exist, and docker inspect on both containers should list it under Mounts.
Point the write file node at the shared path #
In your Node-RED flow, set the write file node, or the CSV logging function feeding it, to a filename under the mount point, for example /mydata/log.csv, with its action set to append. The Node-RED process needs write permission on the mount point. At first creation the volume inherits ownership from the image's directory, so confirm the Node-RED user's UID with docker exec <node-red-container> id and compare it against ls -ld /mydata.
Check: trigger the flow once, then run docker exec <node-red-container> ls -l /mydata. The CSV should exist, be owned by the Node-RED user, and grow on each write. A Permission denied message in the Node-RED debug sidebar means the UID and the directory owner do not match; align them with chown before continuing.
Mount the volume read-only in the Python container #
The same files now appear at /mydata inside the Python container, with no copy step and no network call. Keep the :ro suffix on that service's mount: the Python app only reads, and a read-only mount guarantees that a coding error or an accidental open in write mode cannot truncate or lock the logger's file.
Check: run docker exec <python-container> ls -l /mydata twice, one minute apart. The file should appear in both runs with the same size and modification time the Node-RED container reports. An empty listing means the container resolved a different volume name, which points back to the project-prefix issue above. A Read-only file system error on a deliberate write attempt confirms the flag is active.
Read a CSV that grows while you parse it #
Node-RED appends while Python reads, and nothing synchronizes the two. A read can land mid-write and return a final line without its newline terminator. Open the file fresh on each poll, drop an unterminated last line, and parse the rest:
import csv
def read_complete_rows(path):
with open(path, 'r', newline='') as f:
data = f.read()
if data and not data.endswith('\n'):
data = data[:data.rfind('\n') + 1]
return list(csv.reader(data.splitlines()))
rows = read_complete_rows('/mydata/log.csv')
print(len(rows))
The path argument and the printed row count are illustrative; the documented parts are the Python built-ins open, csv.reader, str.endswith, str.rfind and str.splitlines. The test that proves the function works: call it twice one minute apart while the flow is logging. The row count rises by the number of rows Node-RED wrote in that interval, and every returned row has the same column count as the header.
A worked example of the growth rate, using example inputs of one row per minute and a 64-byte row: 1,440 rows per day times 64 bytes gives 92,160 bytes per day, about 90 KiB, and roughly 2.6 MiB over 30 days. Re-parsing a file that size every minute costs an edge PC almost nothing. If the logging interval tightens to once per second, the same 64-byte row gives 86,400 rows per day, about 5.3 MiB per day, and the file reaches 100 MB in roughly 19 days. At that rate either track the byte offset of the last complete line and read only new bytes, or rotate the CSV by filename from Node-RED. Decide by the product of row size and logging rate, not by a fixed file-size cutoff.
Verify the shared logging path end to end #
- Run
docker inspecton both containers. Each should list a mount of typevolumewith the same name at/mydata, and the Python mount should show read-only. - Note the CSV size and modification time inside the Node-RED container, wait one logging cycle, and note them again. The size should increase and the timestamp should advance.
- Run the same listing in the Python container. The size and timestamp should match step 2 exactly.
- Run the parser across at least two logging cycles. The row count should rise by the rows Node-RED wrote, with no malformed rows.
- Restart only the Python container and confirm it still reads the file. The data must persist, because the named volume lives independently of either container's lifecycle.
- Reboot the edge PC once during commissioning and repeat steps 3 and 4. The named volume should come back with its contents, which is what makes this layout safe for unattended cabinets.
The decision in each case: leave the vendor Flow Creator container exactly as it is and keep treating its bind mount as private; do your logging in a Node-RED instance you own, writing to an explicitly named volume that both compose files declare by the same name. Read the CSV from Python through a read-only mount at /mydata, discarding unterminated last lines, and switch to offset tracking or rotation only when row size times logging rate pushes the file into the tens of megabytes per month.
Questions asked
Can a container started with docker run outside compose use the same volume?
Yes. A named volume is a Docker object, not a compose object, so any container can mount it by name with a flag such as -v mydata:/mydata, using the exact volume name. Run docker volume ls first to confirm only one volume carries that name; two volumes with similar names is the usual cause of an empty directory.
How do I move existing CSV rows from the old bind mount into the new volume?
Start a short helper container that mounts the named volume, then copy the files into it with docker cp, for example docker cp log.csv helper:/mydata/log.csv. Compare the file owner with the Node-RED user's UID afterwards and adjust ownership if needed, so the logger can keep appending to the copied file without a permission error.
Does the CSV survive a container rebuild or an edge PC reboot?
Yes. Named volumes persist until they are deleted with docker volume rm, so rebuilding either container or rebooting the edge PC leaves the data in place. Note that docker compose down keeps named volumes by default; the data is only lost if you run that command with the volume-removal flag or delete the volume explicitly.
What if both containers need to write to the volume?
Keep one writer per file. Let the Node-RED logger own the CSV it appends to, and if the Python app must produce output, write it to a different filename in the same volume, for example a results file beside the log. Two processes appending to one CSV interleave partial lines that neither can parse reliably.