Docker Basic Commands
docker image
Build an image from the Dockerfile in the current directory
1
docker image build .
Build an image with repository and tag from a root directory
1
docker image build -t myRepo:myTag /my/root/dir/
List images
1
docker image ls
1
docker images
Remove image
1
docker image rm web1
Show detailed information of an image
1
docker image inspect web1
Create a tag image from a source image
1
docker image tag web1 adr1/web1:latest
Push an image to a registry
1
docker image push adr1/web1:latest
docker container
List running containers
1
docker container ls
List all containers
1
docker container ls -a
Run a command in a new container
1
docker container run -it alpine sh
or
1
docker run -it alpine sh
with interactive terminal
1
docker container run -it web1
with binding to host port
1
docker container run -it -p5000:5000 web1
with binding to a random host port
1
docker container run -it -p5000 web1
with environment variables
1
docker container run -it -p5000:5000 -e FLASK_APP=app.py -e FLASK_DEBUG=1 web1
in the background and detached
1
docker container run -it -p5000:5000 -d web1
with auto-removal of container when it exits
1
docker container run -it -rm -p5000:5000 web1
with a name assigned to the container
1
docker container run -it --name web1 -p5000:5000 web1
with a restart policy on failure
1
docker container run -it --name web1 --restart on-failure -p5000:5000 web1
with a volume mounted
1
docker container run -it -p 5000:5000 -e FLASK_APP=app.py --rm --name web1 -e FLASK_DEBUG=1 -v $PWD:/app web1
Remove a container
1
docker container rm web1
Remove all containers
1
docker container rm $(docker ps -a -q)
Show container detailed information
1
docker container inspect web1
Run a command in a running container
1
docker container exec -it web1 ls
with option -it = interactive, tty
1
docker container exec -it web1 sh
Set file name permission to user
1
docker container exec -it --user "$(id -u):$(id -g)" touch hello.txt
docker pull
Pull an image from a registry
1
docker pull adr1/web1:latest
docker-compose
docker import / export
Export from container to tarball
1
docker export myContainerId > myImage.tar
Import from tarball to repostory
1
docker import - myRepo < myImage.tar
docker save / load
Save repository to tarball
1
docker save -o myImage.tar myRepo
Load tarball to repository
1
docker load < myImage.tar