Docker

What is a Container?

A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another. A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings. [ Ref ]

Build

  • docker build -t myimage:1.0 . Build an image from the Dockerfile in the current directory and tag the image
  • docker image ls List all images that are locally stored with the Docker Engine

Share

  • docker pull myimage:1.0 Pull an image from a registry
  • docker tag myimage:1.0 myrepo/ myimage:2.0 Retag a local image with a new image name and tag
  • docker push myrepo/myimage:2.0 Push an image to a registry

Network

  • docker network ls List the networks
  • docker network inspect Display detailed information on one or more networks
  • docker network create Create a network

Volumes

  • docker volume ls List the volumes
  • docker volume inspect my-volume Display detailed information on one or more volumes

Run

  • docker container run --name web -p 5000:80 alpine:3.9 Run a container from the Alpine version 3.9 image, name the running container “web” and expose port 5000 externally, mapped to port 80 inside the container.
  • docker container stop web Stop a running container through SIGTERM
  • docker container kill web Stop a running container through SIGKILL
  • docker container ls List the running containers (add --all to include stopped containers)
  • docker container logs --tail 100 web Print the last 100 lines of a container’s logs

Cleanup

  • docker container rm -f $(docker ps -aq) Delete all running and stopped containers
  • docker image rm alpine:3.4 Delete an image from the local image store
  • docker rmi -f $(docker images -q) Remove all docker images and related containers
  • docker network rm my-network Remove the network named ‘my-network’.
  • docker network prune Remove all unused networks
  • docker volume prune Remove all unused local volumes
  • docker volume rm my-volume Remove one or more volumes

More

  • docker stats Display a live stream of container(s) resource usage statistics
  • docker top Display the running processes of a container
  • docker version Show the Docker version information
  • docker info Display system-wide information
  • docker exec -it <container-id> pwd Run a command in a running container
  • docker cp host_source_path/. my-container-id:/target Copy files/folders between a container and the local filesystem
  • docker cp my-container-id:/path/. host_target_path