Docker

Docker is an extremely popular containerization tool that makes it easy to create, deploy, run applications in containers.

Cheat Sheet
# build an image with some version
docker build -t someapp:version .

# build fresh without cache
docker build --no-cache -t someapp:version .

# detach and run
docker container run -d --name someapp -p 5000:5000 someapp:version

# detach and interactive
docker container run -d -it --name someapp -p 5000:5000 someapp:version

# show containers
docker ps

# show container even stopped ones
docker ps -a

docker container stop|rm|run

docker image ls|rm

# interactive container
docker exec -it containerid /bin/sh; exit

# following the logs
docker logs -f appname

# for mounting your code directory into the container
docker run -it --mount "type=bind,source=$(pwd),target=/usr/src/app" -d --name omega-blog -p 80:80 omega-blog:latest
docker run -it --mount "type=bind,source=$(pwd),target=/usr/src/app" -d --name stock_market_dashboard -p 3000:3000 stock_market_dashboard:latest

# create a new container based on a name and ssh into it directly
docker container run -it ubuntu:latest /bin/bash



# EXAMPLE

# to prune old images
docker image prune

# to prune old containers that are stopped
docker container prune

# remove old container
docker container stop omega-blog && docker container rm omega-blog

# remove old image
docker image rm `docker image ls | grep omega-blog | awk '{print $3}'`

# build new image
docker image build --no-cache -t omega-blog:latest .

# running with docker
docker run -it --mount "type=bind,source=$(pwd),target=/usr/src/app" -d --name omega-blog -p 80:80 omega-blog:latest

# to pass an environment file
docker run --env-file .env-doc-dev -it --mount "type=bind,source=$(pwd),target=/usr/src/app" -d --name iot-dashboard -p 9000:9000 iot-dashboard:latest

# to look at logs and follow
docker logs -f omega-blog

# actual codes to run
docker image build --no-cache -t iot-dashboard:latest .
docker run --env-file .env-doc-dev -it --mount "type=bind,source=$(pwd),target=/usr/src/app" -d --name iot-dashboard -p 9000:9000 iot-dashboard:latest
docker logs -f iot-dashboard

# to restart the container
docker container restart iot-dashboard

# to stop and rm the container
docker container stop iot-dashboard && docker container rm iot-dashboard

# to restart all containers
docker restart $(docker ps -a -q)

Docker Command Line

# getting help
$ docker help

Usage:	docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default "/Users/hammer/.docker")
  -c, --context string     Name of the context to use to connect to the daemon (overrides DOCKER_HOST
                           env var and default context set with "docker context use")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket(s) to connect to
  -l, --log-level string   Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default "/Users/hammer/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/Users/hammer/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/Users/hammer/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

Management Commands:
  builder     Manage builds
  config      Manage Docker configs
  container   Manage containers
  context     Manage contexts
  image       Manage images
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  secret      Manage Docker secrets
  service     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Commands:
  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

Run 'docker COMMAND --help' for more information on a command.