Skip to content

Instantly share code, notes, and snippets.

@roalcantara
Last active November 23, 2021 06:20
Show Gist options
  • Save roalcantara/f001db78e9751035f2567a1c397229d1 to your computer and use it in GitHub Desktop.
Save roalcantara/f001db78e9751035f2567a1c397229d1 to your computer and use it in GitHub Desktop.
Docker`s Zsh Cheat Sheet-ish

Build, Publish and Clean-Up and Prune Images

1. Setup

  • ~❯ brew update && brew upgrade && brew cask upgrade && brew cleanup && brew doctor to get brew ready
  • ~❯ brew cask install docker-edge
  • ~❯ echo "export DOCKER_CONFIG=$XDG_CONFIG_HOME/docker" >> $ZDOTDIR/.zshenv

2. Dockerfile

  • ~❯ mkdir -p ~/Docker/ubuntu to create a new working directory
  • ~❯ cd ~/Docker/ubuntu step into the the newly created direactory
  • ~/D/ubuntu ❯ docker pull ubuntu to pull the latest ubutu image
  • ~/D/ubuntu ❯ echo "FROM ubuntu:latest\nCMD /bin/bash" > Dockerfile create a minimum Dockerfile

3. Images

  • ~/D/ubuntu ❯ docker image build --tag roalcantara/ubuntu:01 . to build the image

  • ~/D/ubuntu ❯ docker image ls --all to list the newly build image

    REPOSITORY TAG IMAGE ID CREATED SIZE
    ubuntu latest adafef2e596e 3 hours ago 73.9MB
    roalcantara/ubuntu 01 adafef2e596e 3 hours ago 73.9MB
  • ~/D/ubuntu ❯ docker image rm adafef2e596e to remove the image roalcantara/ubuntu

4. Containers

  • ~/D/ubuntu ❯ docker container ls --all to list containers

    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    ed7c77d5fa07 roalcantara/ubuntu:01 "/bin/bash" 22 minutes ago Created ubuntu
  • ~/D/ubuntu ❯ docker container start ubuntu and start one or more stopped containers

  • ~/D/ubuntu ❯ docker container stop ubuntu and stop one or more running containers

  • ~/D/ubuntu ❯ docker container attach ubuntu and attatech to a running container

  • ~/D/ubuntu ❯ docker container rm ubuntu and remove one or more containers

  • ~/D/ubuntu ❯ docker run --name ubuntu --detach roalcantara/ubuntu:01 to run this container in the background

  • ~/D/ubuntu ❯ docker container run -it --rm --entrypoint /bin/bash --name ubuntu roalcantara/ubuntu:01 to run an interactive command in a new container and automatically remove the container when it exits

5. Publishing

  • ~/D/ubuntu ❯ docker login to login to Docker Hub
  • ~/D/ubuntu ❯ docker tag ubuntu:1.0 roalcantara/ubuntu:01 to tag the image
  • ~/D/ubuntu ❯ docker push roalcantara/ubuntu:01 to publish the image

6. Cleaning Up

  • ~/D/ubuntu ❯ docker rm $(docker ps -qaf status=exited) and clean up exited containers
  • ~/D/ubuntu ❯ docker rmi $(docker images -qf dangling=true) and clean up dangling images
  • ~/D/ubuntu ❯ docker volume rm $(docker volume ls -qf dangling=true) and clean up exited containers
  • ~/D/ubuntu ❯ docker stop $(docker ps -a -q) to stop all contaiers
  • ~/D/ubuntu ❯ docker rm $(docker ps -a -q) to remove all contaiers
  • ~/D/ubuntu ❯ docker rm $(docker ps -a -f status=exited -q) to remove all exited containers
  • ~/D/ubuntu ❯ docker rmi $(docker images -a -q) to remove all images

7. Pruning

  • ~/D/ubuntu ❯ docker image prune and prune all images
  • ~/D/ubuntu ❯ docker container prune and prune all containers
  • ~/D/ubuntu ❯ docker volume prune and prune all volumes
  • ~/D/ubuntu ❯ docker system prune and remove unused images
  • ~/D/ubuntu ❯ docker system prune --all and remove any stopped containers and all unused images

8. Executing commands

docker cp ./localfile.sql containername:/container/path/file.sql
docker exec -u postgresuser containername psql dbname postgresuser -f /container/path/file.sql

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment