In this post, we will see how to copy a file from our system to a Docker container and vice versa.
We start defining and running this docker-compose file to create a SQL Server Docker container:
[DOCKER-COMPOSE.YML]
version: "3.9"
services:
sql-server-db:
container_name: sqlserver
image: mcr.microsoft.com/mssql/server:latest
ports:
- "1439:1433"
environment:
SA_PASSWORD: "password1234"
ACCEPT_EULA: "Y"
MSSQL_PID: Express
volumes:
- sqlvolume:/var/opt/mssql
volumes:
sqlvolume:
driver: local
Then, using a PowerShell console, we check everything works fine:
FROM HOST TO THE CONTAINER
First of all, we create a folder called Test where, we are going to copy a text file called file_test.txt so defined:
Test1
Test2
Then, in order to copy the file in our container, we run the command:
docker cp file_test.txt sqlserver:/file_test.txt
where sqlserver is the name of our SQL Server Docker container.
Now, for checking everything worked fine, we go inside the SQL Server Docker container using the command:
docker exec -it --privileged --user root sqlserver /bin/bash
where sqlserver is the name of our SQL Server Docker Container.
Then, with the command ls, we can verify the existence of the file:
Finally, with the command cat, we can read the file’s content:
FROM DOCKER CONTAINER TO THE HOST
In the SQL Server Docker container, we create a file txt called file_test2.txt using the command:
echo Test from Linux >> file_test2.txt
Then, we open another PowerShell terminal for copying the new file in our system, running the command:
docker cp sqlserver:/file_test2.txt .
where sqlserver is the name of our SQL Server Docker container.
Finally, in order to check everything worked fine, we can read the file’s content with the command:
Get-content file_test2.txt