Installing a web application in a Docker container

2020-07-07 11:19发布

问题:

I'm trying to install a web application in a Docker container.

I'm using OS X Yosemite version 10.10.1

I've been following the tutorial on Dockerizing a Node.js Web App here: https://docs.docker.com/examples/nodejs_web_app/

  1. I downloaded and set up the boot2docker osx-installer at: github.com/boot2docker/osx-installer/releases/tag/v1.4.1

  2. I entered these commands in the console in the following order:

    $ boot2docker init $ boot2docker start $ (boot2docker shellinit)

  3. Then I set the environmental variable, as instructed by the terminal, with the following commands:

    $ export DOCKER_TLS_VERIFY=1 $ export DOCKER_HOST=tcp://192.168.59.103:2376 $ export DOCKER_CERT_PATH=/Users/myusername/.boot2docker/certs/boot2docker-vm

At this point, everything seemed to be working alright.

  1. Then, following the tutorial on Dockerizing a Node.js app, I got to the point here: https://docs.docker.com/examples/nodejs_web_app/#building-your-image, where you enter the command:

    $ sudo docker build -t <your username>/centos-node-hello .

  2. Then is asks for a password, and then after entering it I keep getting this error message:

    Sending build context to Docker daemon FATA[0000] Post http:///var/run/docker.sock/v1.16/build?rm=1&t=myusername%2Fcentos-node- hello: dial unix /var/run/docker.sock: no such file or directory

Here are my env variables:

`TERM_PROGRAM=Apple_Terminal
TERM=xterm-256color
SHELL=/bin/bash
TMPDIR=/var/folders/vb/vmd78vk57mqd6fv7sqb544pc0000gn/T/
DOCKER_HOST=tcp://192.168.59.103:2376
Apple_PubSub_Socket_Render=/private/tmp/com.apple.launchd.BBIW525XNA/Render
TERM_PROGRAM_VERSION=343
OLDPWD=/Users/myusername/Desktop
TERM_SESSION_ID=FAD1F3F1-35DF-4183-A47A-08749EAFEACB
USER=myusername
SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.A6ZjHwx8eq/Listeners
__CF_USER_TEXT_ENCODING=0x1F5:0x0:0x0
DOCKER_TLS_VERIFY=1
PATH=/Users/myusername/.rbenv/shims:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin
PWD=/Users/myusername/Desktop/docker
LANG=en_CA.UTF-8
vXPC_FLAGS=0x0
XPC_SERVICE_NAME=0
DOCKER_CERT_PATH=/Users/myusername/.boot2docker/certs/boot2docker-vm
HOME=/Users/myusername
SHLVL=1
LOGNAME=myusername
DISPLAY=/private/tmp/com.apple.launchd.IesbS7VdCy/org.macosforge.xquartz:0
SECURITYSESSIONID=186a5
_=/usr/bin/env`

My Docker version:

`Client version: 1.4.1
Client API version: 1.16
Go version (client): go1.3.3
Git commit (client): 5bc2ff8
OS/Arch (client): darwin/amd64
Server version: 1.4.1
Server API version: 1.16
Go version (server): go1.3.3
Git commit (server): 5bc2ff8`

Any help would be really appreciated. Thanks

回答1:

Short answer for your specific case: don't use sudo.

Here's why:

When you use boot2docker, the docker daemon is running in a VM, essentially on a different machine. To communicate with a docker daemon on another machine, you must use the TLS connection (over HTTPS to the daemon's REST API). That's why you had to set up DOCKER_HOST and DOCKER_CERT_PATH. Since the daemon is running on a different machine, you don't need to be root to communicate with it because outgoing http connections don't require root, unlike trying to talk with the unix:///var/run/docker.sock socket.

But when you run sudo docker, suddenly you're a different user, you're root. The environment variables you set as your regular account are not set. So the docker CLI uses its default communication method: the unix socket. The socket isn't there, because the daemon is running on a different machine (the boot2docker vm). Hence the error message.