Blog Details

HomeBlogs

Dockerizing Strapi: A Complete Guide

Dockerizing Strapi: A Complete Guide

Dockerizing Strapi: A Complete Guide

Dockerizing Strapi is essential for modern development workflows. This comprehensive guide explores setting up Strapi in Docker containers with multiple version support, enabling consistent development environments and simplified deployment processes.

Key Findings

Using Docker with Strapi allows for consistent environments across all stages of development, simplified version management, and easier deployment to various cloud providers.

1. Getting Started: Two Approaches

You have two options to get started with Dockerizing your Strapi application:\n\n1. Fork the starter code: For convenience, you can fork our starter code repository which contains all the necessary Docker configurations for Strapi. This is the quickest way to get up and running with a properly configured Dockerized Strapi application.\n\n2. Create from scratch: Alternatively, you can create your own Docker configuration files in an existing Strapi project or set up a new project. Simply create the Dockerfile and docker-compose.yml files as shown in the code examples below, and paste the appropriate code.\n\nBoth approaches are valid and depend on your specific requirements and familiarity with Docker.

2. Creating Your Dockerfile

The Dockerfile is the core configuration for your Strapi container. Our approach uses a multi-stage build that allows switching between development and production modes. Before proceeding with Dockerizing your Strapi application, you need to manually set up your Strapi project. Follow these steps to create a Strapi project manually, if you haven't done so yet.\n\nStep 1: First, create a new Strapi project using the command below (ensure you have Node.js and npm installed):\n\n
npx create-strapi-app my-project --quickstart --no-run
javascript
\n\nStep 2: Navigate into the project directory:\n\n
cd my-project
javascript
\n\nStep 3: After setting up the Strapi project, you can proceed to add the Docker configuration. Below is the Dockerfile and Docker Compose setup to run the Strapi project inside Docker containers.

        # Step 1: Create a Strapi project (if not already created)
        npx create-strapi-app my-project --quickstart --no-run
        
        # Step 2: Navigate into the project directory
        cd my-project
        
        # Step 3: After setting up the Strapi project, you can proceed with Docker configuration
            
bash

3. Creating Your Dockerfile

The Dockerfile is the core configuration for your Strapi container. Our approach uses a multi-stage build that allows switching between development and production modes. The Dockerfile below is designed to be versatile—it will create a new Strapi project if one doesn't exist or start your existing project if it's already set up.



# dev | prod
ARG RUN_MODE=dev

# === Dev Mode ===

FROM node:18 as dev


WORKDIR /app

ADD ./ /app

RUN npm install

ENTRYPOINT [ "npm", "run", "develop" ]



# === Dev Mode ===

FROM node:18 as prod

WORKDIR /app

ADD ./ /app

RUN npm install


ENTRYPOINT [ "npm", "start"]

# Pick the container from selected run mode
FROM {RUN_MODE}
            
Dockerfile

4. Docker Compose Setup

Using Docker Compose makes it easier to manage multiple Strapi instances and their dependencies. This setup allows running different Strapi versions side by side for testing or development.


version: "3.9"
services:
  strapi:
    build:
      context: ./server
      dockerfile: Dockerfile
    ports:
      - "24000:1337"
    environment:
      DATABASE_CLIENT: postgres
      DATABASE_NAME: strapi
      DATABASE_HOST: postgres
      DATABASE_PORT: 5432
      DATABASE_USERNAME: strapi
      DATABASE_PASSWORD: strapi
      NODE_ENV: development
    volumes:
      - ./server:/app
      - /app/node_modules
      - /app/build
    depends_on:
      - postgres

  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
    ports:
      - "3000:3000"
    volumes:
      - ./frontend:/app
      - /app/node_modules
    tty: true
    stdin_open: true

  postgres:
    image: postgres:15
    restart: always
    environment:
      POSTGRES_DB: strapi
      POSTGRES_USER: strapi
      POSTGRES_PASSWORD: strapi
    volumes:
      - postgres_data:/var/lib/postgresql/data
    ports:
      - "5432:5432"

