Skip to content

Instantly share code, notes, and snippets.

@AndreiD
Last active June 1, 2019 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndreiD/70086378624abe25f13868e22328d199 to your computer and use it in GitHub Desktop.
Save AndreiD/70086378624abe25f13868e22328d199 to your computer and use it in GitHub Desktop.
My Docker Cheatsheet

Usefull

clear everything with docker

purge_docker.sh

docker system prune -a
docker images purge
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)

sudo docker exec -it 665b4a1e17b6 /bin/bash

docker ps — Lists running containers. Some useful flags include: -a / -all for all containers (default shows just running) and —-quiet /-q to list just their ids (useful for when you want to get all the containers).

docker pull — Most of your images will be created on top of a base image from the Docker Hub registry. Docker Hub contains many pre-built images that you can pull and try without needing to define and configure your own. To download a particular image, or set of images (i.e., a repository), use docker pull.

docker build — The docker build command builds Docker images from a Dockerfile and a “context”. A build’s context is the set of files located in the specified PATH or URL. Use the -t flag to label the image, for example docker build -t my_container . with the . at the end signalling to build using the currently directory.

docker run — Run a docker container based on an image, you can follow this on with other commands, such as -it bash to then run bash from within the container. Also see Top 10 options for docker run — a quick reference guide for the CLI command. docker run my_image -it bash

docker logs — Use this command to display the logs of a container, you must specify a container and can use flags, such as --follow to follow the output in the logs of using the program. docker logs --follow my_container docker volume ls — This lists the volumes, which are the preferred mechanism for persisting data generated by and used by Docker containers.

docker rm — Removes one or more containers. docker rm my_container

docker rmi — Removes one or more images. docker rmi my_image

docker stop — Stops one or more containers. docker stop my_container stops one container, while docker stop $(docker ps -a -q) stops all running containers. A more direct way is to use docker kill my_container, which does not attempt to shut down the process gracefully first.

Use them together, for example to clean up all your docker images and containers:

kill all running containers with docker kill $(docker ps -q)

delete all stopped containers with docker rm $(docker ps -a -q)

delete all images with docker rmi $(docker images -q)

builds & run the container

docker run --rm -it $(docker build -q .)


docker ps -a  # list all images

docker rm $(docker ps -a -q) # remove all images & container
docker rmi $(docker images -q) # remove all images & containers

docker rm -v container_name # remove a container and it's volume

Build

Build an image from the Dockerfile in the current directory and tag the image

-> note the . at the end!

docker build -t myapp:1.0 .    

List all images that are locally stored with the Dockerengine docker images

Delete an image from the local image store docker rmi alpine:3.4

Run

ex: docker run -it --name infinite -d yourimage

docker run

--rm # remove container automatically after it exits -it # connect the container to the terminal --name web # name of the container -p 5000:80 # expose port 5000 externally and map to port 80 -v ~/dev:/code alpine:3.4/bin/sh www.docker.com/getdocker Docker Cheat Sheet docker run

Stoping

docker stop (or kill) containerName

Print the last 100 lines of a container’s logs

docker logs --tail 100 web

PM2

pm2 start script.sh --name "my_script"
pm2 reload "my_script"
pm2 logs "my_script"

# you can add any arguments after --
pm2 start app.js -- --prod
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment