docker基础命令

docker基础命令

安装及使用docker

docker安装

cd /etc/yum.repos.d/
curl -o docker-ce.repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
sed -i 's@https://download.docker.com@https://mirrors.tuna.tsinghua.edu.cn/docker-ce@g' docker-ce.repo

yum -y install docker-ce

docker加速

docker-ce的配置文件是/etc/docker/daemon.json,此文件默认不存在,需要我们手动创建并进行配置,而docker的加速就是通过配置此文件来实现的。

docker的加速有多种方式:

  • docker cn
  • 中国科技大学加速器
  • 阿里云加速器(需要通过阿里云开发者平台注册帐号,免费使用个人私有的加速器)

阿里云平台域名:www.aliyun.com

登录平台后进入控制台

在这里插入图片描述

点击这三条杠

在这里插入图片描述

搜索容器镜像服务

在这里插入图片描述

在这里插入图片描述

配置加速器

sudo mkdir -p /etc/docker    #docker目录启动docker服务后自动存在
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://lgc3bg0m.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

查看加速器

[root@localhost ~]# docker info  #使用这个命令
.......
 Registry Mirrors:
  https://lgc3bg0m.mirror.aliyuncs.com/    #能看到这个就是加速器配置成功
 Live Restore Enabled: false

[root@localhost ~]# docker info

查看drocker的版本号

[root@localhost ~]# docker version
Client: Docker Engine - Community
 Version:           24.0.7
 API version:       1.43
 Go version:        go1.20.10
 Git commit:        afdd53b
 Built:             Thu Oct 26 09:09:18 2023
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          24.0.7
  API version:      1.43 (minimum version 1.12)
  Go version:       go1.20.10
  Git commit:       311b9ff
  Built:            Thu Oct 26 09:08:20 2023
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.24
  GitCommit:        61f9fd88f79f081d64d6fa3bb1a0dc71ec870523
 runc:
  Version:          1.1.9
  GitCommit:        v1.1.9-0-gccaecfc
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
[root@localhost ~]# 

docker常用操作

命令功能
docker search去官方仓库搜索镜像
docker pull下载镜像至本地
docker images查看下载的镜像
docker create创建容器,这种创建的容器不能在外面访问
docker start启动容器
docker run创建一个新的容器并运行
docker attachAttach to a runninng container
docker ps查看启动的容器 加-a查看所有容器
docker logs查看容器日志
docker restart重启容器
docker stop停止容器
docker kill杀死进程
docker rm删除容器 rmi删除镜像
docker exec进入容器
docker info查看容器信息
docker inspect查看容器和镜像的详细信息
docker tag标记本地镜像,将其归入某一仓库。

查看当前仓库的镜像

[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
[root@localhost ~]# 

拉取镜像

[root@localhost ~]# docker pull httpd    #拉取httpd镜像
Using default tag: latest
latest: Pulling from library/httpd
a2abf6c4d29d: Pull complete 
dcc4698797c8: Pull complete 
41c22baa66ec: Pull complete 
67283bbdd4a0: Pull complete 
d982c879c57e: Pull complete 
Digest: sha256:0954cc1af252d824860b2c5dc0a10720af2b7a3d3435581ca788dff8480c7b32
Status: Downloaded newer image for httpd:latest
docker.io/library/httpd:latest
[root@localhost ~]#  
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
httpd        latest    dabbfbe0c57b   22 months ago   144MB
[root@localhost ~]# 

创建容器

[root@localhost ~]# docker create --name httpd dabbfbe0c57b  #create创建容器名字是httpd可以使用ID也可以用名字
8542a6a3ec326ac0b24458633bcefd33837ee0c64bef8c2912dc91426bcc4811
[root@localhost ~]# 
[root@localhost ~]# docker ps    #此时容器还没有启动
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost ~]# 

启动容器

[root@localhost ~]# docker start 8542a6a3ec32   #启动容器,使用容器ID启动
8542a6a3ec32
[root@localhost ~]# docker ps    #查看已经启动的容器
CONTAINER ID   IMAGE          COMMAND              CREATED         STATUS          PORTS     NAMES
8542a6a3ec32   dabbfbe0c57b   "httpd-foreground"   2 minutes ago   Up 10 seconds   80/tcp    httpd
[root@localhost ~]#                                                          正在运行在容器的80端口号上

