sudo, Linux’s most useful command, is occasionally missing from several Linux distributions, most notably docker containers and lightweight Linux distributions.
sudo is a utility that allows users to run commands with
root access. It is the most helpful command and is included in almost all major Linux distributions. Yes, almost all. Several Linux distros, particularly docker images, do not ship the
sudo package by default. If you see the error
bash: sudo: command not found it simply means that
sudo is not installed.
bash: sudo: command not found
sudo will be enough. However, installing a new package necessitates the use of
For Ubuntu or its derivatives
$ apt install sudo
For Fedora or its derivatives
$ dnf install sudo
For RHEL or its derivatives
$ yum install sudo
For Arch Linux or its derivatives
$ pacman install sudo
For Docker images
However, if there is no root account, like in the case of numerous docker images, this may not be possible.
root user. In that case, we’ll have to create a custom image with
sudo pre-installed. Don’t worry, it is easy.
Create custom Docker image
A Dockerfile is required to construct a custom docker image. Dockerfile is used to define everything in a certain syntax, and the docker build command is used to generate an image from it.
nano Dockerfile or gedit Dockerfile
# This is the base image we will use to create our custom docker image FROM ubuntu:latest # # the maintainer of an image LABEL maintainer="[email protected]" # # Install the tools (sudo) RUN apt-get update && apt-get upgrade -y && apt install sudo tasksel -y
We used the RUN label in the third part, which is marked with “Install the tools”, and any command after RUN will be executed before generating our custom image. So, by typing a command here, we can install any package we wish for. If we keep writing commands, we can even spin up a full server.
For more knowledge on Dockerfile, refer to the docker’s official documentation.
Save and close the Dockerfile. Now, open the command line and navigate to the directory containing the Dockerfile and run the following command –
sudo docker build -t image-name:tag .
unable to prepare context: context must be a directory: /home/sandy/Dockerfile. So you must be inside the directory containing Dockerfile and use . to specify the current directory or the full path to the Directory containing Dockerfile.
And that’s it.
docker build will start building the command. It will take a few seconds. Once done, our custom docker image is saved in our local image cache. We can now use the image-name:tag to create a docker container that has sudo pre-installed.