install_docker_on_debian10.html 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  6. <title>How To Install and Use Docker on Debian 10 | DigitalOcean</title>
  7. <meta name="description" content="Docker is an application that simplifies the process of managing application processes in containers. In this tutorial, you&#39;ll install and use Docker Community Edition (CE) on Debian 10. You&#39;ll install Docker itself, work with containers and images, a">
  8. <link rel="apple-touch-icon" sizes="180x180" href="/assets/community/apple-icon-180x180-4b5ac7f80f7cb483dde5afeb99c279301c50ec3670129c73ce568f1b465354be.png" />
  9. <link rel="apple-touch-icon" sizes="152x152" href="/assets/community/apple-icon-152x152-ff64fd3242c24284fefdaec8d6d85c68f98744142d58b950f7ec67cbf03251d7.png" />
  10. <link rel="apple-touch-icon" sizes="144x144" href="/assets/community/apple-icon-144x144-3018401d3d675917b06ded0482cdf53a7c3558b4dc7b6b8de21a485a8da27dfe.png" />
  11. <link rel="apple-touch-icon" sizes="120x120" href="/assets/community/apple-icon-120x120-052cdf68a3af8b01c3fd0d46ed35f8f089c10c690109d20e7b186f0159391863.png" />
  12. <link rel="apple-touch-icon" sizes="114x114" href="/assets/community/apple-icon-114x114-8d862033fc61d86a60b4bdbbbab284da399da714089995ce7efc249f36409517.png" />
  13. <link rel="apple-touch-icon" sizes="76x76" href="/assets/community/apple-icon-76x76-d7c1877f2224ca127b591f65d16635fa7472e7b2cd0e1fe7e7dc8b74d03dd874.png" />
  14. <link rel="apple-touch-icon" sizes="72x72" href="/assets/community/apple-icon-72x72-2d888cc13ddb01fb0d786a0c8567c0a2909eaeb86901da715099b6b88a36b46a.png" />
  15. <link rel="apple-touch-icon" sizes="60x60" href="/assets/community/apple-icon-60x60-01be5b392df60249f168b8b2eed5c928c63aa43ac398c7180cd219c5acdd1e70.png" />
  16. <link rel="apple-touch-icon" sizes="57x57" href="/assets/community/apple-icon-57x57-d060d0fd30794efd579d9edc283917e0a65adfd366e151d9a5cfd8fd7d07cb43.png" />
  17. <link rel="icon" type="image/png" sizes="192x192" href="/assets/community/android-icon-192x192-4d13e6664f412f6904a78be76d626004bcbbd59671f6c755919628134003c2a8.png" />
  18. <link rel="icon" type="image/png" sizes="32x32" href="/assets/community/favicon-32x32-e377577c425642ab495296dfec040ec903e36ffc4cd7a0a4281e84597891a774.png" />
  19. <link rel="icon" type="image/png" sizes="96x96" href="/assets/community/favicon-96x96-967f34b6716bb3cffebefe1fa53206d38d60c72635e826ec5d172c58fb4ab5a9.png" />
  20. <link rel="icon" type="image/png" sizes="16x16" href="/assets/community/favicon-16x16-ce9c1eb6d969d5b2ec276ae4b715756aa0e63106f33dc53a836687fdfee49ecd.png" />
  21. <meta name="msapplication-TileImage" content="/assets/community/ms-icon-144x144-3018401d3d675917b06ded0482cdf53a7c3558b4dc7b6b8de21a485a8da27dfe.png" />
  22. <meta name="msapplication-TileColor" content="#0069ff" />
  23. <meta name="theme-color" content="#0069ff" />
  24. <meta name="og:title" content="How To Install and Use Docker on Debian 10 | DigitalOcean">
  25. <meta name="og:description" content="Docker is an application that simplifies the process of managing application processes in containers. In this tutorial, you&#39;ll install and use Docker Community Edition (CE) on Debian 10. You&#39;ll install Docker itself, work with containers and images, a">
  26. <meta name="og:site_name" content="DigitalOcean">
  27. <meta name="og:type" content="article">
  28. <meta name="og:image" content="https://community-cdn-digitalocean-com.global.ssl.fastly.net/assets/tutorials/images/large/Docker_Install_mostov_twitter-_-facebook-2.png?1562602024">
  29. <meta name="twitter:site" content="DigitalOcean">
  30. <meta name="twitter:title" content="How To Install and Use Docker on Debian 10 | DigitalOcean">
  31. <meta name="twitter:description" content="Docker is an application that simplifies the process of managing application processes in containers. In this tutorial, you&#39;ll install and use Docker Community Edition (CE) on Debian 10. You&#39;ll install Docker itself, work with containers and images, a">
  32. <meta name="twitter:creator" content="DigitalOcean">
  33. <meta name="twitter:card" content="photo" />
  34. <meta name="twitter:url" content="https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-debian-10"/>
  35. <meta name="twitter:image" content="https://community-cdn-digitalocean-com.global.ssl.fastly.net/assets/tutorials/images/large/Docker_Install_mostov_twitter-_-facebook-2.png?1562602024">
  36. <link rel='canonical' href='https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-debian-10'>
  37. <link rel='amphtml' href='https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-debian-10.amp'>
  38. <link rel="preconnect" href="https://digitalocean.cdn.prismic.io">
  39. <link rel="preconnect" href="https://hello.myfonts.net">
  40. <link rel="stylesheet" media="all" href="/assets/community/application-1d6f1f0ccf88e772200c0b6525375710b6023a41177a7e17ae81ab21dcd67c22.css" />
  41. <h1> How to Install and Use Docker on Debian 10 </h1>
  42. <div class="content-body tutorial-content" data-growable-markdown>
  43. <h3 id="introduction">Introduction</h3>
  44. <p><a href="https://www.docker.com/">Docker</a> is an application that simplifies the process of managing application processes in <em>containers</em>. Containers let you run your applications in resource-isolated processes. They&rsquo;re similar to virtual machines, but containers are more portable, more resource-friendly, and more dependent on the host operating system. </p>
  45. <p>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>
  46. <p>In this tutorial, you&rsquo;ll install and use Docker Community Edition (CE) on Debian 10. You&rsquo;ll install Docker itself, work with containers and images, and push an image to a Docker Repository.</p>
  47. <h2 id="prerequisites">Prerequisites</h2>
  48. <p>To follow this tutorial, you will need the following:</p>
  49. <ul>
  50. <li>One Debian 10 server set up by following <a href="https://www.digitalocean.com/community/tutorials/initial-server-setup-with-debian-10">the Debian 10 initial server setup guide</a>, including a sudo non-root user and a firewall.</li>
  51. <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>
  52. </ul>
  53. <h2 id="step-1-—-installing-docker">Step 1 — Installing Docker</h2>
  54. <p>The Docker installation package available in the official Debian repository may not be the latest version. To ensure we get the latest version, we&rsquo;ll install Docker from the official Docker repository. To do that, we&rsquo;ll add a new package source, add the GPG key from Docker to ensure the downloads are valid, and then install the package.</p>
  55. <p>First, update your existing list of packages:</p>
  56. <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">sudo apt update
  57. </li></ul></code></pre>
  58. <p>Next, install a few prerequisite packages which let <code>apt</code> use packages over HTTPS:</p>
  59. <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">sudo apt install apt-transport-https ca-certificates curl gnupg2 software-properties-common
  60. </li></ul></code></pre>
  61. <p>Then add the GPG key for the official Docker repository to your system:</p>
  62. <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -
  63. </li></ul></code></pre>
  64. <p>Add the Docker repository to APT sources:</p>
  65. <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"
  66. </li></ul></code></pre>
  67. <p>Next, update the package database with the Docker packages from the newly added repo:</p>
  68. <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">sudo apt update
  69. </li></ul></code></pre>
  70. <p>Make sure you are about to install from the Docker repo instead of the default Debian repo:</p>
  71. <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">apt-cache policy docker-ce
  72. </li></ul></code></pre>
  73. <p>You&rsquo;ll see output like this, although the version number for Docker may be different:</p>
  74. <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>ocker-ce:
  75. Installed: (none)
  76. Candidate: <span class="highlight">5:18.09.7~3-0~debian-buster</span>
  77. Version table:
  78. <span class="highlight">5:18.09.7~3-0~debian-buster</span> 500
  79. 500 https://download.docker.com/linux/debian buster/stable amd64 Packages
  80. </code></pre>
  81. <p>Notice that <code>docker-ce</code> is not installed, but the candidate for installation is from the Docker repository for Debian 10 (<code>buster</code>). </p>
  82. <p>Finally, install Docker:</p>
  83. <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">sudo apt install docker-ce
  84. </li></ul></code></pre>
  85. <p>Docker is now installed, the daemon started, and the process enabled to start on boot. Check that it&rsquo;s running:</p>
  86. <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">sudo systemctl status docker
  87. </li></ul></code></pre>
  88. <p>The output will be similar to the following, showing that the service is active and running:</p>
  89. <pre class="code-pre "><code><div class="secondary-code-label " title="Output">Output</div>● docker.service - Docker Application Container Engine
  90. Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
  91. Active: <span class="highlight">active</span> (running) since Mon 2019-07-08 15:11:19 UTC; 58s ago
  92. Docs: https://docs.docker.com
  93. Main PID: 5709 (dockerd)
  94. Tasks: 8
  95. Memory: 31.6M
  96. CGroup: /system.slice/docker.service
  97. └─5709 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
  98. </code></pre>
  99. <p>Installing Docker 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>
  100. <h2 id="step-2-—-executing-the-docker-command-without-sudo-optional">Step 2 — Executing the Docker Command Without Sudo (Optional)</h2>
  101. <p>By default, the <code>docker</code> command can only be run the <strong>root</strong> user or by a user in the <strong>docker</strong> group, which is automatically created during Docker&rsquo;s installation process. If you attempt to run the <code>docker</code> command without prefixing it with <code>sudo</code> or without being in the <strong>docker</strong> group, you&rsquo;ll get an output like this:</p>
  102. <pre class="code-pre "><code><div class="secondary-code-label " title="Output">Output</div>docker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?.
  103. See 'docker run --help'.
  104. </code></pre>
  105. <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>
  106. <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">sudo usermod -aG docker ${USER}
  107. </li></ul></code></pre>
  108. <p>To apply the new group membership, log out of the server and back in, or type the following:</p>
  109. <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">su - ${USER}
  110. </li></ul></code></pre>
  111. <p>You will be prompted to enter your user&rsquo;s password to continue. </p>
  112. <p>Confirm that your user is now added to the <strong>docker</strong> group by typing:</p>
  113. <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">id -nG
  114. </li></ul></code></pre><pre class="code-pre "><code><div class="secondary-code-label " title="Output">Output</div>sammy sudo <span class="highlight">docker</span>
  115. </code></pre>
  116. <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>
  117. <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">sudo usermod -aG docker <span class="highlight">username</span>
  118. </li></ul></code></pre>
  119. <p>The rest of this article assumes you are running the <code>docker</code> command as a user in the <strong>docker</strong> group. If you choose not to, please prepend the commands with <code>sudo</code>.</p>
  120. <p>Let&rsquo;s explore the <code>docker</code> command next.</p>
  121. <h2 id="step-3-—-using-the-docker-command">Step 3 — Using the Docker Command</h2>
  122. <p>Using <code>docker</code> consists of passing it a chain of options and commands followed by arguments. The syntax takes this form:</p>
  123. <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">docker [option] [command] [arguments]
  124. </li></ul></code></pre>
  125. <p>To view all available subcommands, type:</p>
  126. <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">docker
  127. </li></ul></code></pre>
  128. <p>As of Docker 18, the complete list of available subcommands includes:</p>
  129. <pre class="code-pre "><code><div class="secondary-code-label " title="Output">Output</div>attach Attach local standard input, output, and error streams to a running container
  130. build Build an image from a Dockerfile
  131. commit Create a new image from a container's changes
  132. cp Copy files/folders between a container and the local filesystem
  133. create Create a new container
  134. diff Inspect changes to files or directories on a container's filesystem
  135. events Get real time events from the server
  136. exec Run a command in a running container
  137. export Export a container's filesystem as a tar archive
  138. history Show the history of an image
  139. images List images
  140. import Import the contents from a tarball to create a filesystem image
  141. info Display system-wide information
  142. inspect Return low-level information on Docker objects
  143. kill Kill one or more running containers
  144. load Load an image from a tar archive or STDIN
  145. login Log in to a Docker registry
  146. logout Log out from a Docker registry
  147. logs Fetch the logs of a container
  148. pause Pause all processes within one or more containers
  149. port List port mappings or a specific mapping for the container
  150. ps List containers
  151. pull Pull an image or a repository from a registry
  152. push Push an image or a repository to a registry
  153. rename Rename a container
  154. restart Restart one or more containers
  155. rm Remove one or more containers
  156. rmi Remove one or more images
  157. run Run a command in a new container
  158. save Save one or more images to a tar archive (streamed to STDOUT by default)
  159. search Search the Docker Hub for images
  160. start Start one or more stopped containers
  161. stats Display a live stream of container(s) resource usage statistics
  162. stop Stop one or more running containers
  163. tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  164. top Display the running processes of a container
  165. unpause Unpause all processes within one or more containers
  166. update Update configuration of one or more containers
  167. version Show the Docker version information
  168. wait Block until one or more containers stop, then print their exit codes
  169. </code></pre>
  170. <p>To view the options available to a specific command, type:</p>
  171. <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">docker <span class="highlight">docker-subcommand</span> --help
  172. </li></ul></code></pre>
  173. <p>To view system-wide information about Docker, use:</p>
  174. <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">docker info
  175. </li></ul></code></pre>
  176. <p>Let&rsquo;s explore some of these commands. We&rsquo;ll start by working with images.</p>
  177. <h2 id="step-4-—-working-with-docker-images">Step 4 — Working with Docker Images</h2>
  178. <p>Docker containers are built from Docker images. By default, Docker pulls these images from <a href="https://hub.docker.com">Docker Hub</a>, a Docker registry managed by Docker, the company behind the Docker project. Anyone can host their Docker images on Docker Hub, so most applications and Linux distributions you&rsquo;ll need will have images hosted there.</p>
  179. <p>To check whether you can access and download images from Docker Hub, type:</p>
  180. <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">docker run hello-world
  181. </li></ul></code></pre>
  182. <p>The output will indicate that Docker in working correctly:</p>
  183. <pre class="code-pre "><code><div class="secondary-code-label " title="Output">Output</div>Unable to find image 'hello-world:latest' locally
  184. latest: Pulling from library/hello-world
  185. 1b930d010525: Pull complete
  186. Digest: sha256:41a65640635299bab090f783209c1e3a3f11934cf7756b09cb2f1e02147c6ed8
  187. Status: Downloaded newer image for hello-world:latest
  188. Hello from Docker!
  189. This message shows that your installation appears to be working correctly.
  190. ...
  191. </code></pre>
  192. <p>Docker was initially unable to find the <code>hello-world</code> image locally, so it downloaded the image from Docker Hub, which is the default repository. Once the image downloaded, Docker created a container from the image and the application within the container executed, displaying the message.</p>
  193. <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>
  194. <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">docker search ubuntu
  195. </li></ul></code></pre>
  196. <p>The script will crawl Docker Hub and return a listing of all images whose name match the search string. In this case, the output will be similar to this:</p>
  197. <pre class="code-pre "><code><div class="secondary-code-label " title="Output">Output</div>NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  198. ubuntu Ubuntu is a Debian-based Linux operating sys&hellip; 9704 [OK]
  199. dorowu/ubuntu-desktop-lxde-vnc Docker image to provide HTML5 VNC interface &hellip; 319 [OK]
  200. rastasheep/ubuntu-sshd Dockerized SSH service, built on top of offi&hellip; 224 [OK]
  201. consol/ubuntu-xfce-vnc Ubuntu container with "headless" VNC session&hellip; 183 [OK]
  202. ubuntu-upstart Upstart is an event-based replacement for th&hellip; 99 [OK]
  203. ansible/ubuntu14.04-ansible Ubuntu 14.04 LTS with ansible 97 [OK]
  204. neurodebian NeuroDebian provides neuroscience research s&hellip; 57 [OK]
  205. 1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 50 [OK]
  206. ubuntu
  207. ...
  208. </code></pre>
  209. <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.</p>
  210. <p>Execute the following command to download the official <code>ubuntu</code> image to your computer:</p>
  211. <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">docker pull ubuntu
  212. </li></ul></code></pre>
  213. <p>You&rsquo;ll see the following output:</p>
  214. <pre class="code-pre "><code><div class="secondary-code-label " title="Output">Output</div>Using default tag: latest
  215. latest: Pulling from library/ubuntu
  216. 5b7339215d1d: Pull complete
  217. 14ca88e9f672: Pull complete
  218. a31c3b1caad4: Pull complete
  219. b054a26005b7: Pull complete
  220. Digest: sha256:9b1702dcfe32c873a770a32cfd306dd7fc1c4fd134adfb783db68defc8894b3c
  221. Status: Downloaded newer image for ubuntu:latest
  222. </code></pre>
  223. <p>After an image has been downloaded, you can then run a container using the downloaded image with the <code>run</code> subcommand. As you saw with the <code>hello-world</code> example, 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>
  224. <p>To see the images that have been downloaded to your computer, type:</p>
  225. <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">docker images
  226. </li></ul></code></pre>
  227. <p>The output should look similar to the following:</p>
  228. <pre class="code-pre "><code><div class="secondary-code-label " title="Output">Output</div>REPOSITORY TAG IMAGE ID CREATED SIZE
  229. ubuntu latest 4c108a37151f 2 weeks ago 64.2MB
  230. hello-world latest fce289e99eb9 6 months ago 1.84kB
  231. </code></pre>
  232. <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>
  233. <p>Let&rsquo;s look at how to run containers in more detail.</p>
  234. <h2 id="step-5-—-running-a-docker-container">Step 5 — Running a Docker Container</h2>
  235. <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>
  236. <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>
  237. <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">docker run -it ubuntu
  238. </li></ul></code></pre>
  239. <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>
  240. <pre class="code-pre second-environment"><code><div class="secondary-code-label " title="Output">Output</div>root@d9b100f2f636:/#
  241. </code></pre>
  242. <p>Note the container id in the command prompt. In this example, it is <code>d9b100f2f636</code>. You&rsquo;ll need that container ID later to identify the container when you want to remove it.</p>
  243. <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>
  244. <pre class="code-pre custom_prefix prefixed second-environment"><code><ul class="prefixed"><li class="line" prefix="root@d9b100f2f636:/#">apt update
  245. </li></ul></code></pre>
  246. <p>Then install any application in it. Let&rsquo;s install Node.js:</p>
  247. <pre class="code-pre custom_prefix prefixed second-environment"><code><ul class="prefixed"><li class="line" prefix="root@d9b100f2f636:/#">apt install nodejs
  248. </li></ul></code></pre>
  249. <p>This installs Node.js in the container from the official Ubuntu repository. When the installation finishes, verify that Node.js is installed:</p>
  250. <pre class="code-pre custom_prefix prefixed second-environment"><code><ul class="prefixed"><li class="line" prefix="root@d9b100f2f636:/#">node -v
  251. </li></ul></code></pre>
  252. <p>You&rsquo;ll see the version number displayed in your terminal:</p>
  253. <pre class="code-pre second-environment"><code><div class="secondary-code-label " title="Output">Output</div>v8.10.0
  254. </code></pre>
  255. <p>Any changes you make inside the container only apply to that container. </p>
  256. <p>To exit the container, type <code>exit</code> at the prompt.</p>
  257. <p>Let&rsquo;s look at managing the containers on our system next.</p>
  258. <h2 id="step-6-—-managing-docker-containers">Step 6 — Managing Docker Containers</h2>
  259. <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>
  260. <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">docker ps
  261. </li></ul></code></pre>
  262. <p>You will see output similar to the following:</p>
  263. <pre class="code-pre "><code><div class="secondary-code-label " title="Output">Output</div>CONTAINER ID IMAGE COMMAND CREATED
  264. </code></pre>
  265. <p>In this tutorial, you started two containers; one from the <code>hello-world</code> image and another from the <code>ubuntu</code> image. Both containers are no longer running, but they still exist on your system.</p>
  266. <p>To view all containers — active and inactive, run <code>docker ps</code> with the <code>-a</code> switch:</p>
  267. <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">docker ps -a
  268. </li></ul></code></pre>
  269. <p>You&rsquo;ll see output similar to this:</p>
  270. <pre class="code-pre "><code>CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  271. d42d0bbfbd35 ubuntu "/bin/bash" About a minute ago Exited (0) 20 seconds ago friendly_volhard
  272. 0740844d024c hello-world "/hello" 3 minutes ago Exited (0) 3 minutes ago elegant_neumann
  273. </code></pre>
  274. <p>To view the latest container you created, pass it the <code>-l</code> switch:</p>
  275. <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">docker ps -l
  276. </li></ul></code></pre><pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  277. </li><li class="line" prefix="$">d42d0bbfbd35 ubuntu "/bin/bash" About a minute ago Exited (0) 34 seconds ago friendly_volhard
  278. </li></ul></code></pre>
  279. <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>d9b100f2f636</code>:</p>
  280. <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">docker start <span class="highlight">d42d0bbfbd35</span>
  281. </li></ul></code></pre>
  282. <p>The container will start, and you can use <code>docker ps</code> to see its status:</p>
  283. <pre class="code-pre "><code>CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  284. d42d0bbfbd35 ubuntu "/bin/bash" About a minute ago Up 8 seconds friendly_volhard
  285. </code></pre>
  286. <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>friendly_volhard</code>:</p>
  287. <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">docker stop <span class="highlight">friendly_volhard</span>
  288. </li></ul></code></pre>
  289. <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>
  290. <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">docker rm <span class="highlight">elegant_neumann</span>
  291. </li></ul></code></pre>
  292. <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>
  293. <p>Containers can be turned into images which you can use to build new containers. Let&rsquo;s look at how that works.</p>
  294. <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>
  295. <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>
  296. <p>This section shows you how to save the state of a container as a new Docker image.</p>
  297. <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>
  298. <p>Then commit the changes to a new Docker image instance using the following command. </p>
  299. <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">docker commit -m "What you did to the image" -a "Author Name" <span class="highlight">container_id</span> <span class="highlight">repository</span>/<span class="highlight">new_image_name</span>
  300. </li></ul></code></pre>
  301. <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 <code>repository</code> is usually your Docker Hub username.</p>
  302. <p>For example, for the user <strong>sammy</strong>, with the container ID of <code>d9b100f2f636</code>, the command would be:</p>
  303. <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">docker commit -m "added Node.js" -a "<span class="highlight">sammy</span>" <span class="highlight">d42d0bbfbd35</span> <span class="highlight">sammy</span>/ubuntu-nodejs
  304. </li></ul></code></pre>
  305. <p>When you <em>commit</em> an image, the new image is saved locally on your computer. Later in this tutorial, you&rsquo;ll learn how to push an image to a Docker registry like Docker Hub so others can access it.</p>
  306. <p>Listing the Docker images again will show the new image, as well as the old one that it was derived from:</p>
  307. <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">docker images
  308. </li></ul></code></pre>
  309. <p>You&rsquo;ll see output like this:</p>
  310. <pre class="code-pre "><code><div class="secondary-code-label " title="Output">Output</div>REPOSITORY TAG IMAGE ID CREATED SIZE
  311. <span class="highlight">sammy/ubuntu-nodejs</span> latest d441c62350b4 10 seconds ago 152MB
  312. ubuntu latest 4c108a37151f 2 weeks ago 64.2MB
  313. hello-world latest fce289e99eb9 6 months ago 1.84kB
  314. </code></pre>
  315. <p>In this example, <code>ubuntu-nodejs</code> is the new image, which was derived from the existing <code>ubuntu</code> image from Docker Hub. The size difference reflects the changes that were made. And in this example, the change was that NodeJS was installed. So next time you need to run a container using Ubuntu with NodeJS pre-installed, you can just use the new image. </p>
  316. <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>
  317. <p>Now let&rsquo;s share the new image with others so they can create containers from it.</p>
  318. <h2 id="step-8-—-pushing-docker-images-to-a-docker-repository">Step 8 — Pushing Docker Images to a Docker Repository</h2>
  319. <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 other 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>
  320. <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>
  321. <p>To push your image, first log into Docker Hub. </p>
  322. <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">docker login -u <span class="highlight">docker-registry-username</span>
  323. </li></ul></code></pre>
  324. <p>You&rsquo;ll be prompted to authenticate using your Docker Hub password. If you specified the correct password, authentication should succeed. </p>
  325. <span class='note'><p>
  326. <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>
  327. <pre class="code-pre command prefixed"><code><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
  328. </li></ul></code></pre>
  329. <p></p></span>
  330. <p>Then you may push your own image using:</p>
  331. <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">docker push <span class="highlight">docker-registry-username</span>/<span class="highlight">docker-image-name</span>
  332. </li></ul></code></pre>
  333. <p>To push the <code>ubuntu-nodejs</code> image to the <strong>sammy</strong> repository, the command would be:</p>
  334. <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">docker push <span class="highlight">sammy</span>/<span class="highlight">ubuntu-nodejs</span>
  335. </li></ul></code></pre>
  336. <p>The process may take some time to complete as it uploads the images, but when completed, the output will look like this:</p>
  337. <pre class="code-pre "><code><div class="secondary-code-label " title="Output">Output</div>The push refers to a repository [docker.io/<span class="highlight">sammy</span>/ubuntu-nodejs]
  338. e3fbbfb44187: Pushed
  339. 5f70bf18a086: Pushed
  340. a3b5c80a4eba: Pushed
  341. 7f18b442972b: Pushed
  342. 3ce512daaf78: Pushed
  343. 7aae4540b42d: Pushed
  344. ...
  345. </code></pre>
  346. <p>After pushing an image to a registry, it should be listed on your account&rsquo;s dashboard, like that show in the image below.</p>
  347. <p><img src="https://assets.digitalocean.com/articles/docker_1804/ec2vX3Z.png" alt="New Docker image listing on Docker Hub"></p>
  348. <p>If a push attempt results in an error of this sort, then you likely did not log in:</p>
  349. <pre class="code-pre "><code><div class="secondary-code-label " title="Output">Output</div>The push refers to a repository [docker.io/<span class="highlight">sammy</span>/ubuntu-nodejs]
  350. e3fbbfb44187: Preparing
  351. 5f70bf18a086: Preparing
  352. a3b5c80a4eba: Preparing
  353. 7f18b442972b: Preparing
  354. 3ce512daaf78: Preparing
  355. 7aae4540b42d: Waiting
  356. unauthorized: authentication required
  357. </code></pre>
  358. <p>Log in with <code>docker login</code> and repeat the push attempt. Then verify that it exists on your Docker Hub repository page.</p>
  359. <p>You can now use <code>docker pull <span class="highlight">sammy</span>/<span class="highlight">ubuntu-nodejs</span></code> to pull the image to a new machine and use it to run a new container.</p>
  360. <h2 id="conclusion">Conclusion</h2>
  361. <p>In this tutorial you installed Docker, worked with images and containers, and pushed a modified image to Docker Hub. Now that you know the basics, explore the <a href="https://www.digitalocean.com/community/tags/docker?type=tutorials">other Docker tutorials</a> in the DigitalOcean Community.</p>
  362. </div>
  363. </div>
  364. </body>
  365. </html>