List images:
Docker images
: Lists all images
Build an image from a dockerfile
docker build -t <image_name>:<tag_name> .
: Create the image from the dockerfile inside of the same directory where this command was executed.
Images with the same tag will not get deleted, but they loose their tag.
run {shell} docker image prune
to delete them
Inspect image metadata:
docker image inspect <image_name>:<tag_name>
List all Docker Containers:
docker ps
Run a docker image/container
You can only run a container, an image is just instructions in how to run a container.
docker container run [OPTIONS] IMAGE [COMMAND] [ARG...]
With output:
docker run ubuntu:12.04
This runs a container. It is not possible to interact with it.
With an interactive shell:
docker run -i -t ubuntu:12.04 /bin/bash
This gives us an interactive bash shell inside of the container. We won't see logs there though.
Stop a docker container
docker stop <myContainer>
Remove a container:
Only removes stopped containers by default. To remove running containers use the -f flag.
docker rm <myContainer>
Remove all stopped containers
docker container prune
Debugging
Hard reset:
Delete all Docker images at once:
docker rmi $(docker image ls -q)
Stop and Delete all Docker Containers at once:
docker rm $(docker ps -aq)
Pause/Unpause a container
Used to temporarily free up resources, debug and troubleshoot
{shell} docker pause <myContainer>
docker unpause <myContainer>
Restart a container
After changing some configs for example
docker restart <myContainer>
Execute shell commands inside of a container
Mostly to start a shell inside of it.
docker exec -it <myContainer> bash
You exit this shell with exit
Docker compose
execute docker compose file:
docker compose up
--build
tag at the endstop all docker containers spawned via docker compose up
docker compose stop
remove all stopped docker containers that were defines via docker compose up
docker compose rm