123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
- <title>How To Install and Use Docker on Debian 10 | DigitalOcean</title>
- <meta name="description" content="Docker is an application that simplifies the process of managing application processes in containers. In this tutorial, you'll install and use Docker Community Edition (CE) on Debian 10. You'll install Docker itself, work with containers and images, a">
- <link rel="apple-touch-icon" sizes="180x180" href="/assets/community/apple-icon-180x180-4b5ac7f80f7cb483dde5afeb99c279301c50ec3670129c73ce568f1b465354be.png" />
- <link rel="apple-touch-icon" sizes="152x152" href="/assets/community/apple-icon-152x152-ff64fd3242c24284fefdaec8d6d85c68f98744142d58b950f7ec67cbf03251d7.png" />
- <link rel="apple-touch-icon" sizes="144x144" href="/assets/community/apple-icon-144x144-3018401d3d675917b06ded0482cdf53a7c3558b4dc7b6b8de21a485a8da27dfe.png" />
- <link rel="apple-touch-icon" sizes="120x120" href="/assets/community/apple-icon-120x120-052cdf68a3af8b01c3fd0d46ed35f8f089c10c690109d20e7b186f0159391863.png" />
- <link rel="apple-touch-icon" sizes="114x114" href="/assets/community/apple-icon-114x114-8d862033fc61d86a60b4bdbbbab284da399da714089995ce7efc249f36409517.png" />
- <link rel="apple-touch-icon" sizes="76x76" href="/assets/community/apple-icon-76x76-d7c1877f2224ca127b591f65d16635fa7472e7b2cd0e1fe7e7dc8b74d03dd874.png" />
- <link rel="apple-touch-icon" sizes="72x72" href="/assets/community/apple-icon-72x72-2d888cc13ddb01fb0d786a0c8567c0a2909eaeb86901da715099b6b88a36b46a.png" />
- <link rel="apple-touch-icon" sizes="60x60" href="/assets/community/apple-icon-60x60-01be5b392df60249f168b8b2eed5c928c63aa43ac398c7180cd219c5acdd1e70.png" />
- <link rel="apple-touch-icon" sizes="57x57" href="/assets/community/apple-icon-57x57-d060d0fd30794efd579d9edc283917e0a65adfd366e151d9a5cfd8fd7d07cb43.png" />
- <link rel="icon" type="image/png" sizes="192x192" href="/assets/community/android-icon-192x192-4d13e6664f412f6904a78be76d626004bcbbd59671f6c755919628134003c2a8.png" />
- <link rel="icon" type="image/png" sizes="32x32" href="/assets/community/favicon-32x32-e377577c425642ab495296dfec040ec903e36ffc4cd7a0a4281e84597891a774.png" />
- <link rel="icon" type="image/png" sizes="96x96" href="/assets/community/favicon-96x96-967f34b6716bb3cffebefe1fa53206d38d60c72635e826ec5d172c58fb4ab5a9.png" />
- <link rel="icon" type="image/png" sizes="16x16" href="/assets/community/favicon-16x16-ce9c1eb6d969d5b2ec276ae4b715756aa0e63106f33dc53a836687fdfee49ecd.png" />
- <meta name="msapplication-TileImage" content="/assets/community/ms-icon-144x144-3018401d3d675917b06ded0482cdf53a7c3558b4dc7b6b8de21a485a8da27dfe.png" />
- <meta name="msapplication-TileColor" content="#0069ff" />
- <meta name="theme-color" content="#0069ff" />
- <meta name="og:title" content="How To Install and Use Docker on Debian 10 | DigitalOcean">
- <meta name="og:description" content="Docker is an application that simplifies the process of managing application processes in containers. In this tutorial, you'll install and use Docker Community Edition (CE) on Debian 10. You'll install Docker itself, work with containers and images, a">
- <meta name="og:site_name" content="DigitalOcean">
- <meta name="og:type" content="article">
- <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">
- <meta name="twitter:site" content="DigitalOcean">
- <meta name="twitter:title" content="How To Install and Use Docker on Debian 10 | DigitalOcean">
- <meta name="twitter:description" content="Docker is an application that simplifies the process of managing application processes in containers. In this tutorial, you'll install and use Docker Community Edition (CE) on Debian 10. You'll install Docker itself, work with containers and images, a">
- <meta name="twitter:creator" content="DigitalOcean">
- <meta name="twitter:card" content="photo" />
- <meta name="twitter:url" content="https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-debian-10"/>
- <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">
- <link rel='canonical' href='https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-debian-10'>
- <link rel='amphtml' href='https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-debian-10.amp'>
- <link rel="preconnect" href="https://digitalocean.cdn.prismic.io">
- <link rel="preconnect" href="https://hello.myfonts.net">
- <link rel="stylesheet" media="all" href="/assets/community/application-1d6f1f0ccf88e772200c0b6525375710b6023a41177a7e17ae81ab21dcd67c22.css" />
-
- <h1> How to Install and Use Docker on Debian 10 </h1>
- <div class="content-body tutorial-content" data-growable-markdown>
-
- <h3 id="introduction">Introduction</h3>
- <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’re similar to virtual machines, but containers are more portable, more resource-friendly, and more dependent on the host operating system. </p>
- <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>
- <p>In this tutorial, you’ll install and use Docker Community Edition (CE) on Debian 10. You’ll install Docker itself, work with containers and images, and push an image to a Docker Repository.</p>
- <h2 id="prerequisites">Prerequisites</h2>
- <p>To follow this tutorial, you will need the following:</p>
- <ul>
- <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>
- <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>
- </ul>
- <h2 id="step-1-—-installing-docker">Step 1 — Installing Docker</h2>
- <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’ll install Docker from the official Docker repository. To do that, we’ll add a new package source, add the GPG key from Docker to ensure the downloads are valid, and then install the package.</p>
- <p>First, update your existing list of packages:</p>
- <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">sudo apt update
- </li></ul></code></pre>
- <p>Next, install a few prerequisite packages which let <code>apt</code> use packages over HTTPS:</p>
- <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
- </li></ul></code></pre>
- <p>Then add the GPG key for the official Docker repository to your system:</p>
- <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 -
- </li></ul></code></pre>
- <p>Add the Docker repository to APT sources:</p>
- <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"
- </li></ul></code></pre>
- <p>Next, update the package database with the Docker packages from the newly added repo:</p>
- <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">sudo apt update
- </li></ul></code></pre>
- <p>Make sure you are about to install from the Docker repo instead of the default Debian repo:</p>
- <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">apt-cache policy docker-ce
- </li></ul></code></pre>
- <p>You’ll see output like this, although the version number for Docker may be different:</p>
- <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:
- Installed: (none)
- Candidate: <span class="highlight">5:18.09.7~3-0~debian-buster</span>
- Version table:
- <span class="highlight">5:18.09.7~3-0~debian-buster</span> 500
- 500 https://download.docker.com/linux/debian buster/stable amd64 Packages
- </code></pre>
- <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>
- <p>Finally, install Docker:</p>
- <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">sudo apt install docker-ce
- </li></ul></code></pre>
- <p>Docker is now installed, the daemon started, and the process enabled to start on boot. Check that it’s running:</p>
- <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">sudo systemctl status docker
- </li></ul></code></pre>
- <p>The output will be similar to the following, showing that the service is active and running:</p>
- <pre class="code-pre "><code><div class="secondary-code-label " title="Output">Output</div>● docker.service - Docker Application Container Engine
- Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
- Active: <span class="highlight">active</span> (running) since Mon 2019-07-08 15:11:19 UTC; 58s ago
- Docs: https://docs.docker.com
- Main PID: 5709 (dockerd)
- Tasks: 8
- Memory: 31.6M
- CGroup: /system.slice/docker.service
- └─5709 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
- </code></pre>
- <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’ll explore how to use the <code>docker</code> command later in this tutorial.</p>
- <h2 id="step-2-—-executing-the-docker-command-without-sudo-optional">Step 2 — Executing the Docker Command Without Sudo (Optional)</h2>
- <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’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’ll get an output like this:</p>
- <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?.
- See 'docker run --help'.
- </code></pre>
- <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>
- <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">sudo usermod -aG docker ${USER}
- </li></ul></code></pre>
- <p>To apply the new group membership, log out of the server and back in, or type the following:</p>
- <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">su - ${USER}
- </li></ul></code></pre>
- <p>You will be prompted to enter your user’s password to continue. </p>
- <p>Confirm that your user is now added to the <strong>docker</strong> group by typing:</p>
- <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">id -nG
- </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>
- </code></pre>
- <p>If you need to add a user to the <code>docker</code> group that you’re not logged in as, declare that username explicitly using:</p>
- <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">sudo usermod -aG docker <span class="highlight">username</span>
- </li></ul></code></pre>
- <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>
- <p>Let’s explore the <code>docker</code> command next.</p>
- <h2 id="step-3-—-using-the-docker-command">Step 3 — Using the Docker Command</h2>
- <p>Using <code>docker</code> consists of passing it a chain of options and commands followed by arguments. The syntax takes this form:</p>
- <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">docker [option] [command] [arguments]
- </li></ul></code></pre>
- <p>To view all available subcommands, type:</p>
- <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">docker
- </li></ul></code></pre>
- <p>As of Docker 18, the complete list of available subcommands includes:</p>
- <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
- build Build an image from a Dockerfile
- commit Create a new image from a container's changes
- cp Copy files/folders between a container and the local filesystem
- create Create a new container
- diff Inspect changes to files or directories on a container's filesystem
- events Get real time events from the server
- exec Run a command in a running container
- export Export a container's filesystem as a tar archive
- history Show the history of an image
- images List images
- import Import the contents from a tarball to create a filesystem image
- info Display system-wide information
- inspect Return low-level information on Docker objects
- kill Kill one or more running containers
- load Load an image from a tar archive or STDIN
- login Log in to a Docker registry
- logout Log out from a Docker registry
- logs Fetch the logs of a container
- pause Pause all processes within one or more containers
- port List port mappings or a specific mapping for the container
- ps List containers
- pull Pull an image or a repository from a registry
- push Push an image or a repository to a registry
- rename Rename a container
- restart Restart one or more containers
- rm Remove one or more containers
- rmi Remove one or more images
- run Run a command in a new container
- save Save one or more images to a tar archive (streamed to STDOUT by default)
- search Search the Docker Hub for images
- start Start one or more stopped containers
- stats Display a live stream of container(s) resource usage statistics
- stop Stop one or more running containers
- tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
- top Display the running processes of a container
- unpause Unpause all processes within one or more containers
- update Update configuration of one or more containers
- version Show the Docker version information
- wait Block until one or more containers stop, then print their exit codes
- </code></pre>
- <p>To view the options available to a specific command, type:</p>
- <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">docker <span class="highlight">docker-subcommand</span> --help
- </li></ul></code></pre>
- <p>To view system-wide information about Docker, use:</p>
- <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">docker info
- </li></ul></code></pre>
- <p>Let’s explore some of these commands. We’ll start by working with images.</p>
- <h2 id="step-4-—-working-with-docker-images">Step 4 — Working with Docker Images</h2>
- <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’ll need will have images hosted there.</p>
- <p>To check whether you can access and download images from Docker Hub, type:</p>
- <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">docker run hello-world
- </li></ul></code></pre>
- <p>The output will indicate that Docker in working correctly:</p>
- <pre class="code-pre "><code><div class="secondary-code-label " title="Output">Output</div>Unable to find image 'hello-world:latest' locally
- latest: Pulling from library/hello-world
- 1b930d010525: Pull complete
- Digest: sha256:41a65640635299bab090f783209c1e3a3f11934cf7756b09cb2f1e02147c6ed8
- Status: Downloaded newer image for hello-world:latest
- Hello from Docker!
- This message shows that your installation appears to be working correctly.
- ...
- </code></pre>
- <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>
- <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>
- <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">docker search ubuntu
- </li></ul></code></pre>
- <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>
- <pre class="code-pre "><code><div class="secondary-code-label " title="Output">Output</div>NAME DESCRIPTION STARS OFFICIAL AUTOMATED
- ubuntu Ubuntu is a Debian-based Linux operating sys… 9704 [OK]
- dorowu/ubuntu-desktop-lxde-vnc Docker image to provide HTML5 VNC interface … 319 [OK]
- rastasheep/ubuntu-sshd Dockerized SSH service, built on top of offi… 224 [OK]
- consol/ubuntu-xfce-vnc Ubuntu container with "headless" VNC session… 183 [OK]
- ubuntu-upstart Upstart is an event-based replacement for th… 99 [OK]
- ansible/ubuntu14.04-ansible Ubuntu 14.04 LTS with ansible 97 [OK]
- neurodebian NeuroDebian provides neuroscience research s… 57 [OK]
- 1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 50 [OK]
- ubuntu
- ...
- </code></pre>
- <p>In the <strong>OFFICIAL</strong> column, <strong>OK</strong> indicates an image built and supported by the company behind the project. Once you’ve identified the image that you would like to use, you can download it to your computer using the <code>pull</code> subcommand.</p>
- <p>Execute the following command to download the official <code>ubuntu</code> image to your computer:</p>
- <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">docker pull ubuntu
- </li></ul></code></pre>
- <p>You’ll see the following output:</p>
- <pre class="code-pre "><code><div class="secondary-code-label " title="Output">Output</div>Using default tag: latest
- latest: Pulling from library/ubuntu
- 5b7339215d1d: Pull complete
- 14ca88e9f672: Pull complete
- a31c3b1caad4: Pull complete
- b054a26005b7: Pull complete
- Digest: sha256:9b1702dcfe32c873a770a32cfd306dd7fc1c4fd134adfb783db68defc8894b3c
- Status: Downloaded newer image for ubuntu:latest
- </code></pre>
- <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>
- <p>To see the images that have been downloaded to your computer, type:</p>
- <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">docker images
- </li></ul></code></pre>
- <p>The output should look similar to the following:</p>
- <pre class="code-pre "><code><div class="secondary-code-label " title="Output">Output</div>REPOSITORY TAG IMAGE ID CREATED SIZE
- ubuntu latest 4c108a37151f 2 weeks ago 64.2MB
- hello-world latest fce289e99eb9 6 months ago 1.84kB
- </code></pre>
- <p>As you’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>
- <p>Let’s look at how to run containers in more detail.</p>
- <h2 id="step-5-—-running-a-docker-container">Step 5 — Running a Docker Container</h2>
- <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>
- <p>As an example, let’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>
- <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">docker run -it ubuntu
- </li></ul></code></pre>
- <p>Your command prompt should change to reflect the fact that you’re now working inside the container and should take this form:</p>
- <pre class="code-pre second-environment"><code><div class="secondary-code-label " title="Output">Output</div>root@d9b100f2f636:/#
- </code></pre>
- <p>Note the container id in the command prompt. In this example, it is <code>d9b100f2f636</code>. You’ll need that container ID later to identify the container when you want to remove it.</p>
- <p>Now you can run any command inside the container. For example, let’s update the package database inside the container. You don’t need to prefix any command with <code>sudo</code>, because you’re operating inside the container as the <strong>root</strong> user:</p>
- <pre class="code-pre custom_prefix prefixed second-environment"><code><ul class="prefixed"><li class="line" prefix="root@d9b100f2f636:/#">apt update
- </li></ul></code></pre>
- <p>Then install any application in it. Let’s install Node.js:</p>
- <pre class="code-pre custom_prefix prefixed second-environment"><code><ul class="prefixed"><li class="line" prefix="root@d9b100f2f636:/#">apt install nodejs
- </li></ul></code></pre>
- <p>This installs Node.js in the container from the official Ubuntu repository. When the installation finishes, verify that Node.js is installed:</p>
- <pre class="code-pre custom_prefix prefixed second-environment"><code><ul class="prefixed"><li class="line" prefix="root@d9b100f2f636:/#">node -v
- </li></ul></code></pre>
- <p>You’ll see the version number displayed in your terminal:</p>
- <pre class="code-pre second-environment"><code><div class="secondary-code-label " title="Output">Output</div>v8.10.0
- </code></pre>
- <p>Any changes you make inside the container only apply to that container. </p>
- <p>To exit the container, type <code>exit</code> at the prompt.</p>
- <p>Let’s look at managing the containers on our system next.</p>
- <h2 id="step-6-—-managing-docker-containers">Step 6 — Managing Docker Containers</h2>
- <p>After using Docker for a while, you’ll have many active (running) and inactive containers on your computer. To view the <strong>active ones</strong>, use:</p>
- <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">docker ps
- </li></ul></code></pre>
- <p>You will see output similar to the following:</p>
- <pre class="code-pre "><code><div class="secondary-code-label " title="Output">Output</div>CONTAINER ID IMAGE COMMAND CREATED
- </code></pre>
- <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>
- <p>To view all containers — active and inactive, run <code>docker ps</code> with the <code>-a</code> switch:</p>
- <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">docker ps -a
- </li></ul></code></pre>
- <p>You’ll see output similar to this:</p>
- <pre class="code-pre "><code>CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- d42d0bbfbd35 ubuntu "/bin/bash" About a minute ago Exited (0) 20 seconds ago friendly_volhard
- 0740844d024c hello-world "/hello" 3 minutes ago Exited (0) 3 minutes ago elegant_neumann
- </code></pre>
- <p>To view the latest container you created, pass it the <code>-l</code> switch:</p>
- <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">docker ps -l
- </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
- </li><li class="line" prefix="$">d42d0bbfbd35 ubuntu "/bin/bash" About a minute ago Exited (0) 34 seconds ago friendly_volhard
- </li></ul></code></pre>
- <p>To start a stopped container, use <code>docker start</code>, followed by the container ID or the container’s name. Let’s start the Ubuntu-based container with the ID of <code>d9b100f2f636</code>:</p>
- <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">docker start <span class="highlight">d42d0bbfbd35</span>
- </li></ul></code></pre>
- <p>The container will start, and you can use <code>docker ps</code> to see its status:</p>
- <pre class="code-pre "><code>CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- d42d0bbfbd35 ubuntu "/bin/bash" About a minute ago Up 8 seconds friendly_volhard
- </code></pre>
- <p>To stop a running container, use <code>docker stop</code>, followed by the container ID or name. This time, we’ll use the name that Docker assigned the container, which is <code>friendly_volhard</code>:</p>
- <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">docker stop <span class="highlight">friendly_volhard</span>
- </li></ul></code></pre>
- <p>Once you’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>
- <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">docker rm <span class="highlight">elegant_neumann</span>
- </li></ul></code></pre>
- <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’s stopped. See the <code>docker run help</code> command for more information on these options and others.</p>
- <p>Containers can be turned into images which you can use to build new containers. Let’s look at how that works.</p>
- <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>
- <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>
- <p>This section shows you how to save the state of a container as a new Docker image.</p>
- <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>
- <p>Then commit the changes to a new Docker image instance using the following command. </p>
- <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>
- </li></ul></code></pre>
- <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>
- <p>For example, for the user <strong>sammy</strong>, with the container ID of <code>d9b100f2f636</code>, the command would be:</p>
- <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
- </li></ul></code></pre>
- <p>When you <em>commit</em> an image, the new image is saved locally on your computer. Later in this tutorial, you’ll learn how to push an image to a Docker registry like Docker Hub so others can access it.</p>
- <p>Listing the Docker images again will show the new image, as well as the old one that it was derived from:</p>
- <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">docker images
- </li></ul></code></pre>
- <p>You’ll see output like this:</p>
- <pre class="code-pre "><code><div class="secondary-code-label " title="Output">Output</div>REPOSITORY TAG IMAGE ID CREATED SIZE
- <span class="highlight">sammy/ubuntu-nodejs</span> latest d441c62350b4 10 seconds ago 152MB
- ubuntu latest 4c108a37151f 2 weeks ago 64.2MB
- hello-world latest fce289e99eb9 6 months ago 1.84kB
- </code></pre>
- <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>
- <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’s outside the scope of this tutorial.</p>
- <p>Now let’s share the new image with others so they can create containers from it.</p>
- <h2 id="step-8-—-pushing-docker-images-to-a-docker-repository">Step 8 — Pushing Docker Images to a Docker Repository</h2>
- <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>
- <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>
- <p>To push your image, first log into Docker Hub. </p>
- <pre class="code-pre command prefixed"><code><ul class="prefixed"><li class="line" prefix="$">docker login -u <span class="highlight">docker-registry-username</span>
- </li></ul></code></pre>
- <p>You’ll be prompted to authenticate using your Docker Hub password. If you specified the correct password, authentication should succeed. </p>
- <span class='note'><p>
- <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>
- <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
- </li></ul></code></pre>
- <p></p></span>
- <p>Then you may push your own image using:</p>
- <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>
- </li></ul></code></pre>
- <p>To push the <code>ubuntu-nodejs</code> image to the <strong>sammy</strong> repository, the command would be:</p>
- <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>
- </li></ul></code></pre>
- <p>The process may take some time to complete as it uploads the images, but when completed, the output will look like this:</p>
- <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]
- e3fbbfb44187: Pushed
- 5f70bf18a086: Pushed
- a3b5c80a4eba: Pushed
- 7f18b442972b: Pushed
- 3ce512daaf78: Pushed
- 7aae4540b42d: Pushed
- ...
- </code></pre>
- <p>After pushing an image to a registry, it should be listed on your account’s dashboard, like that show in the image below.</p>
- <p><img src="https://assets.digitalocean.com/articles/docker_1804/ec2vX3Z.png" alt="New Docker image listing on Docker Hub"></p>
- <p>If a push attempt results in an error of this sort, then you likely did not log in:</p>
- <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]
- e3fbbfb44187: Preparing
- 5f70bf18a086: Preparing
- a3b5c80a4eba: Preparing
- 7f18b442972b: Preparing
- 3ce512daaf78: Preparing
- 7aae4540b42d: Waiting
- unauthorized: authentication required
- </code></pre>
- <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>
- <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>
- <h2 id="conclusion">Conclusion</h2>
- <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>
- </div>
- </div>
- </body>
- </html>
|