Docker基础命令

Posted liuzhijun666

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker基础命令相关的知识,希望对你有一定的参考价值。

Docker常用命令介绍

1、docker常用命令

命令 作用
docker search 查询镜像
docker pull 从网上拉取镜像
docker create 创建容器
docker ps 查看容器进程
docker start 启动容器进程
docker stop 终止容器进程
docker logs 查看日志
docker images 查看docker中的镜像
docker rm 删除镜像
docker rmi 删除docker容器
docker exec 交互式进入docker容器中
docker run 拉取镜像,创建容器、启动容器
dcoker kill 强行终止docker进程

2、例子

2.1、查询、拉取容器

# docker images 			---列出本地镜像
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

# docker search nginx 		  ---- 查询nginx镜像
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                              Official build of Nginx.                        13457               [OK]                
jwilder/nginx-proxy                Automated Nginx reverse proxy for docker con…   1837                                    [OK]
richarvey/nginx-php-fpm            Container running Nginx + PHP-FPM capable of…   780                                     [OK]
# docker pull nginx			 ----拉取镜像
Using default tag: latest
latest: Pulling from library/nginx
8559a31e96f4: Pull complete 
1cf27aa8120b: Pull complete 
67d252a8c1e1: Pull complete 
9c2b660fcff6: Pull complete 
4584011f2cd1: Pull complete 
Digest: sha256:a93c8a0b0974c967aebe868a186e5c205f4d3bcb5423a56559f2f9599074bbcd
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

2.2、创建并且启动容器

# docker ps -a				---查询所有进程
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
8f7f5e453029        nginx               "/docker-entrypoint.…"   13 seconds ago      Created                                 condescending_einstein
# docker start 8f7f5e453029				---启动nginx进程
8f7f5e453029
# docker ps					---查看启动进程
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
8f7f5e453029        nginx               "/docker-entrypoint.…"   49 seconds ago      Up 9 seconds        80/tcp              condescending_einstein
[root@docker ~]# 

最简单的拉取、创建、启动

[root@docker ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@docker ~]# docker run -d -it nginx
Unable to find image ‘nginx:latest‘ locally
latest: Pulling from library/nginx
8559a31e96f4: Pull complete 
1cf27aa8120b: Pull complete 
67d252a8c1e1: Pull complete 
9c2b660fcff6: Pull complete 
4584011f2cd1: Pull complete 
Digest: sha256:a93c8a0b0974c967aebe868a186e5c205f4d3bcb5423a56559f2f9599074bbcd
Status: Downloaded newer image for nginx:latest
d7cd36b5d00879de59e4df9ed4e14bfeb5a626893a5823b66133acd0959f1acd
[root@docker ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
d7cd36b5d008        nginx               "/docker-entrypoint.…"   29 seconds ago      Up 28 seconds       80/tcp              unruffled_swanson
[root@docker ~]# 

3、终止容器进程

# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
8f7f5e453029        nginx               "/docker-entrypoint.…"   5 minutes ago       Up 15 seconds       80/tcp              condescending_einstein
# docker stop 8f7f5e453029				---停止nginx容器进程
8f7f5e453029
# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
# docker restart 8f7f5e453029			---重启
8f7f5e453029
# docker kill 8f7f5e453029				---强行终止
8f7f5e453029
# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
#

4、进入容器

# docker exec -it 8f7f5e453029 /bin/bash
root@8f7f5e453029:/# ls
bin   dev		   docker-entrypoint.sh  home  lib64  mnt  proc  run   srv  tmp  var
boot  docker-entrypoint.d  etc			 lib   media  opt  root  sbin  sys  usr
root@8f7f5e453029:/# exit

5、删除容器和镜像

1、关闭所有该容器进程

2、删除容器

4、删除镜像

# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
8f7f5e453029        nginx               "/docker-entrypoint.…"   10 minutes ago      Up About a minute   80/tcp              condescending_einstein
# docker stop 8f7f5e453029
8f7f5e453029
# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
# docker rm 8f7f5e453029
8f7f5e453029
# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              0901fa9da894        39 hours ago        132MB
# docker rmi 0901fa9da894
Untagged: nginx:latest
Untagged: nginx@sha256:a93c8a0b0974c967aebe868a186e5c205f4d3bcb5423a56559f2f9599074bbcd
Deleted: sha256:0901fa9da894a8e9de5cb26d6749eaffb67b373dc1ff8a26c46b23b1175c913a
Deleted: sha256:89a4b5ca03d8301855dee414fc0631cff6410a48ff3a54f5f271906b81cd4e2b
Deleted: sha256:2e03e0e054440102d46743deb10a98bd4a2da37282a5c955c65e30a7bca53fc7
Deleted: sha256:f7b1f49be3e1a298b598c7822a2a9334e0b806da2da34a740a4d64b7c5f877ce
Deleted: sha256:2cdd3840848b3193bc22db56d42a6debdee858468fe3b506656d7d3c836a23c7
Deleted: sha256:13cb14c2acd34e45446a50af25cb05095a17624678dbafbcc9e26086547c1d74
# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

以上是关于Docker基础命令的主要内容,如果未能解决你的问题,请参考以下文章

docker简介和基础命令

Docker 学习 | 基础命令

Docker 基础知识-入门篇

Docker基础命令详解——镜像及容器操作

1. Docker基础命令

Docker基础命令