I installed Docker on a Synology NAS.
I created a container that runs an ubuntu that runs a website server.
To launch my service (named "dodoc"), I use npm and pm2.
So when my container start, I have to manually open the container terminal and run the following commands :
cd /home
pm2 start dodoc
pm2 startup
And then, it works perfectly.
The problem is that I would like to launch automatically the service at container startup. Usually, a pm2 save should do it. But on a Synology NAS it does not work, don’t know why.
So… I tried a different approach :
using a shell script as a CMD on my container.
so my container CMD line is :
/bin/bash -c /root/Documents/startupCommands.sh
Like this, my container does run my script at startup, but the script does not work whereas it works if I run it manually through the terminal.
The script (named "startupCommands.sh") :
#!/bin/bash
cd /home && pm2 start dodoc
cd /home && pm2 startup
/bin/bash
On a NAS Synology, we have to set a directory on the NAS where the container will have access.
This directory, where I can put my script file is at /root/Documents in the container terminal.
But, my service is to be run at /home.
Why did I put my script in a different directory ? Because on a Synology NAS, you have to set the CMD line at container creation and can’t modify it after. So if you try to run a script.sh that does not exist yet, the container will keep reboot indefinitely. So you have to put the script file in a directory you have access without using container terminal.
Well… if I manually do the following command in the Terminal, it works :
root@ubuntu-dodoc10:/home# /root/Documents/startupCommands.sh
[PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
[PM2] PM2 Successfully daemonized
[PM2] Starting /home/dodoc in fork_mode (1 instance)
[PM2] Done.
But, if I give a look at the container log, that’s what happen at startup :
]0;root@ubuntu-dodoc10: /homeroot@ubuntu-dodoc10:/home#
/root/Documents/startupCommands.sh: line 4: pm2: command not found
/root/Documents/startupCommands.sh: line 5: pm2: command not found
And obviously, the serveur does not start…
I’ve tried :
- to install npm is the root directory
- to use (cd /home && pm2 start dodoc) commands in the script
- to create another script in the /home directory and run it from the first script
Nothing worked :'(
"Dokerfile" :
{
"CapAdd" : null,
"CapDrop" : null,
"cmd" : "/bin/bash -c /root/Documents/startupCommands.sh",
"cpu_priority" : 50,
"enable_publish_all_ports" : false,
"enable_restart_policy" : true,
"enable_service_portal" : null,
"enabled" : true,
"env_variables" : [
{
"key" : "PATH",
"value" : "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
}
],
"exporting" : false,
"id" : "4fc2468eaf2a84c57ddb25f1818cdef6650a792d67ac6ad7d5d69e8fd470f8c0",
"image" : "ubuntu:latest",
"is_ddsm" : false,
"is_package" : false,
"links" : [],
"memory_limit" : 1073741824,
"name" : "ubuntu-dodoc-test",
"network" : [
{
"driver" : "host",
"name" : "host"
}
],
"network_mode" : "host",
"port_bindings" : [],
"privileged" : false,
"shortcut" : {
"enable_shortcut" : false,
"enable_status_page" : false,
"enable_web_page" : false,
"web_page_url" : ""
},
"use_host_network" : true,
"volume_bindings" : [
{
"host_volume_file" : "/docker/dodoc",
"mount_point" : "/root/Documents",
"type" : "rw"
}
]
}
2
Answers
pm2 is available because if I run manually the script, it works.
the directory wasn't in the PATH, because it was installed globally
npm install pm2 -g
but as you asked me, I added it in the PATH (using thewhereis
command to find the location)I've tried your code in the script. There is the result :
pm2: command not found
. That seems pretty clear.pm2
available in the container?PATH
?You can put some debug statements in your script, like
to help you answer the questions in the list above.