How to Run Docker Commands without sudo on Ubuntu/Debian/Arch Linux

by Jack
8 minutes
How to Run Docker Commands without sudo on Ubuntu/Debian/Arch Linux

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.

How to Run Docker Commands without sudo on Ubuntu/Debian/Arch Linux?

To be able to run all docker commands without sudo, execute these commands one after another.

  1. sudo usermod -aG docker USER
  2. sg docker -c "bash"

command to make docker run without sudo

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!

docker running without sudo command

This is it. To understand more about docker and sudo, refer to the following sections.

What is Docker?

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:

  • Portability: Containers are portable, so you can easily move them from one environment to another.
  • Efficiency: Containers share the host operating system's kernel, which makes them more efficient than virtual machines.
  • Scalability: Containers can be easily scaled up or down, making them a good choice for applications that need to be able to handle fluctuating traffic.

What is sudo?

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.

Reasons to run Docker Commands without sudo:

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.