|
| 1 | +--- |
| 2 | +title: Going Production |
| 3 | +description: Learn how to deploy your application in production in Dokploy. |
| 4 | +--- |
| 5 | + |
| 6 | +import { Callout } from 'fumadocs-ui/components/callout'; |
| 7 | + |
| 8 | +By default, dokploy offer multiple [Builds Types](/docs/core/applications/build-type) to deploy your application, the most common is `nixpacks` and `heroku buildpacks` |
| 9 | +however this also comes with problems, first is the resources that are required to build your application which some times can lead to timeout on your server or even freezeing your server |
| 10 | +and all your application will be down for this reasson, this is mainly problem from `Docker` since the comsumption of resources such as RAM, CPU is very high to build an application. |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +## Solution |
| 15 | + |
| 16 | +You have two options to solve this problem: |
| 17 | + |
| 18 | +1. Increase the resources of your server CPU, RAM, Disk (Probably is not a good idea and cheapest solution) |
| 19 | +2. Build & Publish the application in a CI/CD pipeline eg. Github Actions, Gitlab CI, etc. (Recommended) |
| 20 | + |
| 21 | + |
| 22 | +### Build & Publish the application in a CI/CD pipeline |
| 23 | + |
| 24 | +We will use Github Actions as an example, but you can use any CI/CD pipeline that you want. |
| 25 | + |
| 26 | +We will use the following configuration: |
| 27 | + |
| 28 | +1. **Use Git Provider in Your Application**: |
| 29 | + - Repository: `https://github.com/Dokploy/production-example` |
| 30 | + - Branch: `main` |
| 31 | + - Build path: `/` |
| 32 | + |
| 33 | +<Callout type="info"> |
| 34 | +The repo have everything you need, however you can follow the same idea for your own applications. |
| 35 | + |
| 36 | +</Callout> |
| 37 | + |
| 38 | + |
| 39 | +3. The repository already have a Dockerfile, so we will use that, in the case your application is different create your own Dockerfile is required for this guide. |
| 40 | +4. We will use `Dockerhub` as an example, but you can use any container registry that you want. |
| 41 | +5. Make sure to create the repository in the `Dockerhub` , `namespace` is your username and `repository` is `example`. |
| 42 | +6. Create a new Github Actions workflow in `.github/workflows/deploy.yml` |
| 43 | +7. Add the following code to the workflow: |
| 44 | + |
| 45 | +```yaml |
| 46 | +name: Build Docker images |
| 47 | + |
| 48 | +on: |
| 49 | + push: |
| 50 | + branches: ["main"] |
| 51 | + |
| 52 | +jobs: |
| 53 | + build-and-push-dockerfile-image: |
| 54 | + runs-on: ubuntu-latest |
| 55 | + |
| 56 | + steps: |
| 57 | + - name: Checkout repository |
| 58 | + uses: actions/checkout@v3 |
| 59 | + |
| 60 | + - name: Log in to Docker Hub |
| 61 | + uses: docker/login-action@v2 |
| 62 | + with: |
| 63 | + username: ${{ secrets.DOCKERHUB_USERNAME }} # Make sure to add the secrets in your repository in -> Settings -> Secrets (Actions) -> New repository secret |
| 64 | + password: ${{ secrets.DOCKERHUB_TOKEN }} # Make sure to add the secrets in your repository in -> Settings -> Secrets (Actions) -> New repository secret |
| 65 | + |
| 66 | + - name: Build and push Docker image |
| 67 | + uses: docker/build-push-action@v4 |
| 68 | + with: |
| 69 | + context: . |
| 70 | + file: ./Dockerfile |
| 71 | + push: true |
| 72 | + # Make sure to replace with your own namespace and repository |
| 73 | + tags: | |
| 74 | + namespace/example:latest |
| 75 | + platforms: linux/amd64 |
| 76 | +``` |
| 77 | +8. Create your own Dockerfile, in this case we will use the `Dockerfile` from the repository. |
| 78 | + |
| 79 | +```properties |
| 80 | +FROM node:18-alpine AS base |
| 81 | +ENV PNPM_HOME="/pnpm" |
| 82 | +ENV PATH="$PNPM_HOME:$PATH" |
| 83 | +RUN corepack enable |
| 84 | +
|
| 85 | +FROM base AS build |
| 86 | +WORKDIR /app |
| 87 | +COPY . . |
| 88 | +COPY package.json pnpm-lock.yaml ./ |
| 89 | +RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile |
| 90 | +ENV NODE_ENV=production |
| 91 | +RUN pnpm run build |
| 92 | +
|
| 93 | +FROM base AS dokploy |
| 94 | +WORKDIR /app |
| 95 | +ENV NODE_ENV=production |
| 96 | +
|
| 97 | +# Copy only the necessary files |
| 98 | +COPY --from=build /app/dist ./dist |
| 99 | +COPY --from=build /app/package.json ./package.json |
| 100 | +COPY --from=build /app/node_modules ./node_modules |
| 101 | +
|
| 102 | +EXPOSE 3000 |
| 103 | +CMD ["pnpm", "start"] |
| 104 | +``` |
| 105 | + |
| 106 | +9. Now when you make a commit to your repository, the workflow will be triggered and the application will build and push to `Dockerhub`. |
| 107 | +10. Now let's create application in Dokploy. |
| 108 | +11. In `Source Type` select `Docker` |
| 109 | +12. In the docker image field enter `namespace/example:latest` |
| 110 | +13. Click on `Save`. |
| 111 | +14. Click on `Deploy`. |
| 112 | +15. Go to `Domains` and click `Dices` icon to generate a domain and the port set to `3000`. |
| 113 | +16. Now you can access your application. |
| 114 | + |
| 115 | +### Auto deploy |
| 116 | + |
| 117 | +When using Dockerhub as a registry you can also enable auto deploy, this will automatically deploy your application whenever you push to your repository. |
| 118 | + |
| 119 | +To setup auto deploys for Dockerhub, follow the steps below: |
| 120 | + |
| 121 | +1. Go to your application and select `Deployments` tab. |
| 122 | +2. Copy the `Webhook URL`. |
| 123 | +3. Go to your Dockerhub repository and select `Webhooks` tab. |
| 124 | +4. Set a name for the webhook and paste the `Webhook URL` copied in step 2. |
| 125 | +5. That's it, now every time you push to your repository, your application will trigger a deployment in dokploy. |
| 126 | + |
| 127 | +The deployment will trigger only if the `Tag` matches the one specified in Dokploy. |
| 128 | + |
| 129 | + |
| 130 | +#### External Registry |
| 131 | + |
| 132 | +If you have a registry that is not Dockerhub, you can trigger a deployment after pushing to your repository in Github Actions. |
| 133 | + |
| 134 | +Your workflow will look like this: |
| 135 | + |
| 136 | +This method use the [Api Method](/docs/core/auto-deploy#api-method) to trigger a deployment. |
| 137 | + |
| 138 | +```yaml |
| 139 | +name: Build Docker images |
| 140 | +
|
| 141 | +on: |
| 142 | + push: |
| 143 | + branches: ["main"] |
| 144 | +
|
| 145 | +jobs: |
| 146 | + build-and-push-dockerfile-image: |
| 147 | + runs-on: ubuntu-latest |
| 148 | +
|
| 149 | + steps: |
| 150 | + ...Same as step 7 from the previous example |
| 151 | + |
| 152 | + - name: Trigger Dokploy Deployment |
| 153 | + uses: dokploy/dokploy-action@v1 |
| 154 | + run: | |
| 155 | + curl -X 'POST' \ |
| 156 | + 'https://<your-dokploy-domain>/api/trpc/application.deploy' \ |
| 157 | + -H 'accept: application/json' \ |
| 158 | + -H 'Authorization: Bearer YOUR-TOKEN' \ |
| 159 | + -H 'Content-Type: application/json' \ |
| 160 | + -d '{ |
| 161 | + "json":{ |
| 162 | + "applicationId": "YOUR-APPLICATION-ID" |
| 163 | + } |
| 164 | + }' |
| 165 | +``` |
| 166 | + |
| 167 | +You can also use this Github Action [Action](https://github.com/marketplace/actions/dokploy-deployment) to automate the deployment. |
| 168 | + |
| 169 | +## Healthcheck & Rollbacks |
| 170 | + |
| 171 | +When using Dokploy you can also configure healthchecks and rollbacks, this will allow you to configure your application to be able to recover from failures. |
| 172 | + |
| 173 | +In the repo we are using from the `Step 1.` we have a healthcheck endpoint `/health` that returns a 200 status code and running in the port 3000. |
| 174 | + |
| 175 | +Go to `Advanced` Tab and go to Cluster Settings and enter to `Swarm Settings` |
| 176 | + |
| 177 | +There are a couple options that you can use, in this case we will focus on `Health Check` and `Update Config`. |
| 178 | + |
| 179 | +Make sure the API Route exists in your application |
| 180 | + |
| 181 | +```json |
| 182 | +{ |
| 183 | + "Test": [ |
| 184 | + "CMD", |
| 185 | + "curl", |
| 186 | + "-f", |
| 187 | + "http://localhost:3000/health" |
| 188 | + ], |
| 189 | + "Interval": 30000000000, |
| 190 | + "Timeout": 10000000000, |
| 191 | + "StartPeriod": 30000000000, |
| 192 | + "Retries": 3 |
| 193 | +} |
| 194 | +``` |
| 195 | + |
| 196 | +Now in the `Update Config` |
| 197 | + |
| 198 | +Now when the application is getting unhealthy response from the health check, the container will rollback to the previous version. |
| 199 | + |
| 200 | +Paste the following code: |
| 201 | +```json |
| 202 | +{ |
| 203 | + "Parallelism": 1, |
| 204 | + "Delay": 10000000000, |
| 205 | + "FailureAction": "rollback", |
| 206 | + "Order": "start-first" |
| 207 | +} |
| 208 | +``` |
| 209 | + |
| 210 | + |
| 211 | +Now you everything a production ready application with automated deployments, zero downtime, rollbacks and healthchecks. |
| 212 | + |
| 213 | +We recommend strongly to use this approach in production since this will make your server never build the application, will only in charge of the deployment keeping your server without any downtime. |
0 commit comments