3、ubantu系统docker常用命令
1、自助查看docker命令
1.1、查看所有命令
docker 客户端非常简单,可以直接输入 docker 命令来查看到 Docker 客户端的所有命令选项。
angqiang@wangqiang:~$ dockerUsage: docker [OPTIONS] COMMANDA self-sufficient runtime for containersCommon Commands:run Create and run a new container from an imageexec Execute a command in a running containerps List containersbuild Build an image from a Dockerfilepull Download an image from a registrypush Upload an image to a registryimages List imageslogin Log in to a registrylogout Log out from a registrysearch Search Docker Hub for imagesversion Show the Docker version informationinfo Display system-wide informationManagement Commands:builder Manage buildscontainer Manage containerscontext Manage contextsimage Manage imagesmanifest Manage Docker image manifests and manifest listsnetwork Manage networksplugin Manage pluginssystem Manage Dockertrust Manage trust on Docker imagesvolume Manage volumesSwarm Commands:swarm Manage SwarmCommands:attach Attach local standard input, output, and error streams to a running containercommit Create a new image from a container's changescp Copy files/folders between a container and the local filesystemcreate Create a new containerdiff Inspect changes to files or directories on a container's filesystemevents Get real time events from the serverexport Export a container's filesystem as a tar archivehistory Show the history of an imageimport Import the contents from a tarball to create a filesystem imageinspect Return low-level information on Docker objectskill Kill one or more running containersload Load an image from a tar archive or STDINlogs Fetch the logs of a containerpause Pause all processes within one or more containersport List port mappings or a specific mapping for the containerrename Rename a containerrestart Restart one or more containersrm Remove one or more containersrmi Remove one or more imagessave Save one or more images to a tar archive (streamed to STDOUT by default)start Start one or more stopped containersstats Display a live stream of container(s) resource usage statisticsstop Stop one or more running containerstag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGEtop Display the running processes of a containerunpause Unpause all processes within one or more containersupdate Update configuration of one or more containerswait Block until one or more containers stop, then print their exit codesGlobal Options:--config string Location of client config files (default "/home/wangqiang/.docker")-c, --context string Name of the context to use to connect to the daemon (overridesDOCKER_HOST env var and default context set with "dockercontext use")-D, --debug Enable debug mode-H, --host list Daemon socket to connect to-l, --log-level string Set the logging level ("debug", "info", "warn", "error","fatal") (default "info")--tls Use TLS; implied by --tlsverify--tlscacert string Trust certs signed only by this CA (default"/home/wangqiang/.docker/ca.pem")--tlscert string Path to TLS certificate file (default"/home/wangqiang/.docker/cert.pem")--tlskey string Path to TLS key file (default "/home/wangqiang/.docker/key.pem")--tlsverify Use TLS and verify the remote-v, --version Print version information and quitRun 'docker COMMAND --help' for more information on a command.For more help on how to use Docker, head to https://docs.docker.com/go/guides/
1.2、查看命令使用说明
可以通过命令 docker command --help 更深入的了解指定的 Docker 命令使用方法。
wangqiang@wangqiang:~$ docker ps --helpUsage: docker ps [OPTIONS]List containersAliases:docker container ls, docker container list, docker container ps, docker psOptions:-a, --all Show all containers (default shows just running)-f, --filter filter Filter output based on conditions provided--format string Format output using a custom template:'table': Print output in table format with columnheaders (default)'table TEMPLATE': Print output in table format using the givenGo template'json': Print in JSON format'TEMPLATE': Print output using the given Go template.Refer to https://docs.docker.com/go/formatting/ for moreinformation about formatting output with templates-n, --last int Show n last created containers (includes all states) (default -1)-l, --latest Show the latest created container (includes all states)--no-trunc Don't truncate output-q, --quiet Only display container IDs-s, --size Display total file sizes
2、docker命令大全
2.1、容器生命周期管理
docker create
docker create [OPTIONS] IMAGE [COMMAND] [ARG...]
:创建一个容器,但不启动它。
docker run
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
:创建并启动一个容器。
docker start
docker start [OPTIONS] CONTAINER [CONTAINER...]
:启动一个或多个已停止的容器。
docker stop
docker stop [OPTIONS] CONTAINER [CONTAINER...]
:停止一个或多个正在运行的容器。
docker restart
docker restart [OPTIONS] CONTAINER [CONTAINER...]
:重启一个或多个容器。
docker rm
docker rm [OPTIONS] CONTAINER [CONTAINER...]
:删除一个或多个容器。
docker container prune
docker container prune
:删除所有已停止的容器。
docker pause CONTAINER
docker pause CONTAINER
:暂停容器的所有进程。
docker unpause CONTAINER
docker unpause CONTAINER
:恢复容器的所有进程。
2.2、容器操作
docker ps
docker ps [OPTIONS]
:列出容器。
docker ps -a
docker ps -a
:列出所有容器(包括已停止的)。
docker logs
docker logs [OPTIONS] CONTAINER
:获取容器的日志。
docker exec
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
:在运行的容器中执行命令。
docker attach
docker attach [OPTIONS] CONTAINER
:连接到正在运行的容器。
docker export
docker export [OPTIONS] CONTAINER
:将容器导出为 tar 归档文件。
docker import
docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
:从 tar 归档文件或 URL 导入容器。
2.3、容器的 root 文件系统(rootfs)命令
docker diff CONTAINER
docker diff CONTAINER
:检查容器文件系统中的更改。
docker cp
docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH
:从容器复制文件到主机。
docker cp
docker cp [OPTIONS] SRC_PATH CONTAINER:DEST_PATH
:从主机复制文件到容器。
2.4、镜像仓库
docker login
docker login [OPTIONS] [SERVER]
:登录到 Docker 镜像仓库。
docker logout
docker logout [SERVER]
:登出 Docker 镜像仓库。
docker search
docker search [OPTIONS] TERM
:搜索 Docker Hub 上的镜像。
2.5、本地镜像管理
docker images
docker images [OPTIONS] [REPOSITORY[:TAG]]
:列出本地镜像。
docker rmi
docker rmi [OPTIONS] IMAGE [IMAGE...]
:删除一个或多个镜像。
docker image prune
docker image prune
:删除所有未使用的镜像。
docker build
docker build [OPTIONS] PATH | URL | -
:从 Dockerfile 构建镜像。
docker pull
docker pull [OPTIONS] NAME[:TAG|@DIGEST]
:从仓库拉取镜像。
docker push
docker push [OPTIONS] NAME[:TAG]
:将镜像推送到仓库。
docker history
docker history [OPTIONS] IMAGE
:显示镜像的历史。
2.6、info|version
docker info
docker info [OPTIONS]
:显示 Docker 系统信息。
docker version
docker version [OPTIONS]
:显示 Docker 版本信息。
2.7、Docker Compose
通常通过包管理器(如 apt-get)安装Docker Compose。
docker-compose up
docker-compose up
:启动服务。
docker-compose down
docker-compose down
:停止并移除服务。
docker-compose build
docker-compose build
:构建服务。
docker-compose ps
docker-compose ps
:列出服务状态。
docker-compose logs
docker-compose logs
:查看服务日志。
2.8、网络命令
docker network ls
docker network ls
:列出所有网络。
docker network create
docker network create [OPTIONS] NETWORK
:创建一个网络。
docker network rm
docker network rm NETWORK [NETWORK...]
:删除一个或多个网络。
docker network connect
docker network connect [OPTIONS] NETWORK CONTAINER
:将容器连接到网络。
docker network disconnect
docker network disconnect [OPTIONS] NETWORK CONTAINER
:将容器从网络断开。
2.9、卷命令
docker volume ls
docker volume ls
:列出所有卷。
docker volume create
docker volume create [OPTIONS] [VOLUME]
:创建一个卷。
docker volume rm
docker volume rm [OPTIONS] VOLUME [VOLUME...]
:删除一个或多个卷。
docker volume prune
docker volume prune
:删除所有未使用的卷。
2.10、补充说明
docker system df
docker system df
:显示 Docker 磁盘使用情况。
docker system prune
docker system prune
:清理未使用的对象(容器、镜像、网络等)。