October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk8 min

How to Use working_dir in a Docker Compose File

A practical guide to Docker Compose working_dir, with service examples, Dockerfile WORKDIR comparisons, host-path distinctions, verification commands, and troubleshooting.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

working_dir is a per-service Docker Compose setting that chooses the current directory inside the container when that service runs commands. It overrides the image’s Dockerfile WORKDIR; it does not change the host directory from which Compose resolves the project, build context, or other host-side paths.

Set it under the service that needs it, use an absolute container path, and make sure your code or scripts are mounted at that same path. Use the Compose CLI’s --project-directory option when your goal is to change the project’s host-side base directory.

As an Amazon Associate I earn from qualifying purchases.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What working_dir means in Compose

working_dir is a service-level key. It sets the process working directory inside containers created for that service. Relative command paths, tools that read the current directory, and shell prompts that display the current location all use this directory.

For example:

services:
  api:
    image: python:3
    working_dir: /app
    command: python src/server.py

When Compose starts api, the command runs with /app as its current directory. The setting applies to the service container, not to your terminal and not to the directory containing compose.yaml.

#1 Best Overall
Sale
TOZO A1 Wireless Earbuds Bluetooth 5.3 Light Weight in Ear Headphones | IPX5 Waterproof,2 Mics for AI Calls,Immersive Premium Sound Bass Headset with Charging Case,32 EQ via App Black
  • [Ultra-Lightweight Ear Buds Designed for Small Ears] Each earbud weighs only 3.7g and features a compact, ergonomic in-ear design made especially for small ears. Secure, low-profile, and comfortable for workouts, all-day wear or overnight sleeping.The ultra-slim profile sits flush in your ear with no hard edges poking or rubbing, so you can even sleep on your side without discomfort.
  • [Immersive Stereo Sound with TOZO OrigX Technology] TOZO OrigX tuning delivers clear vocals, balanced mids, and natural stereo sound for music, podcasts, and videos.
  • [Long Battery Life for Daily Use] Get up to 7 hours of playtime on a single charge, with up to 32 hours total using the charging case—ideal for workdays, commuting, and extended listening sessions.
  • [Bluetooth 5.3 & Stable Connection] Bluetooth 5.3 provides fast pairing, stable wireless performance, and reduced dropouts as you move around home or office.
  • [Deep Bass with Clear Vocals] High-performance drivers produce punchy bass while keeping vocals clean and detailed for everyday listening.

It overrides the image’s WORKDIR

An image can define a default directory with Dockerfile instructions such as WORKDIR /usr/src/app. If the Compose service does not specify working_dir, that image default is used. If Compose does specify one, the Compose value wins for the service container.

# Dockerfile
FROM python:3
WORKDIR /usr/src/app
COPY . .
CMD ["python", "src/server.py"]

# compose.yaml
services:
  api:
    build: .
    working_dir: /workspace

Here the image still contains files copied under /usr/src/app, but processes launched by the Compose service start in /workspace. Unless you also mount or copy the application into /workspace, a relative command such as python src/server.py may not find the expected files.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

It is container-side, not host-side

Changing working_dir does not make Compose read a different YAML file, move your terminal into another directory, or relocate a bind mount on the host. It changes only the current directory presented to processes in that service’s container.

Minimal working example

A common development service mounts the project into /workspace and sets that directory as its working directory:

services:
  app:
    image: node:alpine
    working_dir: /workspace
    volumes:
      - .:/workspace
    command: node src/index.js

With a project containing src/index.js, Compose starts the container, mounts the host project at /workspace, changes into that directory, and runs node src/index.js. The same command written as node /workspace/src/index.js does not depend on the current directory, but setting working_dir keeps relative paths in scripts and tooling predictable.

Absolute paths are the safest choice

