Skip to content

Fix Portainer Stack Deploy – Stack Not Deploying

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about Fix Portainer Stack Deploy. We cover key concepts, practical examples, and best practices.

You write a docker-compose.yml in Portainer's Stacks editor and click Deploy the stack. The spinner spins, then shows "Stack deployment failed" — or the stack deploys but containers exit immediately.

Wrong ❌

# docker-compose.yml in Portainer's web editor
version: '3'
services:
  app:
    image: myapp:latest
    ports:
      - "8080:80"
    environment:
      DB_HOST: db
      DB_NAME: mydb
  db:
    image: postgres:16
    environment:
      POSTGRES_DB: mydb

Deploy error:

Service "app" mounts volume "app-data" but no volume definition exists.

Or:

Container "app" exited with code 127.

## Right ✅

```yaml
version: '3.8'
services:
  app:
    image: myapp:latest
    ports:
      - "8080:80"
    environment:
      DB_HOST: db
      DB_NAME: mydb
    depends_on:
      - db
    volumes:
      - app-data:/app/data
    restart: unless-stopped

  db:
    image: postgres:16
    environment:
      POSTGRES_DB: mydb
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-changeme}
    volumes:
      - postgres-data:/var/lib/<a href="/databases/postgresql/">postgresql</a>/data
    restart: unless-stopped

volumes:
  app-data:
  postgres-data:

**Deploy from git (recommended for production):**

```yaml
# In Portainer stack settings:
Repository URL: https://github.com/my-org/my-stack
Repository Reference: refs/heads/main
Compose Path: docker-compose.yml
☑ Enable auto-update
Polling Interval: 5m

Click **Deploy**:

✅ Stack 'my-stack' deployed successfully.

Services:
  app     ● running   (port 8080)
  db      ● running   (port 5432)

Deploy time: 4.2 seconds

**If containers still exit — check logs:**

Portainer → **Stacks** → click stack → **Logs**:

app    | exec ./entrypoint.sh: no such file or directory

Fix the entrypoint in the Dockerfile and rebuild, or override in the compose file.

## Root Cause

Common failures: missing `volumes:` section at root level (causes validation error), missing environment variables (causes app crash), or incorrect image name / tag.

## Prevention

- Always define `volumes:` at the root level if any service references a named volume.
- Use `restart: unless-stopped` to auto‑recover from transient failures.
- Deploy via Git repository with auto‑update — Portainer polls and re‑deploys on changes.
- Test the compose file locally with `<a href="/devops/docker-compose/">docker compose</a> up` before uploading to Portainer.


## Common Mistakes with stack deploy

1. **Forgetting `deriving (Show, Eq)` on custom data types needed for debugging**
2. **Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable**
3. **Using `head` and `tail` instead of pattern matching, causing runtime errors on empty lists**

These mistakes appear frequently in real-world PORTAINER code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

## Practice Exercise

**Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.**

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

## FAQ

<details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">**Q: Can I use environment variables in Portainer stacks?**</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>A: Yes — add them in the <strong>Environment variables</strong> section of the stack editor. Reference with <code>${VAR_NAME}</code> in compose.</p>
</div></details><details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">**Q: Why does my stack deploy but the port isn't accessible?**</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>A: Check that the port is published (<code>ports: &quot;8080:80&quot;</code>) and not blocked by a firewall. Also verify the container binds to <code>0.0.0.0</code> inside.</p>
</div></details><details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">**Q: How do I update a stack without downtime?**</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>A: Use <strong>Rolling update</strong> in the stack settings (Swarm mode) or stop one replica at a time manually.</p>
</div></details><details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">**Q: What's the difference between Stack and Container in Portainer?**</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>A: A Stack is a group of containers defined by a compose file. A Container is a single instance. Use Stacks for multi‑service apps.</p>
</div></details>

---

*Stack management is covered in the [DodaTech Portainer Stacks course](https://dodatech.com/courses/portainer).*

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro