DevOps Buzz
  • Initial page
  • About me
  • Ansible
    • Cheat Sheet
    • Dynamic inventory
  • AKS
    • Cheat Sheet
    • Autoscale
    • Backup
    • Dashboard
    • Ingress
    • Node management
    • News
    • Persistent Volumes
  • Arch Linux
    • Docker
    • Install
    • Network
    • VirtualBox guest
  • Azure
    • API
    • CLI/Powershell
    • DevOps
      • Cheat Sheet
    • Application Gateway
    • Tools
    • Sops
  • AWS
    • Cheat Sheet
    • Tools
    • Cognito with Amplify and React
  • Bash / Shell
    • Cheat Sheet
    • Tools
  • Bitbucket
    • Cheat Sheet
  • CSS
    • Cheat Sheet
  • Distros
    • Manjaro
  • Docker
    • Cheat Sheet
    • ELK
    • Ubuntu NoVNC
    • Tools
    • Known errors and solutions
  • Elasticsearch
    • Cheat Sheet
    • Tools
  • ELK
    • Cheat Sheet
  • emacs
    • Cheat sheet
  • Gatekeeper (OPA)
    • Cheat Sheet
    • Developer Quick Start
  • GCP
    • Cheat Sheet
  • General
    • Tools
    • News
  • Git
    • Cheat Sheet
    • Tools
  • Golang
    • Cheat Sheet
  • Guidelines / Standards
    • Cheat Sheet
  • i3wm
    • Cheat Sheet
  • Ipsec
    • Cheat sheet
  • Istio
    • Cheat Sheet
  • Kind
    • Cheat Sheet
  • Kops
    • Cheat Sheet
  • Kubeadm
    • Cheat Sheet
    • Change serviceSubnet CIDR
    • Setup cluster
      • Azure
      • Bare-metal
      • Vagrant + VirtualBox
    • Multi master
    • Known errors and solutions
  • Kubernetes
    • Kubectl Cheat Sheet
    • etcd Cheat Sheet
    • Tools
    • News
    • Deployments
      • Deployment examples
      • Blue/Green Deployment
      • Canary Deployment
    • Dashboard
    • ELK
    • Helm
    • Ingress
    • logz.io
    • Minikube
    • Monitoring
    • Node Management
    • Operators
    • Security
    • Volumes
    • Networking
    • kube-controller-manager
      • Node crash recovery
    • Known errors and solutions
  • Lumen
    • Cheat Sheet
  • MACOS
    • Cheat Sheet
    • Qemu
    • Zsh
  • microk8s
    • Cheat Sheet
  • MongoDB
    • Cheat Sheet
    • Tools
  • MySQL
    • Cheat Sheet
  • Network
    • Tools
    • WDS - Wireless Distribution System
    • Expose server under NAT
  • nvim
    • Cheat Sheet
  • Openvpn
    • OpenVPN server on Ubuntu 18.06
    • Stunnel
  • PHP
    • Composer
  • Prometheus
    • Tools
  • Python
    • Cheat Sheet
    • Pydantic
    • Tools / Modules
    • Virtualenv
  • RabbitMQ
    • Cheat Sheet
  • ReactJS
    • Fixes
    • For beginners
  • Ruby
    • Cheat Sheet
    • Rails
    • rvm
  • Rundeck
    • Cheat Sheet
  • Rust
    • Cheat Sheet
  • Squid
    • Setup server
    • Expose NAT server
  • SRE
    • Cheat Sheet
  • SSH
    • Passwordless auth with RSA key
    • Reverse tunnel
    • Cheat Sheet
  • SSL
    • Cheat Sheet
    • certbot
  • STACK SETUP
    • Using EKS and Gitlab CI to deploy applications
  • Terraform
    • Cheat Sheet
    • Tools
  • Tmux
    • Cheat Sheet
  • Tor
    • Cheat Sheet
  • Ubuntu
    • Cheat Sheet
  • Vagrant
    • Cheat Sheet
  • VirtualBox
    • Cheat Sheet
  • Windows
    • Windows Docker
    • Fingerprint
    • SSH Client
    • Tools
    • VirtualBox
    • WSL