Use an absolute path such as /workspace, /app, or /usr/src/service for the container directory. A path beginning with / is unambiguous regardless of the image’s default WORKDIR. Choose a path that matches the destination of your bind mount or the location where the image places the application.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #2
Sale
Smart Watch for Men Women Compatible with iPhone Samsung Android Phone 1.69 inch Full Touch Screen IP68 Waterproof Bluetooh Fitness Tracker Heart Rate/Sleep Monitor (Black)
  • 【IP68 Waterproof Multifunctinal Smart Watch】Torjalph fitness activity tracker smart watch is IP68 waterproof technology ,you can wear it when swimming,handwashing,it's rain-proof which means that you perform multiple exercise modes outside without worrying about sweat or rain corroding your fitness smart watch,and this watch equipped with a 230 mAh large capacity battery magnetic charging ,and this sports activity tracker watch can be used 5-7 days If fully charged.
  • 【Larger 1.83'' Full Screen Watch & Multi-dial Selection】1.83 inch full HD color touch screen smart watch for men / women provides excellent 240*284 resolution image quality ,This Bluetooth smart watch offers a better touch experience and remarkable immersion and ,more than 200 watch faces for you to choose from App"Da Fit ", you can change the dial style according to your mood to wear keeping the world on your wrist
  • 【Accurate Activity Tracker Smart Watch 】sports watch activity tracker uses sweat and dust resistant breathable silicone strap, our Fitness Tracker Smart Watch for Android iOS Phones supports 12 sports modes,walking,running,bicycle,swimming,badminton,basketball,football,rope skipping. This bluetooth calls smart watch for men women is lightweight and stylish, bringing more convenient and relaxing sports experience while also providing more protection.
  • 【Your 24 Hour Personal Daily Health Monitor】Fitness watch for women men full touch screen IP67 waterproof bluetooth smartwatch for Android iOS Phones with heart rate blood pressure monitor multifunction Andriod smart watch with steps counter sleep monitor sports fitness watch, monitoring blood oxygen saturation allows you to understand your physical condition in real time to track fitness progress ,to know your health status in time.
  • 【More Functions Bluetooth Call Smart Watch】This fitness activity smart watch full screen for iPhone and Android phones includes a bunch of new features including:The bluetooth smart watch can hang up the phone, receive and display WhatsAPP /Text /Facebook /Twitter /Messenger reminder.Support sedentary reminder, alarm clock, stopwatch, timer,weather information,music control, remote camera control compatible with Android 5.0 and above or iOS 9.0 and above smartphones.

How commands use the directory

The setting affects commands launched by the service, including the image’s default command when you do not replace it with Compose’s command or entrypoint. It also affects commands you run interactively with Compose.

Run a one-off command

docker compose run --rm app pwd

The expected output is the configured container path, such as /workspace. To inspect files relative to it:

docker compose run --rm app sh -c 'pwd; ls -la'

docker compose run creates a temporary container for the service. The --rm option removes that container after the command exits.

Inspect a running service

docker compose up -d app
docker compose exec app pwd
docker compose exec app sh

exec requires a running container. The shell opened by the last command starts in the service’s configured directory, so commands such as ls, npm test, or python -m pytest operate from that location.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Relative command paths

With working_dir: /workspace, a command such as ./scripts/start.sh means /workspace/scripts/start.sh. If the script is mounted somewhere else, use its absolute path or change either the mount destination or the working directory.

Remember that Compose command syntax and shell behavior are separate concerns. A command written as a list, for example ["python", "src/server.py"], invokes the executable directly. Shell operators such as &&, variable expansion, and globbing require a shell command such as sh -c '...' and an image that provides that shell.

Rank #3
New Bee Guitar Cable 10ft Electric Instrument Bass AMP Cord
  • [Low Noise High Fidelity] New bee guitar cord with 22AWG oxygen-free-copper center conductors provides enhanced signal transfer, the PVC insulation material and the high-density stranded copper double shield provide noise rejection for quiet operation
  • [Ideal for Choice] New bee 1/4 straight and right angle metal connectors, ensures a secure connection in any instrument, pedal, or amp jack. L shape great fro on stage guitar stands
  • [Wide Compatible] Suitable for mixer, power amplifier, microphone, effector, decoder, equalizer, electoronic organ, electric guitar, electric bass and other electronic equipments. Widely used in the installation of smart home, karaoke speaker, professional stage audio, etc
  • [Easy Disassemble] Making incidental repairs quick and easy! Very nice, flexible cable
  • [Dont Hesite to Order] 24 hours customer services and professional technology team are standing by

