Skip to content

Instantly share code, notes, and snippets.

@ospatil
Created January 7, 2019 23:54
Show Gist options
  • Save ospatil/a5f324d2e6950458cb3c092c0a7efd73 to your computer and use it in GitHub Desktop.
Save ospatil/a5f324d2e6950458cb3c092c0a7efd73 to your computer and use it in GitHub Desktop.
Common docker commands needed for day to day working
# CREATION
## run an image in interactive mode (-it flag), map a folder between host and container (-v flag) and remove the container after stopping and run BASH in the container
docker run --rm -it -v ~/<HOST_DIR>:/<CONTAINER_DIR> <IMAGE_NAME> bash
## create an image from a container (useful when you make changes to a container and create a new image out of it)
docker commit <CONTAINER_NAME> <NEW_IMAGE_NAME>
## create an image from scratch. Create a a Dockerfile in a directory and run the following command in that directory
docker build -t <NEW_IMAGE_NAME> .
# WORKING WITH IMAGES
## List images
docker images
## remove an image
docker rmi <IMAGE_ID/NAME>
# WORKING WITH CONTAINERS
## list containers including stopped ones
docker ps -a
## start container (better practice is to use -rm flag and discard the container after each use, but this can be useful for gradually building a container and then creating an image out of it)
docker start <CONTAINER_ID/NAME>
## Start an existing container in interactive mode
docker exec -it <CONTAINER_ID/NAME> bash
## stop a container
docker stop <CONTAINER_ID/NAME>
## pause the processes running inside a container
docker pause <CONTAINER_ID/NAME>
## unpause the processes running inside a container
docker unpause <CONTAINER_ID/NAME>
## restart a container
docker restart <CONTAINER_ID/NAME>
## kill a container
docker kill <CONTAINER_ID/NAME>
## kill all running containers
docker kill $(docker ps -q)
## delete a stopped container
docker rm <CONTAINER_ID/NAME>
## delete all stopped containers
docker rm $(docker ps -a -q)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment