Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Docker socket within container

Tags:

docker

I am attempting to create a container that can access the host docker remote API via the docker socket file (host machine - /var/run/docker.sock).

The answer here suggests proxying requests to the socket. How would I go about doing this?

like image 909
Desmond Morris Avatar asked Mar 03 '14 00:03

Desmond Morris


People also ask

How do I access the host's Docker daemon socket from within a container?

If you need to access docker on the host from inside a container, you can simply expose the Docker socket inside the container using a host mount ( -v /host/path:/container/path on the docker run command line).

How do I run Docker commands inside a container?

Running Commands in an Alternate Directory in a Docker Container. To run a command in a certain directory of your container, use the --workdir flag to specify the directory: docker exec --workdir /tmp container-name pwd.

Where is my Docker socket path?

By default, a unix domain socket (or IPC socket) is created at /var/run/docker. sock , requiring either root permission, or docker group membership. If you need to access the Docker daemon remotely, you need to enable the tcp Socket.

How do I access a service inside a container?

To access the service from inside the container you need the port that particular host is listening to as no matter where the service is running we need to access it through the host node. If you have a service running on some other port you can access it via 172.17. 42.1:5432.


1 Answers

I figured it out. You can simply pass the the socket file through the volume argument

docker run -v /var/run/docker.sock:/container/path/docker.sock 

As @zarathustra points out, this may not be the greatest idea however. See: https://www.lvh.io/posts/dont-expose-the-docker-socket-not-even-to-a-container/

like image 72
Desmond Morris Avatar answered Oct 02 '22 11:10

Desmond Morris