Skip to content

Instantly share code, notes, and snippets.

@ostens
Last active January 14, 2020 11:39
Show Gist options
  • Save ostens/cf0fd2df72b758dca8b2ad7888c9b375 to your computer and use it in GitHub Desktop.
Save ostens/cf0fd2df72b758dca8b2ad7888c9b375 to your computer and use it in GitHub Desktop.
Docker cheat sheet

Docker

Docker is a platform which uses containers to deploy applications. We use containers because they are:

  • Lightweight - they're much more efficient with system resources than virtual machines (VMs)
  • Portable - they can be efficiently built, deployed and run anywhere
  • Scalable - it's easy to increase and distribute containers across a datacenter
  • Isolated - they are isolated from the host and other containers, and interact with their own private filesystem which is provided by the docker image (which includes everything needed to run an application).

Because containerized applications are so portable and the processes are easily reproducible, they can easily be scaled. To do so, we use orchestrators such as Kubernetes and Docker Swarm.

Contents

Installation

You can install docker for windows from docker hub.

Installing Kubernetes

Kubernetes can be enabled in Docker Desktop from Settings > Enable Kubernetes. This will install Kubernetes.

Docker swarm is built in with docker desktop, so you don't need to install or enable it.

  • You can initialize docker swarm mode using docker swarm init

You should get a log that informs you that a swarm has been initialised, which node is the swarm manager, and how to add a worker or manager to the swarm.

An image is a template with instructions for creating a docker container. This is usually based on another image, pulled from dockerhub, with some additional commands.

Image templates are called Dockerfiles. The instructions contained usually describe how the filesystem should be assembled and sometimes how to run the resulting container. Each instruction in a dockerfile creates an image layer, and when a dockerfile is updated and the corresponding image is rebuilt, only the changed layers are rebuilt.

Common dockerfile instructions:

  • FROM - the starting point of the image - usually an official image pulled down from dockerhub
  • WORKDIR - all subsequent actions should be from the directory specified here (in the image filesystem)
  • COPY - copy a file/directory from the host to the image (relative to the working directory)
  • RUN - run a command inside the image filesystem
  • CMD - this is a little different as it is specifying how to run a container based off of this image

Usually if using npm or yarn, you might see two COPY commands - one, the package.json, or the specification of what needs to be installed - this is then installed - before everything else is copied over with the second COPY.

Before you can run a container, you need to build the image from the instructions in the dockerfile:

docker image build -t app:1.0 .

Containers are runnable instances of images which can be created, started, stopped, moved and deleted. A container is defined by it's image.

Common commands for running containers:

  • docker run -it ubuntu /bin/bash run the ubuntu interactively using bash

Compose is a tool for running multi-container applications. It uses a YAML file to configure the services.

It's commonly used for development environments and automated testing environments.

To start the application, use:

docker-compose up

You can describe pods in a Kubernetes YAML file. You can set up multiple objects, e.g. a Deployment and a Service.

  • replicas determines how many copies of your pod should have.
  • template/spec/containers describes the pod

The Service could be a NodePort service to route traffic from host port to container port.

Once the YAML file is set up, the application as a whole can be deployed to Kubernetes using kubectl apply -f kubeconfig.yml

You can list your deployments using kubectl get deployments and kubectl get services. You should see a default kubernetes service and router service.

You can tear down the application using kubectl delete -f kubeconfig.yml.

Swarm objects are described in manifests called stack files.

You can deploy an application to Swarm using docker stack deploy -c app-stack.yml demo.

Swarm will create your service(s) in addition to a default docker network.

You can list your services using docker service ls.

You can tear down the stack using docker stack rm demo.

Build

  • docker build -t image:1.0 .: build and tag an image from the dockerfile in the current directory
  • docker image ls: list all images stored locally with the docker engine
  • docker image rm image:1.0: remove an image from the local store

Run

  • docker run --name web -p 5000:80 image:1.0: create a new container of an image and run it
  • docker start web: restart a stopped container
  • docker stop web: stop a running container
  • docker kill web: kill a running container
  • docker network ls: list networks
  • docker container ls: list running containers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment