Skip to content
Open
52 changes: 27 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,7 @@
# docl

## Prerequisites
* Docker should be installed and running. It should be started with root privileges because containers are started with the `--privileged` flag.

### TCP socket configuration
Docker should be also accessible on tcp along with the default unix socket, to achieve this:
* Create the file `/etc/systemd/system/docker.service.d/startup_options.conf` (if required create the parent directory)
* Input the below contents:
```
# /etc/systemd/system/docker.service.d/override.conf
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H unix:// -H tcp://0.0.0.0:2375
```
* Run:
```
sudo systemctl daemon-reload
sudo systemctl restart docker.service
```
* Validate docker listens on the unix socket and the tcp socket (both commands should print the installed docker version):
```
docker -H tcp://127.0.0.1:2375 version
sudo docker version
```
* Docker should be installed and running.

## Installation
`docl` can be installed by running
Expand All @@ -37,7 +16,7 @@ Note that `cloudify` CLI is a dependency of `docl`.

Run `docl init` and supply the different configuration options based on your setup.
* `--ssh-key-path` should point to a private key that will have access to created containers.
* `--docker-host` should point to the docker endpoint.
* `--docker-host` should point to the docker endpoint, or empty for the default socket.
* `--source-root` should point to the root directory in which all cloudify related projects are cloned. This is used for mounting code
from the host machine to the relevant manager directories.
* (Optional) `[-m]|[--manager-image-docker-tag]` Default Docker image tag to use (for example `docl run` with no parameters will use this value to run a local image with this tag).
Expand Down Expand Up @@ -186,8 +165,9 @@ docl cp /tmp/some_file :/root/configuration.txt
```


### Troubleshooting
## Troubleshooting

### Docker version
Ubuntu Trusty 14.04 may fail with an error containing:

```bash
Expand All @@ -198,4 +178,26 @@ In this case the solution is to downgrade docker:

