'Docker Run Command as Sudo?

Inside my dockerfile I have:

FROM ubuntu:latest
FROM python:latest

RUN sudo apt-get update
RUN apt-get install nmap

But I have a problem where the last line doesn't work because of sudo, how may I fix this?



Solution 1:[1]

The following works for me:

RUN apt-get update && \
    apt-get install -y --no-install-recommends \
    nmap

-y means automatic "yes" answer to all prompts and runs non-interactively.

Solution 2:[2]

Since your first FROM statement doesn't do anything and you're already running as root, you can change your Dockerfile to

FROM python:latest
RUN apt-get update
RUN apt-get install -y nmap

Build and run with

docker build -t myimage .
docker run -it myimage /bin/bash

Now you're in a shell in the container. You can now run whoami to verify that you're running as root and you can run nmap.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 krskara
Solution 2 Hans Kilian