how-to-install-and-use-docker-on-ubuntu-16-04.html 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. <h1 class="content-title Tutorial-header">How To Install and Use Docker on Ubuntu 16.04</h1>
  2. <span class="meta-section date-views">
  3. <span class="meta-section timestamp"><span class="tutorial-date-text">Updated</span><span class="tutorial-date">October 19, 2018</span></span>
  4. <span class="meta-section pageviews"><span class="icon icon-eye v-mid"></span><span class="views-count v-mid">2.2m</span><span class="sr-only"> views</span></span>
  5. </span>
  6. <span class="meta-section tags">
  7. <a class="tag" href="/community/tags/ubuntu?type=tutorials">Ubuntu</a> <a class="tag" href="/community/tags/docker?type=tutorials">Docker</a> <a class="tag" href="/community/tags/ubuntu-16-04?type=tutorials">Ubuntu 16.04</a>
  8. </span>
  9. </div>
  10. <div class="tutorial-authors">
  11. <div class="component-collaborators-container">
  12. <ul class="component-collaborators-content">
  13. <li class="collaborator-byline-avatar">
  14. <a href="/community/users/finid">
  15. <img alt="finid" class="avatar avatar-large" src="https://secure.gravatar.com/avatar/989a7705a77732d888ddaff8b440fc3d?secure=true&d=identicon" width="80" height="80" />
  16. </a> </li>
  17. <li class="collaborators-byline-data">
  18. <p class="names">By <a href="/community/users/finid">finid</a></p>
  19. <p>
  20. <a class="advice" href="https://www.digitalocean.com/community/write-for-digitalocean">Become an author</a>
  21. </p>
  22. </li>
  23. </ul>
  24. </div>
  25. </div>
  26. <div class="container versioned-tutorial-group-navigation" data-versioned-tutorial-group-navigation>
  27. <div class="versioned-tutorial-group-navigation__header" data-versioned-tutorial-group-navigation-header>
  28. <h5 class="versioned-tutorial-group-navigation__heading">
  29. Not using <strong>Ubuntu 16.04</strong>? Choose a different version:
  30. </h5>
  31. <span class="versioned-tutorial-group-navigation__arrow">
  32. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 105.565 60.872"><path d="M2.526 46.087c-3.37 3.382-3.37 8.868 0 12.246 3.37 3.386 8.832 3.386 12.202 0l43.384-43.547c3.37-3.382 3.37-8.87 0-12.246-3.37-3.386-8.833-3.386-12.202 0L2.526 46.087z" fill="#fff"></path><path d="M90.835 58.333c3.37 3.386 8.833 3.386 12.202 0 3.37-3.378 3.37-8.864 0-12.246L59.654 2.54c-3.37-3.386-8.832-3.386-12.202 0-3.37 3.377-3.37 8.864 0 12.246l43.383 43.547z" fill="#fff"></path></svg>
  33. </span>
  34. </div>
  35. <div class="versioned-tutorial-group-navigation__body" data-versioned-tutorial-group-navigation-body>
  36. <ul class="versioned-tutorial-group-navigation__list">
  37. <li class="versioned-tutorial-group-navigation__list-item">
  38. <a class="versioned-tutorial-group-navigation__link" title="Automated: Ansible" href="/community/tutorials/how-to-use-ansible-to-install-and-set-up-docker-on-ubuntu-18-04">
  39. <span class="text">Automated: Ansible</span>
  40. <span class="icon-arrow-down versioned-tutorial-group-navigation__link-arrow"></span>
  41. </a></li>
  42. <li class="versioned-tutorial-group-navigation__list-item">
  43. <a class="versioned-tutorial-group-navigation__link" title="CentOS 7" href="/community/tutorials/how-to-install-and-use-docker-on-centos-7">
  44. <span class="text">CentOS 7</span>
  45. <span class="icon-arrow-down versioned-tutorial-group-navigation__link-arrow"></span>
  46. </a></li>
  47. <li class="versioned-tutorial-group-navigation__list-item">
  48. <a class="versioned-tutorial-group-navigation__link" title="Debian 9" href="/community/tutorials/how-to-install-and-use-docker-on-debian-9">
  49. <span class="text">Debian 9</span>
  50. <span class="icon-arrow-down versioned-tutorial-group-navigation__link-arrow"></span>
  51. </a></li>
  52. <li class="versioned-tutorial-group-navigation__list-item">
  53. <a class="versioned-tutorial-group-navigation__link" title="Debian 10" href="/community/tutorials/how-to-install-and-use-docker-on-debian-10">
  54. <span class="text">Debian 10</span>
  55. <span class="icon-arrow-down versioned-tutorial-group-navigation__link-arrow"></span>
  56. </a></li>
  57. <li class="versioned-tutorial-group-navigation__list-item">
  58. <a class="versioned-tutorial-group-navigation__link" title="Ubuntu 18.04" href="/community/tutorials/how-to-install-and-use-docker-on-ubuntu-18-04">
  59. <span class="text">Ubuntu 18.04</span>
  60. <span class="icon-arrow-down versioned-tutorial-group-navigation__link-arrow"></span>
  61. </a></li>
  62. <li class="versioned-tutorial-group-navigation__list-item">
  63. <a data-tutorial-request data-distribution-version-id="12" data-versioned-tutorial-group-id="39" href="javascript:;" class="versioned-tutorial-group-navigation__link versioned-tutorial-group-navigation__link--unavailable" title="Automated: Docker">
  64. <span class="text">Automated: Docker</span>
  65. <small class="versioned-tutorial-group-navigation__request" data-tutorial-request-status-indicator>request</small>
  66. </a>
  67. </li>
  68. <li class="versioned-tutorial-group-navigation__list-item">
  69. <a data-tutorial-request data-distribution-version-id="14" data-versioned-tutorial-group-id="39" href="javascript:;" class="versioned-tutorial-group-navigation__link versioned-tutorial-group-navigation__link--unavailable" title="Automated: Bash">
  70. <span class="text">Automated: Bash</span>
  71. <small class="versioned-tutorial-group-navigation__request" data-tutorial-request-status-indicator>request</small>
  72. </a>
  73. </li>
  74. <li class="versioned-tutorial-group-navigation__list-item">
  75. <a data-tutorial-request data-distribution-version-id="7" data-versioned-tutorial-group-id="39" href="javascript:;" class="versioned-tutorial-group-navigation__link versioned-tutorial-group-navigation__link--unavailable" title="Debian 8">
  76. <span class="text">Debian 8</span>
  77. <small class="versioned-tutorial-group-navigation__request" data-tutorial-request-status-indicator>request</small>
  78. </a>
  79. </li>
  80. <li class="versioned-tutorial-group-navigation__list-item">
  81. <a data-tutorial-request data-distribution-version-id="2" data-versioned-tutorial-group-id="39" href="javascript:;" class="versioned-tutorial-group-navigation__link versioned-tutorial-group-navigation__link--unavailable" title="Ubuntu 14.04">
  82. <span class="text">Ubuntu 14.04</span>
  83. <small class="versioned-tutorial-group-navigation__request" data-tutorial-request-status-indicator>request</small>
  84. </a>
  85. </li>
  86. <li class="versioned-tutorial-group-navigation__list-item">
  87. <a class="versioned-tutorial-group-navigation__link" title="How to Install and Use Docker" href="/community/tutorial_collections/39">
  88. <span class="text">View All</span>
  89. <span class="icon-arrow-down versioned-tutorial-group-navigation__link-arrow"></span>
  90. </a> </li>
  91. </ul>
  92. </div>
  93. </div>
  94. <div class="content-body tutorial-content" data-growable-markdown>
  95. <h3 id="introduction">Introduction</h3>
  96. <p>Docker is an application that makes it simple and easy to run application processes in a container, which are like virtual machines, only more portable, more resource-friendly, and more dependent on the host operating system. For a detailed introduction to the different components of a Docker container, check out <a href="https://www.digitalocean.com/community/tutorials/the-docker-ecosystem-an-introduction-to-common-components">The Docker Ecosystem: An Introduction to Common Components</a>.</p>
  97. <p>There are two methods for installing Docker on Ubuntu 16.04. One method involves installing it on an existing installation of the operating system. The other involves spinning up a server with a tool called <a href="https://www.digitalocean.com/community/tutorials/how-to-provision-and-manage-remote-docker-hosts-with-docker-machine-on-ubuntu-16-04">Docker Machine</a> that auto-installs Docker on it.</p>
  98. <p>In this tutorial, you&rsquo;ll learn how to install and use it on an existing installation of Ubuntu 16.04.</p>
  99. <h2 id="prerequisites">Prerequisites</h2>
  100. <p>To follow this tutorial, you will need the following:</p>
  101. <ul>
  102. <li>One Ubuntu 16.04 server set up with a non-root user with sudo privileges and a basic firewall, as explained in the <a href="https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-16-04">Initial Setup Guide for Ubuntu 16.04</a></li>
  103. <li>An account on <a href="https://hub.docker.com/">Docker Hub</a> if you wish to create your own images and push them to Docker Hub, as shown in Steps 7 and 8</li>
  104. </ul>
  105. <h2 id="step-1-—-installing-docker">Step 1 — Installing Docker</h2>
  106. <p>The Docker installation package available in the official Ubuntu 16.04 repository may not be the latest version. To get this latest version, install Docker from the official Docker repository. This section shows you how to do just that.</p>
  107. <p>First, in order to ensure the downloads are valid, add the GPG key for the official Docker repository to your system:</p>
  108. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  109. </li></ul></code></pre>
  110. <p>Add the Docker repository to APT sources:</p>
  111. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
  112. </li></ul></code></pre>
  113. <p>Next, update the package database with the Docker packages from the newly added repo:</p>
  114. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">sudo apt-get update
  115. </li></ul></code></pre>
  116. <p>Make sure you are about to install from the Docker repo instead of the default Ubuntu 16.04 repo:</p>
  117. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">apt-cache policy docker-ce
  118. </li></ul></code></pre>
  119. <p>You should see output similar to the follow:</p>
  120. <div class="code-label " title="Output of apt-cache policy docker-ce">Output of apt-cache policy docker-ce</div><pre class="code-pre "><code langs="">docker-ce:
  121. Installed: (none)
  122. Candidate: <span class="highlight">18.06.1~ce~3-0~ubuntu</span>
  123. Version table:
  124. <span class="highlight">18.06.1~ce~3-0~ubuntu</span> 500
  125. 500 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
  126. </code></pre>
  127. <p>Notice that <code>docker-ce</code> is not installed, but the candidate for installation is from the Docker repository for Ubuntu 16.04 (<code>xenial</code>).</p>
  128. <p>Finally, install Docker:</p>
  129. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">sudo apt-get install -y docker-ce
  130. </li></ul></code></pre>
  131. <p>Docker should now be installed, the daemon started, and the process enabled to start on boot. Check that it&rsquo;s running:</p>
  132. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">sudo systemctl status docker
  133. </li></ul></code></pre>
  134. <p>The output should be similar to the following, showing that the service is active and running:</p>
  135. <pre class="code-pre "><code langs=""><div class="secondary-code-label " title="Output">Output</div>● docker.service - Docker Application Container Engine
  136. Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
  137. Active: <span class="highlight">active (running)</span> since Thu 2018-10-18 20:28:23 UTC; 35s ago
  138. Docs: https://docs.docker.com
  139. Main PID: 13412 (dockerd)
  140. CGroup: /system.slice/docker.service
  141. ├─13412 /usr/bin/dockerd -H fd://
  142. └─13421 docker-containerd --config /var/run/docker/containerd/containerd.toml
  143. </code></pre>
  144. <p>Installing Docker now gives you not just the Docker service (daemon) but also the <code>docker</code> command line utility, or the Docker client. We&rsquo;ll explore how to use the <code>docker</code> command later in this tutorial.</p>
  145. <h2 id="step-2-—-executing-the-docker-command-without-sudo-optional">Step 2 — Executing the Docker Command Without Sudo (Optional)</h2>
  146. <p>By default, running the <code>docker</code> command requires root privileges — that is, you have to prefix the command with <code>sudo</code>. It can also be run by a user in the <strong>docker</strong> group, which is automatically created during the installation of Docker. If you attempt to run the <code>docker</code> command without prefixing it with <code>sudo</code> or without being in the docker group, you&rsquo;ll get an output like this:</p>
  147. <pre class="code-pre "><code langs=""><div class="secondary-code-label " title="Output">Output</div>docker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?.
  148. See 'docker run --help'.
  149. </code></pre>
  150. <p>If you want to avoid typing <code>sudo</code> whenever you run the <code>docker</code> command, add your username to the <code>docker</code> group:</p>
  151. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">sudo usermod -aG docker ${USER}
  152. </li></ul></code></pre>
  153. <p>To apply the new group membership, you can log out of the server and back in, or you can type the following:</p>
  154. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">su - ${USER}
  155. </li></ul></code></pre>
  156. <p>You will be prompted to enter your user&rsquo;s password to continue. Afterwards, you can confirm that your user is now added to the <code>docker</code> group by typing:</p>
  157. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">id -nG
  158. </li></ul></code></pre><pre class="code-pre "><code langs=""><div class="secondary-code-label " title="Output">Output</div><span class="highlight">sammy</span> sudo docker
  159. </code></pre>
  160. <p>If you need to add a user to the <code>docker</code> group that you&rsquo;re not logged in as, declare that username explicitly using:</p>
  161. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">sudo usermod -aG docker <span class="highlight">username</span>
  162. </li></ul></code></pre>
  163. <p>The rest of this article assumes you are running the <code>docker</code> command as a user in the docker user group. If you choose not to, please prepend the commands with <code>sudo</code>.</p>
  164. <h2 id="step-3-—-using-the-docker-command">Step 3 — Using the Docker Command</h2>
  165. <p>With Docker installed and working, now&rsquo;s the time to become familiar with the command line utility. Using <code>docker</code> consists of passing it a chain of options and commands followed by arguments. The syntax takes this form:</p>
  166. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">docker [option] [command] [arguments]
  167. </li></ul></code></pre>
  168. <p>To view all available subcommands, type:</p>
  169. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">docker
  170. </li></ul></code></pre>
  171. <p>As of Docker 18.06.1, the complete list of available subcommands includes:</p>
  172. <pre class="code-pre "><code langs=""><div class="secondary-code-label " title="Output">Output</div>
  173. attach Attach local standard input, output, and error streams to a running container
  174. build Build an image from a Dockerfile
  175. commit Create a new image from a container's changes
  176. cp Copy files/folders between a container and the local filesystem
  177. create Create a new container
  178. diff Inspect changes to files or directories on a container's filesystem
  179. events Get real time events from the server
  180. exec Run a command in a running container
  181. export Export a container's filesystem as a tar archive
  182. history Show the history of an image
  183. images List images
  184. import Import the contents from a tarball to create a filesystem image
  185. info Display system-wide information
  186. inspect Return low-level information on Docker objects
  187. kill Kill one or more running containers
  188. load Load an image from a tar archive or STDIN
  189. login Log in to a Docker registry
  190. logout Log out from a Docker registry
  191. logs Fetch the logs of a container
  192. pause Pause all processes within one or more containers
  193. port List port mappings or a specific mapping for the container
  194. ps List containers
  195. pull Pull an image or a repository from a registry
  196. push Push an image or a repository to a registry
  197. rename Rename a container
  198. restart Restart one or more containers
  199. rm Remove one or more containers
  200. rmi Remove one or more images
  201. run Run a command in a new container
  202. save Save one or more images to a tar archive (streamed to STDOUT by default)
  203. search Search the Docker Hub for images
  204. start Start one or more stopped containers
  205. stats Display a live stream of container(s) resource usage statistics
  206. stop Stop one or more running containers
  207. tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  208. top Display the running processes of a container
  209. unpause Unpause all processes within one or more containers
  210. update Update configuration of one or more containers
  211. version Show the Docker version information
  212. wait Block until one or more containers stop, then print their exit codes
  213. </code></pre>
  214. <p>To view the switches available to a specific command, type:</p>
  215. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">docker <span class="highlight">docker-subcommand</span> --help
  216. </li></ul></code></pre>
  217. <p>To view system-wide information about Docker, use:</p>
  218. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">docker info
  219. </li></ul></code></pre>
  220. <h2 id="step-4-—-working-with-docker-images">Step 4 — Working with Docker Images</h2>
  221. <p>Docker containers are run from Docker images. By default, it pulls these images from Docker Hub, a Docker registry managed by Docker, the company behind the Docker project. Anybody can build and host their Docker images on Docker Hub, so most applications and Linux distributions you&rsquo;ll need to run Docker containers have images that are hosted on Docker Hub.</p>
  222. <p>To check whether you can access and download images from Docker Hub, type:</p>
  223. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">docker run hello-world
  224. </li></ul></code></pre>
  225. <p>In the output, you should see the following message, which indicates that Docker is working correctly:</p>
  226. <pre class="code-pre "><code langs=""><div class="secondary-code-label " title="Output">Output</div>...
  227. Hello from Docker!
  228. This message shows that your installation appears to be working correctly.
  229. ...
  230. </code></pre>
  231. <p>You can search for images available on Docker Hub by using the <code>docker</code> command with the <code>search</code> subcommand. For example, to search for the Ubuntu image, type:</p>
  232. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">docker search ubuntu
  233. </li></ul></code></pre>
  234. <p>The script will crawl Docker Hub and return a listing of all images whose name matches the search string. In this case, the output will be similar to this:</p>
  235. <pre class="code-pre "><code langs=""><div class="secondary-code-label " title="Output">Output</div>
  236. NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  237. ubuntu Ubuntu is a Debian-based Linux operating sys&hellip; 8564 [OK]
  238. dorowu/ubuntu-desktop-lxde-vnc Ubuntu with openssh-server and NoVNC 230 [OK]
  239. rastasheep/ubuntu-sshd Dockerized SSH service, built on top of offi&hellip; 176 [OK]
  240. consol/ubuntu-xfce-vnc Ubuntu container with "headless" VNC session&hellip; 129 [OK]
  241. ansible/ubuntu14.04-ansible Ubuntu 14.04 LTS with ansible 95 [OK]
  242. ubuntu-upstart Upstart is an event-based replacement for th&hellip; 91 [OK]
  243. neurodebian NeuroDebian provides neuroscience research s&hellip; 54 [OK]
  244. 1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 48 [OK]
  245. ubuntu-debootstrap debootstrap --variant=minbase --components=m&hellip; 39 [OK]
  246. nuagebec/ubuntu Simple always updated Ubuntu docker images w&hellip; 23 [OK]
  247. tutum/ubuntu Simple Ubuntu docker images with SSH access 18
  248. i386/ubuntu Ubuntu is a Debian-based Linux operating sys&hellip; 14
  249. 1and1internet/ubuntu-16-apache-php-7.0 ubuntu-16-apache-php-7.0 13 [OK]
  250. ppc64le/ubuntu Ubuntu is a Debian-based Linux operating sys&hellip; 12
  251. eclipse/ubuntu_jdk8 Ubuntu, JDK8, Maven 3, git, curl, nmap, mc, &hellip; 6 [OK]
  252. 1and1internet/ubuntu-16-nginx-php-5.6-wordpress-4 ubuntu-16-nginx-php-5.6-wordpress-4 6 [OK]
  253. codenvy/ubuntu_jdk8 Ubuntu, JDK8, Maven 3, git, curl, nmap, mc, &hellip; 4 [OK]
  254. darksheer/ubuntu Base Ubuntu Image -- Updated hourly 4 [OK]
  255. pivotaldata/ubuntu A quick freshening-up of the base Ubuntu doc&hellip; 2
  256. 1and1internet/ubuntu-16-sshd ubuntu-16-sshd 1 [OK]
  257. smartentry/ubuntu ubuntu with smartentry 1 [OK]
  258. ossobv/ubuntu Custom ubuntu image from scratch (based on o&hellip; 0
  259. paasmule/bosh-tools-ubuntu Ubuntu based bosh-cli 0 [OK]
  260. 1and1internet/ubuntu-16-healthcheck ubuntu-16-healthcheck 0 [OK]
  261. pivotaldata/ubuntu-gpdb-dev Ubuntu images for GPDB development 0
  262. </code></pre>
  263. <p>In the <strong>OFFICIAL</strong> column, <strong>OK</strong> indicates an image built and supported by the company behind the project. Once you&rsquo;ve identified the image that you would like to use, you can download it to your computer using the <code>pull</code> subcommand. Try this with the <code>ubuntu</code> image, like so:</p>
  264. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">docker pull ubuntu
  265. </li></ul></code></pre>
  266. <p>After an image has been downloaded, you may then run a container using the downloaded image with the <code>run</code> subcommand. If an image has not been downloaded when <code>docker</code> is executed with the <code>run</code> subcommand, the Docker client will first download the image, then run a container using it:</p>
  267. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">docker run ubuntu
  268. </li></ul></code></pre>
  269. <p>To see the images that have been downloaded to your computer, type:</p>
  270. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">docker images
  271. </li></ul></code></pre>
  272. <p>The output should look similar to the following:</p>
  273. <pre class="code-pre "><code langs=""><div class="secondary-code-label " title="Output">Output</div>REPOSITORY TAG IMAGE ID CREATED SIZE
  274. ubuntu latest ea4c82dcd15a 16 hours ago 85.8MB
  275. hello-world latest 4ab4c602aa5e 5 weeks ago 1.84kB
  276. </code></pre>
  277. <p>As you&rsquo;ll see later in this tutorial, images that you use to run containers can be modified and used to generate new images, which may then be uploaded (<em>pushed</em> is the technical term) to Docker Hub or other Docker registries.</p>
  278. <h2 id="step-5-—-running-a-docker-container">Step 5 — Running a Docker Container</h2>
  279. <p>The <code>hello-world</code> container you ran in the previous step is an example of a container that runs and exits after emitting a test message. Containers can be much more useful than that, and they can be interactive. After all, they are similar to virtual machines, only more resource-friendly.</p>
  280. <p>As an example, let&rsquo;s run a container using the latest image of Ubuntu. The combination of the <strong>-i</strong> and <strong>-t</strong> switches gives you interactive shell access into the container:</p>
  281. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">docker run -it ubuntu
  282. </li></ul></code></pre>
  283. <p><span class='note'><strong>Note:</strong> The default behavior for the <code>run</code> command is to start a new container. Once you run the preceding the command, you will open up the shell interface of a second <code>ubuntu</code> container.<br></span></p>
  284. <p>Your command prompt should change to reflect the fact that you&rsquo;re now working inside the container and should take this form:</p>
  285. <pre class="code-pre "><code langs=""><div class="secondary-code-label " title="Output">Output</div>root@<span class="highlight">9b0db8a30ad1</span>:/#
  286. </code></pre>
  287. <p><span class='note'><strong>Note:</strong> Remember the container id in the command prompt. In the preceding example, it is <code>9b0db8a30ad1</code>. You&rsquo;ll need that container ID later to identify the container when you want to remove it.<br></span></p>
  288. <p>Now you can run any command inside the container. For example, let&rsquo;s update the package database inside the container. You don&rsquo;t need to prefix any command with <code>sudo</code>, because you&rsquo;re operating inside the container as the <strong>root</strong> user:</p>
  289. <pre class="code-pre custom_prefix second-environment"><code langs=""><ul class="prefixed"><li class="line" prefix="root@9b0db8a30ad1:/#">apt-get update
  290. </li></ul></code></pre>
  291. <p>Then install any application in it. Let&rsquo;s install Node.js:</p>
  292. <pre class="code-pre custom_prefix second-environment"><code langs=""><ul class="prefixed"><li class="line" prefix="root@9b0db8a30ad1:/#">apt-get install -y nodejs
  293. </li></ul></code></pre>
  294. <p>This installs Node.js in the container from the official Ubuntu repository. When the installation finishes, verify that Node.js is installed:</p>
  295. <pre class="code-pre custom_prefix second-environment"><code langs=""><ul class="prefixed"><li class="line" prefix="root@9b0db8a30ad1:/#">node -v
  296. </li></ul></code></pre>
  297. <p>You&rsquo;ll see the version number displayed in your terminal:</p>
  298. <pre class="code-pre second-environment"><code langs=""><div class="secondary-code-label " title="Output">Output</div>v8.10.0
  299. </code></pre>
  300. <p>Any changes you make inside the container only apply to that container.</p>
  301. <p>To exit the container, type <code>exit</code> at the prompt.</p>
  302. <p>Let&rsquo;s look at managing the containers on our system next.</p>
  303. <h2 id="step-6-—-managing-docker-containers">Step 6 — Managing Docker Containers</h2>
  304. <p>After using Docker for a while, you&rsquo;ll have many active (running) and inactive containers on your computer. To view the <strong>active ones</strong>, use:</p>
  305. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">docker ps
  306. </li></ul></code></pre>
  307. <p>You will see output similar to the following:</p>
  308. <pre class="code-pre "><code langs=""><div class="secondary-code-label " title="Output">Output</div>CONTAINER ID IMAGE COMMAND CREATED
  309. </code></pre>
  310. <p>In this tutorial, you started three containers; one from the <code>hello-world</code> image and two from the <code>ubuntu</code> image. These containers are no longer running, but they still exist on your system.</p>
  311. <p>To view all containers — active and inactive — run <code>docker ps</code> with the <code>-a</code> switch:</p>
  312. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">docker ps -a
  313. </li></ul></code></pre>
  314. <p>You&rsquo;ll see output similar to this:</p>
  315. <pre class="code-pre "><code langs=""><div class="secondary-code-label " title="Output">Output</div>CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  316. 9b0db8a30ad1 ubuntu "/bin/bash" 21 minutes ago Exited (0) About a minute ago xenodochial_neumann
  317. d7851eb12e23 ubuntu "/bin/bash" 24 minutes ago Exited (0) 24 minutes ago boring_chebyshev
  318. d54945b6510b hello-world "/hello" 32 minutes ago Exited (0) 32 minutes ago youthful_roentgen
  319. </code></pre>
  320. <p>To view the latest container you created, pass it the <code>-l</code> switch:</p>
  321. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">docker ps -l
  322. </li></ul></code></pre><pre class="code-pre "><code langs=""><div class="secondary-code-label " title="Output">Output</div>CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  323. 9b0db8a30ad1 ubuntu "/bin/bash" 22 minutes ago Exited (127) About a minute ago xenodochial_neumann
  324. </code></pre>
  325. <p>To start a stopped container, use <code>docker start</code>, followed by the container ID or the container&rsquo;s name. Let&rsquo;s start the Ubuntu-based container with the ID of <code>9b0db8a30ad1</code>:</p>
  326. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">docker start <span class="highlight">9b0db8a30ad1 </span>
  327. </li></ul></code></pre>
  328. <p>The container will start, and you can use <code>docker ps</code> to see its status:</p>
  329. <pre class="code-pre "><code langs=""><div class="secondary-code-label " title="Output">Output</div>CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  330. 9b0db8a30ad1 ubuntu "/bin/bash" 23 minutes ago Up 11 seconds xenodochial_neumann
  331. </code></pre>
  332. <p>To stop a running container, use <code>docker stop</code>, followed by the container ID or name. This time, we&rsquo;ll use the name that Docker assigned the container, which is <code>xenodochial_neumann</code>:</p>
  333. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">docker stop <span class="highlight">xenodochial_neumann</span>
  334. </li></ul></code></pre>
  335. <p>Once you&rsquo;ve decided you no longer need a container anymore, remove it with the <code>docker rm</code> command, again using either the container ID or the name. Use the <code>docker ps -a</code> command to find the container ID or name for the container associated with the <code>hello-world</code> image and remove it.</p>
  336. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">docker rm <span class="highlight">youthful_roentgen</span>
  337. </li></ul></code></pre>
  338. <p>You can start a new container and give it a name using the <code>--name</code> switch. You can also use the <code>--rm</code> switch to create a container that removes itself when it&rsquo;s stopped. See the <code>docker run help</code> command for more information on these options and others.</p>
  339. <p>Containers can be turned into images which you can use to build new containers. Let&rsquo;s look at how that works.</p>
  340. <h2 id="step-7-—-committing-changes-in-a-container-to-a-docker-image">Step 7 — Committing Changes in a Container to a Docker Image</h2>
  341. <p>When you start up a Docker image, you can create, modify, and delete files just like you can with a virtual machine. The changes that you make will only apply to that container. You can start and stop it, but once you destroy it with the <code>docker rm</code> command, the changes will be lost for good.</p>
  342. <p>This section shows you how to save the state of a container as a new Docker image.</p>
  343. <p>After installing Node.js inside the Ubuntu container, you now have a container running off an image, but the container is different from the image you used to create it. But you might want to reuse this Node.js container as the basis for new images later.</p>
  344. <p>To do this, commit the changes to a new Docker image instance using the following command structure:</p>
  345. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">docker commit -m "<span class="highlight">What did you do to the image</span>" -a "<span class="highlight">Author Name</span>" <span class="highlight">container-id</span> <span class="highlight">repository</span>/<span class="highlight">new_image_name</span>
  346. </li></ul></code></pre>
  347. <p>The <strong>-m</strong> switch is for the commit message that helps you and others know what changes you made, while <strong>-a</strong> is used to specify the author. The <code>container ID</code> is the one you noted earlier in the tutorial when you started the interactive Docker session. Unless you created additional repositories on Docker Hub, the repository is usually your Docker Hub username.</p>
  348. <p>For example, for the user <strong>sammy</strong>, with the container ID of <code>d9b100f2f636</code>, the command would be:</p>
  349. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">docker commit -m "added node.js" -a "<span class="highlight">sammy</span>" <span class="highlight">d9b100f2f636</span> <span class="highlight">sammy</span>/ubuntu-nodejs
  350. </li></ul></code></pre>
  351. <p><span class='note'><strong>Note:</strong> When you <em>commit</em> an image, the new image is saved locally, that is, on your computer. Later in this tutorial, you&rsquo;ll learn how to push an image to a Docker registry like Docker Hub so that it can be assessed and used by you and others.<br></span></p>
  352. <p>After that operation is completed, listing the Docker images now on your computer should show the new image, as well as the old one that it was derived from:</p>
  353. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">docker images
  354. </li></ul></code></pre>
  355. <p>The output should be similar to this:</p>
  356. <pre class="code-pre "><code langs=""><div class="secondary-code-label " title="Output">Output</div>REPOSITORY TAG IMAGE ID CREATED SIZE
  357. <span class="highlight">sammy</span>/ubuntu-nodejs latest 6a1784a63edf 2 minutes ago 170MB
  358. ubuntu latest ea4c82dcd15a 17 hours ago 85.8MB
  359. hello-world latest 4ab4c602aa5e 5 weeks ago 1.84kB
  360. </code></pre>
  361. <p>In the above example, <strong>ubuntu-nodejs</strong> is the new image, which was derived from the existing ubuntu image from Docker Hub. The size difference reflects the changes that were made. In this example, the change was that Node.js was installed. Next time you need to run a container using Ubuntu with Node.js pre-installed, you can just use the new image.</p>
  362. <p>You can also build images from a <code>Dockerfile</code>, which lets you automate the installation of software in a new image. However, that&rsquo;s outside the scope of this tutorial.</p>
  363. <p>Now let&rsquo;s share the new image with others so they can create containers from it.</p>
  364. <h2 id="step-8-—-pushing-docker-images-to-a-docker-repository">Step 8 — Pushing Docker Images to a Docker Repository</h2>
  365. <p>The next logical step after creating a new image from an existing image is to share it with a select few of your friends, the whole world on Docker Hub, or another Docker registry that you have access to. To push an image to Docker Hub or any other Docker registry, you must have an account there.</p>
  366. <p>This section shows you how to push a Docker image to Docker Hub. To learn how to create your own private Docker registry, check out <a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-a-private-docker-registry-on-ubuntu-14-04">How To Set Up a Private Docker Registry on Ubuntu 14.04</a>.</p>
  367. <p>To push your image, first log into Docker Hub:</p>
  368. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">docker login -u <span class="highlight">docker-registry-username</span>
  369. </li></ul></code></pre>
  370. <p>You&rsquo;ll be prompted to authenticate using your Docker Hub password. If you specified the correct password, authentication should succeed.</p>
  371. <span class='note'><p>
  372. <strong>Note:</strong> If your Docker registry username is different from the local username you used to create the image, you will have to tag your image with your registry username. For the example given in the last step, you would type:</p>
  373. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">docker tag <span class="highlight">sammy</span>/ubuntu-nodejs <span class="highlight">docker-registry-username</span>/ubuntu-nodejs
  374. </li></ul></code></pre>
  375. <p></p></span>
  376. <p>Then you can push your own image using:</p>
  377. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">docker push <span class="highlight">docker-registry-username</span>/ubuntu-nodejs
  378. </li></ul></code></pre>
  379. <p>To push the <code>ubuntu-nodejs</code> image to the <strong>sammy</strong> repository, the command would be:</p>
  380. <pre class="code-pre command"><code langs=""><ul class="prefixed"><li class="line" prefix="$">docker push <span class="highlight">sammy</span>/<span class="highlight">ubuntu-nodejs</span>
  381. </li></ul></code></pre>
  382. <p>The process may take some time to complete as it uploads the images, but when completed, the output will look like this:</p>
  383. <pre class="code-pre "><code langs=""><div class="secondary-code-label " title="Output">Output</div>The push refers to repository [docker.io/<span class="highlight">sammy</span>/ubuntu-nodejs]
  384. 1aa927602b6a: Pushed
  385. 76c033092e10: Pushed
  386. 2146d867acf3: Pushed
  387. ae1f631f14b7: Pushed
  388. 102645f1cf72: Pushed
  389. latest: digest: sha256:2be90a210910f60f74f433350185feadbbdaca0d050d97181bf593dd85195f06 size: 1362
  390. </code></pre>
  391. <p>After pushing an image to a registry, it should be listed on your account&rsquo;s dashboard, like that shown in the image below.</p>
  392. <p><img src="https://assets.digitalocean.com/articles/docker_1804/ec2vX3Z.png" alt="New Docker image listing on Docker Hub"></p>
  393. <p>If a push attempt results in the following error, it is likely that you are not logged in:</p>
  394. <pre class="code-pre "><code langs=""><div class="secondary-code-label " title="Output">Output</div>The push refers to a repository [docker.io/<span class="highlight">sammy</span>/ubuntu-nodejs]
  395. e3fbbfb44187: Preparing
  396. 5f70bf18a086: Preparing
  397. a3b5c80a4eba: Preparing
  398. 7f18b442972b: Preparing
  399. 3ce512daaf78: Preparing
  400. 7aae4540b42d: Waiting
  401. unauthorized: authentication required
  402. </code></pre>
  403. <p>Log in, then repeat the push attempt.</p>
  404. <h2 id="conclusion">Conclusion</h2>
  405. <p>In this tutorial, you&rsquo;ve learned the basics to get you started working with Docker on Ubuntu 16.04. Like most open source projects, Docker is built from a fast-developing codebase, so make a habit of visiting the project&rsquo;s <a href="https://blog.docker.com/">blog page</a> for the latest information.</p>
  406. <p>For further exploration, check out the <a href="https://www.digitalocean.com/community/tags/docker?type=tutorials">other Docker tutorials</a> in the DigitalOcean Community.</p>