Sock: connect: permission denied dial unix /var/run/docker.Docker¶. You should call your mom.”Recently, I have tried to start a MariaDB container with a volume mounted to it. Even my dad’s like, “what’s Docker? I saw someone twitter about it on the Facebook. After upgrading to Docker for Mac v2.5.0.1 interacting with the host's /var/run/docker.sock requires sudo / root.Have you heard of Docker? You probably have—everybody’s talking about it. This was possible with Docker for Mac v2.3. Mounting the /var/run/docker.sock into a container, where the container's user has the docker group should allow that user to run docker commands against the hosts docker daemon.
![]() Docker on OS XHere’s the thing about OS X: it’s not Linux. Here’s how it looks:The laptop is running both the client and the server, thus making it the Docker host. Docker on LinuxSuppose we want to run containers directly on our Linux laptop. The host can be any machine—your laptop, a server in the Cloud™, etc—but, because Docker uses features only available to Linux, that machine must be running Linux (more specifically, the Linux kernel). Docker /Var/Run/Docker.Sock Mount Install Dat SoftwareThis also means boot2docker, not OS X, is the Docker host, not OS X.Make sense? Let’s install dat software. Boot2docker is a “lightweight Linux distribution made specifically to run Docker containers.” Spoiler alert: you’re going to run it in a VM on your Mac.Here’s a diagram of how we’ll use boot2docker:We’ll run the Docker client natively on OS X, but the Docker server will run inside our boot2docker VM. We still need to have Linux running somewhere.Enter boot2docker. Do like it says: > boot2docker up1 13:51:29 Waiting for VM to be started.1 13:51:51 Trying to get IP one more time1 13:51:51 To connect the Docker client to the Docker daemon, please set:1 13:51:51 export DOCKER_HOST=tcp://192.168.59.103:2375Step 4: Set the DOCKER_HOST environment variableThe Docker client assumes the Docker host is the current machine. Type `boot2docker up` to start the VM.Next, we can start up the VM. > brew updateFirst, we need to initialize boot2docker (we only have to do this once): > boot2docker init1 13:49:33 Downloading boot2docker ISO image.1 13:49:50 Done. I prefer homebrew because I like to manage my environment from the command line. Step 2: Install Docker and boot2dockerYou have two choices: the offical package from the Docker site or homebrew. You don’t need my help with that. Common ProblemsWe have a “working” Docker installation. The VM runs the Docker server, and we’re communicating with it using the Docker client on OS X.Bueno. To recap: we’ve set up a VirtualBox VM running boot2docker. Step 5: ProfitSockets: Great success. You probably want to add that environment variable to your shell config. Dstwo mac emulatorWhat’s the deal with that nonsense.Turns out, boot2docker ip outputs the IP address to standard output and The VM's Host only interface IP address is: to standard error. We got the web page, but we got The VM’s Host only interface IP address is:, too. It’s our VM.Boot2docker comes with a command to get the IP address of the VM: > boot2docker ipThe VM’s Host only interface IP address is: 192.168.59.103Let’s plug that into our curl command: > curl $(boot2docker ip):49153The VM’s Host only interface IP address is:Success! Sort of. If we were on Linux, our Docker host would be localhost, but we aren’t, so it’s not. Our new container has the unique identifier 0092c03e1eba.Verify the container is running: > docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES0092c03e1eba nginx:latest nginx 44 seconds ago Up 41 seconds 0.0.0.0:49153->80/tcp webUnder the PORTS heading, we can see our container exposes port 80, and Docker has forwarded this port from the container to a random port, 49153, on the host.Let’s curl our new site: > curl localhost:49153Curl: (7) Failed connect to localhost:49153 Connection refusedRemember, Docker is mapping port 80 to port 49153 on the Docker host. Problem #1: Port ForwardingThe Problem: Docker forwards ports from the container to the host, which is boot2docker, not OS X.Let’s start a container running nginx: > docker run -d -P -name web nginx0092c03e1eba5da5ccf9f858cf825af307aa24431978e75c1df431e22e03b4c3This command starts a new container as a daemon ( -d), automatically forwards the ports specified in the image ( -P), gives it the name ‘web’ ( -name web), and uses the nginx image. ![]() You should really just use the VM’s IP address mmkay. Here’s a bash script, taken from here, to do that:VBoxManage modifyvm "boot2docker-vm" -natpf1 "tcp-port$i,tcp,$i,$i" VBoxManage modifyvm "boot2docker-vm" -natpf1 "udp-port$i,udp,$i,$i" By doing this, Docker will forward port 80 to, say, port 49153 on the VM, and VirtualBox will forward port 49153 from the VM to localhost. Fair warning.Bonus Alternate Solution: Forward all of Docker’s ports from the VM to localhost.If you really want to access your Docker containers via localhost, you can forward all of the ports in Docker’s port range from the VM to localhost. If you’re only using one VM, it should always get the same IP, but if you’re VMing on the reg, it could change. Boot2docker doesn’t know anything about files on OS X.The Solution: Mount OS X’s /Users directory into the VM.By mounting /Users into our VM, boot2docker gains a /Users volume that points to the same directory on OS X. Docker is trying to mount /Users/Chris/web from the host into our container, but the host is boot2docker, not OS X. The problem, again, is our VM. Let’s try it out.First, let’s create a new directory and add an index.html: > cd /Users/Chris(Make sure to replace /Users/Chris with your own path).Next, we’ll start another nginx container, this time mounting our new directory inside the container at nginx’s web root: > docker run -d -P -v /Users/Chris/web:/usr/local/nginx/html -name web nginx485386b95ee49556b2cf669ea785dffff2ef3eb7f94d93982926579414eec278We need the port number for port 80 on our container: > docker port web 80Let’s try to curl our new page: > curl dockerhost:49154Well, that didn’t work. For example, we could start an nginx container that serves files from the host using a volume. Volumes are one way to give your container access to resources in the outside world.
0 Comments
Leave a Reply. |
AuthorMatthew ArchivesCategories |