Basic commands

docker image

Build an image from the Dockerfile in the current directory

docker image build .

Build an image with repository and tag from a root directory

docker image build -t myRepo:myTag /my/root/dir/

List images

docker image ls
docker images

Remove image

docker image rm web1

Show detailed information of an image

docker image inspect web1

Create a tag image from a source image

docker image tag web1 adr1/web1:latest

Push an image to a registry

docker image push adr1/web1:latest

docker container

List running containers

docker container ls

List all containers

docker container ls -a

Run a command in a new container

docker container run -it alpine sh

or docker run -it alpine sh

with interactive terminal

docker container run -it web1

with binding to host port

docker container run -it -p5000:5000 web1

with binding to a random host port

docker container run -it -p5000 web1

with environment variables

docker container run -it -p5000:5000 -e -e FLASK_DEBUG=1 web1

in the background and detached

docker container run -it -p5000:5000 -d web1

with auto-removal of container when it exits

docker container run -it -rm -p5000:5000 web1

with a name assigned to the container

docker container run -it --name web1 -p5000:5000 web1

with a restart policy on failure

docker container run -it --name web1 --restart on-failure -p5000:5000 web1

with a volume mounted

docker container run -it -p 5000:5000 -e --rm --name web1 -e FLASK_DEBUG=1 -v $PWD:/app web1    

Remove a container

docker container rm web1

Remove all containers

docker container rm $(docker ps -a -q)

Show container detailed information

docker container inspect web1

Run a command in a running container

docker container exec -it web1 ls

with option -it = interactive, tty

docker container exec -it web1 sh

set file name permission to user

docker container exec -it --user "$(id -u):$(id -g)" touch hello.txt

docker pull

Pull an image from a registry

docker pull adr1/web1:latest


docker import / export

Export from container to tarball

docker export myContainerId > myImage.tar

Import from tarball to repostory

docker import - myRepo < myImage.tar

docker save / load

Save repository to tarball

docker save -o myImage.tar myRepo

Load tarball to repository

docker load < myImage.tar