Search code examples
dockerboot2docker

Use last container name as default in docker client


docker client for docker ps has very useful flag -l which shows container information which was run recently. However all other docker commands requires providing either CONTAINER ID or NAME.

Is there any nice trick which would allow to call:

docker logs -f -l

instead of:

docker logs -f random_name

Solution

  • After a while playing with docker tutorial, I created small set of aliases:

    alias docker_last="docker ps -l | tail -n +2 | awk '{ print \$(NF) }' | xargs docker $1"
    alias docker_all="docker ps -a | tail -n +2 | awk '{ print \$(NF) }' | xargs docker $1"
    alias docker_up="docker ps | tail -n +2 | awk '{ print \$(NF) }' | xargs docker $1"
    alias docker_down="docker ps -a | tail -n +2 | grep -v Up | awk '{ print \$(NF) }' | xargs docker $1"
    

    Which allow to call command on last, all, up and down containers:

    docker_last logs # Display logs from last created container
    docker_down rm   # Remove all stopped containers
    docker_up stop   # Stop all running containers