How To Use Docker WORKDIR Command For Efficient Container Setup

Docker WORKDIR

Table of Contents

Get up to 50% off now

Become a partner with CyberPanel and gain access to an incredible offer of up to 50% off on CyberPanel add-ons. Plus, as a partner, you’ll also benefit from comprehensive marketing support and a whole lot more. Join us on this journey today!

Docker can automatically create images by following the instructions in a Dockerfile. A Dockerfile is a text file that includes all the commands a user might use in the command line to build an image. Now, the Docker WORKDIR instruction sets the working directory for all following Dockerfile instructions. Common instructions in a Dockerfile include RUN, ADD, CMD, ENTRYPOINT, and COPY. If the WORKDIR is not created manually, it will be created automatically when processing the instructions. Here are some important points to consider when using the WORKDIR instruction:

Today, you’ll find straightforward solutions for efficient container setup, and for that, I’ll teach you how to use the Docker WORKDIR command.

The Dockerfile supports the following instructions:

InstructionDescription
ADDAdd local or remote files and directories.
ARGUse build-time variables.
CMDSpecify default commands.
COPYCopy files and directories.
ENTRYPOINTSpecify default executable.
ENVSet environment variables.
EXPOSEDescribe which ports your application is listening on.
FROMCreate a new build stage from a base image.
HEALTHCHECKCheck a container’s health on startup.
LABELAdd metadata to an image.
MAINTAINERSpecify the author of an image.
ONBUILDSpecify instructions for when the image is used in a build.
RUNExecute build commands.
SHELLSet the default shell of an image.
STOPSIGNALSpecify the system call signal for exiting a container.
USERSet user and group ID.
VOLUMECreate volume mounts.
WORKDIRChange working directory.

How WORKDIR Improves Dockerfile Efficiency

The Docker WORKDIR instruction defines the working directory for any RUN, CMD, ENTRYPOINT, COPY, and ADD commands that follow it in the Dockerfile. If the WORKDIR does not exist, it will be created even if it is not used in any later Dockerfile command.

The WORKDIR instruction can be applied multiple times in a Dockerfile. If a relative path is given, it will be relative to the previous WORKDIR path. For instance:

WORKDIR /a<br>WORKDIR b<br>WORKDIR c<br>RUN pwd

The result of the last pwd command in this Dockerfile would be /a/b/c.

Tech Delivered to Your Inbox!

Get exclusive access to all things tech-savvy, and be the first to receive 

the latest updates directly in your inbox.

The WORKDIR instruction can utilize environment variables that were set earlier using ENV. You can only use environment variables that are explicitly defined in the Dockerfile. For example:

ENV DIRPATH=/path<br>WORKDIR $DIRPATH/$DIRNAME<br>RUN pwd

The result of the last pwd command in this Dockerfile would be /path/$DIRNAME.

If not defined, the default working directory is /. In practice, if you are not building a Dockerfile from scratch (FROM scratch), the WORKDIR is likely set by the base image you are using.

Thus, to prevent unintended actions in unknown directories, it is advisable to set your WORKDIR clearly.

Docker WORKDIR Syntax (With Examples)

The basic Docker Workdir syntax is:

WORKDIR /path/to/directory

Examples

Poor: this presumes that WORKDIR in the base image is / (if that changes upstream, the web stage will fail).

FROM nginx AS web<br>WORKDIR usr/share/nginx/html<br>COPY public .

Excellent: a leading slash guarantees that WORKDIR consistently points to the correct path.

FROM nginx AS web<br>WORKDIR /usr/share/nginx/html<br>COPY public .

This Results in:

PS E:\myproject> docker build -t cmd .

Sending build context to Docker daemon 3.072 kB
Step 1/2 : FROM microsoft/nanoserver
 ---> 22738ff49c6d
Step 2/2 : COPY testfile.txt c:\RUN dir c:
GetFileAttributesEx c:RUN: The system cannot find the file specified.
PS E:\myproject>

Reusing WORKDIR

This Docker WORKDIR command can be used again to define a new working directory at any point in the Dockerfile. The path to the new working directory should be specified relative to the existing working directory.

Enhance Your CyerPanel Experience Today!
Discover a world of enhanced features and show your support for our ongoing development with CyberPanel add-ons. Elevate your experience today!

FROM ubuntu:16.04 WORKDIR /project RUN npm install WORKDIR ../project2 RUN touch file1.cpp


Although you can create and change directories manually, it is highly advised to use Docker WORKDIR to indicate the current directory you want to work in, as it simplifies troubleshooting.

Real-World Examples of Using WORKDIR in Dockerfiles

Node.js Example

FROM node:18
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "start"]

Go Example

FROM golang:1.20
WORKDIR /go/src/app
COPY . .
RUN go build -o main .
CMD ["./main"]

Let’s Wrap Up!

The Docker WORKDIR command is straightforward yet effective in Docker. It helps keep your Dockerfiles tidy, minimizes mistakes, and simplifies container maintenance. When you establish an appropriate working directory, you create a more efficient, polished, and production-ready container configuration.

FAQ’s

1. Is Docker WORKDIR required in a Dockerfile?
No, WORKDIR is not mandatory. If you don’t set it, Docker will default to / (root). But using WORKDIR makes Dockerfiles cleaner and avoids repetitive cd commands.

2. Why should I use WORKDIR if Docker already knows where files go?
Docker doesn’t “guess” where to run commands. WORKDIR defines the default directory inside the container, so commands like RUN, COPY, or CMD execute in the right place. Without it, you risk misplaced files or build errors.

3. Does WORKDIR affect all users inside a container?
Yes. WORKDIR sets the starting directory for processes inside the container, no matter who runs them. You can change the user with the USER instruction, but the default working directory will remain as defined by WORKDIR Docker unless overridden.

4. Can I override Docker WORKDIR when running a container?
Yes. You can override it with commands like:

docker run -w /custom/path myimage

This temporarily changes the working directory without modifying the Dockerfile.

5. What happens if I set multiple WORKDIR instructions?

Each WORKDIR updates the current path. If you chain them, they build on each other. Example:

WORKDIR /app<br>WORKDIR logs
Final path = /app/logs.
Areeba Nauman
Areeba is a Content Writer with expertise in web content and social media, she can simplify complex concepts to engage diverse audiences. Fueled by creativity and driven by results, she brings a unique perspective and a keen attention to detail to every project she undertakes with her creativity and passion for delivering impactful content strategies for success. Let's connect on Linkedin: https://www.linkedin.com/in/areeba-bhatti/
Unlock Benefits

Become a Community Member

SIMPLIFY SETUP, MAXIMIZE EFFICIENCY!
Setting up CyberPanel is a breeze. We’ll handle the installation so you can concentrate on your website. Start now for a secure, stable, and blazing-fast performance!