Docker remove root user from image at build time












0














I have a simple Dockerfile that is based on debian



FROM debian

USER 1001


I build it



docker build -t no-root:1 .


I run it



docker run --name test -it no-root:1


Now I have a non-root shell



I have no name!@2ac786ca2265:/$


But, I can exec into the container from another shell with



docker exec -it --user 0 test bash


And I get a root shell



root@e043edec4585:/#


And run root commands...



Is it possible the remove the root user from the system at build time so docker exec -it --user 0 test bash will fail?



I tried deleting the root line from /etc/passwd and /etc/group. It did not help. I'm able to docker exec -it --user 0 test bash in.



So - is there a way?










share|improve this question






















  • AFAIK the answer is no. You can't prevent docker from creating a process as root (uid 0).
    – vstm
    Nov 21 '18 at 13:16
















0














I have a simple Dockerfile that is based on debian



FROM debian

USER 1001


I build it



docker build -t no-root:1 .


I run it



docker run --name test -it no-root:1


Now I have a non-root shell



I have no name!@2ac786ca2265:/$


But, I can exec into the container from another shell with



docker exec -it --user 0 test bash


And I get a root shell



root@e043edec4585:/#


And run root commands...



Is it possible the remove the root user from the system at build time so docker exec -it --user 0 test bash will fail?



I tried deleting the root line from /etc/passwd and /etc/group. It did not help. I'm able to docker exec -it --user 0 test bash in.



So - is there a way?










share|improve this question






















  • AFAIK the answer is no. You can't prevent docker from creating a process as root (uid 0).
    – vstm
    Nov 21 '18 at 13:16














0












0








0


1





I have a simple Dockerfile that is based on debian



FROM debian

USER 1001


I build it



docker build -t no-root:1 .


I run it



docker run --name test -it no-root:1


Now I have a non-root shell



I have no name!@2ac786ca2265:/$


But, I can exec into the container from another shell with



docker exec -it --user 0 test bash


And I get a root shell



root@e043edec4585:/#


And run root commands...



Is it possible the remove the root user from the system at build time so docker exec -it --user 0 test bash will fail?



I tried deleting the root line from /etc/passwd and /etc/group. It did not help. I'm able to docker exec -it --user 0 test bash in.



So - is there a way?










share|improve this question













I have a simple Dockerfile that is based on debian



FROM debian

USER 1001


I build it



docker build -t no-root:1 .


I run it



docker run --name test -it no-root:1


Now I have a non-root shell



I have no name!@2ac786ca2265:/$


But, I can exec into the container from another shell with



docker exec -it --user 0 test bash


And I get a root shell



root@e043edec4585:/#


And run root commands...



Is it possible the remove the root user from the system at build time so docker exec -it --user 0 test bash will fail?



I tried deleting the root line from /etc/passwd and /etc/group. It did not help. I'm able to docker exec -it --user 0 test bash in.



So - is there a way?







docker dockerfile






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 12:20









Eldad AK

4,26473150




4,26473150












  • AFAIK the answer is no. You can't prevent docker from creating a process as root (uid 0).
    – vstm
    Nov 21 '18 at 13:16


















  • AFAIK the answer is no. You can't prevent docker from creating a process as root (uid 0).
    – vstm
    Nov 21 '18 at 13:16
















AFAIK the answer is no. You can't prevent docker from creating a process as root (uid 0).
– vstm
Nov 21 '18 at 13:16




AFAIK the answer is no. You can't prevent docker from creating a process as root (uid 0).
– vstm
Nov 21 '18 at 13:16












1 Answer
1






active

oldest

votes


















0














Well it's simple enough to change users, something like the below which is an example I pinched off another site.



RUN useradd -c 'Node.js user' -m -d /home/node -s /bin/bash node
RUN chown -R node.node /src
USER node
ENV HOME /home/node


I don't recommend trying to delete the root user. The above should stop root from being used on docker exec.



And alternative is to create a .profile in the root users home directory which logs into a different user instead but this'll only affect usage of exec where you're running -it {container} bash (aka, interactive shell)






