Docker and Kubernetes

What is Docker?

  • Docker defines itself as “a platform for developers and sysadmins to develop, ship, and run applications.”
  • Gabriel N. Schenker defines Docker 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 processes 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 applications massively.

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 follows this form:

  • 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 thecontext thecommand theprocess

Docker General

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

Docker Container

  • Start Container: docker run name-of-container
    • If we do not have a local copy of the image it will be pulled from the Docker repository.
    • 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 <port>:<port> --name <name-of-container> <name-of-image>
    • Example: docker run --detach --publish 80:80 --name nginx
    • One can use -P to map all ports.
  • 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

Dockerfile

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
  • List Stacks: dockerstack ls
  • Run Docker Compose File: docker stack deploy -c <composefile> <appname>
  • 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

Tooling

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.