$ docker build -t my-image . # Create image using this directory's Dockerfile
$ docker run -p 4200:80 my-image # Run "my-image" mapping port 4200 to 80
$ docker run -d -p 4200:80 my-image # Same thing, but in detached mode
$ docker run username/repository:tag # Run image from a registry
$ 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 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 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 system prune # Remove all unused containers, networks and images.
$ docker build -t my-image .
This command will create a image using this directory's Dockerfile
and the current directory as the context.
$ docker build -f Dockerfile.dev -t my-image .
This command will create a image using this directory's Dockerfile.dev
and the current directory as the context.
Option | Description |
---|---|
--file , -f |
Name of the Dockerfile (Default is PATH/Dockerfile ). |
--tag , -t |
Name and optionally a tag in the "name:tag" format. |
$ docker run -p 4200:80 my-image
This command will run "my-image" mapping port 4200 on the host machine to 80 from the container.
$ docker run -d -p 4200:80 my-image
This command will do the same thing but in detached mode.
Option | Description |
---|---|
--detach , -d |
Run container in background and print container ID. |
--publish , -p HOSTPORT:CONTAINERPORT |
Publish a container’s port(s) to the host |
$ docker run username/repository:tag
This command will run the username/repository:tag
image.
$ docker run my-image <command>
This command will run the <command>
on a container started from "my-image".
$ docker run -it my-image /bin/bash
This command will start a new bash session on a container started from "my-image".
Option | Description |
---|---|
--interactive , -i |
Keep STDIN open even if not attached. |
--tty , -t |
Allocate a pseudo-TTY. |
$ docker exec -it <container id> <command>
This command will run the <command>
on the container.
$ docker exec -it <container id> /bin/bash
This command will start a new bash session on the container.
Option | Description |
---|---|
--interactive , -i |
Keep STDIN open even if not attached. |
--tty , -t |
Allocate a pseudo-TTY. |
$ docker image ls -a
This command will list all images on this machine.
$ docker image ls -a -q
This command will do the same but show only the IDs.
Option | Description |
---|---|
-a , --all |
Show all images (default hides intermediate images). |
-q , --quiet |
Only show numeric IDs. |
$ docker image rm <image id>
This command will remove the specified image from this machine.
$ docker image rm $(docker image ls -a -q)
This command will delete all images. The command docker image ls -a -q
will return all existing image IDs and pass them to the rm command which will delete them.
$ docker container ls
This command will list all running containers.
$ docker ps
This command will do the same.
$ docker container ls -q
This command will do the same but only show the IDs.
Option | Description |
---|---|
-q , --quiet |
Only show numeric IDs. |
$ docker container ls -a
This command will list all containers, even those not running.
$ docker container ls -a -q
This command will do the same but only show the IDs.
Option | Description |
---|---|
-a , --all |
Show all containers (default shows just running). |
-q , --quiet |
Only show numeric IDs. |
$ docker container stop <hash>
This command will gracefully stop the specified container.
$ docker container stop $(docker ps -q)
This command will gracefully stop all running containers. The command docker ps -q
will return all running container IDs and pass them to the stop command which will gracefully stop them.
$ docker container kill <hash>
This command will force shutdown of the specified container.
$ docker container kill $(docker ps -q)
This command will force shutdown all runnning containers. The command docker ps -q
will return all running container IDs and pass them to the kill command which will force shutdown them.
$ docker container rm <hash>
This command will remove the specified container from this machine.
$ docker rm $(docker ps -a -q)
This command will delete all stopped containers. The command docker ps -a -q
will return all existing container IDs and pass them to the rm command which will delete them. Any running containers will not be deleted.
$ 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 logout
Log out of this CLI session.