A lot more is possible than what I present here, I'll try to update this document when I meet something very specific.

Docker is a set of platform as service (PaaS) products that uses OS-level virtualization to deliver software in package called containers. -- Wikipedia

Difference between container and image

A docker image can be compared to a SNAPSHOT which we may meet in VMWare or other VME (Virtual Machine Environment).

On the other hand, a docker container is a running instance of a docker image.

Pull a docker image

By default, docker will work with the central registry which is located at https://index.docker.io.

So, to fetch the latest Archlinux image (for example):

$ docker pull archlinux/base

Pull a docker from another registry

If I had another registry, I could do something like this

$ docker pull docker_mirror.example.org/archlinux/base

Example 1: Image and container usage/interaction

After pulling the Archlinux image locally, it's now possible to run it inside a container.

  • To run and get a pseudo-TTY. In other words, run inside a container and create an interactive bash. Here an example of a session.

    $ docker run --name test -it archlinux/base
    [root@1c6f6c9972f9 /]# pacman -S git
    warning: database file for 'core' does not exist (use '-Sy' to download)
    warning: database file for 'extra' does not exist (use '-Sy' to download)
    warning: database file for 'community' does not exist (use '-Sy' to download)
    error: target not found: git
    [root@1c6f6c9972f9 /]# pacman -Sy
    :: Synchronizing package databases...
    core                                                                                                              133.8 KiB   168 KiB/s 00:01 [#######################################################################################] 100%
    extra                                                                                                            1642.3 KiB   378 KiB/s 00:04 [#######################################################################################] 100%
    community                                                                                                           4.8 MiB   372 KiB/s 00:13 [#######################################################################################] 100%
    [root@1c6f6c9972f9 /]# exit

    Note: As we exited the interactive bash, the container stopped running.

  • To see the list of the running containers.

    $ docker ps
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                       PORTS               NAMES
  • To see the list of all containers.

    $ docker ps -a
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                       PORTS               NAMES
    1c6f6c9972f9        archlinux/base      "/usr/bin/bash"     9 minutes ago       Exited (130) 2 minutes ago                       test
  • To delete the container.

    WARNING: Be sure that the container is not running.

    $ docker rm test # It's working because we gave the name `test` previously.
    $ docker rm 1c6f6c9972f9 # This is the one to use if no name was set.

Example 2: Image and container interaction while developing

Let's say we have a web application that I want to develop. I will normally use my local Nginx environment. But for learning purpose, let's say I want to develop and have a docker container which will serve my application locally.

My application has one file: index.html

<!DOCTYPE html>
<html>
  <head>
    <title>Lorem Ipsum</title>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
  </head>
  <body>
    <h1>Lorem Ipsum</h1>
    <p>
      Quis nostra cubilia cubilia dapibus consectetur suscipit. Euismod rutrum
      ac odio inceptos vehicula. Conubia accumsan congue fringilla dui lacinia
      est curabitur vel ligula integer est! Dictum nascetur nibh suspendisse
      rutrum bibendum sem senectus quisque. Mus lacus curabitur feugiat taciti
      taciti ac, posuere venenatis porta. Erat aenean himenaeos nisi, placerat
      pellentesque primis. Dictumst class natoque vestibulum consectetur
      posuere, elit consectetur. Libero consequat proin nisl vel viverra pretium
      urna; fames facilisi curabitur velit ligula. Duis etiam facilisis nostra
      orci faucibus consectetur! Placerat morbi vehicula, praesent lectus
      praesent nisi. Eros magna inceptos hendrerit ante. Ultrices purus nibh
      litora.
    </p>
    <p>
      Pulvinar facilisis donec volutpat neque turpis dignissim ad eleifend.
      Nibh; facilisis eget dapibus! Nisl quam sollicitudin himenaeos semper
      euismod eu nisi potenti habitasse faucibus! Conubia est tellus odio dis
      turpis congue tincidunt tellus phasellus, ultrices ornare eros. Nec in
      commodo; tincidunt dis in ac mauris dapibus mus ac habitasse! Fames
      elementum sapien congue duis in lacinia velit nostra viverra elementum
      aenean. Integer iaculis accumsan dictum amet himenaeos odio tortor
      parturient feugiat habitasse mollis eget. Torquent ornare ligula sagittis
      felis augue euismod mauris. Scelerisque dictum facilisi lectus taciti.
      Mauris platea.
    </p>
    <p>
      Elit duis odio pulvinar egestas nulla accumsan tincidunt ut suspendisse
      cras orci! Platea aliquam morbi tristique nec, lacinia sagittis proin
      porta magna cras nec praesent! Sit elementum at nulla quis gravida. Orci,
      ridiculus suscipit pretium! Sapien cursus bibendum congue tincidunt est
      posuere class nascetur. Sed rutrum egestas curae; sapien facilisi vehicula
      tempor non et vitae. Senectus montes ad scelerisque sociis non. Fermentum
      venenatis nisi interdum augue dui. Vehicula facilisi phasellus hac sit
      sociosqu ullamcorper justo aliquet dapibus nec. Laoreet arcu dictum
      egestas, primis sapien. Integer commodo senectus, luctus tempor nullam
      natoque suscipit.
    </p>
  </body>
</html>
  1. I pulled the latest Nginx image.

    $ docker pull nginx:alpine
  2. I ran a new container with everything I need.

    $ docker run --name lorem_ipsum -v ${PWD}:/usr/share/nginx/html:ro -p 8888:80  -d nginx
    20f5b2183a22bd1cadbaf9953ba9376f7c2c5d2c9ec61f9e1cb17483cd377075

    Notes:

    • The -v stands for --volume. What I'm saying to docker is that I want to current directory to be mounted as /user/share/nginx/html in the container's filesystem.
    • The :ro stands for read-only. That means that I forbid any modification to the mounted volume.
    • The -p stands for --port. What I'm saying to docker is that when a request comes through the 8888 port, it redirects to the 80 port inside the docker container.
  3. I checked that it's running.

    $ docker ps
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
    20f5b2183a22        nginx               "nginx -g 'daemon of…"   3 minutes ago       Up 4 minutes        0.0.0.0:8888->80/tcp   lorem_ipsum
    
  4. I checked that I could access with curl through http://host-ip:8888.

    $ curl http://host-ip:8888
    <!DOCTYPE html>
    <html>
       <head>
           <title>Lorem Ipsum</title>
           <meta name="viewport" content="width=device-width, initial-scale=1">
       </head>
       <body>
           <h1>Lorem Ipsum</h1>
           <p>
           Quis nostra cubilia cubilia dapibus consectetur suscipit. Euismod rutrum ac odio inceptos vehicula. Conubia accumsan congue fringilla dui lacinia est curabitur vel ligula integer est! Dictum nascetur nibh suspendisse rutrum bibendum sem senectus quisque. Mus lacus curabitur feugiat taciti taciti ac, posuere venenatis porta. Erat aenean himenaeos nisi, placerat pellentesque primis. Dictumst class natoque vestibulum consectetur posuere, elit consectetur. Libero consequat proin nisl vel viverra pretium urna; fames facilisi curabitur velit ligula. Duis etiam facilisis nostra orci faucibus consectetur! Placerat morbi vehicula, praesent lectus praesent nisi. Eros magna inceptos hendrerit ante. Ultrices purus nibh litora.
           </p>
           <p>
           Pulvinar facilisis donec volutpat neque turpis dignissim ad eleifend. Nibh; facilisis eget dapibus! Nisl quam sollicitudin himenaeos semper euismod eu nisi potenti habitasse faucibus! Conubia est tellus odio dis turpis congue tincidunt tellus phasellus, ultrices ornare eros. Nec in commodo; tincidunt dis in ac mauris dapibus mus ac habitasse! Fames elementum sapien congue duis in lacinia velit nostra viverra elementum aenean. Integer iaculis accumsan dictum amet himenaeos odio tortor parturient feugiat habitasse mollis eget. Torquent ornare ligula sagittis felis augue euismod mauris. Scelerisque dictum facilisi lectus taciti. Mauris platea.
           </p>
           <p>
           Elit duis odio pulvinar egestas nulla accumsan tincidunt ut suspendisse cras orci! Platea aliquam morbi tristique nec, lacinia sagittis proin porta magna cras nec praesent! Sit elementum at nulla quis gravida. Orci, ridiculus suscipit pretium! Sapien cursus bibendum congue tincidunt est posuere class nascetur. Sed rutrum egestas curae; sapien facilisi vehicula tempor non et vitae. Senectus montes ad scelerisque sociis non. Fermentum venenatis nisi interdum augue dui. Vehicula facilisi phasellus hac sit sociosqu ullamcorper justo aliquet dapibus nec. Laoreet arcu dictum egestas, primis sapien. Integer commodo senectus, luctus tempor nullam natoque suscipit.
           </p>
       </body>
    </html>
  5. I could then continue the development. I removed the last paragram.

  6. The changes were active (directly) without any change to the container.

    $ curl http://host-ip:8888
    <!DOCTYPE html>
    <html>
       <head>
           <title>Lorem Ipsum</title>
           <meta name="viewport" content="width=device-width, initial-scale=1">
       </head>
       <body>
           <h1>Lorem Ipsum</h1>
           <p>
           Quis nostra cubilia cubilia dapibus consectetur suscipit. Euismod rutrum ac odio inceptos vehicula. Conubia accumsan congue fringilla dui lacinia est curabitur vel ligula integer est! Dictum nascetur nibh suspendisse rutrum bibendum sem senectus quisque. Mus lacus curabitur feugiat taciti taciti ac, posuere venenatis porta. Erat aenean himenaeos nisi, placerat pellentesque primis. Dictumst class natoque vestibulum consectetur posuere, elit consectetur. Libero consequat proin nisl vel viverra pretium urna; fames facilisi curabitur velit ligula. Duis etiam facilisis nostra orci faucibus consectetur! Placerat morbi vehicula, praesent lectus praesent nisi. Eros magna inceptos hendrerit ante. Ultrices purus nibh litora.
           </p>
           <p>
           Pulvinar facilisis donec volutpat neque turpis dignissim ad eleifend. Nibh; facilisis eget dapibus! Nisl quam sollicitudin himenaeos semper euismod eu nisi potenti habitasse faucibus! Conubia est tellus odio dis turpis congue tincidunt tellus phasellus, ultrices ornare eros. Nec in commodo; tincidunt dis in ac mauris dapibus mus ac habitasse! Fames elementum sapien congue duis in lacinia velit nostra viverra elementum aenean. Integer iaculis accumsan dictum amet himenaeos odio tortor parturient feugiat habitasse mollis eget. Torquent ornare ligula sagittis felis augue euismod mauris. Scelerisque dictum facilisi lectus taciti. Mauris platea.
           </p>
       </body>
    </html>

Other useful commands

  • Stop a running container.

    $ docker stop test # Replace `test` with the container name or ID.
  • Kill a running container.

    $ docker kill test # Replace `test` with the container name or ID.
  • Delete a container.

    WARNING: The container must be stopped or killed.

    $ docker rm test # Replace `test` with the container name or ID.
  • List all locally available images.

    $ docker images
    $ docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

Cleanup

I could delete everything which was not in use with the following.

$ docker system prune -a
WARNING! This will remove:
  - all stopped containers
  - all networks not used by at least one container
  - all images without at least one container associated to them
  - all build cache

Are you sure you want to continue? [y/N] y
[...]

Links

Here is a list of useful links that provide more information if needed.