查看容器是否使用成功

[root@localhost ~]# ip a
......
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:88:d2:c5:7d brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0   #网桥的ip地址是172.17.0.1
       valid_lft forever preferred_lft forever
    inet6 fe80::42:88ff:fed2:c57d/64 scope link 
       valid_lft forever preferred_lft forever
......
[root@localhost ~]# curl 172.17.0.2   #所以第一个启动的容器ip是172.17.0.2
<html><body><h1>It works!</h1></body></html>        #访问成功
[root@localhost ~]# 

查看容器信息

[root@localhost ~]# docker inspect httpd
......
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",         #ip
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
......

重启容器

[root@localhost ~]# docker ps    #这个容器是12分钟前启动的
CONTAINER ID   IMAGE          COMMAND              CREATED          STATUS          PORTS     NAMES
8542a6a3ec32   dabbfbe0c57b   "httpd-foreground"   14 minutes ago   Up 12 minutes   80/tcp    httpd
[root@localhost ~]# docker restart httpd   #重启容器
httpd
[root@localhost ~]# docker ps     #启动时间变成4秒前
CONTAINER ID   IMAGE          COMMAND              CREATED          STATUS         PORTS     NAMES
8542a6a3ec32   dabbfbe0c57b   "httpd-foreground"   15 minutes ago   Up 4 seconds   80/tcp    httpd
[root@localhost ~]# 

停止容器

[root@localhost ~]# docker stop httpd
httpd
[root@localhost ~]# docker ps   #发现没有容器正在运行
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND              CREATED          STATUS                     PORTS     NAMES
8542a6a3ec32   dabbfbe0c57b   "httpd-foreground"   18 minutes ago   Exited (0) 5 seconds ago             httpd
[root@localhost ~]# 

杀死进程

[root@localhost ~]# docker kill httpd
httpd
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND              CREATED          STATUS                      PORTS     NAMES
8542a6a3ec32   dabbfbe0c57b   "httpd-foreground"   20 minutes ago   Exited (137) 1 second ago             httpd                                                               #状态码137表示不正常退出,90表示正常退出
[root@localhost ~]# 

创建一个可以在外面访问的容器

[root@localhost ~]# docker run --name web -p 80:80 httpd    
         #创建一个新容器名为web -p 将容器80端口映射到主机80端口,使用httpd镜像
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[Fri Nov 03 03:36:23.587791 2023] [mpm_event:notice] [pid 1:tid 139709650111808] AH00489: Apache/2.4.52 (Unix) configured -- resuming normal operations
[Fri Nov 03 03:36:23.587924 2023] [core:notice] [pid 1:tid 139709650111808] AH00094: Command line: 'httpd -D FOREGROUND'
192.168.10.10 - - [03/Nov/2023:03:36:43 +0000] "GET / HTTP/1.1" 200 45
192.168.10.10 - - [03/Nov/2023:03:36:43 +0000] "GET /favicon.ico HTTP/1.1" 404 196
192.168.10.10 - - [03/Nov/2023:03:37:35 +0000] "-" 408 -

#这种方式运行的容器不能关闭必须保存在前台工作,只能另开终端

