How to run latest Strapi with docker?

Hi!

I want to use latest Strapi with SQLite database in Docker container.

From tutorial I got this docker-compose.yaml:
version: ‘3’

services:
  strapi:
    image: strapi/strapi
    volumes:
      - ./:/srv/app
    ports:
      - '1337:1337'

But when I run it with docker-compose up creates all the files in same directory as docker-compose.yaml.

version: '3'
services:
  strapi:
    image: strapi/strapi
    volumes:
      - ./tmp:/srv/app/.tmp
      - ./config:/srv/app/.config
      - ./public/uploads:/srv/app/public/uploads
    ports:
      - '1337:1337'

But then I started to get error.:

strapi_1 | Using strapi 3.6.8
strapi_1 | No project found at /srv/app. Creating a new strapi project
strapi_1 | :no_entry: You can only create a Strapi app in an empty directory.
strapi_1 | Make sure /srv/app is empty.
strapi_strapi_1 exited with code 1

How do I make it so that this error is fixed and docker-compose.yaml directory is not littered with with all those files? So that all files are inside docker and only SQLite database is persisted outside of docker to avoid it getting deleted with docker restart? Also it seems to be Strapi 3.6.8? But version 4 is out. Is there way to get version 4? Also do I still need Dockerfile if this docker-compose.yaml seems to make it all already?

You can use the tool i built for this

Just run

npx @strapi-community/dockerize

Note the image you are using is for V3 and won’t work for V4.
Also please note it’s HIGLY not recommended to use SQLITE for Docker as everytime you kill the container you will delete the database, unless you bind mount.

The tool I created can spin up Mysql / MariaDB or postgres databases locally so you can use it for testing.

Thanks!

There is no way to have just docker-compose file? Using SQLite by binding mount is bad practise?

Anyways with that command I ran into errors:

osboxes@osboxes:~/strapi$ npx @strapi-community/dockerize

Need to install the following packages:

  @strapi-community/dockerize

Ok to proceed? (y) y

npm WARN EBADENGINE Unsupported engine {

npm WARN EBADENGINE   package: 'liquidjs@10.4.0',

npm WARN EBADENGINE   required: { node: '>=14' },

npm WARN EBADENGINE   current: { node: 'v12.22.9', npm: '8.5.1' }

npm WARN EBADENGINE }

internal/modules/cjs/loader.js:818

  throw err;

  ^



Error: Cannot find module 'fs/promises'

Require stack:

- /home/osboxes/.npm/_npx/c78dafd2dc881eeb/node_modules/@strapi-community/dockerize/utils/utils.js

- /home/osboxes/.npm/_npx/c78dafd2dc881eeb/node_modules/@strapi-community/dockerize/utils/goodbye.js

- /home/osboxes/.npm/_npx/c78dafd2dc881eeb/node_modules/@strapi-community/dockerize/utils/index.js

- /home/osboxes/.npm/_npx/c78dafd2dc881eeb/node_modules/@strapi-community/dockerize/cli/quickstart.js

- /home/osboxes/.npm/_npx/c78dafd2dc881eeb/node_modules/@strapi-community/dockerize/cli/index.js

