Using Strapi v4 with Docker
Table of Contents
Introduction
This tutorial will guide you through the process of using Strapi v4 with Docker. You'll learn how to create a Docker image for running Strapi, which can streamline deployment and management of your applications. This is particularly useful for developers looking to standardize their development environments or deploy applications in a containerized format.
Step 1: Project Setup
Before creating a Docker image for Strapi, you'll need to set up your project.
-
Create a new Strapi project:
- Open your terminal.
- Run the following command to create a new Strapi project:
npx create-strapi-app my-project --quickstart
- Replace
my-project
with your desired project name.
-
Navigate to your project directory:
cd my-project
Step 2: Dockerfile Creation
Next, you'll create a Dockerfile which defines the environment for your Strapi application.
-
Create a new file named
Dockerfile
in the root of your project:- Use a text editor to create the file.
-
Add the following content to the Dockerfile:
FROM node:14 WORKDIR /app COPY package.json yarn.lock ./ RUN yarn install COPY . . RUN yarn build EXPOSE 1337 CMD ["yarn", "start"]
-
Explanation of Dockerfile instructions:
FROM node:14
: Uses Node.js version 14 as the base image.WORKDIR /app
: Sets the working directory inside the container.COPY package.json yarn.lock ./
: Copies package files for dependency installation.RUN yarn install
: Installs dependencies.COPY . .
: Copies the rest of your Strapi application files.RUN yarn build
: Builds the Strapi application.EXPOSE 1337
: Exposes port 1337 for the Strapi server.CMD ["yarn", "start"]
: Starts the Strapi application.
Step 3: Docker Ignore
To optimize the Docker build process, create a .dockerignore
file.
-
Create a new file named
.dockerignore
in the root of your project. -
Add the following content to the
.dockerignore
file:node_modules build .git .DS_Store
-
Purpose of
.dockerignore
:- This file prevents unnecessary files and directories from being included in the Docker image, which reduces image size and build time.
Step 4: Building the Docker Image
Now it’s time to build your Docker image.
- Open your terminal in the root of your project.
- Run the following command to build the Docker image:
docker build -t my-strapi-app .
- Replace
my-strapi-app
with your preferred image name.
- Replace
Step 5: Running the Docker Image
After building the image, you can run your Strapi application.
-
Execute the following command:
docker run -p 1337:1337 my-strapi-app
- This command maps port 1337 of your Docker container to port 1337 on your host machine.
-
Access your Strapi application:
- Open your web browser and go to
http://localhost:1337
to access the Strapi admin panel.
- Open your web browser and go to
Conclusion
You've successfully created a Docker image for Strapi v4 and learned how to run it. By using Docker, you can streamline deployment and ensure consistency across environments. For further exploration, consider integrating a database with your Strapi application or deploying it to a cloud platform. Happy coding!