Powered by GitBook
On this page
  • Container management
  • Avoid container termination after executing CMD or Entrypoint
  • Bash TTY connect
  • Run container
  • Run docker-in-docker
  • Start container on boot
  • CLI tricks
  • Attach
  • Image management
  • Create image from container
  • Create image from Dockerfile
  • Create image from AWS AMI
  • Cleanup dangling images
  • Inspect
  • List run command used
  • Show mounted volumes
  1. Docker

Cheat Sheet

Docker useful commands.

Container management

Avoid container termination after executing CMD or Entrypoint

If you have a Dockerfile similar to this one:

FROM nginx
COPY my-custom-script.sh /
ENTRYPOINT ["/my-custom-script.sh"]
# OR
# CMD /my-custom-script.sh

And after my-custom-script.sh exits, docker stops the container, you can fix the problem as follows.

Fist, make sure you are using COPY instead of ADD in your Dockerfile to copy your script.

Edit my-custom-script.sh and add the following line at the end:

...
# Run all command line arguments
exec "$@";

Then make sure to use a command when running your container, for example /bin/bash

docker run \
  -tid \
  -p 8080:8080 \
  --name test \
  my-custom-image \
  /bin/bash

Bash TTY connect

docker exec -ti CONTAINER-NAME-HERE /bin/bash

Run container

docker run \
  -tid \
  --name test \
  -p 8080:8080 \
  -e MY_VAR='MY-VALUE' \
  nginx

Run docker-in-docker

docker run --rm -it -v /var/run/docker.sock:/var/run/docker.sock docker

Start container on boot

Run your container with --restart=always flag.

Or add the the flag to an existing container:

docker update --restart=always <container>

CLI tricks

Attach

docker attach CONTAINER-NAME

If you press CTRL+C to exit it will stop the container. To exit without stopping the container press CTRL+P then CTRL+Q

Image management

Create image from container

docker commit CONTAINER-NAME NEW-IMAGE-NAME

Create image from Dockerfile

docker build -t NEW-IMAGE-NAME .

Create image from AWS AMI

Get your AMI snapshot ID

Access your AWS console and go to EC2, AMIs. Find your AMI and check its Block Devices, for example:

/dev/sda1=snap-081e01478359d5bec:30:true:gp2

snap-081e01478359d5bec is the AMI snapshot ID, which will be used as the source for a new device.

Create a new volume from your AMI snapshot ID

In your AWS console go to Snapshots. Filter using your snapshot ID, for example: snap- 081e01478359d5bec.

Right click on it, then select Create Volume. Add a tag key Name and input your new volume’s name.

Attach your new volume to an instance

Launch a new instance or use an existing one.

Keep in mind the instance must have enough space to store a temporary tar le from your AMI volume.

In your AWS console go to Volumes. Filter by your Volume Name (you created previously).

Right click on it, then select Attach Volume.

Select your instance and choose a Device, for example: /dev/sdf

Click on Attach.

Mount your new volume

SSH connect to your instance and mount the volume you just created.

mount /dev/xvdf1 /mnt

Note your Device was renamed to /dev/xvdf

Install Docker

Install Docker in your instance.

Create Docker image

SSH connect to your instance and mount the volume you just created.

tar -c -C /mnt/ . | docker import - YOUR-IMAGE-NAME

Replace YOUR-IMAGE-NAME

This command might take a while depending on the size of your volume.

Testing

List your docker images. Find YOUR-IMAGE-NAME and get its Image ID.

Run a container from your new image.

docker run -tid dd5935d36306 /bin/bash

Cleanup

If everything is OK you can umount your volume:

umount /mnt

Then go to your AWS Console, Volumes. Find the volume you created previously, right click on it then select Detach Volume. Then right click on it again then select Delete Volume.

References

Cleanup dangling images

docker rmi -f $(docker images -f "dangling=true" -q)

Or.. the docker system prune command will remove all stopped containers, all dangling images, and all unused networks:

docker system prune

Inspect

List run command used

docker run --rm -v /var/run/docker.sock:/var/run/docker.sock \
    assaflavie/runlike YOUR-CONTAINER

Show mounted volumes

docker inspect -f '{{ .Mounts }}' containerid
PreviousManjaroNextELK

Last updated 2 years ago

https://stackoverflow.com/a/35124911