A concise cheat sheet for everyday Docker commands.

Installation

Ubuntu Docker installation
Docker for Mac
Docker Desktop for Windows


Docker Lifecycle

Commands Descriptions
docker create creates a container but does not start it.
docker images allows the container to be renamed.
docker run creates and starts a container in one operation.
docker rm deletes a container.
docker update updates a container's resource limits.

Docker Ship Commands

Commands Descriptions
docker login Log in to a Docker Hub registry.
docker pull rommelporras/laravel-php:7.3 Pull an image from a registry.
docker tag laravel-php:7.3 rommelporras/laravel-php:7.3 Retag a local image with a new image name and tag.
docker push yourrepository/laravel-php:3.4 Push an image to a registry

Docker Build Commands

Commands Descriptions
docker build -t laravel-php:7.3 Build an image from the Dockerfile in the current directory and tag the image
docker images List all images that are locally stored with the Docker engine
docker rmi laravel-php:7.3 Delete an image from the local image store

Docker Run Commands

Commands Descriptions
docker run
-d
-p 3000:80
laravel-php:7.3
Docker run command
To run the container in the background process.
expose port 3000 externally and map to port 80.
container name.
docker stop laravel-php:7.3 Stop a running container through SIGTERM
docker kill laravel-php:7.3 Stop a running container through SIGKILL
docker rm -f $(docker ps -aq) Delete all running and stopped containers
docker ps List the running containers
docker ps –a To see all the running and stopped containers
docker exec -it web bash Create a new bash process inside the container and connectit to the terminal
docker logs --tail 100 web Print the last 100 lines of a container’s logs
docker events Get Events

Docker Repository and Registry Commands

Commands Descriptions
docker login login to a registry.
docker logout logout from a registry.
docker search searches registry for image.
docker pull pulls an image from registry to local machine.
docker push pushes an image to the registry from local machine.

Docker Commands Basic Structure:

  • Image — A snapshot file of a container. Create containers by pulling and running Docker Images.
  • Docker Hub / Registry — A public cloud-based registry service for developers to share their code on.
  • [options] — Add options to your Docker commands to change the results. Each command has multiple options available.
  • [command] — Enter the parameters for the Docker command, this could be a filename or container ID.
  • [arg] — Set additional argument variables.
  • [env] — Use to configure environment variables.
  • [init] — Initialize a swarm (by default, Docker will designate chosen machine as manager node).
  • [ca] — Show root Certificate Authority (auto-generated by init command).
  • [join] — Join a swarm as a worker/manager node.
  • [join-token] — Manage join tokens—define worker or manager accordingly.
  • [leave] — When implemented on a worker, the command prompts the node to leave the swarm.
  • [–force] — When implemented on a manager, the command forces the node to leave the swarm.
  • [tag] — The tag parameter is often part of the image name, but as your swarm develops you can adjust the tag without changing the base image name.
  • [unlock] — Unlock swarm manager with a user-own unlock key.
  • [unlock-key] — Manage the unlock key, useful to unlock a manager node.
  • [update] — Update the swarm.

Starting & Stopping Containers

Starting

docker start nginx

Stopping

docker stop nginx

Restarting

docker restart nginx

Pausing

docker pause nginx

Unpausing

docker unpause nginx

Blocking a Container

docker wait nginx

Sending a SIGKILL

docker kill nginx

Connecting to an Existing Container

docker attach nginx

Getting Information about Containers

Running Containers

docker ps
docker ps -a

Container Logs

docker logs infinite

Inspecting Containers

docker inspect infinite
docker inspect --format '{{ .NetworkSettings.IPAddress }}' $(docker ps -q)

Containers Events

docker events infinite

Public Ports

docker port infinite

Running Processes

docker top infinite

Container Resource Usage

docker stats infinite

Inspecting changes to files or directories on a container’s filesystem

docker diff infinite