FG
☁️ Cloud & DevOpsDocker

docker-compose copy file or directory to container

Freshabout 19 hours ago
Mar 14, 20260 views
Confidence Score95%
95%

Problem

we miss a possibility to copy a file or directory using docker-compose. I find this really useful. Please check many +1 in premature closed https://github.com/docker/compose/issues/2105

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Implement Volume Mounting in Docker Compose for File Copying

Medium Risk

Docker Compose does not currently support a direct command to copy files or directories into a running container. This limitation arises from the design of Docker, which primarily uses volumes for persistent data storage and sharing between the host and containers. As a workaround, we can utilize volume mounts to achieve similar functionality during container initialization.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Define Volume in docker-compose.yml

    Add a volume definition in your docker-compose.yml file to specify the host directory or file you want to copy into the container.

    yaml
    version: '3'
    services:
      my_service:
        image: my_image
        volumes:
          - ./host_directory:/container_directory
  2. 2

    Start the Container with Docker Compose

    Run the docker-compose command to start the container. The specified host directory will be mounted to the container, effectively copying its contents.

    bash
    docker-compose up -d
  3. 3

    Verify File Presence in the Container

    Use the docker exec command to check if the files from the host directory are present in the container's specified directory.

    bash
    docker exec -it <container_name> ls /container_directory
  4. 4

    Cleanup Resources

    If you no longer need the mounted volume, you can stop and remove the container using docker-compose down to clean up resources.

    bash
    docker-compose down

Validation

To confirm the fix worked, check the output of the 'ls' command executed in step 3. The files from the host directory should be listed in the container's directory. Additionally, ensure that the container operates as expected with the mounted files.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

dockerdocker-composecontainerskind/featurestatus/0-triage