docker notes
Check docker version
docker version
Get docker IP address
docker-machine ip
List all installed images
docker images
Examine layers in an image
docker history <image name>
List all containers, running or not
docker ps -a
Remove a container
docker rm <container name or id>
Remove an image
docker rmi <image name>
Create and run a container interactively and remove it once it is finished
docker run -it --rm <image name>
Create and run a container in detached state
docker run -d <image name>
Create and run a container with all network ports mapped to the host network ports
docker run -d -P <image name> <inside container commands>
Creating without running the container
docker create <image name>
Run a stopped container
docker run <container name or id>
Stop a container from running
docker stop <container name or id>
Pull an image from an alternate registry
docker pull [REGISTRYHOST/][USERNAME/]NAME[:TAG]
Create and run container from an image of an alternate registry
docker run [REGISTRYHOST/][USERNAME/]NAME[:TAG]
Watch container logs
docker logs -f <container name or id>
Executing command in a running container
docker exec -it <container id or name> <command>
Check the configuration and status of a container
docker inspect <container id or name>
Information can be narrowed down like this
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container id or name>
Save a docker image to a file
docker save -o <output file name> <image name>
Load a docker image from a file
docker load -i <image file>
Show changes of a container
docker diff <container id or name>
Export container's file system as a tar archive
docker export --output <tar file name> <container id or name>
Committing to an image after make changes to a container
docker commit -m <message> -a <author> <container id> <account name>/<image name>:<version tag>
Example of Dockerfile content
FROM busybox:latest MAINTAINER dia@allingeek.com ADD demo.sh /demo/ WORKDIR /demo/ CMD ./demo.sh
Another example
# This is a comment FROM ubuntu:14.04 MAINTAINER Kate Smith RUN apt-get update && apt-get install -y ruby ruby-dev RUN gem install sinatra
Building a docker image from a Dockerfile
docker build -t <image name and tag> <directory containing Dockerfile>
Tag an image with account/repo name before pushing it to the repo
docker tag <image id> <account name>/<image name>:<version label or tag>
Login to docker hub
docker login --username=<docker hub username> --email=<docker hub email>
Push owned image to docker hub
docker push <account name>/<image name>
List dangling volumes
docker volume ls -f dangling=true
Remove volume that is no longer needed
docker volume rm <volume name>
Create a named volume before use
docker volume create -d <existing driver name, like flocker or local> --name my-named-volume -o size=20GB
Mount a shared storage volume as a data volume
docker run -d -P --volume-driver=driver_name -v my_volume_name:/webapp --name web training/ webapp python app.py
Create and run a shared volume for Cassandra data
docker run -d --volume /var/lib/cassandra/data --name cass-shared alpine echo Data Container
Create and run a container running Cassandra
docker run -d --volumes-from cass-shared --name cass1 cassandra:2.2
Create and run an interactive Cassandra client tool
docker run -it --rm --link cass1:cass cassandra:2.2 cqlsh cass
Run a httpd container whose htdocs point to a host's folder
docker run -d --name bmweb -v ~/example-docs:/usr/local/apache2/htdocs -p 80:80 httpd:latest
By adding :ro, the mounted host folder become read only
docker run --name bmweb_ro --volume ~/example-docs:/usr/local/apache2/htdocs/:ro -p 80:80 httpd:latest
If the mounted host folder doesn't exist, it will be created. If the mounted name is a file, then the file will be mounted.
Backup volume to tar file
docker run --rm --volumes-from dbstore -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /dbdata
Restore the backup file to a volume
docker run -v /dbdata --name dbstore2 ubuntu /bin/bash docker run --rm --volumes-from dbstore2 -v $(pwd):/backup ubuntu bash -c "cd /dbdata && tar xvf /backup/backup.tar --strip 1"
List networks
docker network ls
Finding IP addressess of containers in a network
docker network inspect <network name>
Remove a container from a network
docker network disconnect bridge <container id or name>
Create a bridge network
docker network create -d bridge <network name>
Create, run, and add a container to a network
docker run -d --network=<network name> --name db training/postgres
Inspect a container to see where it is connected
docker inspect -f '{{json .NetworkSettings.Networks}}' <container id or name>
Find the IP address of a container
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container id or name>
Add a container to a network
docker network connect <network name> <container id or name>
Check to which host port a container port has been mapped
docker port <container id or name> <network port inside container>