If you want to extract a TAR file inside a Docker Container or copy files from a URL or local directory, you can specify ADD Instructions inside your Dockerfile. This is different from COPY instruction because COPY instruction only allows you to copy files and directories from the local machine.
In this article, we will see practical examples where you can use ADD instruction to extract a tar file inside your Docker Image.
Step 1: Create a Tar File
For this example, we are simply going to create a TAR file of a folder. You can use this command to create a tar file.
tar -zcvf my-tar-folder.tar.gz ~/Desktop/my-tar-folder
Step 2: Creating the Dockerfile
After you have your Tar file ready, you can now create a Dockerfile with ADD instruction.
FROM ubuntu:latest RUN apt-get -y update ADD my-tar-folder.tar.gz .
In the above Dockerfile, we have pulled the Ubuntu base Image from Docker Hub and updated the Image using an apt-get update. After that, we have included the ADD instruction to extract the TAR file located inside the same directory as that of the Dockerfile.
Step 3: Building the Docker Image
After creating the Dockerfile, you can now create the Docker Image using the Docker build command.
sudo docker build -t sample-image .
To confirm whether the image has been successfully built or not, use the Image list command.
sudo docker images
Step 4: Running the Docker Container
After you have created the Docker Image, you can now run the Docker Container associated with the Docker Image using the Docker Run command.
sudo docker run -it sample-image bash
Step 5: Verifying the Extraction
After you have the bash of the Container running, you can use the list command to list the directories and verify the instruction.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
- Docker - USER Instruction
- Docker - HEALTHCHECK Instruction
- Docker - WORKDIR Instruction
- Docker - ARG Instruction
- Docker - EXPOSE Instruction
- Docker - LABEL Instruction
- Docker - COPY Instruction
- How to Install and Configure Docker in Ubuntu?
- Setup Web Server Over Docker Container in Linux
- Create your own secure Home Network using Pi-hole and Docker
- How to create your own Docker Customised Image ?
- Docker compose tool to run multi container applications
- Introduction to Docker
- How to Build a Web Server Docker File?
- How to Map Ports in Docker?
- Running GUI Applications on Docker in Linux
- Difference between RUN vs CMD vs ENTRYPOINT Docker Commands
- Creating a Network in Docker and Connecting a Container to That Network
- Backing up a Docker Container
- How to Run a Python Script using Docker?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.