Build context, project directory, and working directory compared

These settings solve different path problems. Confusing them is the most common reason people conclude that working_dir is “not working.”

Setting or option Where it applies What it controls
working_dir Inside a service container The current directory for the service’s main process, one-off commands, and supported lifecycle commands
Image WORKDIR Inside the image/container The default container directory when Compose does not override it
build.context On the host during an image build The files sent to the Docker build; it is independent of the runtime container directory
--project-directory Compose CLI on the host The project’s alternate base directory; use it when you need Compose to resolve the project from a different host location

In the legacy Compose reference, a relative build context is interpreted relative to the location of the Compose YAML file, and that directory is sent as the build context. That behavior is separate from working_dir. Check the current Compose build documentation if your setup depends on subtle path resolution rules.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Changing the host project base

If the Compose file is in one directory but you want to run the project as though another host directory were the base, use the CLI option:

docker compose --project-directory /path/to/project up

Do not try to achieve this by putting a host path in working_dir. A value such as /home/alex/project in working_dir refers to a path inside the container, where that host path normally does not exist.

Bind mounts still need an explicit destination

A bind mount maps a host source to a container destination:

services:
  app:
    image: alpine
    working_dir: /workspace
    volumes:
      - ./src:/workspace/src
    command: sh -c 'ls -la src'

The host’s ./src is resolved by Compose according to its project-file path rules; /workspace is the container destination. The working directory does not rewrite either side of the mapping.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #4
BSIMB 15.6" Digital Family Calendar & Chore Chart, No Subscription Fees
  • Digital Family Calendar: BSIMB digital calendar wall touch screen puts an end to your chaotic life. Designed for busy families, the digital calendar keeps you organised and in step with your family. Assign different colours and tasks to each family member to make family scheduling easier and more intuitive.
  • Easy Setup and Auto-Sync: Plug in, connect to WLAN and auto-syn your electric calendar in minutes. Digital wall calendar compatible with Google calendar, iCloud calendar, Outlook calendar, Cozi calendar and Yahoo calendar. Add events directly on the device or through the ecalendar app. Electronic calendar stays current whether you're at work or traveling—no manual updates needed.
  • All-In-One Home Assistant: Smart calendar builds habits and encourage independence with personalised chores. Display daily meal plan to reduce the daily ‘What's for dinner?’ stress. To-do lists keep track of everything you want. Turning chores into valuable achievements with stars and rewards, making life easier and more enjoyable.
  • Manage Anytime Anywhere: BSIMB electronic calendar for family stay connected with the powerful ecalendar app. Access and manage your digital wall calendar anytime, anywhere with free features like stars & rewards, photo screensaver, chore charts, meals plans, to-do lists etc. Electronic planner paid features like Magic Import tailor advanced tools for users with special needs.
  • Stay in Touch with Loved Ones: Share photos or videos from your phone to electronic wall calendar with app, instantly update your life moment, and keep in touch with your family, especially those who are far away; you can also surprise your smart calendar digital wall by sending him a wish card; it's also great for keeping tech-illiterate elders in touch with the youngsters.

Step-by-step setup for a project

  1. Choose a container path. Pick a stable absolute directory such as /workspace. Avoid using a host-only path.
  2. Map or copy the application there. For development, use a bind mount such as .:/workspace. For an image, copy files into the same path or use the image’s existing location.
  3. Add working_dir under the correct service. YAML indentation matters; it must be aligned with keys such as image, build, volumes, and command.
  4. Make commands relative to that path deliberately. Write ./manage.py, npm test, or pytest tests only when those files are present beneath the configured directory.
  5. Recreate the container. Run docker compose up -d --force-recreate service_name if an existing container still reflects an older configuration.
  6. Verify from inside the container. Use docker compose exec service_name pwd and list the expected files before debugging the application command.

