Docker快速入门


下面的例子是在CentOS 7.9下测试的。

安装

配置yum

如果没有安装yum-config-manager,执行下面的命令安装。

yum install yum-utils.noarch

配置docker安装所需的软件源

yum-config-manager --add-repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo

安装docker

列出可用的docker版本

yum list docker-ce --showduplicates | sort -r
[root@rz-153-nw-test ~]# yum list docker-ce --showduplicates | sort -r
 * updates: mirrors.huaweicloud.com
Loading mirror speeds from cached hostfile
Loaded plugins: fastestmirror
 * extras: mirrors.aliyun.com
docker-ce.x86_64            3:26.1.2-1.el7                      docker-ce-stable
docker-ce.x86_64            3:26.1.1-1.el7                      docker-ce-stable
docker-ce.x86_64            3:26.1.0-1.el7                      docker-ce-stable
docker-ce.x86_64            3:26.0.2-1.el7                      docker-ce-stable
docker-ce.x86_64            3:26.0.1-1.el7                      docker-ce-stable
docker-ce.x86_64            3:26.0.0-1.el7                      docker-ce-stable
docker-ce.x86_64            3:25.0.5-1.el7                      docker-ce-stable
docker-ce.x86_64            3:25.0.4-1.el7                      docker-ce-stable
docker-ce.x86_64            3:25.0.3-1.el7                      docker-ce-stable
......
docker-ce.x86_64            17.06.2.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.06.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.06.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.03.3.ce-1.el7                    docker-ce-stable
docker-ce.x86_64            17.03.2.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.03.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.03.0.ce-1.el7.centos             docker-ce-stable
 * base: mirrors.huaweicloud.com
Available Packages
[root@rz-153-nw-test ~]# 

选择指定的版本进行安装。

通过其完整的软件包名称安装特定版本,该软件包名称是软件包名称(docker-ce)加上版本字符串(第二列),从第一个冒号(:)一直到第一个连字符,并用连字符(-)分隔。例如: docker-ce.x86_64            3:20.10.0-3.el7                     docker-ce-stable
yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io

3:25.0.5-1.el7的版本信息为25.0.5

yum install docker-ce-25.0.5 docker-ce-cli-25.0.5 containerd.io

启动docker服务

安装后可以查看docker服务的状态和启动服务

systemctl status docker
systemctl start docker

使用docker

搜索镜像

docker search nginx

示例如下:

[root@rz-153-nw-test ~]# docker search nginx
NAME                                              DESCRIPTION                                     STARS     OFFICIAL
nginx                                             Official build of Nginx.                        19833     [OK]
unit                                              Official build of NGINX Unit: Universal Web …   29        [OK]
nginx/nginx-ingress                               NGINX and  NGINX Plus Ingress Controllers fo…   90        
nginxinc/nginx-unprivileged                       Unprivileged NGINX Dockerfiles                  147       
nginx/nginx-prometheus-exporter                   NGINX Prometheus Exporter for NGINX and NGIN…   40        
nginxinc/nginx-s3-gateway                         Authenticating and caching gateway based on …   6         
nginx/unit                                        This repository is retired, use the Docker o…   63        
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 …   136       
nginxproxy/acme-companion                         Automated ACME SSL certificate generation fo…   132       
bitnami/nginx                                     Bitnami container image for NGINX               186       
bitnami/nginx-ingress-controller                  Bitnami container image for NGINX Ingress Co…   33        
nginxproxy/docker-gen                             Generate files from docker container meta-da…   17        
bitnami/nginx-exporter                            Bitnami container image for NGINX Exporter      5         
nginxinc/mra-fakes3                                                                               0         
ubuntu/nginx                                      Nginx, a high-performance reverse proxy & we…   112       
rancher/nginx-ingress-controller                                                                  13        
nginxinc/ngx-rust-tool                                                                            0         
nginxinc/mra_python_base                                                                          0         
kasmweb/nginx                                     An Nginx image based off nginx:alpine and in…   7         
rancher/nginx-ingress-controller-defaultbackend                                                   2         
[root@rz-153-nw-test ~]# 

拉取镜像

docker pull nginx

示例如下:

[root@rz-153-nw-test ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
09f376ebb190: Pull complete 
a11fc495bafd: Pull complete 
933cc8470577: Pull complete 
999643392fb7: Pull complete 
971bb7f4fb12: Pull complete 
45337c09cd57: Pull complete 
de3b062c0af7: Pull complete 
Digest: sha256:a484819eb60211f5299034ac80f6a681b06f89e65866ce91f356ed7c72af059c
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@rz-153-nw-test ~]#

查看镜像

docker images

示例如下

[root@rz-153-nw-test ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        latest    e784f4560448   12 days ago   188MB
[root@rz-153-nw-test ~]# 

启动容器

docker run -d nginx
[root@rz-153-nw-test ~]# docker run -d nginx
2d87ab1baa9aa75739cb234ad2908b4a4d51d743c2fadfa5193340a15e73c742
[root@rz-153-nw-test ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
2d87ab1baa9a   nginx     "/docker-entrypoint.…"   6 seconds ago   Up 6 seconds   80/tcp    musing_robinson
[root@rz-153-nw-test ~]# 

进入容器

docker exec -it 2d87ab1baa9a bash

示例如下:

[root@rz-153-nw-test ~]# docker exec -it 2d87ab1baa9a bash
root@2d87ab1baa9a:/# 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@2d87ab1baa9a:/# 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值