4

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 to exit the container, and I have to exit twice (as if I changed users).

What am I doing wrong?

1 Answer 1

3

If you are trying to run the containers alongside one another (not container inside container), you should mount the docker socket from the host system and execute commands to other containers that way:

    docker run --name containera \
      -v /var/run/docker.sock:/var/run/docker.sock \
      yourimage

With the the docker socket mounted you can control docker on the host system.

Sign up to request clarification or add additional context in comments.

3 Comments

Yup. That was it. You need to do two things: 1) mount the docker socket from the host system, and 2) install docker in the container. But now you don't have to worry about anything else (setting/running as users, etc) because the host is going to handle that for you. Cool. Thanks a lot.
You don't actually have to install docker inside the container if you don't mind using the docker API. You can directly communicate with the docker socket.
Mounting the docker socket does not work for me. Im using centos 7 inside docker and redhat 7 in host. Am i missing something?