```bash
apt-get -y install --force-yes docker-ce=18.06.1~ce~3-0~ubuntu
```
```

### TCP socket configuration
Docker can be also accessible on tcp along with the default unix socket (note that this is not required since 0.51).
To achieve this:
* Create the file `/etc/systemd/system/docker.service.d/startup_options.conf` (if required create the parent directory)
* Input the below contents:
```
# /etc/systemd/system/docker.service.d/override.conf
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H unix:// -H tcp://0.0.0.0:2375
```
* Run:
```
sudo systemctl daemon-reload
sudo systemctl restart docker.service
```
* Validate docker listens on the unix socket and the tcp socket (both commands should print the installed docker version):
```
docker -H tcp://127.0.0.1:2375 version
sudo docker version
```
73 changes: 61 additions & 12 deletions docl/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
def init(manager_image_url=constants.MANAGER_IMAGE_URL,
manager_image_docker_tag=constants.MANAGER_IMAGE_DOCKER_TAG,
manager_image_commit_sha_url=constants.MANAGER_IMAGE_COMMIT_SHA_URL,
docker_host=constants.DOCKER_HOST,
docker_host=None,
ssh_key_path=constants.SSH_KEY,
clean_image_docker_tag=constants.CLEAN_IMAGE_DOCKER_TAG,
source_root=constants.SOURCE_ROOT,
Expand Down Expand Up @@ -248,9 +248,12 @@ def remove_image(tag=None):
@command
@argh.arg('-l', '--label', action='append')
@argh.arg('-n', '--name')
def run(mount=False, label=None, name=None, details_path=None, tag=None):
def run(mount=False, label=None, name=None, details_path=None, tag=None,
mount_docker=False):
docker_tag = tag or configuration.manager_image_docker_tag
volumes = _build_volumes() if mount else None
volumes = _build_volumes() if mount else []
if mount_docker:
volumes += _mount_docker_volumes()
container_id, container_ip = _run_container(docker_tag=docker_tag,
volume=volumes,
label=label,
Expand All @@ -263,6 +266,34 @@ def run(mount=False, label=None, name=None, details_path=None, tag=None):
_update_container(container_id, container_ip)


def _mount_docker_volumes():
"""Volume configuration for mounting the docker socket.

This allows mounting the docker socket (if specified in docker_host,
or /var/run/docker.sock by default).
Systemd will clean /var/run on startup, so this mounts the socket
in /tmp, and provides a systemd unit that links that to /var/run.

Returns a list of arguments to be used with --volume in docker run.
"""
docker_host = configuration.docker_host or 'unix:///var/run/docker.sock'
if not docker_host.startswith('unix:'):
raise argh.CommandError('Mounting the docker socket is only possible '
'when docker_host is a unix socket (and '
'starts with unix://), but was {0}'
.format(configuration.docker_host))

docker_host = docker_host.replace('unix://', '')
copy_service_path = ('/etc/systemd/system/multi-user.target.wants'
'/link-docker-socket.service')

return [
'{0}:{1}'.format(resources.DIR / 'link-docker-socket.service',
copy_service_path),
'{0}:/tmp/docker.sock'.format(docker_host)
]


def _retry(func, *args, **kwargs):
for _ in range(300):
try:
Expand Down Expand Up @@ -331,22 +362,40 @@ def _update_container(container_id, container_ip):


@command
def install_docker(version=None, container_id=None):
def install_docker(version=None, container_id=None, sudo_user='cfyuser'):
container_id = container_id or work.last_container_id
if sudo_user:
_allow_docker_sudo(container_id, sudo_user)
try:
quiet_docker('exec', container_id, *'which docker'.split(' '))
quiet_docker('exec', container_id, 'which', 'docker')
logger.info('Docker already installed on container. Doing nothing')
return
except sh.ErrorReturnCode:
pass
cp(resources.DIR / 'docker.repo', ':/etc/yum.repos.d/docker.repo',
container_id=container_id)
version = version or _get_docker_version()
install_docker_command = 'yum install -y -q docker-engine-{}'.format(
install_docker_command = 'yum install -y -q docker-client-{}'.format(
version)
docker('exec', container_id, *install_docker_command.split(' '))


def _allow_docker_sudo(container_id, user):
"""Allow user to sudo docker.

To use docker inside of the container, allow the user to sudo docker.
The usual way of creating a docker group is not very useful because
the GID would be different than on the host, and so it still wouldn't
allow the user to access the socket or other docker things.
"""
sudoers_line = '{0} ALL=(root) NOPASSWD:/usr/bin/docker'.format(user)
sudoers_file = '/etc/sudoers.d/{0}'.format(user)
docker('exec', container_id, 'touch', sudoers_file)
# sed command to add the line to the end of the file
docker('exec', container_id, 'sed', '-ie',
'$a{0}'.format(sudoers_line), sudoers_file)


def _get_docker_version():
try:
version = quiet_docker.version('-f', '{{.Client.Version}}').strip()
Expand Down Expand Up @@ -553,12 +602,12 @@ def _run_container(docker_tag, volume=None, label=None, name=None,
docker_args.append('--name={}'.format(name))
container_id = quiet_docker.run(
*docker_args +
['--hostname={}'.format(hostname)] +
['--expose={}'.format(e) for e in expose] +
['--publish={}'.format(p) for p in publish] +
['--volume={}'.format(v) for v in volume] +
['--label={}'.format(l) for l in label] +
[docker_tag]).strip()
['--hostname={}'.format(hostname)] +
['--expose={}'.format(e) for e in expose] +
['--publish={}'.format(p) for p in publish] +
['--volume={}'.format(v) for v in volume] +
['--label={}'.format(l) for l in label] +
[docker_tag]).strip()
container_ip = _extract_container_ip(container_id)
work.save_last_container_id_and_ip(container_id=container_id,
container_ip=container_ip)
Expand Down
1 change: 0 additions & 1 deletion docl/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# limitations under the License.

CONFIG_YAML = 'config.yaml'
DOCKER_HOST = 'fd://'
SSH_KEY = '~/.ssh/.id_rsa'
CLEAN_IMAGE_DOCKER_TAG = 'cloudify/centos:7'
MANAGER_IMAGE_DOCKER_TAG = 'cloudify/centos-manager:7'
Expand Down
5 changes: 5 additions & 0 deletions docl/install_rpm_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ def start(invalidate_cache=False, no_progress=False):

def get_host():
host = configuration.docker_host
if not host or host.startswith('unix:'):
logger.warning('TCP docker_host address not provided (docker_host: '
'`{0}`). Using 127.0.0.1 instead to serve the manager '
'install RPM'.format(host))
return '127.0.0.1'
if '://' in host:
host = host.split('://')[1]
if ':' in host:
Expand Down
9 changes: 9 additions & 0 deletions docl/resources/link-docker-socket.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[Unit]
Description=Link docker socket to the default location

[Service]
Type=oneshot
ExecStart=/usr/bin/ln -s /tmp/docker.sock /var/run/docker.sock

[Install]
WantedBy=multi-user.target
4 changes: 3 additions & 1 deletion docl/subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ def bake(cmd):


def docker_proxy(quiet=False):
result = sh.docker.bake('-H', configuration.docker_host)
result = sh.docker
if configuration.docker_host:
result = result.bake('-H', configuration.docker_host)
if not quiet:
result = bake(result)
return result
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

setup(
name='docl',
version='0.51',
version='0.52',
author='Cloudify',
author_email='cosmo-admin@cloudify.co',
packages=[
Expand Down