- /home/osboxes/.npm/_npx/c78dafd2dc881eeb/node_modules/@strapi-community/dockerize/index.js

    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)

    at Function.Module._load (internal/modules/cjs/loader.js:667:27)

    at Module.require (internal/modules/cjs/loader.js:887:19)

    at require (internal/modules/cjs/helpers.js:74:18)

    at Object.<anonymous> (/home/osboxes/.npm/_npx/c78dafd2dc881eeb/node_modules/@strapi-community/dockerize/utils/utils.js:2:30)

    at Module._compile (internal/modules/cjs/loader.js:999:30)

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)

    at Module.load (internal/modules/cjs/loader.js:863:32)

    at Function.Module._load (internal/modules/cjs/loader.js:708:14)

    at Module.require (internal/modules/cjs/loader.js:887:19) {

  code: 'MODULE_NOT_FOUND',

  requireStack: [

    '/home/osboxes/.npm/_npx/c78dafd2dc881eeb/node_modules/@strapi-community/dockerize/utils/utils.js',

    '/home/osboxes/.npm/_npx/c78dafd2dc881eeb/node_modules/@strapi-community/dockerize/utils/goodbye.js',

    '/home/osboxes/.npm/_npx/c78dafd2dc881eeb/node_modules/@strapi-community/dockerize/utils/index.js',

    '/home/osboxes/.npm/_npx/c78dafd2dc881eeb/node_modules/@strapi-community/dockerize/cli/quickstart.js',

    '/home/osboxes/.npm/_npx/c78dafd2dc881eeb/node_modules/@strapi-community/dockerize/cli/index.js',

    '/home/osboxes/.npm/_npx/c78dafd2dc881eeb/node_modules/@strapi-community/dockerize/index.js'

  ]

}

osboxes@osboxes:~/strapi$

I also tried this on work computer today.

On windows 10
Command: npx @strapi-community/dockerize@latest

Output:

 @strapi-community/dockerize  v1.11.0 by Simen Daehlin                                 
Easy add docker support for a Strapi Project                                           
https://github.com/strapi-community/strapi-tool-dockerize                              
                                                                                       
�  You, and 755 other people have used this tool this month                            
                                                                                       
�  JavaScript project detected                                                         
                                                                                       
�  NPM detected                                                                        
                                                                                       
⛔️  Strapi project not detected                                                        
                                                                                       
√ Do you want to create a new Strapi Project ... No / Yes                              
√ Whats the name of the project? ... mystrapi                                          
√ Do you want to use TypeScript ... No / Yes                                           
�  Creating Strapi Project from npx create-strapi-app@latest - please wait...          
                                                                                       
node:events:498                                                                        
      throw er; // Unhandled 'error' event                                             
      ^                                                                                
                                                                                       
Error: spawn npx ENOENT                                                                
    at Process.ChildProcess._handle.onexit (node:internal/child_process:283:19)        
    at onErrorNT (node:internal/child_process:478:16)                                  
    at processTicksAndRejections (node:internal/process/task_queues:83:21)             
Emitted 'error' event on ChildProcess instance at:                                     
    at Process.ChildProcess._handle.onexit (node:internal/child_process:289:12)        
    at onErrorNT (node:internal/child_process:478:16)                                  
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {           
  errno: -4058,                                                                        
  code: 'ENOENT',                                                                      
  syscall: 'spawn npx',                                                                
  path: 'npx',                                                                         
  spawnargs: [                                                                         
    'create-strapi-app@latest',                                                        
    'mystrapi',                                                                        
    '--quickstart',                                                                    
    '--typescript',                                                                    
    '--no-run'                                                                         
  ]                                                                                    
}                                                                                      

When I try npx @strapi-community/dockerize (without latest at the end) I get the same error.

On WSL Ubuntu I get /usr/bin/env: ‘bash\r’: No such file or directory with this command

First please note
:no_entry: Strapi project not detected You need to create a strapi project first. then run the command inside that project.

For the error itself feel free to post this data on a issue and I can test it out or try get a fix for it as it’s a separate issue. Creating a new strapi project from the plugin is a bit of hit and miss and I will need more test data to fully fledge out bugs.

Ah ok, I understood this command will automatically create project.

It even asks

√ Do you want to create a new Strapi Project … No / Yes

But I still need to do it manually?I don’t have other information than just that I run this command inside empty directory.

What needs to be done then to get new Strapi Docker with SQLite database that is mapped outside of docker?

It’s just released and a bit buggy :slight_smile:

You can do it quite simply by run

npx create-strapi-app@latest my-project --quickstar

Then you can run the dockerize tool when it’s created

The whole idea with docker is to containerize it, so you want to use a propper database.
The docker-compose will ask you what you want to use and you can use a propper database instead.

