To run docker commands such as docker run, docker ps, docker system, docker-compose without sudo, you need to add your user (assuming it has root privileges) to the docker group.
To be able to run all docker commands without sudo, execute these commands one after another.
sudo usermod -aG docker USER
sg docker -c "bash"
In the first command, replace USER with your username. Issue this command and then maybe restart your terminal.
From now, on you will be able to use all the docker commands including docker-compose without sudo!
This is it. To understand more about docker and sudo, refer to the following sections.
Docker is a tool that helps you build, run, and share containerized applications. Containers are a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings.
Docker makes it possible to ship an application as a single package that can run on any machine, regardless of its operating system. This makes it easy to deploy applications to different environments, such as development, staging, and production.
See some benefits of using Docker:
sudo
is a Linux command that allows users to run system and application commands with privileges that only root user have. It helps users to do tasks with administrative power without logging in as the root user. It is basically a prefix that you put before any command that you want to run with root privileges.
For example, if you want to edit the /etc/passwd
file, which is a file that contains the list of all users on the system, you would need to run the this command: sudo vi /etc/passwd
This will open the passwd file in vi editor with root privileges so that you will be able to edit it as the root user.
The sudo command is a powerful tool, and it should be used with caution. If you are not sure what you are doing, it is better to not use sudo. However, if you need to run a command that requires root privileges, sudo is a safe and convenient way to do it.
There are many reasons one would want to run docker commands without sudo, such as:
Security: By default, Docker commands run as root, which means that they have full access to the system. This can be a security risk, as it allows a malicious user to run commands that could damage the system. If a user is only running Docker commands that they trust, then they may want to run them without sudo to reduce the security risk.
Convenience: Running Docker commands without sudo can be more convenient. You will not have to type sudo before every docker command you issue. This can be especially helpful if the user is running Docker commands frequently.
Environment Variables: If you have to use sudo to run docker commands, then it would not possible to pass environment variables to docker this way. You will have to add an extra -E switch to do that.
Performance: Running Docker commands without sudo can sometimes improve performance, as it eliminates the overhead of switching to root mode. This is because Docker commands that run as root do not have to check permissions for every operation.