Docker

docker – Docker image and container command line interface

https://docs.docker.com/engine/reference/commandline/docker/

https://docs.docker.com/engine/reference/commandline/cli/

Management Commands:
  builder     Manage builds
  config      Manage Docker configs
  container   Manage containers
  context     Manage contexts
  engine      Manage the docker engine
  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

docker-container – Manage containers

https://docs.docker.com/engine/reference/commandline/container/

docker container inspectDisplay detailed information on one or more containers
docker container lsList containers
docker container inspect 802264e0b5b5

docker-image – Manage images

https://docs.docker.com/engine/reference/commandline/image/

docker image lsList images
docker image pruneRemove unused images
docker image rmRemove one or more images
docker image historyShow the history of an image
docker image ls
docker image rm IMAGE_ID
docker image history getting-started

Pull all new images

docker images --format "{{.Repository}}:{{.Tag}}"
docker images --format "{{.Repository}}:{{.Tag}}" | xargs -L1 docker pull

Thx to https://dev.to/imjoseangel

docker-network – Manage networks

https://docs.docker.com/engine/reference/commandline/network/

docker network connectConnect a container to a network
docker network createCreate a network
docker network disconnectDisconnect a container from a network
docker network inspectDisplay detailed information on one or more networks
docker network lsList networks
docker network pruneRemove all unused networks
docker network rmRemove one or more networks
docker network create todo-app

docker-system – Manage Docker

https://docs.docker.com/engine/reference/commandline/system/

docker system dfShow docker disk usage
docker system eventsGet real time events from the server
docker system infoDisplay system-wide information
docker system pruneRemove unused data

Prune everything

docker system prune

docker-volume – Manage volumes

https://docs.docker.com/engine/reference/commandline/volume/

docker volume createCreate a volume
docker volume inspectDisplay detailed information on one or more volumes
docker volume lsList volumes
docker volume pruneRemove all unused local volumes
docker volume rmRemove one or more volumes
docker volume create todo-db
docker volume inspect todo-db

docker-cp – Copy files/folders between a container and the local filesystem

https://docs.docker.com/engine/reference/commandline/cp/

docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-      docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
docker cp tmp1:/etc/nginx ./config

docker-exec – Run a command in a running container

--interactive , -iKeep STDIN open even if not attached
--tty , -tAllocate a pseudo-TTY

Enter a container (container ID given by docker ps):

docker exec -it 659eb156db55 /bin/bash
docker exec 985cd3b9eae4 more /data.txt

Manage MySQL in a container:

docker exec -it a535a22b59f2 mysql --user=allowed_user -p

Test and reload configuration of nginx in container fc499fea8938:

docker exec fc499fea8938 nginx -t
docker exec fc499fea8938 nginx -s reload

docker-logs – Fetch the logs of a container

docker logs 2dc4059b4ade
docker logs -f ed8d574e096b

docker-rm – Remove one or more containers

Stop and remove container ID 35ed911da1ec

# docker rm -f 35ed911da1ec

Remove all containers:

# docker rm $(docker ps -aq)

docker-run – Run a command in a new container

https://docs.docker.com/engine/reference/commandline/run/

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
--interactive , -iKeep STDIN open even if not attached
--tty , -tAllocate a pseudo-TTY
--rmAutomatically remove the container when it exits
--detach , -dRun container in background and print container ID
--publish , -pPublish a container’s port(s) to the host
--name=""Assign a name to the container
--volume , -vBind mount a volume
--networkConnect a container to a network
--network-aliasAdd network-scoped alias for the container

Run a container:

docker run -d -p 80:80 docker/getting-started
  • -d – run the container in detached mode (in the background)
  • -p 80:80 – map port 80 of the host to port 80 in the container
  • docker/getting-started – the image to use
docker run --rm -it ubuntu /bin/bash

run a container based on the image ubuntu interactively and start /bin/bash in it.

docker run -dp 3000:3000 -v "$(pwd):/app" node:12-alpine

With a bind mount

docker-start – Start one or more stopped containers

docker start c26bbca70a75

docker-stop – Stop one or more running containers

docker start c26bbca70a75

Stop all containers

docker stop $(docker ps -q)

docker-compose

https://docs.docker.com/compose/reference/

https://docs.docker.com/compose/compose-file/

Commands:
  build              Build or rebuild services
  config             Validate and view the Compose file
  create             Create services
  down               Stop and remove containers, networks, images, and volumes
  events             Receive real time events from containers
  exec               Execute a command in a running container
  help               Get help on a command
  images             List images
  kill               Kill containers
  logs               View output from containers
  pause              Pause services
  port               Print the public port for a port binding
  ps                 List containers
  pull               Pull service images
  push               Push service images
  restart            Restart services
  rm                 Remove stopped containers
  run                Run a one-off command
  scale              Set number of containers for a service
  start              Start services
  stop               Stop services
  top                Display the running processes
  unpause            Unpause services
  up                 Create and start containers
  version            Show version information and quit

-d Detached mode. Run container in the background, print new container name.

docker-compose pull
docker-compose up -d
docker-compose logs -f
docker-compose logs -f app
docker-compose down
docker-compose down --volumes

Unsorted

Where are my container files ?

$ docker info | grep Root
 Docker Root Dir: /var/lib/docker
$ sudo ls -la
[...]
drwx-----x  13 root root  4096 Sep 13 10:55 volumes
/var/lib/docker/volumes/mycontainer/_data/

Difference between load and import

src: https://stackoverflow.com/questions/36925261/what-is-the-difference-between-import-and-load-in-docker

Restart policy

https://docs.docker.com/config/containers/start-containers-automatically/

Random

docker ps --all
docker stop d40214ea1a9d
docker rm CONTAINER_ID
docker system prune --all

To avoid weird “connection refused” errors, you have to put 8.8.8.8 in your DNS servers (I don’t exactly know why… >_<).

leopardb@cephadmin:~$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
docker: Error response from daemon: Get https://registry-1.docker.io/v2/: dial tcp 18.213.137.78:443: connect: connection refused.
See 'docker run --help'.