docker cli cheat sheet a year ago
PREFACE
Docker provides the ability to package and run an application in a loosely isolated environment called a container.
Docker 提供了松散隔离环境中打包和运行应用程序的能力, 通常我们称之为容器
The isolation and security allows you to run many containers simultaneously on a given host.
隔离和安全性允许你在给定的主机上同时运行多个容器。
Containers are lightweight and contain everything needed to run the application, so you do not need to rely on what is currently installed on the host.
容器是轻量级的并且包含运行应用程序所需的一切,因此你不需要关心主机上安装了什么
You can easily share containers while you work, and be sure that everyone you share with gets the same container that works in the same way.
工作中你可以轻松的share容器,并能确保你share的每个人都能拿到同一个容器。
INSTALLATION
Docker Desktop 是跨平台的, 支持 Mac, Linux 和 Windows
这里有一些使用docker的项目
docker官方文档见 https://docs.docker.com
GENERAL COMMANDS
# Start the docker daemon
docker -d
# Get help with Docker. Can also use –help on all subcommands
docker --help
# Display system-wide information
docker info
IMAGES
Docker images are a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings.
Docker 镜像是一个轻量级的、独立的、可执行包的, 包含运行应用程序所需的一切(代码、runtime、系统工具、系统库和设置灯)的软件:
# Build an Image from a Dockerfile
docker build -t <image_name>
# Build an Image from a Dockerfile without the cache
docker build -t <image_name> . –no-cache
# List local images
docker images
# Delete an Image
docker rmi <image_name>
# Remove all unused images
docker image prune
DOCKER HUB
Docker Hub is a service provided by Docker for finding and sharing container images with your team. Learn more and find images at https://hub.docker.com
docker hub简单来说就是一个镜像仓库
# Login into Docker
docker login -u <username>
# Publish an image to Docker Hub
docker push <username>/<image_name>
# Search Hub for an image
docker search <image_name>
# Pull an image from a Docker Hub
docker pull <image_name>
CONTAINERS
A container is a runtime instance of a docker image. A container will always run the same, regardless of the infrastructure. Containers isolate software from its environment and ensure that it works uniformly despite differences for instance between development and staging.
容器是 Docker 镜像的运行时的实例。 一个容器无论“基础设施”如何,都将始终以相同的方式运行。容器将软件与其环境隔离并确保运行效果是一致的 尽管示例在development和staging之间存在差异。
# Create and run a container from an image, with a custom name:
docker run --name <container_name> <image_name>
# Run a container with and publish a container’s port(s) to the host.
docker run -p <host_port>:<container_port> <image_name>
# Run a container in the background
docker run -d <image_name>
# Start or stop an existing container:
docker start|stop <container_name> (or <container-id>)
# Remove a stopped container:
docker rm <container_name>
# Open a shell inside a running container:
docker exec -it <container_name> sh
# Fetch and follow the logs of a container:
docker logs -f <container_name>
# To inspect a running container:
docker inspect <container_name> (or <container_id>)
# To list currently running containers:
docker ps
# List all docker containers (running and stopped):
docker ps --all
# View resource usage stats
docker container stats