share|improve this answer





















  • Adding your lines didn't help. I'm still logged in as root. Just with the /home/node as my default home. Did I miss something?
    – Eldad AK
    Nov 21 '18 at 15:00










  • Those were just an example, you're using a Debian image so look up how to create a new user on Debian
    – George Appleton
    Nov 21 '18 at 16:01










  • The user is created correctly and the default container starts with the node user. It's an exec --user 0 from a new shell that allows root access.
    – Eldad AK
    Nov 21 '18 at 16:03






  • 1




    Only people you trust should be accessing your docker socket. Restrict their permissions on their machines if you really have to I suppose. You cannot delete the root user without breaking your container though
    – George Appleton
    Nov 21 '18 at 17:42










  • sudo passwd -l root disables root login, don't know why I didn't suggest that before
    – George Appleton
    Nov 21 '18 at 17:48











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411914%2fdocker-remove-root-user-from-image-at-build-time%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Well it's simple enough to change users, something like the below which is an example I pinched off another site.



RUN useradd -c 'Node.js user' -m -d /home/node -s /bin/bash node
RUN chown -R node.node /src
USER node
ENV HOME /home/node


I don't recommend trying to delete the root user. The above should stop root from being used on docker exec.



And alternative is to create a .profile in the root users home directory which logs into a different user instead but this'll only affect usage of exec where you're running -it {container} bash (aka, interactive shell)






share|improve this answer





















  • Adding your lines didn't help. I'm still logged in as root. Just with the /home/node as my default home. Did I miss something?
    – Eldad AK
    Nov 21 '18 at 15:00










  • Those were just an example, you're using a Debian image so look up how to create a new user on Debian
    – George Appleton
    Nov 21 '18 at 16:01










  • The user is created correctly and the default container starts with the node user. It's an exec --user 0 from a new shell that allows root access.
    – Eldad AK
    Nov 21 '18 at 16:03






  • 1




    Only people you trust should be accessing your docker socket. Restrict their permissions on their machines if you really have to I suppose. You cannot delete the root user without breaking your container though
    – George Appleton
    Nov 21 '18 at 17:42










  • sudo passwd -l root disables root login, don't know why I didn't suggest that before
    – George Appleton
    Nov 21 '18 at 17:48
















0














Well it's simple enough to change users, something like the below which is an example I pinched off another site.



RUN useradd -c 'Node.js user' -m -d /home/node -s /bin/bash node
RUN chown -R node.node /src
USER node
ENV HOME /home/node


I don't recommend trying to delete the root user. The above should stop root from being used on docker exec.



And alternative is to create a .profile in the root users home directory which logs into a different user instead but this'll only affect usage of exec where you're running -it {container} bash (aka, interactive shell)






share|improve this answer





















  • Adding your lines didn't help. I'm still logged in as root. Just with the /home/node as my default home. Did I miss something?
    – Eldad AK
    Nov 21 '18 at 15:00










  • Those were just an example, you're using a Debian image so look up how to create a new user on Debian
    – George Appleton
    Nov 21 '18 at 16:01










  • The user is created correctly and the default container starts with the node user. It's an exec --user 0 from a new shell that allows root access.
    – Eldad AK
    Nov 21 '18 at 16:03






  • 1




    Only people you trust should be accessing your docker socket. Restrict their permissions on their machines if you really have to I suppose. You cannot delete the root user without breaking your container though
    – George Appleton
    Nov 21 '18 at 17:42










  • sudo passwd -l root disables root login, don't know why I didn't suggest that before
    – George Appleton
    Nov 21 '18 at 17:48














0












0








0






Well it's simple enough to change users, something like the below which is an example I pinched off another site.



RUN useradd -c 'Node.js user' -m -d /home/node -s /bin/bash node
RUN chown -R node.node /src
USER node
ENV HOME /home/node


I don't recommend trying to delete the root user. The above should stop root from being used on docker exec.



