Jan 25, 2018

Interacting with Docker containers

  • Running an interactive shell on the container:
    
    # Your container may have /bin/sh instead
    docker exec -t -i [container] /bin/bash
    

  • Running a command on an image instead (i.e not a running instance):
    
    docker run -t -i [image] /bin/bash
    

  • Get the output of a running container (for logs etc):
    
    docker attach --no-stdin [container]

Cleaning docker

You can use the following bash script to clean out your docker instance:

#!/bin/bash
echo "docker-clean.sh [container|image|volume|cache|data|debugbar]"
if [ "$1" == "container" ]; then
    #Cleaning containers
    docker ps --no-trunc -aqf "status=exited" | xargs docker rm
elif [ "$1" == "image" ]; then
    #Cleaning images
    docker images --no-trunc -aqf "dangling=true" | xargs docker rmi
elif [ "$1" == "volume" ]; then
    #Cleaning volumes
    docker volume ls -qf "dangling=true" | xargs docker volume rm
fi

No comments:

Post a Comment

Thanks for contributing!! Try to keep on topic and please avoid flame wars!!