volumes:
  postgres_data:
yaml

5. Project Structure

Your project structure will look something like this. The Dockerfile and docker-compose.yml files are located in the root of your Strapi project. The strapi-4 and strapi-5 directories contain the respective Strapi versions.

// Directory structure for a Strapi Project width Docker


strapi-docker-starter/
├── frontend/
│   └── Dockerfile
├── server/
│   ├── Dockerfile
│   ├── .strapi/
│   ├── .tmp/
│   ├── build/
│   ├── config/
│   ├── database/
│   ├── node_modules/
│   ├── public/
│   ├── src/
│   ├── types/
│   ├── .env
│   ├── package.json
│   └── ...
├── docker-compose.yml

            
javascript

6. Remote Development with VS Code

To work efficiently with Docker containers, you should install the 'Remote - Containers' extension for VS Code. This allows you to work directly inside your Docker container without needing to install dependencies on your local machine.


**Installing Remote Development Extension:**

1. Open VS Code  
2. Go to Extensions (or press Ctrl+Shift+X)  
3. Search for "Remote - Containers"  
4. Install the extension from Microsoft  
   [VS Code Remote Development Extension Pack](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.vscode-remote-extensionpack)

**Working with Containers:**

After installing the extension:

1. Click on the green button in the bottom-left corner of VS Code  
2. Select "Remote-Containers: Attach to Running Container" from the options  
3. Choose your Strapi container from the list  

**Setting Up Auto Save:**

Auto Save is crucial for efficient development. To enable it:

*For Windows 10 Pro/Enterprise/Education:*

1. In VS Code (while connected to container), go to File > Auto Save  
2. Or go to Settings (Ctrl+,) and search for "Auto Save"  
3. Set "Auto Save" to "afterDelay" and adjust the delay time as needed  

*For Windows 10 Home:*

Since Windows 10 Home has some limitations with Docker, you might need to manually configure Auto Save:

1. Open VS Code Settings (Ctrl+,)  
2. Search for "Auto Save"  
3. Set "Files: Auto Save" to "afterDelay"  
4. Set "Files: Auto Save Delay" to your preferred value (e.g., 1000 for 1 second)  

This ensures your changes are automatically saved, which is particularly important when working with containerized applications.

> 💡 Remember that all file operations happen inside the container, so any changes you make will be reflected directly in the container environment without needing to rebuild.            
            
            
{
  "files.autoSave": "afterDelay",
  "files.autoSaveDelay": 1000
}
json
Containerization isn't just about packaging applications; it's about creating predictable, scalable environments that evolve with your project's needs. Docker and Strapi together represent the perfect marriage of flexible content management and deployment stability.

- Katherine Jenkins, CTO at CloudScale

Implications for Professionals

This study has several important implications for professionals in the field:

  1. Consistent environments across development, testing, and production
  2. Simplified onboarding for new team members
  3. Easy version management and migration testing
  4. Reduced configuration drift between environments
  5. More reliable CI/CD pipelines
  6. Better isolation between application components
  7. Simplified horizontal scaling in production
  8. Easier backup and restore procedures
  9. Improved local development workflow
  10. Streamlined cloud deployment options

Conclusion

Dockerizing Strapi brings tremendous benefits to development teams, from ensuring environment consistency to simplifying deployment workflows. By following the approach outlined in this guide, you can create a robust Docker setup that works across development, testing, and production environments. Whether you choose to fork our starter code or create your Docker files from scratch, the containerized approach will make your Strapi development and deployment much more manageable. The initial investment in containerization pays dividends through improved team productivity and application reliability.

Ready to Dockerize Your Strapi?

Start with our ready-made Docker configuration for Strapi. Clone the repository, follow the instructions in the README, and you'll have your Strapi instance running in Docker in minutes.

Fork Starter Repository