Skip to content
thesarfo

Reference

Docker: Command Cheatsheet

Common Docker CLI commands, plus the steps to create a Dockerfile and run a container from it.

views 0
CommandDescription
curl localhostPings the application.
docker buildBuilds an image from a Dockerfile.
docker build . -tBuilds the image and tags the image id.
docker CLIStart the Docker command line interface.
docker container rmRemoves a container.
docker imagesLists the images.
docker psLists the containers.
docker ps -aLists the containers that ran and exited successfully.
docker pullPulls the latest image or repository from a registry.
docker pushPushes an image or a repository to a registry.
docker runRuns a command in a new container.
docker run -pRuns the container by publishing the ports.
docker stopStops one or more running containers.
docker stop $(docker ps -q)Stops all running containers.
docker tagCreates a tag for a target image that refers to a source image.
docker --versionDisplays the version of the Docker CLI.
exitCloses the terminal session.
export MY_NAMESPACEExports a namespace as an environment variable.
git cloneClones the git repository that contains the artifacts needed.
ibmcloud cr imagesLists images in the IBM Cloud Container Registry.
ibmcloud cr loginLogs your local Docker daemon into IBM Cloud Container Registry.
ibmcloud cr namespacesViews the namespaces you have access to.
ibmcloud cr region-setEnsures that you are targeting the region appropriate to your cloud account.
ibmcloud targetProvides information about the account you’re targeting.
ibmcloud versionDisplays the version of the IBM Cloud CLI.
lsLists the contents of this directory to see the artifacts.

Steps to create and run containers

  1. Create a Dockerfile. A Dockerfile is a text file that contains instructions needed to create an image. It’s created using any editor, either from the console/terminal. It has a few instructions in it:

    • FROM — defines the base image
    • RUN — executes arbitrary commands
    • CMD — defines default commands for container execution
  2. Use the Dockerfile to create a container image. An image is simply a template for creating a container — it’s read-only, and a writable layer is added when an image is run as a container.

    Create a container image using the docker build command, along with the tag, repository, version, and current directory:

    Terminal window
    docker build -t my-app:v1 .

    To verify the creation of the image, run docker images.

  3. Use the container image to create a running container — create it with the run command along with the container image name and tag:

    Terminal window
    docker run my-app:v1

    To verify the details of the container created, run docker ps -a.

Most used commands

  1. docker image ls or docker images — lists all images
  2. docker image rm <image> or docker rmi — removes an image
  3. docker image pull <image> or docker pull — pulls an image from a Docker registry
  4. docker container ls -a or docker ps -a — lists all containers
  5. docker container run <image> or docker run — runs a container from an image
  6. docker container rm <container> or docker rm — removes a container
  7. docker container stop <container> or docker stop — stops a container
  8. docker container exec <container> or docker exec — executes a command inside the container
  9. docker system prune — clears almost everything