Complete Python example

services:
  api:
    build:
      context: .
    working_dir: /workspace
    volumes:
      - .:/workspace
    environment:
      PYTHONDONTWRITEBYTECODE: "1"
    command: python src/server.py

The build context tells Docker which host files are available to the build. The volume places the live project at /workspace at runtime. The working directory makes src/server.py resolve beneath that mount.

Multiple services can use different directories

services:
  api:
    image: python:3
    working_dir: /srv/api
    volumes:
      - ./api:/srv/api
    command: python app.py

  worker:
    image: python:3
    working_dir: /srv/worker
    volumes:
      - ./worker:/srv/worker
    command: python worker.py

Each service has its own container and current directory. Setting working_dir for api does not affect worker.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Lifecycle hooks and working_dir

Compose’s services reference also defines a working_dir for a pre_start lifecycle hook. The hook command runs in the directory specified for that hook. If the hook does not set one, it runs in the same working directory as the main service command.

services:
  api:
    image: python:3
    working_dir: /workspace
    volumes:
      - .:/workspace
    pre_start:
      - command: ./scripts/check-config.sh
        working_dir: /workspace/scripts

Use this only with a Compose version that supports lifecycle hooks. A hook-specific directory is useful when preparation scripts live in a subdirectory, but the script and its dependencies must exist at that container path.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Dockerfile WORKDIR versus Compose working_dir

Use Dockerfile WORKDIR when… Use Compose working_dir when…
The directory is part of the image’s normal runtime contract. A particular deployment or local service should run from a different directory.
You want subsequent Dockerfile COPY, RUN, and default command instructions to use that path. You need to override an image’s default without rebuilding it.
Every consumer of the image should receive the same default. Different Compose services or environments need different container paths.

Compose cannot retroactively change where Dockerfile instructions copied files during the build. If the image contains the application under /opt/app and you set working_dir: /workspace without a mount or copy there, the process starts in an empty or unrelated location.

Why “working_dir is not working”

The key is at the wrong YAML level

This is correct:

services:
  app:
    image: example
    working_dir: /workspace

Placing working_dir beside services, under volumes, or inside an unrelated service has no effect on app. Validate the file with:

docker compose config

The rendered configuration should show working_dir: /workspace under the intended service.

The container was not recreated

Compose may continue using a container created from an earlier configuration. Recreate it with:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
docker compose up -d --force-recreate app

Then run docker compose exec app pwd again. If the service exits immediately, use docker compose run --rm app pwd to test the setting in a temporary container.

The files are somewhere else

Check both the current directory and the mount:

docker compose exec app pwd
docker compose exec app ls -la
docker compose exec app find / -maxdepth 3 -name server.py 2>/dev/null

If the application is under /usr/src/app but the service starts in /workspace, either set working_dir: /usr/src/app or mount/copy the project into /workspace. Do not assume the host directory name is available inside the container.

The path or executable is unavailable

Use an absolute executable path or inspect the image’s shell and tools. Minimal images may not include bash, editors, or diagnostic utilities; use the shell the image provides, often sh. A command can fail because the executable is missing even though working_dir is correct.

Permissions prevent access

A bind-mounted directory retains host-side ownership and permission characteristics as seen by the container’s user. If the process cannot read or write files, inspect the container user, mount permissions, and application directory ownership. Changing working_dir does not grant permissions.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Practical checks before committing the file

  • The value is under the intended service and is an absolute container path.
  • The image’s files or a volume are actually present at that path.
  • Relative commands are written with that directory in mind.
  • docker compose config displays the value after variable interpolation and file merging.
  • A one-off pwd check returns the expected path.
  • Host-side path goals are handled with build.context, volume mappings, or --project-directory, not with working_dir.
  • If a lifecycle hook is used, its command directory is set intentionally and the installed Compose version supports that hook.

Key takeaway

Use working_dir to establish a predictable current directory inside one Compose service. Pair it with a matching image path or volume destination, verify it from inside the container, and keep host project-path decisions in the Compose CLI and build or volume configuration.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.