Copy Files To/From A Running Docker Container

Rajesh
2 min readApr 13, 2024

Challenge from iximiuz labs

Scenario:

In this challenge, there is a running Nginx container named web. Whoever started it forgot to add the required nginx.conf file to the container. Your task is to copy the config file to the web container without restarting it. After that, you will need to copy the nginx binary from the container to the host for further analysis by the security team.

  • You can access the container from the host at localhost:8080.
  • The correct config file is located on the host at /root/nginx.conf.
  • Use docker exec web nginx -s reload to trigger a config reload.
  • The Nginx binary is located in the container at /usr/sbin/nginx.
  • Copy the Nginx binary to the host at /root/nginx-bin.

Solution:

Step 1 : Copy the nginx config file from the host to the running container(web)

  • Using “docker cp” command , we can copy the file from host to container
#copy the nginx.conf file from host to the web container
docker cp /root/nginx.conf web:/etc/nginx/nginx.conf
#reload the nginx server to use the config file
docker exec web nginx -s reload

Step 2: Copy the nginx binary from running container(web) to the host

  • Using “docker cp” command , we can also copy the file from container to the host .
# Copy nginx binary from web container to the host 
docker cp web:/usr/sbin/nginx /root/nginx-bin

labs.iximiuz.com will verify the tasks on the go. Shown below

This challenge is considered as easy. The iximiuz labs consists of several devops related challenges(especially docker and k8s) with easy, medium and hard difficulty. His articles provides in-depth knowledge of containers and kubernetes and after learning you can also practice it in the labs.

Thank you for viewing this article. Have a nice day !!!

--

--