{"id":132170,"date":"2019-01-16T09:32:19","date_gmt":"2019-01-16T01:32:19","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=132170"},"modified":"2019-01-07T09:47:38","modified_gmt":"2019-01-07T01:47:38","slug":"use-nsenter-docker","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/use-nsenter-docker.html","title":{"rendered":"\u4f7f\u7528nsenter\u5de5\u5177\u8fdb\u5165Docker\u5bb9\u5668"},"content":{"rendered":"

\u67e5\u770b\u672c\u673a\u88c5\u6ca1\u6709nsenter<\/p>\n

whereis nsenter\u6216\u8005whatis nsenter<\/p>\n

\u672a\u5b89\u88c5\u5148\u5b89\u88c5\uff0c\u7f51\u4e0a\u6709\u5f88\u591a\u8fd9\u6837\u7684\u811a\u672c<\/p>\n

\r\nvi nsenter.sh\r\n#!\/bin\/bash\r\ncurl https:\/\/www.kernel.org\/pub\/linux\/utils\/util-linux\/v2.24\/util-linux-2.24.tar.gz | tar -zxf-\r\ncd util-linux-2.24\r\n.\/configure --without-ncurses\r\nmake nsenter\r\nsudo cp nsenter \/usr\/local\/bin\r\ncd .. && rm -rf util-linux-2.24\r\n<\/pre>\n
\u4fdd\u5b58\u9000\u51fa\uff0c\u6267\u884c\u811a\u672c\u5c31\u53ef\u4ee5\u5b89\u88c5nsenter\u3002\u5982\u679c\u62a5\u9519\uff0c\u6ce8\u610f\u770b\u62a5\u9519\u4fe1\u606f\uff0c\u6211\u662f\u5b89\u88c5gcc\u89e3\u51b3\u4e86\u8fd9\u4e2a\u95ee\u9898\uff0c\u4e4b\u540e\u8fd0\u7528\u4e0b\u9762\u811a\u672c\u8fdb\u5165\u5bb9\u5668\u3002<\/pre>\n
vi docker_enter.sh\r\n#!\/bin\/sh\r\n# \u6587 \u6863\u540d: docker_enter.sh\r\n# \u7248 \u672c: 1.0.1\r\n# \u7528 \u9014: \u4f7f\u7528nsenter\u5de5\u5177\u8fdb\u5165Docker\u5bb9\u5668\r\n# \u7528 \u6cd5\uff1a. docker_enter \u5bb9\u5668ID\u6216bash -x docker_enter.sh \u5bb9\u5668ID\r\n# \u6b63 \u6587\uff1a\u5982\u4ee5\u4e0b\u4ee3\u7801\r\n\r\nif [ -e $(dirname \"$0\")\/nsenter ]; then\r\nNSENTER=$(dirname \"$0\")\/nsenter\r\nelse\r\nNSENTER=nsenter\r\nfi\r\nif [ -z \"$1\" ]; then\r\necho \"Usage: `basename \"$0\"` CONTAINER [COMMAND [ARG]...]\"\r\necho \"\"\r\necho \"Enters the Docker CONTAINER and executes the specified COMMAND.\"\r\necho \"If COMMAND is not specified, runs an interactive shell in CONTAINER.\"\r\nelse\r\nPID=$(docker inspect --format \"{{.State.Pid}}\" \"$1\")\r\nif [ -z \"$PID\" ]; then\r\nexit 1\r\nfi\r\nshift\r\nOPTS=\"--target $PID --mount --uts --ipc --net --pid --\"\r\nif [ -z \"$1\" ]; then\r\n# No command given.\r\n# Use su to clear all host environment variables except for TERM,\r\n# initialize the environment variables HOME, SHELL, USER, LOGNAME, PATH,\r\n# and start a login shell.\r\n\"$NSENTER\" $OPTS su - root\r\nelse\r\n# Use env to clear all host environment variables.\r\n\"$NSENTER\" $OPTS env --ignore-environment -- \"$@\"\r\nfi\r\nfi\r\n<\/pre>\n

\u4fdd\u5b58\u9000\u51fa\uff0c\u547d\u4ee4\u884c\u4e0b\u6267\u884c. docker_enter \u5bb9\u5668ID\u6216bash -x docker_enter.sh \u5bb9\u5668ID\uff0c\u5373\u53ef\u6210\u529f\u8fdb\u5165\u5bb9\u5668<\/p>\n","protected":false},"excerpt":{"rendered":"

\u67e5\u770b\u672c\u673a\u88c5\u6ca1\u6709nsenter whereis nsenter\u6216\u8005whatis nsenter \u672a\u5b89\u88c5\u5148\u5b89\u88c5\uff0c\u7f51 […]<\/p>\n","protected":false},"author":643,"featured_media":132172,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-132170","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/132170","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/643"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=132170"}],"version-history":[{"count":5,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/132170\/revisions"}],"predecessor-version":[{"id":133601,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/132170\/revisions\/133601"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/132172"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=132170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=132170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=132170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}