Copy database backup file from localhost to docker container

One of the most common tasks when working with Docker containers is copying files from the local host to a running container.

When a Docker container is running, the docker cp command is the quickest, least complicated, and most effective way to copy files into it. With this command, we can transfer files and directories from our local machine to a container's filesystem.

  1. Ensure that your container is running prior to starting the copy process.
docker ps -a

đź’ˇ For improved formatting, consider utilizing this command as a helpful tip.

docker ps --format "table {{.ID}}\t{{.Image}}\t{{.Names}}"
  1. Copy the container image from your terminal.

In this example, my MSSQL container image is 1fb3402f9825

Docker Images

Generally, the database files are stored in /var/opt/mssql/data. However, you can confirm the location by executing the command below.

docker exec -it 1fb3402f9825 sh
  1. Run the docker cp command to copy the file

Now that we have the container ID and the destination directory, we can proceed with running the command to copy the file.

docker cp SampleDatabase.bak 1fb3402f9825:/var/opt/mssql/data/SampleDatabase.bak

Once the process is complete, you should expect to receive a successful message on your terminal.

Hopefully, this article has provided valuable insights into the process of copying a database backup file from localhost to a Docker container.

Cheers! 🍻