And alternative is to create a .profile in the root users home directory which logs into a different user instead but this'll only affect usage of exec where you're running -it {container} bash (aka, interactive shell)






share|improve this answer












Well it's simple enough to change users, something like the below which is an example I pinched off another site.



RUN useradd -c 'Node.js user' -m -d /home/node -s /bin/bash node
RUN chown -R node.node /src
USER node
ENV HOME /home/node


I don't recommend trying to delete the root user. The above should stop root from being used on docker exec.



And alternative is to create a .profile in the root users home directory which logs into a different user instead but this'll only affect usage of exec where you're running -it {container} bash (aka, interactive shell)







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 12:41









George Appleton

609427




609427












  • Adding your lines didn't help. I'm still logged in as root. Just with the /home/node as my default home. Did I miss something?
    – Eldad AK
    Nov 21 '18 at 15:00










  • Those were just an example, you're using a Debian image so look up how to create a new user on Debian
    – George Appleton
    Nov 21 '18 at 16:01










  • The user is created correctly and the default container starts with the node user. It's an exec --user 0 from a new shell that allows root access.
    – Eldad AK
    Nov 21 '18 at 16:03






  • 1




    Only people you trust should be accessing your docker socket. Restrict their permissions on their machines if you really have to I suppose. You cannot delete the root user without breaking your container though
    – George Appleton
    Nov 21 '18 at 17:42










  • sudo passwd -l root disables root login, don't know why I didn't suggest that before
    – George Appleton
    Nov 21 '18 at 17:48


















  • Adding your lines didn't help. I'm still logged in as root. Just with the /home/node as my default home. Did I miss something?
    – Eldad AK
    Nov 21 '18 at 15:00










  • Those were just an example, you're using a Debian image so look up how to create a new user on Debian
    – George Appleton
    Nov 21 '18 at 16:01










  • The user is created correctly and the default container starts with the node user. It's an exec --user 0 from a new shell that allows root access.
    – Eldad AK
    Nov 21 '18 at 16:03






  • 1




    Only people you trust should be accessing your docker socket. Restrict their permissions on their machines if you really have to I suppose. You cannot delete the root user without breaking your container though
    – George Appleton
    Nov 21 '18 at 17:42










  • sudo passwd -l root disables root login, don't know why I didn't suggest that before
    – George Appleton
    Nov 21 '18 at 17:48
















Adding your lines didn't help. I'm still logged in as root. Just with the /home/node as my default home. Did I miss something?
– Eldad AK
Nov 21 '18 at 15:00




Adding your lines didn't help. I'm still logged in as root. Just with the /home/node as my default home. Did I miss something?
– Eldad AK
Nov 21 '18 at 15:00












Those were just an example, you're using a Debian image so look up how to create a new user on Debian
– George Appleton
Nov 21 '18 at 16:01




Those were just an example, you're using a Debian image so look up how to create a new user on Debian
– George Appleton
Nov 21 '18 at 16:01












The user is created correctly and the default container starts with the node user. It's an exec --user 0 from a new shell that allows root access.
– Eldad AK
Nov 21 '18 at 16:03




The user is created correctly and the default container starts with the node user. It's an exec --user 0 from a new shell that allows root access.
– Eldad AK
Nov 21 '18 at 16:03




1




1




Only people you trust should be accessing your docker socket. Restrict their permissions on their machines if you really have to I suppose. You cannot delete the root user without breaking your container though
– George Appleton
Nov 21 '18 at 17:42




Only people you trust should be accessing your docker socket. Restrict their permissions on their machines if you really have to I suppose. You cannot delete the root user without breaking your container though
– George Appleton
Nov 21 '18 at 17:42












sudo passwd -l root disables root login, don't know why I didn't suggest that before
– George Appleton
Nov 21 '18 at 17:48




sudo passwd -l root disables root login, don't know why I didn't suggest that before
– George Appleton
Nov 21 '18 at 17:48


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411914%2fdocker-remove-root-user-from-image-at-build-time%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

To store a contact into the json file from server.js file using a class in NodeJS

Marschland