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.
Similar Reads
Docker - ARG Instruction You can use the ARG command inside a Dockerfile for defining the name of a parameter and its default value. This default value can also be overridden using a simple option with the Docker build command. The difference between ENV and ARG is that after you set an environment variable using ARG, you w
5 min read
Docker - COPY Instruction In Docker, there are two ways to copy a file: ADD and COPY. Though there is a slight difference between them regarding the scope of the functions, they more or less perform the same task. In this article, we will primarily focus on the COPY instruction of Docker. So, before discussing the COPY instr
4 min read
Docker - USER Instruction By default, a Docker Container runs as a Root user. This poses a great security threat if you deploy your applications on a large scale inside Docker Containers. You can change or switch to a different user inside a Docker Container using the USER Instruction. For this, you first need to create a us
2 min read
Docker - LABEL Instruction Labels are used in Dockerfile to help organize your Docker Images. Labels are key-value pairs and simply adds custom metadata to your Docker Images. Some key points associated with the LABEL instructions are as follows: To include spaces inside a label, you can use quotes.For multi line labels, you
2 min read
Docker - WORKDIR Instruction In Docker, organizing and managing file paths within a container is key to building efficient and easy-to-maintain applications. One way to streamline this is by using the WORKDIR instruction in your Dockerfile. Setting the working directory helps control where commands run, making your Dockerfile m
6 min read