从docker容器运行docker命令(Running docker command from docker container)
需要编写一个在dock-a中安装docker的Dockerfile。 因为container-a需要执行docker命令到container-b,它与container-a一起运行。 我的理解是你在编写Dockerfile时不应该使用“sudo”。 但我陷入困境 - 我指派给docker组的用户是什么? 当您运行
docker exec -it
,您将自动为root。
sudo usermod -a -G docker whatuser?
另外(我在集装箱内手动尝试这个 - 看它是否有效)你必须做一个
newgrp docker
来激活对组的更改。 我这样做的任何时候,当我没有sudo'ed时,我最终sudo'ing。 那有意义吗? 症状是 - 我去exit
容器,我必须退出两次(好像我改变了用户)。我究竟做错了什么?
Need to write a Dockerfile that installs docker in container-a. Because container-a needs to execute a docker command to container-b that's running alongside container-a. My understanding is you're not supposed to use "sudo" when writing the Dockerfile. But I'm getting stuck -- what user to I assign to docker group? When you run
docker exec -it
, you are automatically root.
sudo usermod -a -G docker whatuser?
Also (and I'm trying this out manually inside container-a to see if it even works) you have to do a
newgrp docker
to activate the changes to groups. Anytime I do that, I end up sudo'ing when I haven't sudo'ed. Does that make sense? The symptom is -- I go toexit
the container, and I have to exit twice (as if I changed users).What am I doing wrong?
原文:https://stackoverflow.com/questions/42237352
最满意答案
使用
const_cast
修改const
数据结构中的数据确实是未定义的行为。 例外情况是标记为mutable
项目。 这些值的重点在于,即使对象的其余部分是const
,它们也是可修改的。 它的确意味着“但这个不是const
”。由于几乎所有的
const
都是关于编译器检测修改的,尽管从技术上讲,编译器允许在“不可写的内存”中放置一些const
变量。mutable
关键字允许“绕过”const
,因此编译器不会将const
对象放入不可写的内存中(如果它有可变组件),当然,它不会“对象”到const
在它的可变组件中修改的对象 - 甚至在const
函数内部。Using
const_cast
to modify data in aconst
data structure is indeed undefined behaviour. The exception is items markedmutable
. The whole point of these values are that they are modifiable even when the rest of the object isconst
. It really means "but this one is notconst
".Since nearly all of
const
is about the compiler detecting modification, although technically, the compiler is allowed place someconst
variables in "non-writeable memory". Themutable
keyword is there to allow "bypass" of the constness, so the compiler will NOT put aconst
object into memory that is non-writeable if it has a mutable component, and of course, it won't "object" toconst
objects being modified in it's mutable components - even inside aconst
function.
相关问答
更多-
没有。 引用仅仅是现有对象的别名。 const由编译器强制执行; 它只是检查你是否试图通过引用r修改对象。 *这不需要创建副本。 鉴于const只是编译器强制执行“只读”的一条指令,那么为什么最终的示例不能编译,应该马上明白。 如果你可以通过对一个const对象进行非const引用来轻易地规避它, const将毫无意义。 *当然,你仍然可以通过x修改对象。 任何更改也将通过r可见,因为它们指向同一个对象。 No. A reference is simply an alias for an existing ...
-
之前的const还是const之后?(Const before or const after?)[2022-06-20]
“为什么有两种正确的方式来指定const数据,在什么情况下你会喜欢或需要一个超过另一个呢? 基本上,在星号之前的const中的const的位置不重要的原因在于C语法是由Kernighan和Ritchie定义的。 他们以这种方式定义语法的原因很可能是它们的C编译器从左到右分析输入,并在完成处理每个令牌时消耗它。 使用*令牌将当前声明的状态更改为指针类型。 表示const限定符应用于指针声明; 在*之前遇到它意味着将限定符应用于指向的数据。 因为如果const限定符出现在类型说明符之前或之后,语义的意义不会改变 ... -
您需要在每个不具有const访问权限的地方排除const修饰符: containerVec_t & outputVars() { return _outputVars; } //Change definition in MiddleMan class MiddleMan::containerVec_t & outputs = theMiddleMan.outputVars(); //Change reference type float * data_ptr = outputs[0].dataNonCon ...
-
不,不是你想要使用它的方式。 在编译时具有不同行为的唯一方法是使用不同的类型。 但是,您可以使用起来相当容易: #include
template class SometimesConst { public: SometimesConst(T* buffer) : buffer(buffer) { } T* get() { return buffer; } void increment() { ++counter; } priv ... -
请注意,如果你这样做并且对象确实是const ,那么在抛弃const之后修改它是未定义的行为。 fgBlocks.CopyInto(const_cast
(backUpCopy)); 另一个是同样的事情: const_cast (backUpCopy).RemoveAll(true); Be aware that if you do this and the object really is const, then modifying it after casti ... -
因为编译器将这两个参数视为不同的引用,所以没有问题。 要理解代码,请考虑以下示例 int i = 10; const int &cr = i; int &r = i; r = 20; std::cout << cr << std::endl; There is no problem becuase the compiler consideres these two parameters as different references. To understand the code consider t ...
-
使用const_cast修改const数据结构中的数据确实是未定义的行为。 例外情况是标记为mutable项目。 这些值的重点在于,即使对象的其余部分是const ,它们也是可修改的。 它的确意味着“但这个不是const ”。 由于几乎所有的const都是关于编译器检测修改的,尽管从技术上讲,编译器允许在“不可写的内存”中放置一些const变量。 mutable关键字允许“绕过” const ,因此编译器不会将const对象放入不可写的内存中(如果它有可变组件),当然,它不会“对象”到const在它的可变组 ...
-
编译器吃了它。 但落后演员更有用。 而且 - 最好不要使用它,const_cast通常只是一个快速而肮脏的解决方案,只有在没有任何其他解决方案时才应用。 回答更新 如果我理解正确,你将使用一个对象和两个函数。 第一个函数接受const对象和const成员函数,第二个函数接受非const对象和非const成员函数。 根据给定的信息,您可以更改第二个函数以接受非const对象和const成员函数。 并给它们一个非const对象及其const成员函数。 Compiler eats it. But the back ...
-
假设x具有静态存储持续时间,则左值表达式x是完全有效的常量表达式。 如果在需要prvalue的上下文中使用x ,这会导致将lvalue-to-rvalue转换应用于它,那么生成的prvalue表达式 - 称之为TO_RVALUE(x) - 将不是常量表达式,原因显而易见。 但是在引用绑定的情况下,没有这样的转换。 Assuming that x has static storage duration, the lvalue expression x is a perfectly valid constant ...
-
是的,根据7.1.5.1/4,这是未定义的行为: 除了可以修改任何声明mutable的类成员(7.1.1)外,任何在其生命周期(3.8)期间修改const对象的尝试都会导致未定义的行为。 请注意,当构造函数调用完成时(3.8 / 1),对象的生存期开始。 Yes, it is undefined behavior, as per 7.1.5.1/4: Except that any class member declared mutable (7.1.1) can be modified, any atte ...