Common Commands

1
2
3
4
5
6
7
8
docker info     # View local Docker information
docker search openresty   # Search remote image repository
docker images      # View images in the local image repository
docker ps          # View currently running containers
docker pull centos  # Pull an image from the remote repository (default tag is 'latest' if not specified)
docker container run -p 8000:80 --rm -t -i centos:latest /bin/bash  # Map container port 80 to host port 8000; --rm removes the container after termination (useful for temporary debugging); -t allocates a pseudo-terminal and binds it to the container's stdin; -i keeps the container's stdin open
docker container exec -i -t b5d7bad57561 /bin/bash # Enter a running container
docker rmi $(docker images -f "dangling=true" -q) # Batch remove dangling (unnamed) images

Importing and Exporting Images

1
2
3
docker save -o centos7.tar centos  # Export image to tar file
docker load < centos7.tar   # Import image (via standard input, including original metadata such as tags)
docker load --input centos7.tar   # Load image from tar file (non-standard input method)

Importing and Exporting Containers

1
2
3
4
5
6
docker ps   # Default: show currently running containers
docker ps -a   # Show all containers, including stopped ones
docker ps -l   # Show the most recently created container (including stopped ones)

docker export 5a80afa126ba > centos7.5a80afa126ba.tar  # Export container snapshot
docker import 5a80afa126ba > centos7.5a80afa126ba.tar  # Import container snapshot into image repository

Note: Users can use docker load to import image files into the local image library, or docker import to import container snapshots. The key difference is that container snapshot files discard all historical records and metadata (preserving only the state at the time of the snapshot), while image storage files preserve full history and are larger in size. Additionally, when importing from a container snapshot, you can reassign metadata like tags.

Removing Images or Containers

1
2
3
4
5
6
docker rmi jdeathe/centos-ssh # Remove local image
or
docker image rm jdeathe/centos-ssh # Remove local image
docker rm dbd4d83097f5    # Remove local container
or
docker container rm dbd4d83097f5 # Remove local container

Stopping and Restarting Containers

1
2
docker container kill dbd4d83097f5  # Stop a running container
docker container restart dbd4d83097f5  # Restart a container

Building Images

1
2
docker image build .      # Build image from Dockerfile in current directory
docker image build -t cm-navigation:v0.0.1 .  # Build image from Dockerfile in current directory and assign a tag