├── .gitignore ├── AUTHORS.md ├── Dockerfile ├── LICENSE ├── Makefile ├── README.md ├── RELEASE.md ├── image-version.sh └── wrapdocker /.gitignore: -------------------------------------------------------------------------------- 1 | # IntelliJ 2 | .idea -------------------------------------------------------------------------------- /AUTHORS.md: -------------------------------------------------------------------------------- 1 | Authors 2 | ------- 3 | 4 | Name | Email | Status 5 | -----|-------|-------- 6 | Karl Isenberg | karl@mesosphere.com | Current Maintainer 7 | Stefan Schimanski | sttts@mesosphere.com | Current Maintainer 8 | 9 | 10 | Prior Artists 11 | ------------- 12 | 13 | This project was forked from [Docker-in-Docker](https://github.com/jpetazzo/dind), which had the following additional authors at the time of forking: 14 | 15 | Name | Status 16 | -----|-------- 17 | Jérôme Petazzoni | Author 18 | Aaron Huslage | Contributor 19 | Amit | Contributor 20 | Antoine Roy-Gobeil | Contributor 21 | Carl Boettiger | Contributor 22 | Charlie Lewis | Contributor 23 | David Gageot | Contributor 24 | Eike Waldt | Contributor 25 | Esben Haabendal | Contributor 26 | Imran Hayder | Contributor 27 | James Harris | Contributor 28 | Johan Haleby | Contributor 29 | Logan Koester | Contributor 30 | Maru Newby | Contributor 31 | Michael A. Smith | Contributor 32 | Michael Crosby | Contributor 33 | Michael Elsdörfer | Contributor 34 | Rodrigo B. de Oliveira | Contributor 35 | Steffen Roegner | Contributor 36 | Tianon Gravi | Contributor 37 | Tim Dettrick | Contributor 38 | Tim Kretschmer | Contributor 39 | Timothy Hobbs | Contributor 40 | Tom Arnfeld | Contributor 41 | Tony Hesjevik | Contributor 42 | Zach Latta | Contributor 43 | Lajos Papp | Contributor 44 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM mesosphere/mesos-slave:1.4.1 2 | MAINTAINER Mesosphere 3 | 4 | RUN apt-get update -qq && \ 5 | DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -qqy \ 6 | apt-transport-https \ 7 | ca-certificates \ 8 | curl \ 9 | lxc \ 10 | iptables \ 11 | ipcalc \ 12 | linux-image-extra-virtual \ 13 | software-properties-common \ 14 | dmsetup \ 15 | && \ 16 | apt-get clean 17 | 18 | # Install specific Docker version 19 | ENV DOCKER_VERSION 17.05.0~ce-0~ubuntu-xenial 20 | RUN curl -fsSL 'https://sks-keyservers.net/pks/lookup?op=get&search=0xee6d536cf7dc86e2d7d56f59a178ac6c6238f52e' | apt-key add - && \ 21 | add-apt-repository \ 22 | "deb https://packages.docker.com/1.12/apt/repo/ \ 23 | ubuntu-$(lsb_release -cs) \ 24 | main" \ 25 | && \ 26 | apt-get update -qq && \ 27 | DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -qqy \ 28 | docker-engine=${DOCKER_VERSION} \ 29 | && \ 30 | apt-get clean 31 | 32 | ENV WRAPPER_VERSION 0.4.0 33 | COPY ./wrapdocker /usr/local/bin/ 34 | 35 | ENTRYPOINT ["wrapdocker"] 36 | CMD ["mesos-slave"] 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | all: image tag 2 | .PHONY: all 3 | 4 | help: 5 | @echo 'Goals:' 6 | @echo ' image - build a docker image' 7 | @echo ' tag - tag the latest image with a version based on its contents' 8 | @echo ' push - push a docker image to the locally configured docker repository' 9 | @echo ' release - create release tag from latest version' 10 | .PHONY: help 11 | 12 | ORG=mesosphere 13 | REPO=$(shell git rev-parse --show-toplevel | xargs basename) 14 | 15 | define image-version 16 | $(shell ./image-version.sh "$(ORG)/$(REPO):latest") 17 | endef 18 | 19 | version: 20 | $(eval VERSION=$(image-version)) 21 | .PHONY: version 22 | 23 | push: version 24 | docker push $(ORG)/$(REPO):$(VERSION) 25 | .PHONY: push 26 | 27 | image: 28 | docker build -t $(ORG)/$(REPO):latest . 29 | .PHONY: image 30 | 31 | tag: version 32 | docker tag $(ORG)/$(REPO):latest $(ORG)/$(REPO):$(VERSION) 33 | .PHONY: tag 34 | 35 | release: version 36 | git tag -a "$(VERSION)" -m '$(REPO) version $(VERSION)' 37 | .PHONY: release 38 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Mesos Slave Docker-in-Docker (dind) 2 | 3 | Mesos-Slave that runs in a Ubuntu-based Docker container. 4 | 5 | Launches tasks using the included Docker Engine, rather than the host's Docker Engine. 6 | 7 | ## Features 8 | 9 | - Runs Mesos tasks inside the container (instead of in the parent Docker env). 10 | - Mesos tasks (in docker containers) are stopped when the mesos-slave-dind container is stopped. 11 | - Supports OverlayFS (new hotness) and AUFS (legacy) 12 | - Allocates disk space (via loop mount) to allow mounting AUFS on AUFS 13 | - Allocates IP space on the parent Docker's network, making docker-in-docker containers IP accessible from the host. 14 | 15 | ## Networking 16 | 17 | There are two networking mode options: default & bridged. 18 | 19 | By default, the inner docker daemon gets its own network. These inner container IPs will not be accessible by the host. 20 | 21 | Bridged networking mode can be enabled by populating `DOCKER_NETWORK_OFFSET` (env var). In bridged networking mode, containers launched by the inner docker daemon will be given IPs on the host docker network. This requires reserving a range of IPs on the host docker network for each mesos-slave-dind container by specifying an offset (`DOCKER_NETWORK_OFFSET`) and a size (`DOCKER_NETWORK_SIZE`). The offset should be specific to the container, and the range (offset <-> offset + size - 1) should not overlap with other reserved ranges. 22 | 23 | ## Required Docker Parameters 24 | 25 | - `--privileged=true` - Provides access to cgroups 26 | 27 | ## Recommended Environment Variables 28 | 29 | - **MESOS_CONTAINERIZERS** - Include docker to enable running tasks as docker containers. Ex: `docker,mesos` 30 | - **MESOS_RESOURCES** - Specify resources to avoid oversubscribing via auto-detecting host resources. Ex: `cpus:4;mem:1280;disk:25600;ports:[21000-21099]` 31 | - **DOCKER_NETWORK_OFFSET** - Specify an IP offset to give each mesos-slave-dind container (enables bridged network mode). Ex: `0.0.1.0` (slave A), `0.0.2.0` (slave B) 32 | - **DOCKER_NETWORK_SIZE** - Specify a CIDR range to apply to the above offset (default=`24`). 33 | - **VAR_LIB_DOCKER_SIZE** - Specify the max size (in GB) of the loop device to be mounted at /var/lib/docker (default=`5`). This is only used if OverlayFS is not supported by the kernel or the parent docker is configured to use AUFS. 34 | 35 | Source: 36 | 37 | Inspiration: 38 | 39 | ## License 40 | 41 | Copyright 2015-2018 [The Mesos Slave Docker-in-Docker Authors](./AUTHORS.md) 42 | 43 | Licensed under the Apache License, Version 2.0 (the "License"); 44 | you may not use this file except in compliance with the License. 45 | You may obtain a copy of the License at 46 | 47 | http://www.apache.org/licenses/LICENSE-2.0 48 | 49 | Unless required by applicable law or agreed to in writing, software 50 | distributed under the License is distributed on an "AS IS" BASIS, 51 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 52 | See the License for the specific language governing permissions and 53 | limitations under the License. 54 | -------------------------------------------------------------------------------- /RELEASE.md: -------------------------------------------------------------------------------- 1 | ## Release Process 2 | 3 | 1. Modify master branch 4 | 5 | 1. Make docker image 6 | 7 | ``` 8 | $ make image 9 | docker build -t mesosphere/mesos-slave-dind:latest . 10 | ... 11 | Successfully built 1e0c8618d736 12 | ``` 13 | 14 | 1. Tag docker image 15 | 16 | ``` 17 | $ make tag 18 | docker tag mesosphere/mesos-slave-dind:latest mesosphere/mesos-slave-dind:0.2.3_mesos-0.24.0_docker-1.7.1_ubuntu-14.04.3 19 | ``` 20 | 21 | 1. Validate release image (e.g. with mesos-compose-dind) 22 | 23 | 1. Create release branch 24 | 25 | ``` 26 | $ git checkout -b release-${VERSION} 27 | ``` 28 | 29 | (VERSION is the docker tag produced by the previous step) 30 | 31 | 1. Push release branch 32 | 33 | ``` 34 | $ git push --set-upstream origin release-${VERSION} 35 | ``` 36 | 37 | 1. Create & merge release branch to master branch 38 | 39 | 1. Switch to updated master branch 40 | 41 | ``` 42 | $ git checkout master 43 | $ git pull 44 | ``` 45 | 46 | 1. Tag git release 47 | 48 | ``` 49 | $ make release 50 | git tag -a "${VERSION}" -m "mesos-slave-dind version ${VERSION}" 51 | ``` 52 | 53 | 1. Push git tag 54 | 55 | ``` 56 | $ git push --follow-tags 57 | Counting objects: 1, done. 58 | Writing objects: 100% (1/1), 203 bytes | 0 bytes/s, done. 59 | Total 1 (delta 0), reused 0 (delta 0) 60 | To git@github.com:mesosphere/mesos-slave-dind.git 61 | * [new tag] 0.2.3_mesos-0.24.0_docker-1.7.1_ubuntu-14.04.3 -> 0.2.3_mesos-0.24.0_docker-1.7.1_ubuntu-14.04.3 62 | ``` 63 | 64 | 1. Push docker image 65 | 66 | ``` 67 | $ make push 68 | docker push mesosphere/mesos-slave-dind:0.2.3_mesos-0.24.0_docker-1.7.1_ubuntu-14.04.3 69 | ``` 70 | 71 | 72 | To create releases with different docker versions, you will need to create a new release, modify the Dockerfile, and make a commit to the branch before you can make the new docker image. This requires knowing the release version before producing an image, which is why it's not automated. Hopefully we won't need to create too many releases with multiple docker versions. 73 | -------------------------------------------------------------------------------- /image-version.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Prints the image version to be used for the Docker image tag 4 | 5 | image=${1:-} 6 | [ -z "${image}" ] && echo "No image supplied" && exit 1 7 | 8 | # Get image version 9 | wrapper_version=$(docker run --rm --entrypoint bash "${image}" -c 'echo ${WRAPPER_VERSION}') 10 | 11 | # Get mesos-slave version 12 | mesos_version=$(docker run --rm --entrypoint bash "${image}" -c "mesos-slave --version | tr -s ' ' | cut -d ' ' -f 2") 13 | 14 | # Get docker version from DOCKER_VERSION env variable 15 | docker_version=$(docker run --rm --entrypoint bash "${image}" -c 'echo "${DOCKER_VERSION}" | cut -d "-" -f 1 | cut -d "~" -f 1') 16 | 17 | # Get ubuntu version (with patch) 18 | ubuntu_version=$(docker run --rm --entrypoint bash "${image}" -c "lsb_release -sd | tr -s ' ' | cut -d ' ' -f 2") 19 | 20 | echo "${wrapper_version}_mesos-${mesos_version}_docker-${docker_version}_ubuntu-${ubuntu_version}" 21 | -------------------------------------------------------------------------------- /wrapdocker: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Copyright 2013-2015 Jérôme Petazzoni, Johan Haleby, lalyos, James Harris, 4 | # Michael Elsdörfer, Tony Hesjevik, Esben Haabendal, 5 | # Michael A. Smith, Stefan Schimanski, Karl Isenberg 6 | # Original source: https://github.com/jpetazzo/dind 7 | # Modified to add docker network reservations. 8 | 9 | # Ensure that all nodes in /dev/mapper correspond to mapped devices currently loaded by the device-mapper kernel driver 10 | dmsetup mknodes 11 | 12 | # First, make sure that cgroups are mounted correctly. 13 | CGROUP=/sys/fs/cgroup 14 | : {LOG:=stdio} 15 | 16 | if ! [[ -d "${CGROUP}" ]]; then 17 | mkdir "${CGROUP}" 18 | fi 19 | 20 | if ! mountpoint -q "${CGROUP}"; then 21 | if ! mount -n -t tmpfs -o uid=0,gid=0,mode=0755 cgroup "${CGROUP}"; then 22 | echo "Could not make a tmpfs mount. Did you use --privileged?" 23 | exit 1 24 | fi 25 | fi 26 | 27 | if [[ -d /sys/kernel/security ]] && ! mountpoint -q /sys/kernel/security; then 28 | if ! mount -t securityfs none /sys/kernel/security; then 29 | echo "Could not mount /sys/kernel/security." 30 | echo "AppArmor detection and --privileged mode might break." 31 | fi 32 | fi 33 | 34 | # Mount the cgroup hierarchies exactly as they are in the parent system. 35 | for SUBSYS in $(cut -d: -f2 /proc/1/cgroup); do 36 | if ! [[ -d "${CGROUP}/${SUBSYS}" ]]; then 37 | mkdir "${CGROUP}/${SUBSYS}" 38 | fi 39 | if ! mountpoint -q "${CGROUP}/${SUBSYS}"; then 40 | mount -n -t cgroup -o "${SUBSYS}" cgroup "${CGROUP}/${SUBSYS}" 41 | fi 42 | 43 | # The two following sections address a bug which manifests itself 44 | # by a cryptic "lxc-start: no ns_cgroup option specified" when 45 | # trying to start containers within a container. 46 | # The bug seems to appear when the cgroup hierarchies are not 47 | # mounted on the exact same directories in the host, and in the 48 | # container. 49 | 50 | # Named, control-less cgroups are mounted with "-o name=foo" 51 | # (and appear as such under /proc//cgroup) but are usually 52 | # mounted on a directory named "foo" (without the "name=" prefix). 53 | # Systemd and OpenRC (and possibly others) both create such a 54 | # cgroup. To avoid the aforementioned bug, we symlink "foo" to 55 | # "name=foo". This shouldn't have any adverse effect. 56 | 57 | if [[ "${SUBSYS}" =~ ^name=(.*)$ ]]; then 58 | echo "Linking ${SUBSYS} to ${CGROUP}/${BASH_REMATCH[1]}" 59 | ln -s "${SUBSYS}" "${CGROUP}/${BASH_REMATCH[1]}" 60 | fi 61 | 62 | # Likewise, on at least one system, it has been reported that 63 | # systemd would mount the CPU and CPU accounting controllers 64 | # (respectively "cpu" and "cpuacct") with "-o cpuacct,cpu" 65 | # but on a directory called "cpu,cpuacct" (note the inversion 66 | # in the order of the groups). This tries to work around it. 67 | 68 | if [[ "${SUBSYS}" == "cpuacct,cpu" ]]; then 69 | echo "Linking ${SUBSYS} to ${CGROUP}/cpu,cpuacct" 70 | ln -s "${SUBSYS}" "${CGROUP}/cpu,cpuacct" 71 | fi 72 | done 73 | 74 | # Note: as I write those lines, the LXC userland tools cannot setup 75 | # a "sub-container" properly if the "devices" cgroup is not in its 76 | # own hierarchy. Let's detect this and issue a warning. 77 | if ! grep -q :devices: /proc/1/cgroup; then 78 | echo "WARNING: the 'devices' cgroup should be in its own hierarchy." 79 | fi 80 | if ! grep -qw devices /proc/1/cgroup; then 81 | echo "WARNING: it looks like the 'devices' cgroup is not mounted." 82 | fi 83 | 84 | # Now, close extraneous file descriptors. 85 | pushd /proc/self/fd >/dev/null 86 | for FD in *; do 87 | case "$FD" in 88 | # Keep stdin/stdout/stderr 89 | [012]) 90 | ;; 91 | # Nuke everything else 92 | *) 93 | eval exec "$FD>&-" 94 | ;; 95 | esac 96 | done 97 | popd >/dev/null 98 | 99 | # find supported filesystem to use for docker image mounts 100 | if grep -q overlay /proc/filesystems; then 101 | STORAGE_FS=overlay 102 | elif grep -q aufs /proc/filesystems; then 103 | STORAGE_FS=aufs 104 | else 105 | echo "No supported filesystem found (aufs, overlay)" 106 | exit 1 107 | fi 108 | 109 | # find filesystem below /var/lib/docker 110 | STORAGE_DIR="/var/lib/docker" 111 | mkdir -p "${STORAGE_DIR}" 112 | 113 | # Smoke test the overlay filesystem: 114 | # 1. create smoke dir in the storage dir being mounted (possibly on an overlay fs) 115 | # 2. try to mount an overlay fs on top of the smoke dir 116 | # 3. try to write a file in the overlay mount 117 | # 4. if that fails, default to using AUFS 118 | # 119 | # Rational: There are kernels with broken overlay-over-overlay support (4.2 and 120 | # probably 3.19). On those it's possible to mount an overlay in an overlay, but 121 | # writing to a file results in a "No device" error. 122 | if [[ "${STORAGE_FS}" == "overlay" ]]; then 123 | D="${STORAGE_DIR}/smoke" 124 | mkdir -p "${D}/upper" "${D}/lower" "${D}/work" "${D}/mount" 125 | 126 | mount -t overlay overlay -o"lowerdir=${D}/lower,upperdir=${D}/upper,workdir=${D}/work" "${D}/mount" && 127 | echo foo > "${D}/mount/probe" || STORAGE_FS=aufs 128 | 129 | umount -f "${D}/mount" || true 130 | rm -rf "${D}" || true 131 | fi 132 | 133 | # For AUFS, create an ext3 loop device as an intermediary layer. 134 | # The max size of the loop device is $VAR_LIB_DOCKER_SIZE in GB (default=5). 135 | if [[ "${STORAGE_FS}" == "aufs" ]]; then 136 | STORAGE_FILE="/data/docker" 137 | VAR_LIB_DOCKER_SIZE=${VAR_LIB_DOCKER_SIZE:-5} 138 | mkdir -p "$(dirname "${STORAGE_FILE}")" 139 | if [ ! -f "${STORAGE_FILE}" ]; then 140 | dd if=/dev/zero of="${STORAGE_FILE}" bs=1G seek=${VAR_LIB_DOCKER_SIZE} count=0 141 | echo y | mkfs.ext3 "${STORAGE_FILE}" 142 | fi 143 | mount -o loop "${STORAGE_FILE}" "${STORAGE_DIR}" 144 | fi 145 | 146 | # Tell Docker to use Overlay2 if the OS supports overlay (assuming Ubuntu Xenial and Docker >= 17) 147 | if [[ "${STORAGE_FS}" != "overlay" ]]; then 148 | STORAGE_FS=overlay2 149 | fi 150 | 151 | DOCKER_DAEMON_ARGS="${DOCKER_DAEMON_ARGS:-} --storage-driver=${STORAGE_FS}" 152 | 153 | # If a pidfile is still around (for example after a container restart), 154 | # delete it so that docker can start. 155 | rm -rf /var/run/docker.pid 156 | 157 | # Bridge the containerized docker network to the host docker network. 158 | # To enable this bridge, specify an IP offset for this container to use. 159 | # Other containers on the same host should have different offsets 160 | # such that the IPs within the range (offset <-> offset + size - 1) do not overlap. 161 | if [ ! -z "${DOCKER_NETWORK_OFFSET:-}" ]; then 162 | # create docker0 bridge manually and attach it to the veth interface eth0 163 | brctl addbr docker0 164 | brctl addif docker0 eth0 165 | ip link set docker0 up 166 | 167 | # move ip to the bridge and restore routing via the old gateway 168 | IP_CIDR=$(ip addr show eth0 | grep -w inet | awk '{ print $2; }') 169 | IP=$(echo $IP_CIDR | sed 's,/.*,,') 170 | NETWORK_SIZE=$(echo $IP_CIDR | sed 's,.*/,,') 171 | DEFAULT_ROUTE=$(ip route | grep default | sed 's/eth0/docker0/') 172 | 173 | ip addr del $IP_CIDR dev eth0 174 | ip addr add $IP_CIDR dev docker0 175 | ip route add $DEFAULT_ROUTE 176 | 177 | # compute a network for the containers to live in 178 | # by adding DOCKER_NETWORK_OFFSET to the current IP and cutting off 179 | # non-network bits according to DOCKER_NETWORK_SIZE 180 | DOCKER_NETWORK_SIZE=${DOCKER_NETWORK_SIZE:-24} 181 | NETWORK=$(ip route | grep docker0 | grep -v default | sed 's,/.*,,') 182 | 183 | IFS=. read -r i1 i2 i3 i4 <<< $IP 184 | IFS=. read -r n1 n2 n3 n4 <<< $NETWORK 185 | IFS=. read -r o1 o2 o3 o4 <<< $DOCKER_NETWORK_OFFSET 186 | IFS=. read -r w1 w2 w3 w4 <<< $(ipcalc $IP_CIDR | grep Wildcard | awk '{print $2;}') 187 | 188 | IP_PLUS_OFFSET=$(printf "%d.%d.%d.%d\n" \ 189 | "$(( n1 + ((i1 - n1 + o1) & w1) ))" \ 190 | "$(( n2 + ((i2 - n2 + o2) & w2) ))" \ 191 | "$(( n3 + ((i3 - n3 + o3) & w3) ))" \ 192 | "$(( n4 + ((i4 - n4 + o4) & w4) ))") 193 | 194 | FIXED_CIDR=$(ipcalc $IP_PLUS_OFFSET/$DOCKER_NETWORK_SIZE | grep Network | awk '{print $2;}') 195 | echo "Using network $FIXED_CIDR for docker containers" 196 | 197 | # let docker reuse the given IP. If you run more than one dind slave, add 198 | # --fixed-cidr=a.b.c.d/24 to DOCKER_DAEMON_ARGS with disjunct networks. 199 | DOCKER_DAEMON_ARGS="${DOCKER_DAEMON_ARGS} --bip=${IP_CIDR} --fixed-cidr=${FIXED_CIDR}" 200 | fi 201 | 202 | # stop docker daemon on shutdown to avoid loopback leaks 203 | trap "service docker stop" EXIT 204 | 205 | # start docker daemon 206 | if hash dockerd &>/dev/null; then 207 | DOCKER_DAEMON="dockerd" 208 | elif docker daemon --help &>/dev/null; then 209 | DOCKER_DAEMON="docker daemon" 210 | else 211 | DOCKER_DAEMON="docker -d" 212 | fi 213 | if [ "$LOG" == "file" ]; then 214 | ${DOCKER_DAEMON} ${DOCKER_DAEMON_ARGS} &>/var/log/docker.log & 215 | else 216 | ${DOCKER_DAEMON} ${DOCKER_DAEMON_ARGS} & 217 | fi 218 | (( timeout = 60 + SECONDS )) 219 | until docker info >/dev/null 2>&1; do 220 | if (( SECONDS >= timeout )); then 221 | echo 'Timed out trying to connect to internal docker host.' >&2 222 | exit 1 223 | fi 224 | sleep 1 225 | done 226 | [[ $1 ]] && exec "$@" 227 | exec bash --login 228 | --------------------------------------------------------------------------------