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 nginx Create container “my-nginx” based on nginx image and run in -d detached 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 nginx Run the nginx container and map container port 80 to docker host port 8088.

  • $ docker container run -it insight /bin/bash Start a container from the insight image and run /bin/bash interactively.

  • $ docker container run -it --name c3 oraclelinux /bin/bash Start 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 1d Start 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 sh Start /bin/sh from an alpine container but delete it when we’re done interacting with it.

    • # apk update && apk add iputils Install the iputils so that we can use ping.

    • # ping 192.168.1.1

  • $ docker container run -it thomaspk/tom-web:latest /bin/bash to run interactive bash shell in the container. Especially useful for debugging. Replace with $ docker exec -it <container_name> /bin/bash to run bash in an existing running container for debugging.

  • $ docker run ubuntu will download, run the ubuntu image and promptly end as there are no user processes running in it.

  • $ docker run --user=1000 ubuntu sleep 3600 will download the ubuntu image and run the sleep command for 1 hour as user id 1000.

Manage Containers

  • $ docker container ps Lists all running containers.

  • $ docker container ls -a List all containers, even those terminated.

  • $ docker container rm $(docker container ls -qa) -f Kill 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/web rename 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 ls lists all the images in the current system.

  • $ docker image ls --digests displays the digest SHA key for the image

  • $ docker image pull oraclelinux:latest Get the latest full image from the oraclelinux docker repository

  • $ docker image inspect ubuntu:latest Displays detailed information such as the layers

  • $ docker login to 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/web to 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 -a Remove all images not associated with any containers.

  • $ docker image rm ubuntu:latest Only remove that particular image.

  • $ docker image rm 94e814e2efa8 Only removes the image with this image ID.

  • $ docker image rm $(docker image ls -q) -f To delete all images on a docker host.