Docker Commands
Container Commands
Run a Container
$ docker container run <image> <app>General format to run a container from an image on docker hub.$ docker container run --name my-nginx -P -d nginxCreate container “my-nginx” based on nginx image and run in-ddetached mode, i.e. it runs till it is stopped and does not listen to the command line. The -P option tells Docker to map ports 80 & 443 to randomly selected ports on the docker host.$ docker container run --name my-nginx -p8088:80 -d nginxRun the nginx container and map container port 80 to docker host port 8088.$ docker container run -it insight /bin/bashStart a container from the insight image and run /bin/bash interactively.$ docker container run -it --name c3 oraclelinux /bin/bashStart an oracle linux container and run bash interactively. Press Ctrl P + Ctrl Q to leave the container with bash running.$ docker container run -d --name c1 oraclelinux sleep 1dStart an oracle linux container and run the sleep command for 1d; i.e. it will be running in the background for a day.$ docker container run --rm -it alpine shStart /bin/sh from an alpine container but delete it when we’re done interacting with it.# apk update && apk add iputilsInstall the iputils so that we can use ping.# ping 192.168.1.1
$ docker container run -it thomaspk/tom-web:latest /bin/bashto run interactive bash shell in the container. Especially useful for debugging. Replace with$ docker exec -it <container_name> /bin/bashto run bash in an existing running container for debugging.$ docker run ubuntuwill download, run the ubuntu image and promptly end as there are no user processes running in it.$ docker run --user=1000 ubuntu sleep 3600will download the ubuntu image and run the sleep command for 1 hour as user id 1000.
Manage Containers
$ docker container psLists all running containers.$ docker container ls -aList all containers, even those terminated.$ docker container rm $(docker container ls -qa) -fKill all containers, even those terminated ones.$ docker container start <name / id>$ docker container stop <name / id>Gracefully stop the container.
Image Commands
Image Builds
$ docker build -t web .builds a docker image based on the Dockerfile in the current directory and names it web.$ docker tag web thomaspk/webrename the image tagged as web to thomaspk/web before pushing to Docker Hub.$ docker image build -t insight:latest .Build an image based on the current Dockerfile contents. You need to be in a directory that holds the Dockerfile.$ docker images lslists all the images in the current system.$ docker image ls --digestsdisplays the digest SHA key for the image$ docker image pull oraclelinux:latestGet the latest full image from the oraclelinux docker repository$ docker image inspect ubuntu:latestDisplays detailed information such as the layers$ docker loginto login to docker hub from the command line (in case you haven’t already done so). The command will prompt you for your username and password and return with.
thomas-pk@deb-k8master:~/k8-app$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: thomaspk
Password:
WARNING! Your password will be stored unencrypted in /home/thomas-pk/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
$ docker push thomaspk/webto push the image to docker hub.
thomas-pk@deb-k8master:~/k8-app$ docker push thomaspk/web
The push refers to repository [docker.io/thomaspk/web]
00cc63633ca5: Pushed
cb18f283cf97: Mounted from library/openjdk
14cdbe52759f: Mounted from library/openjdk
33328dafa5c7: Mounted from library/openjdk
f2cb0ecef392: Mounted from library/openjdk
latest: digest: sha256:c4290e799e0cb9e8cb27cd20ba77450b45172dec04789fe538ea40cc2187c216 size: 1371
thomas-pk@deb-k8master:~/k8-app$
Deleting Images
$ docker image prune -aRemove all images not associated with any containers.$ docker image rm ubuntu:latestOnly remove that particular image.$ docker image rm 94e814e2efa8Only removes the image with this image ID.$ docker image rm $(docker image ls -q) -fTo delete all images on a docker host.