Introduction
In this lab, we are transported to the enchanting Royal Magic Academy, where we take on the role of a Royal Wizard. As a wizard, we are charged with the task of mastering the art of Docker magic to seamlessly transport valuable data between the mystical realms of the host and the container. Our ultimate goal is to wield the docker cp command to transfer data back and forth with finesse and precision.
Transferring Data from Host to Container
In this step, we will harness the power of docker cp to transfer a magical spell script from the host to the container.
- First, let's create a file named
spell.shin the host's project directory with the following content:
#!/bin/bash
echo "Abracadabra! The spell is cast."
- Now, we will transfer this spell to the
/vardirectory within a running Docker container namedmagic-appusing thedocker cpcommand:
docker cp ~/project/spell.sh magic-app:/var/spell.sh
Retrieving Data from Container to Host
In this step, we will tap into the arcane abilities of docker cp to transport a mystical potion recipe from the running container back to the host.
- Let's first ensure that the container
magic-apphas the filepotion_recipe.txtin its/vardirectory by using the following command:
docker exec magic-app /bin/sh -c 'echo "hello" > /var/potion_recipe.txt'
docker exec magic-app ls /var/potion_recipe.txt
- Now, we will prestidigitate the
potion_recipe.txtfrom the/vardirectory within themagic-appcontainer to the host's project directory:
docker cp magic-app:/var/potion_recipe.txt ~/project/potion_recipe.txt
Mastery Challenge (Optional)
As a wizard seeking mastery, take on the challenge of transferring a whole directory of mystical artifacts between the host and the container using the docker cp command. Stun the instructors with your magical prowess!
Summary
In this lab, we delved into the enchanting world of Docker magic, mastering the art of seamlessly copying data between the host and the container using the docker cp command. By creating a captivating scenario within the Royal Magic Academy, we empowered aspiring wizards to harness this powerful Docker skill, ensuring they can wield it with confidence and finesse.



