> ## Documentation Index
> Fetch the complete documentation index at: https://tally.wharflab.com/llms.txt
> Use this file to discover all available pages before exploring further.

# hadolint/DL3003

> Use WORKDIR to switch to a directory.

Use WORKDIR to switch to a directory.

| Property | Value         |
| -------- | ------------- |
| Severity | Warning       |
| Category | Best Practice |
| Default  | Enabled       |
| Auto-fix | Yes (`--fix`) |

## Description

Only use `cd` in a subshell. Most commands can work with absolute paths. Docker provides the `WORKDIR` instruction if you really need to change the
current working directory.

When executed in a subshell, `cd` only affects the single `RUN` instruction, not any subsequent instructions. This can be an advantage over `WORKDIR`
which affects all subsequent instructions.

## Examples

### Problematic code

```dockerfile theme={null}
FROM busybox
RUN cd /usr/src/app && git clone git@github.com:lukasmartinelli/hadolint.git
```

### Correct code (with WORKDIR)

```dockerfile theme={null}
FROM busybox
WORKDIR /usr/src/app
RUN git clone git@github.com:lukasmartinelli/hadolint.git
```

### Correct code (with absolute paths)

```dockerfile theme={null}
FROM busybox
RUN cp somedir/somefile /usr/src/app/someDirInUsrSrcApp/
```

## Auto-fix

Splits `RUN` with `cd` into `WORKDIR` + `RUN` instructions. Removes redundant `mkdir` commands before `cd` targets.

```dockerfile theme={null}
# Before
RUN cd /tmp && git clone ... && cd repo && make

# After (with --fix)
WORKDIR /tmp
RUN git clone ...
WORKDIR repo
RUN make
```

## Reference

* [hadolint/DL3003](https://github.com/hadolint/hadolint/wiki/DL3003)
