Friday, November 1, 2019

Docker Commands

Set Environment Settings:

  • DOCKER_CERT_PATH=C:\Users\jini\.docker\machine\certs
  • DOCKER_HOST=tcp://192.168.99.100:2376
  • DOCKER_TLS_VERIFY=1
  • DOCKER_TOOLBOX_INSTALL_PATH= C:\Program Files\Docker Toolbox

Docker Lifecycle:

  • docker run creates a container.
  • docker stop stops it.
  • docker start will start it again.
  • docker restart restarts a container.
  • docker rm deletes a container.
  • docker kill sends a SIGKILL to a container.
  • docker attach will connect to a running container.
  • docker wait blocks until container stops.
If you want to run and then interact with a container, docker start then docker attach to get in.
If you want to poke around in an image, docker run -t -i to open a tty.

Docker Info:

  • docker ps -a shows running and stopped containers.
  • docker inspect looks at all the info on a container (including IP address).
  • docker logs gets logs from container.
  • docker events gets events from container.
  • docker port shows public facing port of container.
  • docker top shows running processes in container.
  • docker diff shows changed files in the container’s FS.

Docker Images/Container Lifecycle:

  • docker images shows all images.
  • docker import creates an image from a tarball.
  • docker build creates image from Dockerfile.
  • docker commit creates image from a container.
  • docker rmi removes an image.
  • docker insert inserts a file from URL into image. (kind of odd, you’d think images would be immutable after create)
  • docker load loads an image from a tar archive as STDIN, including images and tags (as of 0.7).
  • docker save saves an image to a tar archive stream to STDOUT with all parent layers, tags & versions (as of 0.7).

Info

  • docker history shows history of image.
  • docker tag tags an image to a name (local or registry).

Docker Compose

Define and run multi-container applications with Docker.
  • docker-compose --help
  • create docker-compose.yml
    version: '3'
    services:
      eureka:
        restart: always
        build: ./micro1-eureka-server
        ports:
          - "8761:8761"
  • docker-compose stop
    docker-compose rm -f
    docker-compose build
    docker-compose up -d
    docker-compose start
    docker-compose ps
  • Scaling containers running a given service
    docker-compose scale eureka=3
  • Healing, i.e., re-running containers that have stopped
    docker-compose up --no-recreate

Docker Hub

Docker.io hosts its own index to a central registry which contains a large number of repositories.

Dockerfile

Instructions

  •     .dockerignore
  •     FROM Sets the Base Image for subsequent instructions.
  •     MAINTAINER (deprecated - use LABEL instead)
  •     RUN execute any commands in a new layer on top of the current image
  •     CMD provide defaults for an executing container.
  •     EXPOSE informs Docker that the container listens on the specified network ports at runtime.
  •     ENV sets environment variable.
  •     ADD copies new files, directories or remote file to container. Invalidates caches.
        Avoid ADD and use COPY instead.
  •     COPY copies new files or directories to container. By default this copies as root regardless    of the USER/WORKDIR settings. Use --chown=: to give ownership to another user/group. (Same for ADD.)
  •     ENTRYPOINT configures a container that will run as an executable.
  •     VOLUME creates a mount point for externally mounted volumes or other containers.
  •     USER sets the user name for following RUN / CMD / ENTRYPOINT commands.
  •     WORKDIR sets the working directory.
  •     ARG defines a build-time variable.
  •     ONBUILD adds a trigger instruction when the image is used as the base for another build.
  •     STOPSIGNAL sets the system call signal that will be sent to the container to exit.
  •     LABEL apply key/value metadata to your images, containers, or daemons.

Examples: Examples
Best Practices: Best to look at http://github.com/wsargent/docker-devenv and the best practices / take 2 for more details.
Volumes:
Docker volumes are free-floating filesystems. They don’t have to be connected to a particular container.
Volumes are useful in situations where you can’t use links (which are TCP/IP only). For instance, if you need to have two docker instances communicate by leaving stuff on the filesystem.
You can mount them in several docker containers at once, using docker run -volume-from

Get Environment Settings
docker run --rm ubuntu env
Delete old containers
docker ps -a | grep 'weeks ago' | awk '{print $1}' | xargs docker rm
Delete stopped containers
docker rm `docker ps -a -q`
Show image dependencies
docker images -viz | dot -Tpng -o docker.png
Original
https://github.com/wsargent/docker-cheat-sheet/blob/master/README.md