Advertisement
segal96

Docker

Aug 28th, 2016 (edited)
1,552
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. // Terms
  2. Image: Operating system image, stored in a repo. Single file. A template for an image
  3. Dockerfile: Definition of an image (source code of an image)
  4. Container: Instance of an image
  5. Registry: A repository to push and pull containers (like git)
  6. Microservices: Single process per container
  7. Fat containers: Similar to a virtual machine
  8.  
  9. // Notes
  10. - Digitalocean guides are a great resource!
  11. - By default, only root can use docker (attach to the docker daemon)
  12. - If you want non-priv users to use docker, do that through sudo
  13. OR
  14. - create a docker group and add them to it
  15. - By default, Docker containers do persist data when stopped, then started/exec'd later
  16. - By default, Docker containers do NOT persist data across "runs" (new containers from image)
  17. - Use docker data volumes to persist changes
  18. - See "data volume" containers and "--volumes-from" option when starting a container
  19. - You can also share data between the host and container
  20. - See docker run "-v" option
  21. - Docker generates random container names for you if you don't specify a name
  22.  
  23. // Determine how much space docker is using
  24. docker system df
  25.  
  26. // Clean up unused base images and shit taking up disk space, especially in /var/lib/docker/overlay2/
  27. docker system prune -a
  28.  
  29. // Remove all build cache
  30. docker buildx prune --all
  31. docker builder prune --all
  32.  
  33. // Installing LATEST version of docker on CentOS 7 (not version that comes with CentOS)
  34. https://github.com/NaturalHistoryMuseum/scratchpads2/wiki/Install-Docker-and-Docker-Compose-(Centos-7)
  35.  
  36. // Installing LATEST docker-compose
  37. curl -L "https://github.com/docker/compose/releases/download/1.23.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  38. chmod +x /usr/local/bin/docker-compose
  39.  
  40. // Show docker version
  41. docker version
  42.  
  43. // show all info about the docker environment
  44. docker info
  45.  
  46. // Search docker registry for any image with name "centos"
  47. docker search centos
  48.  
  49. // Download latest centos image from the registry
  50. docker pull docker.io/centos
  51.  
  52. // Download specific tagged centos image
  53. docker pull docker.io/centos:centos7.0.1406
  54.  
  55. // Push centos 7.0 image with id cc2cf48cc784 to a remote registry
  56. docker tag cc2cf48cc784 gitlabserver.nucleussec.com:8443/root/vulndockerimages/centos7.0
  57. docker push gitlabserver.nucleussec.com:8443/root/vulndockerimages/centos7.0
  58.  
  59.  
  60. // See all images that have been downloaded
  61. docker images -a
  62.  
  63. // Save a docker image to a tarfile
  64. docker save someimage > someimage.tar
  65.  
  66. // show all running containers
  67. docker ps
  68.  
  69. // stop a running container
  70. docker stop <container id>
  71.  
  72. // stop all containers
  73. docker stop $(docker ps -aq)
  74.  
  75. // restart a container
  76. docker start <container id>
  77.  
  78. // commit a copy of the container (presumably with changes from the base image) to a new image
  79. docker commit -m "<comment>" -a "<author>" <container id> <new image name>
  80.  
  81. // remove a stopped docker containers
  82. docker rm <container id>
  83.  
  84. // remove all containers
  85. docker rm $(docker ps -aq)
  86.  
  87. // show all docker containers running or stopped
  88. docker ps -a
  89.  
  90. // show running processes in a container
  91. docker top <container id>
  92.  
  93. // look at all info on a container
  94. docker inspect <container id>
  95.  
  96. // Downloads/pulls latest centos (centos:latest) image from docker.io and runs cat command
  97. docker run -it centos cat /etc/redhat-release
  98.  
  99. // Same thing but automatically clean up the container and remove the file system when the container exits
  100. docker run --rm -it centos cat /etc/redhat-release
  101.  
  102. // spin up a new container named "mycontainer" from a docker image and get shell inside
  103. docker run -it --name="mycontainer" <image name> /bin/bash
  104.  
  105. // mount a host volume/dir
  106. -v
  107. // after container runs the command, stop, then delete itself
  108. --rm
  109.  
  110. // Get a shell in a running container
  111. docker exec -it <container name> /bin/bash
  112.  
  113. // Edit a file in a container that doesn't have an editor
  114. docker cp <container>:/path/to/file.ext .
  115. // copies it to your local machine (to your current directory).
  116. // Then edit the file locally using your favorite editor, and then:
  117. docker cp file.ext <container>:/path/to/file.ext
  118. // to replace the old file.
  119.  
  120. #### Docker Compose ####
  121. # docker-compose up // in same directory as docker-compose.yml
  122. # docker-compose up -d // start containers in background (instead of dying when terminal dies)
  123. # docker-compose stop // in same directory as docker-compose.yml
  124. # docker-compose ps // show running containers
  125. # docker-compose rm // start from scratch
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement