欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Docker---基本命令

程序员文章站 2024-01-31 12:46:52
...

Docker—基本命令

1.查看docker的版本

docker version
[aaa@qq.com yum.repos.d]# docker version
Client: Docker Engine - Community
 Version:           19.03.8
 API version:       1.40
 Go version:        go1.12.17
 Git commit:        afacb8b
 Built:             Wed Mar 11 01:27:04 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.8
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.17
  Git commit:       afacb8b
  Built:            Wed Mar 11 01:25:42 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

2.搜索镜像

docker search 名称
[aaa@qq.com yum.repos.d]# docker search nginx
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                              Official build of Nginx.                        12948               [OK]                
jwilder/nginx-proxy                Automated Nginx reverse proxy for docker con…   1768                                    [OK]
richarvey/nginx-php-fpm            Container running Nginx + PHP-FPM capable of…   764                                     [OK]
linuxserver/nginx                  An Nginx container, brought to you by LinuxS…   102                                     
bitnami/nginx                      Bitnami nginx Docker Image                      82                                      [OK]
tiangolo/nginx-rtmp                Docker image with Nginx using the nginx-rtmp…   67                                      [OK]
jc21/nginx-proxy-manager           Docker container for managing Nginx proxy ho…   51                                      
nginxdemos/hello                   NGINX webserver that serves a simple page co…   47                                      [OK]
jlesage/nginx-proxy-manager        Docker container for Nginx Proxy Manager        37                                      [OK]
nginx/unit                         NGINX Unit is a dynamic web and application …   36                                      
nginx/nginx-ingress                NGINX Ingress Controller for Kubernetes         28                                      
privatebin/nginx-fpm-alpine        PrivateBin running on an Nginx, php-fpm & Al…   23                                      [OK]
schmunk42/nginx-redirect           A very simple container to redirect HTTP tra…   18                                      [OK]
centos/nginx-18-centos7            Platform for running nginx 1.8 or building n…   13                                      
nginxinc/nginx-unprivileged        Unprivileged NGINX Dockerfiles                  13                                      
centos/nginx-112-centos7           Platform for running nginx 1.12 or building …   13                                      
blacklabelops/nginx                Dockerized Nginx Reverse Proxy Server.          13                                      [OK]
raulr/nginx-wordpress              Nginx front-end for the official wordpress:f…   12                                      [OK]
nginx/nginx-prometheus-exporter    NGINX Prometheus Exporter                       10                                      
sophos/nginx-vts-exporter          Simple server that scrapes Nginx vts stats a…   7                                       [OK]
mailu/nginx                        Mailu nginx frontend                            6                                       [OK]
bitnami/nginx-ingress-controller   Bitnami Docker Image for NGINX Ingress Contr…   5                                       [OK]
ansibleplaybookbundle/nginx-apb    An APB to deploy NGINX                          1                                       [OK]
wodby/nginx                        Generic nginx                                   0                                       [OK]
centos/nginx-110-centos7           Platform for running nginx 1.10 or building …   0                                

3.下载镜像

docker pull nginx
[aaa@qq.com docker]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
c499e6d256d6: Pull complete 
74cda408e262: Pull complete 
ffadbd415ab7: Pull complete 
Digest: sha256:282530fcb7cd19f3848c7b611043f82ae4be3781cb00105a1d593d7e6286b596
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

4.查看下载镜像

