Default

Basic commands

docker image

Build an image from the Dockerfile in the current directory

class="highlight">
1
docker image build .

Build an image with repository and tag from a root directory

class="highlight">
1
docker image build -t myRepo:myTag /my/root/dir/

List images

class="highlight">
1
docker image ls
class="highlight">
1
docker images

Remove image

class="highlight">
1
docker image rm web1

Show detailed information of an image

class="highlight">
1
docker image inspect web1

Create a tag image from a source image

class="highlight">
1
docker image tag web1 adr1/web1:latest

Push an image to a registry

class="highlight">
1
docker image push adr1/web1:latest

docker container

List running containers

class="highlight">
1
docker container ls

List all containers

class="highlight">
1
docker container ls -a

Run a command in a new container

class="highlight">
1
docker container run -it alpine sh

or docker run -it alpine sh

with interactive terminal

class="highlight">
1
docker container run -it web1

with binding to host port

class="highlight">
1
docker container run -it -p5000:5000 web1

with binding to a random host port

class="highlight">
1
docker container run -it -p5000 web1

with environment variables

class="highlight">
1
docker container run -it -p5000:5000 -e FLASK_APP=app.py -e FLASK_DEBUG=1 web1

in the background and detached

class="highlight">
1
docker container run -it -p5000:5000 -d web1

with auto-removal of container when it exits

class="highlight">
1
docker container run -it -rm -p5000:5000 web1

with a name assigned to the container

class="highlight">
1
docker container run -it --name web1 -p5000:5000 web1

with a restart policy on failure

class="highlight">
1
docker container run -it --name web1 --restart on-failure -p5000:5000 web1

with a volume mounted

class="highlight">
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

class="highlight">
1
docker container rm web1

Remove all containers

class="highlight">
1
docker container rm $(docker ps -a -q)

Show container detailed information

class="highlight">
1
docker container inspect web1

Run a command in a running container

class="highlight">
1
docker container exec -it web1 ls

with option -it = interactive, tty

class="highlight">
1
docker container exec -it web1 sh

set file name permission to user

class="highlight">
1
docker container exec -it --user "$(id -u):$(id -g)" touch hello.txt

docker pull

Pull an image from a registry

class="highlight">
1
docker pull adr1/web1:latest

docker-compose

docker import / export

Export from container to tarball

class="highlight">
1
docker export myContainerId > myImage.tar

Import from tarball to repostory

class="highlight">
1
docker import - myRepo < myImage.tar

docker save / load

Save repository to tarball

class="highlight">
1
docker save -o myImage.tar myRepo

Load tarball to repository

class="highlight">
1
docker load < myImage.tar