Docker Community Forums

Share and learn in the Docker community.

Dockerfile - Concatenate multiple commands

Hello,

In my Dockerfile, I would like to concatenate these commands into two big commands ( one to each kind of not useful ressources ) :

# Removes unnecessary files/folders in node modules
# Files
RUN find "$(pwd)/node_modules" -type f -name "*.ts" -exec rm -f {} \;
RUN find "$(pwd)/node_modules" -type f -name "*.md" -exec rm -f {} \;
RUN find "$(pwd)/node_modules" -type f -name "*.swp" -exec rm -f {} \;
RUN find "$(pwd)/node_modules" -type f -name "LICENSE" -exec rm -f {} \;
RUN find "$(pwd)/node_modules" -type f -name "AUTHORS" -exec rm -f {} \;
RUN find "$(pwd)/node_modules" -type f -name "CONTRIBUTORS" -exec rm -f {} \;
RUN find "$(pwd)/node_modules" -type f -name "CHANGES" -exec rm -f {} \;
RUN find "$(pwd)/node_modules" -type f -name ".prettierrc*" -exec rm -f {} \;
RUN find "$(pwd)/node_modules" -type f -name ".npm*" -exec rm -f {} \;
RUN find "$(pwd)/node_modules" -type f -name ".travis.yml" -exec rm -f {} \;
RUN find "$(pwd)/node_modules" -type f -name "appveyor.yml" -exec rm -f {} \;
RUN find "$(pwd)/node_modules" -type f -name "*.coveralls.yml" -exec rm -f {} \;

# Folders
RUN find "$(pwd)/node_modules" -type d -name "docs?" -prune -exec rm -rf {} +;
RUN find "$(pwd)/node_modules" -type d -name "tests?" -prune -exec rm -rf {} +;
RUN find "$(pwd)/node_modules" -type d -name ".idea" -prune -exec rm -rf {} +;
RUN find "$(pwd)/node_modules" -type d -name ".vscode" -prune -exec rm -rf {} +;
RUN find "$(pwd)/node_modules" -type d -name "__tests__" -prune -exec rm -rf {} +;
RUN find "$(pwd)/node_modules" -type d -name "coverage" -prune -exec rm -rf {} +;

The escape and backslash after each command doesn’t seems to work.

Thanks for the help

My solution ( if anyone interested) :

RUN find "$(pwd)/node_modules" -type f \( \
    -name "*.ts" -o \
    -name "*.md" -o \
    -name "*.swp" -o \
    -name ".npm*" -o \
    -name "LICENSE" -o \
    -name "AUTHORS" -o \
    -name "CONTRIBUTORS" -o \
    -name "CHANGES" -o \
    -name ".prettierrc*" -o \
    -name ".travis.yml" -o \
    -name "appveyor.yml" -o \
    -name ".coveralls.yml" \
\) -exec rm -f {} \;

# Folders
RUN find "$(pwd)/node_modules" -type d \( \
    -name "docs" -o \
    -name "doc" -o \
    -name "tests" -o \
    -name "test" -o \
    -name "__tests__" -o \
    -name ".idea" -o \
    -name ".vscode" -o \
    -name "coverage" -o \
    -name ".github" -o \
    -name ".circleci" \
\) -prune -exec rm -rf {} +;

Are you aware that files can only be deleted in the layer they have been added? If you delete a file in a different later then it was created, two things happen: 1. the file still exists in the layer it was created, 2. the files gets flagged as deleted and becomes invisible for every following layer.

If the files were copied into the image using the COPY directive, you should consider to execlude unwaned files by defining them in a .dockerignore file

My purpose was to clean some files in node modules to build a small image. For reminder :

Even a great and noble idea will not lift the restrictions how image layers work in docker :slight_smile:

1 Like