Thanks.

That worked! But how do I turn this into docker container? It did not create any docker files.

So if you run the normal CLI to create a strapi application.
You can then use the Dockerize tool that you tried to use inside the strapi project.
It will ask you a bunch of questions answer them and it creates dockerfiles and a docker-compose file (if you say yes) for you. It also configures the database file to match the database you selected.

So technically run both tools and it should all be ready to go.

This one?

So that’s the blog i wrote about it yes :slight_smile:

So the steps you do
1.

npx create-strapi-app@latest myproject --quickstart

This will create the project etc. Then stop it and make sure you are in the strapi directory myproject in this example
then run
2.

npx @strapi-community/dockerize

Answer the questions you might want “both” and docker-compose
And you are good to go.

Here is the github for the tool

1 Like

Hi,

I am on a apple m1 laptop, and running node v18.15.0.

After creating a empty strapi project with the command

npx create-strapi-app@latest myproject --quickstart

I check that the version of strapi in the package.json file is 4.9.1

I then ran the command

npx @strapi-community/dockerize

I went through the interactive setup, using the following setup:
:popcorn: JavaScript project detected

:package: YARN detected

:heavy_check_mark: Do you want to create a docker-compose file? :whale: · No / Yes
:heavy_check_mark: What enviroments do you want to configure? · Both
:heavy_check_mark: What database do you want to use? · PostgreSQL
:heavy_check_mark: Database Host · localhost
:heavy_check_mark: Database Name · strapi
:heavy_check_mark: Database Username · strapi
:heavy_check_mark: Database Password · ******
:heavy_check_mark: Database Port · 5432
:whale: Added docker-compose file with POSTGRESQL configuration

:whale: Dockerfile for PRODUCTION added

:male_detective: Added Dockerize variables in .env file

:floppy_disk: Added POSTGRESQL configuration to database.js

:package: No old dependencies to clean up

:package: PostgreSQL dependencies installed with YARN

:point_up: Strapi is now dockerized :whale: - have a look at the logs above for more info. :rocket:

:star: Star the project on GitHub if you liked this tool :pray:.

:point_right: GitHub - strapi-community/strapi-tool-dockerize: Easy add support for docker to your strapi project :point_left:

password was set to strapi too… this is just a testbed so I don’t mind showing the credentials.

I got one error running the command

docker-compose up -d

I got one error regarding the platform, but I changed the strapiDB services to postgres:12.0

Then it created the container, but when logging into the admin panel I see that I am running strapi v4.9.0.
In Docker desktop, I go into files and check the content of package.json in the opt/app folder and see that strapi is set to v4.9.0 instead of v4.9.1 in the local root package.json file. Why is this?

The second issue is after shutting the container, I try to run the local instance with yarn develop, this gives me the following error:
[2023-04-15 19:10:11.705] debug: :no_entry: Server wasn’t able to start properly.
[2023-04-15 19:10:11.706] error: role “strapi” does not exist

On a positive note, the tool does create a docker container in an effortless manner, but could you explain why I am getting the strange behavior? This is repeated regardless if creating a completely new strapi instance or running the tool on a previous strapi project.

Really grateful for all the hard work you have put into this

brgds
Sohail

This depends on how the image is setup.
You can force a re-creation with docker by doing docker-compose up -d --force-recreate
This forces docker to rebuild with latest image etc.

Thanks for you reply, I used the

docker-compose up -d --force-recreate

as you suggested, but the result is the same, and looking into /opt/app/package.json the content is:

You will need to rebuild the image itself then, :slight_smile:
Would suggest delete the image in Docker Desktop and then build the image again.

Thanks, that worked out, I deleted all images that the container was running, then recreated the container with its images.

The other issue I had was after stopping the container I couldn’t run strapi locally with yarn develop.
Am I missing something fundamental in my understanding?

I am getting role strapi does not exist when I run yarn develop