docker images
[aaa@qq.com docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              ed21b7a8aee9        9 days ago          127MB

5.查看镜像的信息

docker inspect 镜像id

Docker---基本命令

6.添加镜像的标签(相当于新生成一个镜像)

docker tag 镜像名:原标签名 镜像名:新标签名
[aaa@qq.com docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              ed21b7a8aee9        9 days ago          127MB
[aaa@qq.com docker]# docker tag nginx:latest nginx:web
[aaa@qq.com docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              ed21b7a8aee9        9 days ago          127MB
nginx               web                 ed21b7a8aee9        9 days ago          127MB

7.镜像删除

docker rmi 镜像名:标签名
[aaa@qq.com docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              ed21b7a8aee9        9 days ago          127MB
nginx               web                 ed21b7a8aee9        9 days ago          127MB
[aaa@qq.com docker]# docker rmi nginx:web 
Untagged: nginx:web
[aaa@qq.com docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              ed21b7a8aee9        9 days ago          127MB

8.导出已经下载好的镜像

docker save -o nginx nginx:latest
[aaa@qq.com docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              ed21b7a8aee9        9 days ago          127MB
[aaa@qq.com docker]# cd /opt
[aaa@qq.com opt]# docker save -o nginx nginx:latest 
[aaa@qq.com opt]# ls -lh nginx 
-rw-------. 1 root root 125M 4月   9 20:23 nginx

9.导入镜像到docker

docker load < 镜像名
[aaa@qq.com opt]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
[aaa@qq.com opt]# docker load < nginx 
c3a984abe8a8: Loading layer  72.48MB/72.48MB
99134ec7f247: Loading layer  58.11MB/58.11MB
d37eecb5b769: Loading layer  3.584kB/3.584kB
Loaded image: nginx:latest
[aaa@qq.com opt]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              ed21b7a8aee9        9 days ago          127MB

10.上传镜像到共有仓库(需要docker官方账号)

登录:

docker login

输入账号,密码

上传:

docker push 镜像名:标签名

11.创建容器(i:输入,t:绑定终端)

docker create -it nginx:latest /bin/bash
[aaa@qq.com opt]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              ed21b7a8aee9        9 days ago          127MB
[aaa@qq.com opt]# docker create -it nginx:latest /bin/bash
4ef193d2930223a81262ea95f8d5fc2789b4c883874a1017f5b9c9597d3c5bf5

12.查看容器:

docker ps -a
[aaa@qq.com opt]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
4ef193d29302        nginx:latest        "/bin/bash"         About a minute ago   Created                                 funny_goodall

13.启动容器(启动后状态为up)

docker start 容器ID
[aaa@qq.com opt]# docker start 4ef193d29302
4ef193d29302
[aaa@qq.com opt]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
4ef193d29302        nginx:latest        "/bin/bash"         4 minutes ago       Up 9 seconds        80/tcp              funny_goodall

14.run命令

(docker run命令会执行从创建容器,开启容器,执行命令,关闭容器等一系列过程)
(如果没有创建容器,没有镜像,就会从下载镜像开始执行,一直到关闭容器)
(执行完成后直接关闭,状态为exited)
docker run 镜像名 /usr/bin/bash -c ls /
/usr/bin/bash指定运行环境,-c指定执行命令,/表示在根目录下
[aaa@qq.com opt]# docker run paigeeworld/centos7 /usr/bin/bash -c ls /
bin
boot
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var

15.进入和退出容器(容器得是UP状态)

docker exec -it 容器ID /bin/bash
[aaa@qq.com opt]# docker exec -it 8c2933f02d06 /bin/bash
bash-4.2# ls /
bin  boot  dev	etc  home  lib	lib64  lost+found  media  mnt  opt  proc  root	run  sbin  srv	sys  tmp  usr  var
bash-4.2# exit
exit
[aaa@qq.com opt]# 

16.关闭容器

docker stop 容器ID
[aaa@qq.com opt]# docker stop 8c2933f02d06
8c2933f02d06
[aaa@qq.com opt]# docker ps -a
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS                       PORTS               NAMES
03b1b87b64e0        paigeeworld/centos7   "/usr/bin/bash -c ls…"   21 minutes ago      Exited (0) 21 minutes ago                        stupefied_haslett
8c2933f02d06        paigeeworld/centos7   "/bin/bash"              29 minutes ago      Exited (137) 7 seconds ago                       gifted_yalow
4ef193d29302        nginx:latest          "/bin/bash"              41 minutes ago      Up 37 minutes                80/tcp              funny_goodall

17.导出容器

docker export 容器ID > 容器名
[aaa@qq.com opt]# ls
containerd  nginx  rh
[aaa@qq.com opt]# docker export 4ef193d29302 > nginx_c
[aaa@qq.com opt]# ls
containerd  nginx  nginx_c  rh

18.导入容器(导入容器只会生成镜像,不会生成容器)

cat 容器名 | docker import - 镜像:标签
[aaa@qq.com opt]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              ed21b7a8aee9        9 days ago          127MB
[aaa@qq.com opt]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
4ef193d29302        nginx:latest        "/bin/bash"         About an hour ago   Up 57 minutes       80/tcp              funny_goodall
[aaa@qq.com opt]# ls
containerd  nginx  nginx_c  rh
[aaa@qq.com opt]# cat nginx_c | docker import - nginx:web
sha256:1642b0350e944ca6b17efbb471b8bcdbbe9947f3e4d3e278d185be75292f20a7
[aaa@qq.com opt]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               web                 1642b0350e94        12 seconds ago      125MB
nginx               latest              ed21b7a8aee9        9 days ago          127MB
[aaa@qq.com opt]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
4ef193d29302        nginx:latest        "/bin/bash"         About an hour ago   Up 59 minutes       80/tcp              funny_goodall

19.删除容器

docker rm 容器ID
批量删除:
docker ps -aq | awk ‘{print "docker rm " $1}’ | bash
相关标签: Docker