Copy Files to and From Docker Containers

Docker is a powerful tool for containerizing applications, and it’s important to know how to copy files to and from Docker containers.

This article will lead you through the method for copying files to and from Docker containers step by step, as well as show you an example of how to copy files using a Dockerfile.

Copying Files from Your Local System to a Docker Container:

Learn how to use the docker cp command to transfer files from your local system to a Docker container and simplify your file management in Docker.

Step 1: Launch a Container

You must first run a Docker container before you can copy files to and from it. Here’s an example command for launching a Docker container with the Ubuntu image, naming it, and running it in detached mode:

$ docker run -itd –name my-container ubuntu

This command starts a Docker container with the Ubuntu image, names it my-container, and runs it in detached mode. After launching the container, you can use it to run commands and copy files.

Step 2: Find the Container ID or Name

To copy files to or from a Docker container, you must first know its ID or name. You can accomplish this by using the command:

$ docker ps

This command will display a list of Docker containers that are currently running, along with their container IDs and names.

Step 3: Copying Files to a Docker Container

The docker cp command can be used to copy files from your local system to a Docker container.

The basic syntax is as follows:

$docker cp /source__path  containerNameOrID:/contain_destination_path

For example, if you wanted to copy a file called Harry.txt from your local system to a container called my-container.

You would execute the following command:

$ docker cp /root/COPY/Harry.txt   my-container:/tmp/

This would copy the Harry.txt to the container’s /tmp/ directory.

Copying Files from a Docker Container to Your Local System:

To copy files from a Docker container to your local system, use the docker cp command again, but with a different syntax.

Here’s how it would seem:

$ docker cp containerNameOrID:/container_Source_Path /destinationPath

For example, if you wanted to copy a file called Potter.txt from a container named my-container to your local system’s /root/COPY directory.

You would execute the following command:

$ docker cp my-container:/opt/Potter.txt  /root/COPY

This would copy the Potter.txt file from the container’s /opt/ directory to your local system’s /root/COPY directory.

Copying Files Using a Dockerfile:

You can copy files to and from Docker containers using a Dockerfile in addition to the docker cp command. Here’s an example of how to use a Dockerfile to copy a file from your local system to a Docker container:

Step 1: Create a Dockerfile file with the following contents

This Dockerfile begins with the Ubuntu image and then copies a file called Wednesday.txt from your local machine to the container’s /tmp/ directory. It should be remembered that the file Wednesday.txt must be located in the same directory as the Dockerfile.

Step 2: Use the following command to build the Docker image

$ docker build -t my-image .

This will build a Docker image with the tag my-image. The [.] at the end of the command tells Docker to use the current directory as the build context.

Step 3: Run a Container from the Docker Image

Once the Docker image is built, you can start a container from it, and the Wednesday.txt file will be transferred to the container’s /tmp/ directory.

Use the following command to start a container from a Docker image:

$ docker run -itd –name ninotronix my-image

This will launch a container named ninotronix from the Docker image my-image. Once the container has been launched, execute the following command to check that the Wednesday.txt file is in the /tmp/ directory:

$ docker exec –it  my-container  ls  /tmp/

This command displays the contents of the /tmp/ directory in the ninotronix container.

We hope you found this article useful in getting started with Docker.

Leave a Reply

Your email address will not be published. Required fields are marked *