You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从Docker Web容器向关联的Tomcat容器写入文件?

How to Write Files from a Docker Container to Another Container

Hey there! That error makes total sense—Docker containers have completely isolated filesystems by default, so your web container can't directly reach into the tomcat_service container's /bitnami/tomcat/data/ path like you tried. Here are three reliable ways to solve this:

1. Use a Shared Docker Volume (Best for Persistent File Sharing)

This is the most straightforward approach for ongoing file sync between containers. You'll create a shared volume that both the tomcat_service and web containers mount to their own filesystem paths. Any files written to the volume from one container will instantly be accessible in the other.

Update your docker-compose.yml like this:

version: '2'
services:
  mongo_service:
    image: mongo
    command: mongod
    ports:
      - '27017:27017'
  tomcat_service:
    image: 'bitnami/tomcat:latest'
    ports:
      - '8080:8080'
    volumes:
      - tomcat_data:/bitnami/tomcat/data/  # Mount shared volume to Tomcat's target directory
  web:
    links:
      - mongo_service
      - tomcat_service
    depends_on:
      - mongo_service
      - tomcat_service
    environment:
      PYTHONUNBUFFERED: 'true'
    build: .
    ports:
      - '8000:8000'
    volumes:
      - tomcat_data:/app/shared_files  # Mount the SAME volume to a path in your web container
volumes:
  tomcat_data:  # Define the shared volume at the bottom

After updating, restart your containers with docker-compose up -d. Now, when your web container writes files to /app/shared_files, those files will automatically appear in tomcat_service's /bitnami/tomcat/data/ directory. Just make sure the user running your web app has write permissions to /app/shared_files (you might need to adjust user IDs in your web container's Dockerfile if needed).

2. Upload Files via Tomcat's HTTP API (Best for Service-to-Service Interaction)

If you prefer a network-based approach instead of shared storage, you can build or use an HTTP endpoint on Tomcat to accept file uploads. Then your web container can send files to this endpoint using HTTP requests.

For example, if you deploy a simple file upload servlet on Tomcat (or use an existing one), your web container's Python code might look like this:

import requests

# Path to the file you want to send in the web container
file_path = "/path/to/your/local/file.txt"

# Send the file to Tomcat's upload endpoint
with open(file_path, "rb") as f:
    response = requests.post(
        "http://tomcat_service:8080/upload",
        files={"file": f}
    )

# Check if the upload succeeded
if response.status_code == 200:
    print("File uploaded successfully!")
else:
    print(f"Upload failed with status code: {response.status_code}")

The servlet on Tomcat would then save the uploaded file to /bitnami/tomcat/data/. You'll need to configure Tomcat's user permissions and deploy the servlet to its webapps directory to make this work.

3. Use SCP (For One-Time/Ad-Hoc Transfers)

If you just need to move files occasionally, you can use scp between containers. This is less ideal for automated workflows, but it works for quick tasks.

First, you'll need to install openssh-server in your tomcat_service container (you can do this by extending the Bitnami Tomcat image with a custom Dockerfile) and set up a user with SSH access. Then, from your web container (after installing openssh-client), run:

scp /path/to/your/file.txt tomcat_user@tomcat_service:/bitnami/tomcat/data/

Again, this is more of a temporary solution—stick with shared volumes or HTTP uploads for regular use.


内容的提问来源于stack exchange,提问作者duhaime

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:02:00