Search code examples
sql-serverdockerubunturoot

Switch to Root User in a Dockerfile


I ran this command:

docker pull mcr.microsoft.com/mssql/server:2019-latest

I then made a dockerfile to use this container image as a base image for another container

# escape=`
FROM  mcr.microsoft.com/mssql/server:2019-latest
SHELL ["/bin/bash", "-c"]

COPY ./CompanyCert.crt  /usr/local/share/ca-certificates/CompanyCert.crt
RUN update-ca-certificates

When I try to build that docker file, I get this error:

ln: failed to create symbolic link '/etc/ssl/certs/CompanyCert.pem': Permission denied

So I added a RUN whoami to my docker file and it returns mssql. When I run id -u it returns 10001. So it seems that the user mssql does not have root permissions.

I tried putting sudo in front of my call to update-ca-certificates but it says:

/bin/bash: sudo: command not found

I tried to RUN su - and that returns:

su: must be run from a terminal

I have successfully used the above dockerfile to install my company certificates on other containers from Microsoft, but it is failing spectacularly this time.

How can I get root access so I can install my company certificate on this SQL Server Container?


Solution

  • Add USER root to your Dockerfile:

    FROM  mcr.microsoft.com/mssql/server:2019-latest
    USER root
    SHELL ["/bin/bash", "-c"]
    
    COPY ./CompanyCert.crt  /usr/local/share/ca-certificates/CompanyCert.crt
    RUN update-ca-certificates