Docker stuff
my quick how-to commands
The first two commands are really cool
docker ps -a will list all containers, whereas docker ps -a -q will list all containers but show only the container ids. So docker <outer command> $(<inner command>) passes the result of <inner command> to the <outer command>
docker ps -a --format '{{.Namea}}' to list only the names of all running containers, see https://docs.docker.com/engine/reference/commandline/ps/
Stop all currently running containers
docker stop $(docker ps -a -q)
Remove all stopped containers
docker rm $(docker ps -a -q)
docker rm $(docker ps --filter "status=exited" -q)
Build a docker image
docker build -t <image id> <directory where Dockerfile is located>
How to run an image
docker run [-p <host port>:<container exposed port> [-p <further port mappings>] [--name <unique name>] ] <image id>
Listing all running instances and container IDs
docker ps -a
Command to get IP address of docker container
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container_name_or_id>
Shell into a running Docker container (make sure you specify the -it parameter)
Docker exec -it <container id> bash
Shell into a running Docker container as root user (make sure you specify the -it parameter)
docker exec -it -u root <container id> bash
Injecting a command from the host into a container
docker exec -it <container id> <command>
e.g. docker exec -it <container id> bash|sh
Will execute a bash command in the container, the -it gives control back to the host console
e.g. docker exec -it <container id> ls
Will execute the ls command in the container
e.g. docker exec -it <container id> ls -al
Will execute the ls command and pass -al to it as a param
e.g. docker exec -it <container id> bash "<sequence of semicolon separated commands>"
e.g. docker exec -it <container id> bash -c "cd /opt/tomcat; mkdir warfiles"
Starting a docker machine locally on windows
docker-machine -start
How to start a container that might exit immediately
docker run -dit <image name>
Renaming an image
docker tag <image id> <docker repo id>/<image name>[:<tag id>]
Overriding the ENTRYOINT on docker run
docker run --entrypoint "<command to execute>" <image> <param to command to execute, ...>
e.g. docker run --entrypoint "/bin/ls" tomcat -al /root
Cleaning docker space - if you find docker images won't run
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock:ro -v /var/lib/docker:/var/lib/docker martin/docker-cleanup-volumes
Creating a new machine
docker-machine create -d virtualbox default
Removing untagged images
docker rmi $(docker images -f "dangling=true" -q)
Removing images based on a pattern
docker images | grep <pattern> | awk '{print $3}' | xargs docker rmi
docker-compose commands
Consider the following specification in the compose file
services: identity-api: image: ${REGISTRY:-eshop}/identity.api:${PLATFORM:-linux}-${TAG:-latest} build: context: . dockerfile: Services/Identity/Identity.API/Dockerfile depends_on: - sqldata
The field ${REGISTRY:-eshop}
is a switch parameter. If no REGISTRY variable is passed into the docker-compose build command, the value of eshop
is used
The command
docker-compose build --build-arg REGISTRY=s2s
Will replace eshop
with s2s