Docker cheat sheet
Recently I started using docker extensively and found the number of commands you need to have on your tips quite overwhelming. So here is my list of most common commands which I referred the most.
NOTE: The list below is of commands that I needed most commonly to work with docker and does not elaborate on all the options that are available with these commands (hence the cheat sheet).
Build a docker image
docker build -f <dockerfile path> -t <image-name> <build-context>
Example: Build a docker-file in the current directory and tag it with myapp version 1.0. Build context is the current directory (notice the ‘.’)
docker build -f dockerfile -t myapp:1.0 .
List all the images
docker images
Add option ‘-a’ to list all images including intermediate images
Pull an image from the registry
docker pull <image-path>
Example: Pull an image from docker hub
docker pull microsoft/aspnetcore
Remove a docker image
docker rmi <image-name>
Example: Remove image ‘sampleapp:dev’
docker rmi sampleapp:dev
List all containers
docker ps –all
Run a container
docker run -d -p <host-port>:<container-port> –name <container-name> <image>
Example: Run a container mycontainer from image myimage:v1 and publish ports (-p) 80 from container to 8080 on the host.
docker run -d -p 8080:80 –name mycontainer myimage:v1
Example: Run a SQL Server container.
docker run -e “ACCEPT_EULA=Y” -e “SA_PASSWORD=abcd@1234” -p 1433:1433 –name sqlserver-test -d mcr.microsoft.com/mssql/server:2017-latest
Run a command inside a container
docker exec –it <container-name> <command-name>
Example: Run bash inside a container named ‘mycontainer’ in interactive mode (–it)
docker exec -it mycontainer bash
Get logs on a container
docker logs <containername>
Start a container
docker start <containername>
Stop a container
docker stop <container-name>
Remove a container
docker rm -f <container-name>
‘-f’ option forces the container to be removed. Removing -f option would first require us to stop the container and then remove.
Remove all stopped or running containers (for windows command line)
FOR /f “tokens=*” %i IN (‘docker ps -aq’) DO docker rm -f %i