Skip to content

Instantly share code, notes, and snippets.

@marceltn
Last active April 1, 2020 13:52
Show Gist options
  • Save marceltn/0d4f6266929bc92ad5850a1d615b27de to your computer and use it in GitHub Desktop.
Save marceltn/0d4f6266929bc92ad5850a1d615b27de to your computer and use it in GitHub Desktop.
Docker
############################################################################
# #
# ------- Useful Docker Aliases -------- #
# #
# # Installation : #
# copy/paste below line into your .bash_profile or .bashrc file #
# test -f ~/.bash_aliases_docker && . ~/.bash_aliases_docker #
# type the following in your current shell to try it out: #
# wget -O - https://gist.github.com/marcelnakamine/0d4f6266929bc92ad5850a1d615b27de/raw/66460e6d41d82f51393dc846071cdb7174ed9843/.bash_aliases_docker | bash
# #
# # Usage: #
# dcu : docker-compose up -d #
# dcd : docker-compose down #
# dex <container>: execute a bash shell inside the RUNNING <container> #
# di <container> : docker inspect <container> #
# dim : docker images #
# dip : IP addresses of all running containers #
# dl <container> : docker logs -f <container> #
# dnames : names of all running containers #
# dps : docker ps #
# dpsa : docker ps -a #
# drmc : remove all exited containers #
# drmid : remove all dangling images #
# drun <image> : execute a bash shell in NEW container from <image> #
# dsr <container>: stop then remove <container> #
# #
############################################################################
function dnames-fn {
for ID in `docker ps | awk '{print $1}' | grep -v 'CONTAINER'`
do
docker inspect $ID | grep Name | head -1 | awk '{print $2}' | sed 's/,//g' | sed 's%/%%g' | sed 's/"//g'
done
}
function dip-fn {
echo "IP addresses of all named running containers"
for DOC in `dnames-fn`
do
IP=`docker inspect $DOC | grep -m3 IPAddress | cut -d '"' -f 4 | tr -d "\n"`
echo $DOC : $IP
done
}
function dex-fn {
docker exec -it $1 $2
}
function di-fn {
docker inspect $1
}
function dl-fn {
docker logs -f $1
}
function drun-fn {
docker run -it $1 /bin/bash
}
function dsr-fn {
docker stop $1;docker rm $1
}
alias dcu="docker-compose up -d"
alias dcd="docker-compose down"
alias dex=dex-fn
alias di=di-fn
alias dim="docker images"
alias dip=dip-fn
alias dl=dl-fn
alias dnames=dnames-fn
alias dps="docker container ls"
alias dpsa="docker container ls -a"
alias drmc="docker container prune"
alias drmid="docker images -f dangling=true -q | xargs docker rmi"
alias drun=drun-fn
alias dsr=dsr-fn
## Vue Dev #
alias docker-vue-dev='docker run --rm -v $(pwd):/src -w /src -p 8080:8080 node:carbon bash -c "npm install && npm run dev"'
alias docker-vue-build='docker run --rm -v $(pwd):/src -w /src node:carbon bash -c "npm install && npm run build"'

List Docker CLI commands

docker
docker container --help

Display Docker version and info

docker --version
docker version
docker info

Execute Docker image

docker run hello-world

List Docker images

docker image ls

List Docker containers (running, all, all in quiet mode)

docker container ls
docker container ls --all
docker container ls -aq

Images

docker image ls

Container

docker run -p 3000:8080 <image>
docker run -d -p 3000:8080 <image>
docker container stop <container id>
docker build -t friendlyhello .  # Create image using this directory's Dockerfile
docker run -p 4000:80 friendlyhello  # Run "friendlyname" mapping port 4000 to 80
docker run -d -p 4000:80 friendlyhello         # Same thing, but in detached mode
docker container ls                                # List all running containers
docker container ls -a             # List all containers, even those not running
docker container stop <hash>           # Gracefully stop the specified container
docker container kill <hash>         # Force shutdown of the specified container
docker container rm <hash>        # Remove specified container from this machine
docker container rm $(docker container ls -a -q)         # Remove all containers
docker image ls -a                             # List all images on this machine
docker image rm <image id>            # Remove specified image from this machine
docker image rm $(docker image ls -a -q)   # Remove all images from this machine
docker login             # Log in this CLI session using your Docker credentials
docker tag <image> username/repository:tag  # Tag <image> for upload to registry
docker push username/repository:tag            # Upload tagged image to registry
docker run username/repository:tag                   # Run image from a registry

info

Technology Version
Docker 17.12.1-ce, build 7390fc6
node JS 8.9.4

Ways to run a Docker container

  1. To run a single task: This could be a shell script or a custom app.
docker container run alpine hostname
  1. Interactively: This connects you to the container similar to the way you SSH into a remote server.
docker container run --interactive --tty --rm ubuntu bash
  1. In the background: For long-running services like websites and databases.
docker container run \
 --detach \
 --name mydb \
 -e MYSQL_ROOT_PASSWORD=my-secret-pw \
 mysql:latest

Useful commands

command description
docker container logs mydb show logs from mydb container
docker container top mydb show PID from mydb container
docker exec -it mydb sh execute the sh command in mydb container
docker build -t friendlyhello . Create image using this directory's Dockerfile
docker run -p 4000:80 friendlyhello Run "friendlyname" mapping port 4000 to 80
docker run -d -p 4000:80 friendlyhello Same thing, but in detached mode
docker container ls List all running containers
docker container ls -a List all containers, even those not running
docker container stop Gracefully stop the specified container
docker container kill Force shutdown of the specified container
docker container rm Remove specified container from this machine
docker container rm $(docker container ls -a -q) Remove all containers
docker image ls -a List all images on this machine
docker image rm Remove specified image from this machine
docker image rm $(docker image ls -a -q) Remove all images from this machine
docker login Log in this CLI session using your Docker credentials
docker tag username/repository:tag Tag for upload to registry
docker push username/repository:tag Upload tagged image to registry
docker run username/repository:tag Run image from a registry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment