install.sh 16 KB

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