Copy Directory to Another Directory Using ADD Command
TLDR
The ADD command in Docker allows you to copy directories from your local filesystem or a URL into a Docker image. This is useful for including application files, configurations, or other resources during the build process.
The ADD command in Docker is a versatile instruction that can copy files and directories into a Docker image. This guide explains how to use it effectively to copy directories.
Step 1: Understand the ADD Command
The ADD command has the following syntax:
ADD <source> <destination>
<source>: The path to the directory or file to copy.<destination>: The path inside the image where the content will be copied.
Step 2: Copy a Local Directory
To copy a local directory into a Docker image, specify the source and destination paths.
Example
FROM ubuntu:latest
# Copy the local 'app' directory to '/usr/src/app' in the image
ADD app /usr/src/app
WORKDIR /usr/src/app
CMD ["bash"]
Explanation
ADD app /usr/src/app: Copies theappdirectory from the build context to/usr/src/appin the image.WORKDIR /usr/src/app: Sets the working directory to/usr/src/app.
Step 3: Copy a Directory from a URL
The ADD command can also download and extract files from a URL.
Example
FROM ubuntu:latest
# Download and extract a tarball from a URL
ADD https://example.com/files/app.tar.gz /usr/src/app
WORKDIR /usr/src/app
CMD ["bash"]
Explanation
ADD https://example.com/files/app.tar.gz /usr/src/app: Downloads and extracts the tarball to/usr/src/app.
Step 4: Preserve Directory Structure
The ADD command preserves the directory structure of the source.
Example
FROM ubuntu:latest
# Copy the entire 'config' directory
ADD config /etc/config
CMD ["bash"]
Explanation
ADD config /etc/config: Copies theconfigdirectory and its contents to/etc/config.
Step 5: Use .dockerignore for Exclusions
To exclude certain files or directories, use a .dockerignore file.
Example .dockerignore
node_modules
*.log
Explanation
- Excludes
node_modulesand all.logfiles from being copied.
Best Practices
- Prefer
COPYfor Simplicity: UseCOPYinstead ofADDunless you need its advanced features (e.g., downloading from URLs). - Use
.dockerignore: Exclude unnecessary files to reduce image size. - Validate Paths: Ensure source paths are correct and within the build context.
By following these steps, you can effectively use the ADD command to copy directories into Docker images, enabling efficient and organized builds.
Found an issue?