46

This is the content of my Dockerfile.

FROM ubuntu

RUN sudo apt-get update

RUN sudo apt-get install -y wget

CMD wget -O- -q http://ifconfig.me/ip

When I run the Dockerfile to build a docker image, I get the below error:

/bin/sh: 1: sudo: not found

Can you please help me in solving the above error?

GileBrt
  • 1,830
  • 3
  • 20
  • 28
mspms
  • 501
  • 1
  • 5
  • 8
  • 1
    Possible duplicate of [How to use sudo inside a docker container?](https://stackoverflow.com/questions/25845538/how-to-use-sudo-inside-a-docker-container) – yeputons May 22 '19 at 05:04
  • $ docker run -it ubuntu then inside container # su – Abhishek D K May 22 '19 at 05:07
  • 2
    remove sudo from Dockerfile – Abhishek D K May 22 '19 at 05:09
  • After removing SUDO from dockerfile, i am getting a new error E: Unable to locate package wget – mspms May 22 '19 at 05:17
  • @yeputons I would not say it is a duplicate of [How to use sudo inside a docker container?](https://stackoverflow.com/questions/25845538/how-to-use-sudo-inside-a-docker-container) since it does not mention the error message, and the answer to this question here is already the first sentence in the question there. – questionto42 Feb 14 '21 at 22:26
  • for large Dockerfile files, make sure the `USER` variable isn't being assigned/changed. This will make all further commands run as that user and not root. – rob May 22 '23 at 10:24

3 Answers3

49

by default docker container runs as root user
remove the sudo from Dockerfile and run again.

enter image description here

Abhishek D K
  • 2,257
  • 20
  • 28
  • 3
    also prompt with # signifies root user – Abhishek D K Apr 09 '20 at 04:20
  • https://www.digitalocean.com/community/questions/how-to-fix-docker-got-permission-denied-while-trying-to-connect-to-the-docker-daemon-socket this should help you for Docker Daemon permission related – Abhishek D K Oct 25 '20 at 14:42
13

Your commands in the Dockerfile already run as root during docker build. For this reason you do not need to use sudo

Mihai
  • 9,526
  • 2
  • 18
  • 40
6

You don't need sudo in this case. Change Dockerfile as below -

FROM ubuntu

RUN apt-get update -y && \ 
    apt-get install -y wget

CMD wget -O- -q http://ifconfig.me/ip

PS - Merge the RUN statements as much as possible.

vivekyad4v
  • 13,321
  • 4
  • 55
  • 63