install.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. #!/bin/sh
  2. set -e
  3. #
  4. # This script is meant for quick & easy install via:
  5. # 'curl -sSL https://get.docker.com/ | sh'
  6. # or:
  7. # 'wget -qO- https://get.docker.com/ | sh'
  8. #
  9. # For test builds (ie. release candidates):
  10. # 'curl -fsSL https://test.docker.com/ | sh'
  11. # or:
  12. # 'wget -qO- https://test.docker.com/ | sh'
  13. #
  14. # For experimental builds:
  15. # 'curl -fsSL https://experimental.docker.com/ | sh'
  16. # or:
  17. # 'wget -qO- https://experimental.docker.com/ | sh'
  18. #
  19. # Docker Maintainers:
  20. # To update this script on https://get.docker.com,
  21. # use hack/release.sh during a normal release,
  22. # or the following one-liner for script hotfixes:
  23. # aws s3 cp --acl public-read hack/install.sh s3://get.docker.com/index
  24. #
  25. url="https://get.docker.com/"
  26. apt_url="https://apt.dockerproject.org"
  27. yum_url="https://yum.dockerproject.org"
  28. gpg_fingerprint="58118E89F3A912897C070ADBF76221572C52609D"
  29. key_servers="
  30. ha.pool.sks-keyservers.net
  31. pgp.mit.edu
  32. keyserver.ubuntu.com
  33. "
  34. command_exists() {
  35. command -v "$@" > /dev/null 2>&1
  36. }
  37. echo_docker_as_nonroot() {
  38. if command_exists docker && [ -e /var/run/docker.sock ]; then
  39. (
  40. set -x
  41. $sh_c 'docker version'
  42. ) || true
  43. fi
  44. your_user=your-user
  45. [ "$user" != 'root' ] && your_user="$user"
  46. # intentionally mixed spaces and tabs here -- tabs are stripped by "<<-EOF", spaces are kept in the output
  47. cat <<-EOF
  48. If you would like to use Docker as a non-root user, you should now consider
  49. adding your user to the "docker" group with something like:
  50. sudo usermod -aG docker $your_user
  51. Remember that you will have to log out and back in for this to take effect!
  52. EOF
  53. }
  54. # Check if this is a forked Linux distro
  55. check_forked() {
  56. # Check for lsb_release command existence, it usually exists in forked distros
  57. if command_exists lsb_release; then
  58. # Check if the `-u` option is supported
  59. set +e
  60. lsb_release -a -u > /dev/null 2>&1
  61. lsb_release_exit_code=$?
  62. set -e
  63. # Check if the command has exited successfully, it means we're in a forked distro
  64. if [ "$lsb_release_exit_code" = "0" ]; then
  65. # Print info about current distro
  66. cat <<-EOF
  67. You're using '$lsb_dist' version '$dist_version'.
  68. EOF
  69. # Get the upstream release info
  70. lsb_dist=$(lsb_release -a -u 2>&1 | tr '[:upper:]' '[:lower:]' | grep -E 'id' | cut -d ':' -f 2 | tr -d '[[:space:]]')
  71. dist_version=$(lsb_release -a -u 2>&1 | tr '[:upper:]' '[:lower:]' | grep -E 'codename' | cut -d ':' -f 2 | tr -d '[[:space:]]')
  72. # Print info about upstream distro
  73. cat <<-EOF
  74. Upstream release is '$lsb_dist' version '$dist_version'.
  75. EOF
  76. else
  77. if [ -r /etc/debian_version ] && [ "$lsb_dist" != "ubuntu" ]; then
  78. # We're Debian and don't even know it!
  79. lsb_dist=debian
  80. dist_version="$(cat /etc/debian_version | sed 's/\/.*//' | sed 's/\..*//')"
  81. case "$dist_version" in
  82. 8|'Kali Linux 2')
  83. dist_version="jessie"
  84. ;;
  85. 7)
  86. dist_version="wheezy"
  87. ;;
  88. esac
  89. fi
  90. fi
  91. fi
  92. }
  93. rpm_import_repository_key() {
  94. local key=$1; shift
  95. local tmpdir=$(mktemp -d)
  96. chmod 600 "$tmpdir"
  97. for key_server in $key_servers ; do
  98. gpg --homedir "$tmpdir" --keyserver "$key_server" --recv-keys "$key" && break
  99. done
  100. gpg --homedir "$tmpdir" -k "$key" >/dev/null
  101. gpg --homedir "$tmpdir" --export --armor "$key" > "$tmpdir"/repo.key
  102. rpm --import "$tmpdir"/repo.key
  103. rm -rf "$tmpdir"
  104. }
  105. semverParse() {
  106. major="${1%%.*}"
  107. minor="${1#$major.}"
  108. minor="${minor%%.*}"
  109. patch="${1#$major.$minor.}"
  110. patch="${patch%%[-.]*}"
  111. }
  112. do_install() {
  113. case "$(uname -m)" in
  114. *64)
  115. ;;
  116. *)
  117. cat >&2 <<-'EOF'
  118. Error: you are not using a 64bit platform.
  119. Docker currently only supports 64bit platforms.
  120. EOF
  121. exit 1
  122. ;;
  123. esac
  124. if command_exists docker; then
  125. version="$(docker -v | awk -F '[ ,]+' '{ print $3 }')"
  126. MAJOR_W=1
  127. MINOR_W=10
  128. semverParse $version
  129. shouldWarn=0
  130. if [ $major -lt $MAJOR_W ]; then
  131. shouldWarn=1
  132. fi
  133. if [ $major -le $MAJOR_W ] && [ $minor -lt $MINOR_W ]; then
  134. shouldWarn=1
  135. fi
  136. cat >&2 <<-'EOF'
  137. Warning: the "docker" command appears to already exist on this system.
  138. If you already have Docker installed, this script can cause trouble, which is
  139. why we're displaying this warning and provide the opportunity to cancel the
  140. installation.
  141. If you installed the current Docker package using this script and are using it
  142. EOF
  143. if [ $shouldWarn -eq 1 ]; then
  144. cat >&2 <<-'EOF'
  145. again to update Docker, we urge you to migrate your image store before upgrading
  146. to v1.10+.
  147. You can find instructions for this here:
  148. https://github.com/docker/docker/wiki/Engine-v1.10.0-content-addressability-migration
  149. EOF
  150. else
  151. cat >&2 <<-'EOF'
  152. again to update Docker, you can safely ignore this message.
  153. EOF
  154. fi
  155. cat >&2 <<-'EOF'
  156. You may press Ctrl+C now to abort this script.
  157. EOF
  158. ( set -x; sleep 20 )
  159. fi
  160. user="$(id -un 2>/dev/null || true)"
  161. sh_c='sh -c'
  162. if [ "$user" != 'root' ]; then
  163. if command_exists sudo; then
  164. sh_c='sudo -E sh -c'
  165. elif command_exists su; then
  166. sh_c='su -c'
  167. else
  168. cat >&2 <<-'EOF'
  169. Error: this installer needs the ability to run commands as root.
  170. We are unable to find either "sudo" or "su" available to make this happen.
  171. EOF
  172. exit 1
  173. fi
  174. fi
  175. curl=''
  176. if command_exists curl; then
  177. curl='curl -sSL'
  178. elif command_exists wget; then
  179. curl='wget -qO-'
  180. elif command_exists busybox && busybox --list-modules | grep -q wget; then
  181. curl='busybox wget -qO-'
  182. fi
  183. # check to see which repo they are trying to install from
  184. if [ -z "$repo" ]; then
  185. repo='main'
  186. if [ "https://test.docker.com/" = "$url" ]; then
  187. repo='testing'
  188. elif [ "https://experimental.docker.com/" = "$url" ]; then
  189. repo='experimental'
  190. fi
  191. fi
  192. # perform some very rudimentary platform detection
  193. lsb_dist=''
  194. dist_version=''
  195. if command_exists lsb_release; then
  196. lsb_dist="$(lsb_release -si)"
  197. fi
  198. if [ -z "$lsb_dist" ] && [ -r /etc/lsb-release ]; then
  199. lsb_dist="$(. /etc/lsb-release && echo "$DISTRIB_ID")"
  200. fi
  201. if [ -z "$lsb_dist" ] && [ -r /etc/debian_version ]; then
  202. lsb_dist='debian'
  203. fi
  204. if [ -z "$lsb_dist" ] && [ -r /etc/fedora-release ]; then
  205. lsb_dist='fedora'
  206. fi
  207. if [ -z "$lsb_dist" ] && [ -r /etc/oracle-release ]; then
  208. lsb_dist='oracleserver'
  209. fi
  210. if [ -z "$lsb_dist" ] && [ -r /etc/centos-release ]; then
  211. lsb_dist='centos'
  212. fi
  213. if [ -z "$lsb_dist" ] && [ -r /etc/redhat-release ]; then
  214. lsb_dist='redhat'
  215. fi
  216. if [ -z "$lsb_dist" ] && [ -r /etc/os-release ]; then
  217. lsb_dist="$(. /etc/os-release && echo "$ID")"
  218. fi
  219. lsb_dist="$(echo "$lsb_dist" | tr '[:upper:]' '[:lower:]')"
  220. # Special case redhatenterpriseserver
  221. if [ "${lsb_dist}" = "redhatenterpriseserver" ]; then
  222. # Set it to redhat, it will be changed to centos below anyways
  223. lsb_dist='redhat'
  224. fi
  225. case "$lsb_dist" in
  226. ubuntu)
  227. if command_exists lsb_release; then
  228. dist_version="$(lsb_release --codename | cut -f2)"
  229. fi
  230. if [ -z "$dist_version" ] && [ -r /etc/lsb-release ]; then
  231. dist_version="$(. /etc/lsb-release && echo "$DISTRIB_CODENAME")"
  232. fi
  233. ;;
  234. debian)
  235. dist_version="$(cat /etc/debian_version | sed 's/\/.*//' | sed 's/\..*//')"
  236. case "$dist_version" in
  237. 8)
  238. dist_version="jessie"
  239. ;;
  240. 7)
  241. dist_version="wheezy"
  242. ;;
  243. esac
  244. ;;
  245. oracleserver)
  246. # need to switch lsb_dist to match yum repo URL
  247. lsb_dist="oraclelinux"
  248. dist_version="$(rpm -q --whatprovides redhat-release --queryformat "%{VERSION}\n" | sed 's/\/.*//' | sed 's/\..*//' | sed 's/Server*//')"
  249. ;;
  250. fedora|centos|redhat)
  251. dist_version="$(rpm -q --whatprovides ${lsb_dist}-release --queryformat "%{VERSION}\n" | sed 's/\/.*//' | sed 's/\..*//' | sed 's/Server*//' | sort | tail -1)"
  252. ;;
  253. *)
  254. if command_exists lsb_release; then
  255. dist_version="$(lsb_release --codename | cut -f2)"
  256. fi
  257. if [ -z "$dist_version" ] && [ -r /etc/os-release ]; then
  258. dist_version="$(. /etc/os-release && echo "$VERSION_ID")"
  259. fi
  260. ;;
  261. esac
  262. # Check if this is a forked Linux distro
  263. check_forked
  264. # Run setup for each distro accordingly
  265. case "$lsb_dist" in
  266. amzn)
  267. (
  268. set -x
  269. $sh_c 'sleep 3; yum -y -q install docker'
  270. )
  271. echo_docker_as_nonroot
  272. exit 0
  273. ;;
  274. 'opensuse project'|opensuse)
  275. echo 'Going to perform the following operations:'
  276. if [ "$repo" != 'main' ]; then
  277. echo ' * add repository obs://Virtualization:containers'
  278. fi
  279. echo ' * install Docker'
  280. $sh_c 'echo "Press CTRL-C to abort"; sleep 3'
  281. if [ "$repo" != 'main' ]; then
  282. # install experimental packages from OBS://Virtualization:containers
  283. (
  284. set -x
  285. zypper -n ar -f obs://Virtualization:containers Virtualization:containers
  286. rpm_import_repository_key 55A0B34D49501BB7CA474F5AA193FBB572174FC2
  287. )
  288. fi
  289. (
  290. set -x
  291. zypper -n install docker
  292. )
  293. echo_docker_as_nonroot
  294. exit 0
  295. ;;
  296. 'suse linux'|sle[sd])
  297. echo 'Going to perform the following operations:'
  298. if [ "$repo" != 'main' ]; then
  299. echo ' * add repository obs://Virtualization:containers'
  300. echo ' * install experimental Docker using packages NOT supported by SUSE'
  301. else
  302. echo ' * add the "Containers" module'
  303. echo ' * install Docker using packages supported by SUSE'
  304. fi
  305. $sh_c 'echo "Press CTRL-C to abort"; sleep 3'
  306. if [ "$repo" != 'main' ]; then
  307. # install experimental packages from OBS://Virtualization:containers
  308. echo >&2 'Warning: installing experimental packages from OBS, these packages are NOT supported by SUSE'
  309. (
  310. set -x
  311. zypper -n ar -f obs://Virtualization:containers/SLE_12 Virtualization:containers
  312. rpm_import_repository_key 55A0B34D49501BB7CA474F5AA193FBB572174FC2
  313. )
  314. else
  315. # Add the containers module
  316. # Note well-1: the SLE machine must already be registered against SUSE Customer Center
  317. # Note well-2: the `-r ""` is required to workaround a known issue of SUSEConnect
  318. (
  319. set -x
  320. SUSEConnect -p sle-module-containers/12/x86_64 -r ""
  321. )
  322. fi
  323. (
  324. set -x
  325. zypper -n install docker
  326. )
  327. echo_docker_as_nonroot
  328. exit 0
  329. ;;
  330. ubuntu|debian)
  331. export DEBIAN_FRONTEND=noninteractive
  332. did_apt_get_update=
  333. apt_get_update() {
  334. if [ -z "$did_apt_get_update" ]; then
  335. ( set -x; $sh_c 'sleep 3; apt-get update' )
  336. did_apt_get_update=1
  337. fi
  338. }
  339. # aufs is preferred over devicemapper; try to ensure the driver is available.
  340. if ! grep -q aufs /proc/filesystems && ! $sh_c 'modprobe aufs'; then
  341. if uname -r | grep -q -- '-generic' && dpkg -l 'linux-image-*-generic' | grep -qE '^ii|^hi' 2>/dev/null; then
  342. kern_extras="linux-image-extra-$(uname -r) linux-image-extra-virtual"
  343. apt_get_update
  344. ( set -x; $sh_c 'sleep 3; apt-get install -y -q '"$kern_extras" ) || true
  345. if ! grep -q aufs /proc/filesystems && ! $sh_c 'modprobe aufs'; then
  346. echo >&2 'Warning: tried to install '"$kern_extras"' (for AUFS)'
  347. echo >&2 ' but we still have no AUFS. Docker may not work. Proceeding anyways!'
  348. ( set -x; sleep 10 )
  349. fi
  350. else
  351. echo >&2 'Warning: current kernel is not supported by the linux-image-extra-virtual'
  352. echo >&2 ' package. We have no AUFS support. Consider installing the packages'
  353. echo >&2 ' linux-image-virtual kernel and linux-image-extra-virtual for AUFS support.'
  354. ( set -x; sleep 10 )
  355. fi
  356. fi
  357. # install apparmor utils if they're missing and apparmor is enabled in the kernel
  358. # otherwise Docker will fail to start
  359. if [ "$(cat /sys/module/apparmor/parameters/enabled 2>/dev/null)" = 'Y' ]; then
  360. if command -v apparmor_parser >/dev/null 2>&1; then
  361. echo 'apparmor is enabled in the kernel and apparmor utils were already installed'
  362. else
  363. echo 'apparmor is enabled in the kernel, but apparmor_parser missing'
  364. apt_get_update
  365. ( set -x; $sh_c 'sleep 3; apt-get install -y -q apparmor' )
  366. fi
  367. fi
  368. if [ ! -e /usr/lib/apt/methods/https ]; then
  369. apt_get_update
  370. ( set -x; $sh_c 'sleep 3; apt-get install -y -q apt-transport-https ca-certificates' )
  371. fi
  372. if [ -z "$curl" ]; then
  373. apt_get_update
  374. ( set -x; $sh_c 'sleep 3; apt-get install -y -q curl ca-certificates' )
  375. curl='curl -sSL'
  376. fi
  377. (
  378. set -x
  379. for key_server in $key_servers ; do
  380. $sh_c "apt-key adv --keyserver hkp://${key_server}:80 --recv-keys ${gpg_fingerprint}" && break
  381. done
  382. $sh_c "apt-key adv -k ${gpg_fingerprint} >/dev/null"
  383. $sh_c "mkdir -p /etc/apt/sources.list.d"
  384. $sh_c "echo deb \[arch=$(dpkg --print-architecture)\] ${apt_url}/repo ${lsb_dist}-${dist_version} ${repo} > /etc/apt/sources.list.d/docker.list"
  385. $sh_c 'sleep 3; apt-get update; apt-get install -y -q docker-engine'
  386. )
  387. echo_docker_as_nonroot
  388. exit 0
  389. ;;
  390. fedora|centos|redhat|oraclelinux)
  391. if [ "${lsb_dist}" = "redhat" ]; then
  392. # we use the centos repository for both redhat and centos releases
  393. lsb_dist='centos'
  394. fi
  395. $sh_c "cat >/etc/yum.repos.d/docker-${repo}.repo" <<-EOF
  396. [docker-${repo}-repo]
  397. name=Docker ${repo} Repository
  398. baseurl=${yum_url}/repo/${repo}/${lsb_dist}/${dist_version}
  399. enabled=1
  400. gpgcheck=1
  401. gpgkey=${yum_url}/gpg
  402. EOF
  403. if [ "$lsb_dist" = "fedora" ] && [ "$dist_version" -ge "22" ]; then
  404. (
  405. set -x
  406. $sh_c 'sleep 3; dnf -y -q install docker-engine'
  407. )
  408. else
  409. (
  410. set -x
  411. $sh_c 'sleep 3; yum -y -q install docker-engine'
  412. )
  413. fi
  414. echo_docker_as_nonroot
  415. exit 0
  416. ;;
  417. gentoo)
  418. if [ "$url" = "https://test.docker.com/" ]; then
  419. # intentionally mixed spaces and tabs here -- tabs are stripped by "<<-'EOF'", spaces are kept in the output
  420. cat >&2 <<-'EOF'
  421. You appear to be trying to install the latest nightly build in Gentoo.'
  422. The portage tree should contain the latest stable release of Docker, but'
  423. if you want something more recent, you can always use the live ebuild'
  424. provided in the "docker" overlay available via layman. For more'
  425. instructions, please see the following URL:'
  426. https://github.com/tianon/docker-overlay#using-this-overlay'
  427. After adding the "docker" overlay, you should be able to:'
  428. emerge -av =app-emulation/docker-9999'
  429. EOF
  430. exit 1
  431. fi
  432. (
  433. set -x
  434. $sh_c 'sleep 3; emerge app-emulation/docker'
  435. )
  436. exit 0
  437. ;;
  438. esac
  439. # intentionally mixed spaces and tabs here -- tabs are stripped by "<<-'EOF'", spaces are kept in the output
  440. cat >&2 <<-'EOF'
  441. Either your platform is not easily detectable, is not supported by this
  442. installer script (yet - PRs welcome! [hack/install.sh]), or does not yet have
  443. a package for Docker. Please visit the following URL for more detailed
  444. installation instructions:
  445. https://docs.docker.com/engine/installation/
  446. EOF
  447. exit 1
  448. }
  449. # wrapped up in a function so that we have some protection against only getting
  450. # half the file during "curl | sh"
  451. do_install