Working directory set with RUN cd

Declare a working directory with WORKDIR when later instructions need it.

Description

RUN cd changes directories only for the rest of that RUN.

Potential impact

Later commands that use relative paths can read or write files in the wrong directory.

Remediation

Use WORKDIR with an absolute path when later instructions need the same directory. Keep cd inside a single RUN for a temporary change.

Examples

Set NGINX_IMAGE to a supported image. Both COPY instructions use the same absolute destination, /usr/share/nginx/html.

Before

dockerfile
ARG NGINX_IMAGE
FROM ${NGINX_IMAGE}

RUN cd /usr/share/nginx/html
COPY index.html /usr/share/nginx/html

After

dockerfile
ARG NGINX_IMAGE
FROM ${NGINX_IMAGE}

WORKDIR /usr/share/nginx/html
COPY index.html /usr/share/nginx/html

References