Docker

What is Docker?

Gabriel N. Schenker defines Docker containers as, “a means to package applications and their dependencies in a standardized way.”

Why Use Docker?

For a more extensive discussion of Docker’s advantages see Schenker’s Learn Docker – Fundamentals of Docker 18.x, specifically Chapter 1.

  • Automation – Using containers significantly increases the amount of automation that can occur.
  • Cost Savings – By using a thin layer as opposed to the thicker Virtual Machine model it allows more containers to be run on less hardware.
  • Developer Productivity – Allows for the creation of reusable and shareable development environments.
  • Deployability – It is easier and quicker to deploy applications to production when containerized.
  • Security – Use isolation built into Docker containers to keep applications from affecting one another.
  • Scalability – Using Kubernetes one can scale one’s software to a massive scale.

Docker Commands

The Docker CLI can be used in a number of different ways, some of the more common commands are listed below.

In general, a Docker command begins with docker (the name of the CLI we are using), a context (like containers), a command (like run), and can include specific processes to run in the container. Generically our command looks like this: docker somecontext somecommand someprocess

Docker General

  • Check Docker Version: docker –version
  • View Docker CLI Help: docker –help
    • Can also be run in a specific context/command like:
      docker container ls -h
  • Login to Docker Public Registry: docker login

Docker Container

  • Start Container: docker run name-of-container
    • Use –d to specify that this is a Linux daemon.Use –name to provide a friendly name for the container.
  • Gracefully stop container: docker container stop <hash>
    • If we have provided a container with a friendly name we can use that instead of the hash.
  • Force shutdown of container: docker container kill <hash>
  • List Containers: docker container ls –all
    • Show Only Running Containers: docker container ls
  • Give Container Friendly Name: docker build -t name .
  • Run Interactively: docker run –interactive –tty name-of-container bash
  • Run Detached: docker run –detach –publish 80:80 –name <name-of-container> <name-of-image>
  • Attach to Running Container: docker container attach <nameofcontainer>
    • To exit container without stopping press Ctrl+P Ctrl+Q
  • Remove specified container from local machine: docker container rm <hash>
    • Use -f or –force to force the removal.
  • Inspect Container: docker container inspect <name>
  • View Container Logs: docker container logs <nameofcontainer>
    • Use –tail or -t before nameofcontainer to get only the tail of the logs (include number of lines to return)Use –tail with –flow in above manner to continue getting newer log entries.
  • View Container Diff: docker container diff <nameofcontainer>
    • Shows a list of files added/modified/deleted since the base layer.

Docker Images

  • List Downloaded Images: docker image ls
  • Associate Docker Image with username/repo and tag: docker tag image username/repository:tag
  • Publish Image to the Repository: docker push username/repository:tag
  • Remove specified image from local machine: docker image rm <image id>
  • Persist Changes to a Container Into a New Image: docker container commit <name-of-container> <name-of-image>
  • View Changes Made to Build Image: docker image history <name-of-container>

Docker Virtual Machines

  • List Virtual Machines: docker-machine ls
  • Connect to Docker Virtual Machine: docker-machine ssh nameofvm

Minikube Commands

  • Start: minikube start
    • If running on Windows you may need to specify if you are using Hyper-V:
      minikube start –vm-driver=hyper –hyperv-virtual-switch=”Name of Switch”

Kubernetes Commands

  • See Current Context: kubectl config current-context
  • See All Contexts: kubectl config get-contexts
  • Change Context: kubectl config use-context name-of-context
  • Get Nodes in Current Context: kubectl get nodes

Dockerfile

  • Specify Base Image: FROM name-of-image
  • Define a Working Directory: WORKDIR /nameofdir
    • The name of directory in which the container will execute commands
    • Must be used, CD commands are not persisted across image layers.
  • Copy current directory contents into container at /nameofdir: ADD . /nameofdir
  • Expose a port outside of the container: EXPOSE portnumber
  • Define an environment variable: ENV NAME variable
  • Define Command to Be Run: RUN <linux-command>
  • Add Files to Image: ADD –CHOWN=<user>:<group> <name-of-file-source> <name-of-file-destination>
    • Can also pull from URL
    • Can unpack TAR files
  • Copy Files to Image: COPY –CHOWN=<user>:<group> <name-of-file-source> <name-of-file-destination>
    • Doesn’t have capabilities of ADD named above.
  • Run Command:
    ENTRYPOINT: <name-of-command>
    CMD: <parameters-of-command>
    • Can provide in Exec or Shell Format, former is preferred, uses JSON array.
    • Some choose not to use ENTRYPOINT and put both command and parameters in CMD.
  • Create Image Command: docker image build -t <name-of-image> .
    • Uses the Dockerfile in the current folder.
    • Use .dockerignore file to exclude files from being added to image.

Docker Compose

  • Used to define how Docker containers should behave in production (via docker-compose.yml).
  • Must run docker swarm init before attempting to deploy
  • Then run docker stack deploy: docker stack deploy -c docker-compose.yml nameofimage
  • dockerstack ls # list stacks or apps
  • docker stack deploy -c <composefile> <appname> # Run specified Compose file
  • docker inspect <task or container>
  • docker service ls
  • List tasks of service: docker service ps nameofservice
  • Shutdown app: docker stack rm nameofstack
  • Shutdown swarm: docker swarm leave –force

Terminology

  • Image – “An image is an executable package that includes everything needed to run an application–the code, a runtime, libraries, environment variables, and configuration files.” (source)
  • Container – “A container is a runtime instance of an image–what the image becomes in memory when executed (that is, an image with state, or a user process).” (source)
  • Container ID – A SHA-256 unique ID for a container.
  • Containerization
  • Dockerfile – “Defines what goes on in the environment inside your container. Access to resources like networking interfaces and disk drives is virtualized inside this environment, which is isolated from the rest of your system, so you need to map ports to the outside world, and be specific about what files you want to “copy in” to that environment.” (uncertain source)
  • Exposing a Port
  • Nodes – Each machine in a swarm.
  • Process ID
  • Publishing a Port
  • Service – Provides a specific portion of a distributed application (e.g. web server, database server, etc.)
    • “Services are really just “containers in production.” A service only runs one image, but it codifies the way that image runs—what ports it should use, how many replicas of the container should run so the service has the capacity it needs, and so on.” (source)
  • Stack
  • Swarm – A Dockerized cluster of multiple machines.
  • Swarm Manager – Only machines able to execute commands, authorize other machines to join.
  • Task – “A single container running in a service.” (uncertain source)
  • Virtual Machine
  • Virtualization
  • Workers – Machines that aren’t Swarm Managers.

Some Other Stuff…

  • Start with Install Docker for Windows.
  • Then Get Started with Docker for Windows.
  • Docker for Windows runs in the taskbar notifications area.
    • You’ll want to check “Expose daemon on tcp://localhost:2375 without TLS” if using PhpStorm.
    • Allows one to configure auto startup, sharing local drives with containers (only needed for Linux containers), set resources used by Docker, configure network, proxies, configure registries (where images are hosted), Kubernetes, reset configuration, and etc.

Bibliography

  • This page is based primarily on Docker Documentation and Gabriel N. Schenker’s Learn Docker 18.x.