0

I've installed and built my workflows within the n8n container.
https://docs.n8n.io/getting-started/installation/advanced/server-setup.html
It's using docker-compose and I'm running the container as a non-root user.

Now, I'd like to install PHP (cli) inside the existing (alpine) docker container.

So I've tried this:

docker run -it n8nio/n8n  /bin/ash
/data $ apk update
ERROR: Unable to lock database: Permission denied
ERROR: Failed to open apk database: Permission denied

after reading: Install packages in Alpine docker

More digging and I got to:

docker run -it --user=root n8nio/n8n  /bin/ash
/data $ apk update
ERROR: Unable to lock database: Permission denied
ERROR: Failed to open apk database: Permission denied

Is there a way to use docker run to install PHP in my existing container?

And if not, should I better add the install command to docker-compose.yml file?

I don't want to re-create my container, since I've already worked on it.

Chris
  • 1,265
  • 4
  • 18
  • 37
  • 1
    A Docker container wraps a single process, and as such, is intended to be temporary. You should almost never install software in a running container since that will be lost as soon as the container exits. Instead, [write a Dockerfile](https://docs.docker.com/get-started/02_our_app/#build-the-apps-container-image) that preinstalls software into an image so that when you `docker run` a new container it will have everything you need. – David Maze Sep 03 '21 at 11:24

2 Answers2

0

The issue is that the n8n image runs a script on startup, that runs any command you put in you docker run command as the user node. You can see that you're not root by the prompt '$'. If you were root, the prompt would be '#'.

The node user doesn't have the necessary rights to run apk update.

To run as root, you need to override the entrypoint of the image so it doesn't run the script. Like this

docker run -it --entrypoint /bin/ash n8nio/n8n

Then you'll run as the root user and can install more packages.

Be aware that your installed packages will be gone once the container is shut down. If you need to have PHP installed permanently, you should make your own image.

Hans Kilian
  • 18,948
  • 1
  • 26
  • 35
0

You mentioned next:

I don't want to re-create my container, since I've already worked on it.

This means you shouldn't use docker run to new a container again, you should use docker exec into a existed container like next:

$ docker exec -it $id_of_existed_container /bin/ash
/data # id
uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel),11(floppy),20(dialout),26(tape),27(video)
/data # apk update
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/community/x86_64/APKINDEX.tar.gz
v3.11.12-1-gede026da09 [http://dl-cdn.alpinelinux.org/alpine/v3.11/main]
v3.11.11-124-gf2729ece5a [http://dl-cdn.alpinelinux.org/alpine/v3.11/community]
OK: 11280 distinct packages available
atline
  • 28,355
  • 16
  • 77
  • 113