[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND              CREATED          STATUS                       PORTS                               NAMES
a7bdfe241b16   httpd          "httpd-foreground"   2 minutes ago    Up 2 minutes                 0.0.0.0:80->80/tcp, :::80->80/tcp   web
8542a6a3ec32   dabbfbe0c57b   "httpd-foreground"   28 minutes ago   Exited (137) 7 minutes ago                                       httpd
[root@localhost ~]# 

访问主机ip 80端口

在这里插入图片描述

创建一个在后台跑的容器

[root@localhost ~]# docker run -d --name web1 -p 8080:80 httpd      #-d: 后台运行容器,并返回容器ID
b3133d7bfcee4ba2c73b87e098bd70a63b87a5d2ccfbdcd0a4a1adf866071aa7
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS          PORTS                                   NAMES
b3133d7bfcee   httpd     "httpd-foreground"   12 seconds ago   Up 11 seconds   0.0.0.0:8080->80/tcp, :::8080->80/tcp   web1
[root@localhost ~]# 
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS          PORTS                                   NAMES
b3133d7bfcee   httpd     "httpd-foreground"   12 seconds ago   Up 11 seconds   0.0.0.0:8080->80/tcp, :::8080->80/tcp   web1
[root@localhost ~]# 

在这里插入图片描述

查看日志

[root@localhost ~]# docker logs web1
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[Fri Nov 03 06:59:04.489863 2023] [mpm_event:notice] [pid 1:tid 140559679032640] AH00489: Apache/2.4.52 (Unix) configured -- resuming normal operations
[Fri Nov 03 06:59:04.490013 2023] [core:notice] [pid 1:tid 140559679032640] AH00094: Command line: 'httpd -D FOREGROUND'
192.168.10.10 - - [03/Nov/2023:07:06:50 +0000] "GET / HTTP/1.1" 200 45
192.168.10.10 - - [03/Nov/2023:07:06:50 +0000] "GET /favicon.ico HTTP/1.1" 404 196
192.168.10.10 - - [03/Nov/2023:07:07:42 +0000] "-" 408 -
[root@localhost ~]# 

删除容器和镜像

删除容器

[root@localhost ~]# docker ps   查看正在运行的容器
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS          PORTS                                   NAMES
b3133d7bfcee   httpd     "httpd-foreground"   11 minutes ago   Up 11 minutes   0.0.0.0:8080->80/tcp, :::8080->80/tcp   web1
[root@localhost ~]# docker stop b3133d7bfcee   #先停止容器
b3133d7bfcee
[root@localhost ~]# docker ps   #再次查看,容器没有运行
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost ~]# 
[root@localhost ~]# docker rm b3133d7bfcee   #删除容器
b3133d7bfcee
[root@localhost ~]# 

删除镜像

[root@localhost ~]# docker images    #查看镜像
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
httpd        latest    dabbfbe0c57b   22 months ago   144MB
[root@localhost ~]# docker rmi dabbfbe0c57b     #rmi删除镜像
Untagged: httpd:latest
Untagged: httpd@sha256:0954cc1af252d824860b2c5dc0a10720af2b7a3d3435581ca788dff8480c7b32
Deleted: sha256:dabbfbe0c57b6e5cd4bc089818d3f664acfad496dc741c9a501e72d15e803b34
Deleted: sha256:0e16a5a61bcb4e6b2bb2d746c2d6789d6c0b66198208b831f74b52198d744189
Deleted: sha256:f79670638074ff7fd293e753c11ea2ca0a2d92ab516d2f6b0bac3f4c6fed5d86
Deleted: sha256:189d55cdd18e4501032bb700a511c2d69c82fd75f1b619b5218ea6870e71e4aa
Deleted: sha256:cb038ed3e490a8c0f195cf135ac0d27dd8d3872598b1cb858c2666f2dae95a61
Deleted: sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
[root@localhost ~]# 

进入容器

使用docker attach命令进入容器不能使用终端

[root@localhost ~]# docker attach web
使用docker exec命令进入容器

-d :分离模式: 在后台运行

-i :即使没有附加也保持STDIN 打开

-t :分配一个伪终端

[root@localhost ~]# docker run -d --name web httpd     #启动容器
53aca96b9e169e3c0f5db3447b4c633eebb6dff2a54a98fd83eff443861b177d
[root@localhost ~]# docker exec -it web /bin/bash    #进入容器
root@53aca96b9e16:/usr/local/apache2# 

在另一个终端访问

[root@localhost ~]# curl 172.17.0.2
<html><body><h1>It works!</h1></body></html>
[root@localhost ~]# 

创建新的镜像

docker tag : 标记本地镜像,将其归入某一仓库。

语法

docker tag [OPTIONS] IMAGE[:TAG] [REGISTRYHOST/][USERNAME/]NAME[:TAG]
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
httpd        latest    dabbfbe0c57b   22 months ago   144MB
[root@localhost ~]# docker tag httpd:latest hte666/httpd:v1.0
[root@localhost ~]# docker images
REPOSITORY     TAG       IMAGE ID       CREATED         SIZE
hte666/httpd   v1.0      dabbfbe0c57b   22 months ago   144MB
httpd          latest    dabbfbe0c57b   22 months ago   144MB
[root@localhost ~]# 
"Cmd": [
                "/opt/apache/bin/httpd",
                "-X",
                "-D",
                "FOREGROUND"
            ],
            "Image": "dd2f9d68fb98",

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值