Sudo isn't shipped with Alpine images normally, and it rarely makes sense to include it inside of any container. What you need isn't sudo to bind to a low numbered port, but the root user itself, and sudo is just a common way to get root access in multi-user environments. If a container included sudo, you would need to either setup the user with a password, or allow commands to run without a password. Regardless of which you chose, you now have a privilege escalation inside the container, defeating the purpose of running the container as a normal user, so you may as well run the container as root at that point.
If the upstream image is configured to run as a non-root user (unlikely since you run apk
commands during the build), you can specify USER root
in your Dockerfile, and all following steps will run as root by default, including the container entrypoint/cmd.
If you start your container as a different user, e.g. docker run -u 1000 your_image
, then to run your command as root, you'd remove the -u 1000
option. This may be an issue if you run your container in higher security environments that restrict containers to run as non-root users.
If your application itself is dropping the root privileges, then including sudo is unlikely not help, unless the application itself has calls to sudo internally. If that's the case, update the application to drop root privileges after binding to the ports.
Most importantly, if the only reason for root inside your container is to bind to low numbered ports, then configure your application inside the container to bind to a high numbered port, e.g. 8080 and 8443. You can map this container port to any port on the host, including 80 and 443, so the outside world does not see any impact. E.g. docker run -p 80:8080 -p 443:8443 your_image
. This simplifies your image (removing tools like sudo) and increases your security at the same time.
USER
set in the image, which aren't in the base image or your Dockerfile. Can you explain a bit more about what you're trying to do and what is going wrong? Is there an error message? – InfestationCMD
command is executed, my question is how to execute him withsudo
– Oruropm2
command withsudo
privileges because I need to run it with port 443. Locally in my machine I can runpm2
with commandsudo pm2 sart
but to deploy my app I'm using docker and I need to run the pm2 command with sudo too – Oruroerror: listen eacces 0.0.0.0:443
the reason is to run node app using ports bellow 1024 I need to execute it withsudo
– Orurodocker run
command as well? Is that error appearing in the docker logs/output? – Infestationsudo
. This is explained here: #35069212 However, I also would like to be able to set up a docker image that allows node to listen on port 80 without requiring a proxy server, just for testing purposes. It would be great to find out how to do this. In my Dockerfile I useCMD ["npm", "start"]
which causes an error if the port is set to 80. I triedCMD ["sudo", "npm", "start"]
but that fails -sudo not found
– Radiobroadcast