Table of Contents
Basic Syntax:
Step-by-step:
Tips:
Pro tip:
Home Operation and Maintenance Docker How to copy files from host to Docker container?

How to copy files from host to Docker container?

Jul 27, 2025 am 02:37 AM

Use the docker cp command to copy files from the host to the Docker container without the container running; 2. Ensure that the container exists and execute docker cp /path/on/host file.txt container-name:/path/in/container; 3. If the target directory does not exist, you need to create it first. The content will be copied recursively when copying the entire folder. After the operation is completed, the file will be available in the container without rebuilding the image or mounting the volume.

How to copy files from host to Docker container?

To copy files from your host machine to a Docker container, use the docker cp command — it's straightforward and doesn't require the container to be running beforehand.

How to copy files from host to Docker container?

Basic Syntax:

 docker cp <src-path> <container-name-or-id>:<dest-path>

Step-by-step:

  • Make sure the container exists (it can be stopped or running).
    • List containers:
       docker ps -a
  • Run the copy command :
     docker cp /path/on/your/host/file.txt mycontainer:/path/inside/container/

    Example:

     docker cp ./app.log myapp-container:/var/log/app.log

Tips:

  • If the container is stopped , the file will still be copied — it'll be there when you start it again.
  • Use container name (eg, mycontainer ) or full/short ID .
  • If the destination directory doesn't exist in the container, the copy will fail — create it first if needed (eg, using docker exec -it mycontainer mkdir -p /path/inside/container ).

Pro tip:

Need to copy multiple files or a whole directory?
Just point to the folder:

How to copy files from host to Docker container?
 docker cp ./my-folder mycontainer:/app/my-folder

Docker will recursively copy the contents.

That's it — no need to rebuild the image or use volumes just for a one-time file transfer.

How to copy files from host to Docker container?

The above is the detailed content of How to copy files from host to Docker container?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1540
276
How to install Docker on Mac? How to install Docker on Mac? Jul 26, 2025 am 05:55 AM

DockercanbeinstalledonaMacusingDockerDesktopbyfollowingthesesteps:1.VerifyyourMacmeetstherequirements—macOS10.15ornewer,IntelorAppleSiliconchip,atleast4GBRAM,andvirtualizationenabled;2.DownloadDockerDesktopfromhttps://www.docker.com/products/docker-d

How to uninstall Docker? How to uninstall Docker? Jul 28, 2025 am 12:02 AM

OnWindows,uninstallDockerDesktopviaSettings→Apps,thenremoveleftoverfilesinWSLwithsudorm-rf/var/lib/docker.2.OnmacOS,quitDockerDesktop,moveDocker.apptoTrash,anddeleteconfigurationfilesfrom~/Library.3.OnUbuntu/Debian,stoptheservice,purgeDockerpackagesw

How to back up a Docker container with its data? How to back up a Docker container with its data? Jul 26, 2025 am 02:59 AM

TobackupaDockercontainerwithitsdata,firstbackupnamedvolumesusingatemporarycontainertocreateatarballofthevolumecontents.2.Next,preservethecontainerconfigurationbyexportingitwithdockerinspector,preferably,bydefiningitinaDockerComposefile.3.Finally,back

How to copy files from host to Docker container? How to copy files from host to Docker container? Jul 27, 2025 am 02:37 AM

Use the dockercp command to copy files from the host to the Docker container without the need to run the container; 2. Ensure that the container exists and execute dockercp/path/on/hostfile.txtcontainer-name:/path/in/container; 3. If the target directory does not exist, you need to create it first. When copying the entire folder, the content will be copied recursively. After the operation is completed, the file will be available in the container, without rebuilding the image or mounting the volume.

How to run GUI applications in a Docker container? How to run GUI applications in a Docker container? Jul 27, 2025 am 12:25 AM

To run the GUI application in the Docker container, you must share the host display service and configure the permissions correctly: 1. Run xhost local:docker on Linux to allow the container to access X11; 2. Mount /tmp/.X11-unix and DISPLAY environment variables when starting the container; 3. Ensure that X11-related dependencies are installed in the mirror; 4. Optionally add GPU, audio, and input device support; 5. Or use VNC/NoVNC solution to achieve cross-platform secure access; 6. MacOS needs to install XQuartz and set DISPLAY as the host IP; 7. Windows needs to cooperate with VcXsrv and other X servers through WSL2 and configure DISPLAY and configure DISPLAY through WSL2 and use VcXsrv and other X servers and configure DISPLAY

How do you deploy applications to a Docker Swarm cluster? How do you deploy applications to a Docker Swarm cluster? Jul 21, 2025 am 02:05 AM

The key to deploying applications to DockerSwarm is to use services rather than standalone containers, and to manage configurations, keys, and rolling updates through built-in tools. 1. First, containerize the application and push it to a mirror repository accessible to the Swarm node; 2. Use dockerservicecreate to define the service and its expected state, such as replica count, port mapping, etc.; 3. Use dockersecret and dockerconfig to manage sensitive information and non-sensitive configurations respectively and reference them in the service; 4. Use dockerservicescale to achieve scaling, use dockerserviceupdate to perform interrupted version updates, and can control the update policy through parameters.

How do you set resource limits (CPU, memory) for a Docker container? How do you set resource limits (CPU, memory) for a Docker container? Jul 24, 2025 am 12:33 AM

To limit resource usage of Docker containers, you can specify CPU and memory limits through command line parameters when running the container. The specific methods are as follows: 1. Use --memory (or -m) to set the memory upper limit, such as --memory="512m" means the limit is 512MB RAM; 2. Use --memory-swap to set the total memory plus swap space, such as --memory-swap="1g" means a total of 1GB available; 3. Use --cpus to limit the number of CPU cores, such as --cpus="1.5" means that up to 1.5 CPU cores are used; 4. Use --cpu-s

How to remove dangling Docker images? How to remove dangling Docker images? Aug 02, 2025 am 11:37 AM

DanglingimagesareuntaggedlayersnotassociatedwithanycontainerandcanberemovedusingDocker’sbuilt-incommands.1.Usedockerimageprunetosafelyremovedanglingimagesafterconfirmation,oradd-ftoforceremovalwithoutprompt.2.Usedockerimageprune-atoalsoremoveallunuse

See all articles