docker基础操作
docker基础操作

docker基础操作

基础镜像管理命令

从dockerhub查找镜像,以nginx为例

[root@node1 docker]# docker search nginx
NAME                                              DESCRIPTION                                     STARS     OFFICIAL
nginx                                             Official build of Nginx.                        19631     [OK]
unit                                              Official build of NGINX Unit: Universal Web …   21        [OK]
nginx/nginx-ingress                               NGINX and  NGINX Plus Ingress Controllers fo…   88        
nginxinc/nginx-unprivileged                       Unprivileged NGINX Dockerfiles                  141       
nginx/nginx-prometheus-exporter                   NGINX Prometheus Exporter for NGINX and NGIN…   36        
nginxinc/nginx-s3-gateway                         Authenticating and caching gateway based on …   6         
nginx/unit                                        This repository is retired, use the Docker o…   64        
nginx/nginx-ingress-operator                      NGINX Ingress Operator for NGINX and NGINX P…   2         
nginxinc/amplify-agent                            NGINX Amplify Agent docker repository           1         
nginx/nginx-quic-qns                              NGINX QUIC interop                              1         
nginxinc/ingress-demo                             Ingress Demo                                    4         
nginxproxy/nginx-proxy                            Automated nginx proxy for Docker containers …   131       
nginxproxy/acme-companion                         Automated ACME SSL certificate generation fo…   130       
bitnami/nginx                                     Bitnami nginx Docker Image                      182       
解释说明:
NAME:镜像仓库源的名称
DESCRIPTION :镜像的描述
STARS:类似点赞,喜欢的意思
OFFICIAL:是否docker官方发布

下载镜像

[root@node1 docker]# docker pull nginx:latest   #latest表示最新版本
latest: Pulling from library/nginx
a2abf6c4d29d: Pull complete 
a9edb18cadd1: Pull complete 
589b7251471a: Pull complete 
186b1aaa4aa6: Pull complete 
b4df32aa5a72: Pull complete 
a0bcbecc962e: Pull complete 
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@node1 docker]# 

把镜像做成离线压缩包(可拷贝到其他机器上)

[root@node1 docker]# docker save -o nginx.tar.gz nginx
[root@node1 docker]# ls
nginx.tar.gz
[root@node1 docker]#

删除镜像

root@node1 docker]# docker rmi nginx:latest
Untagged: nginx:latest
Deleted: sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85
Deleted: sha256:b625d8e29573fa369e799ca7c5df8b7a902126d2b7cbeb390af59e4b9e1210c5
Deleted: sha256:7850d382fb05e393e211067c5ca0aada2111fcbe550a90fed04d1c634bd31a14
Deleted: sha256:02b80ac2055edd757a996c3d554e6a8906fd3521e14d1227440afd5163a5f1c4
Deleted: sha256:b92aa5824592ecb46e6d169f8e694a99150ccef01a2aabea7b9c02356cdabe7c
Deleted: sha256:780238f18c540007376dd5e904f583896a69fe620876cabc06977a3af4ba4fb5
Deleted: sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f
[root@node1 docker]# 

解压离线压缩包

[root@node1 docker]# docker load -i nginx.tar.gz 
2edcec3590a4: Loading layer [==================================================>]  83.86MB/83.86MB
e379e8aedd4d: Loading layer [==================================================>]     62MB/62MB
b8d6e692a25e: Loading layer [==================================================>]  3.072kB/3.072kB
f1db227348d0: Loading layer [==================================================>]  4.096kB/4.096kB
32ce5f6a5106: Loading layer [==================================================>]  3.584kB/3.584kB
d874fd2bc83b: Loading layer [==================================================>]  7.168kB/7.168kB
Loaded image: nginx:latest
[root@node1 docker]# 

容器相关命令

1、以交互式方式启动容器,以centos镜像为例

[root@node1 docker]# docker run --name=hello -it centos /bin/bash
--name 容器名称
-i 交互式
-t 分配伪终端
centos  指定使用的镜像
/bin/bash  说明容器的shell类型为bash

运行报错,排查发现确实libseccomp依赖,安装依赖

再次运行成功,进入交互式界面

exit退出容器

2、以守护进程的方式启动容器

[root@node1 lib]# docker run --name=hello01 -td centos
# -d 在后台运行docker

3、查看正在运行的容器

[root@node1 lib]# docker ps
参数:
-a 查看所有容器,包括正在运行和退出的容器

4、停止容器

[root@node1 lib]# docker stop hello01

5、启动已经停止的容器

[root@node1 lib]# docker start hello01

6、删除容器

[root@node1 lib]# docker rm -f hello01
-f  强制删除

部署nginx服务

[root@node1 lib]# docker run  --name nginx -p 80 -itd nginx
-p  把容器端口映射到物理机的一个随机端口,可通过80:80指定物理机映射端口

通过docker ps 查看到物理机的随机端口为32769,测试用浏览器访问

查看容器 配置文件,可以查看IP,镜像等配置信息

[root@node1 lib]#docker inspect nginx

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注