docker学习笔记

此博文为学习docker期间所做的笔记,供查阅复习使用。
学习docker是通过b站up主:遇见狂神说
学习链接:https://www.bilibili.com/video/BV1og4y1q7M4?p=11

docker常用命令

帮助命令

docker version  	# docker的版本信息
docker info			# docker系统信息,包括镜像和容器数量
docker 命令 --help   # 帮助命令

docker帮助地址:https://docs.docker.com/engine/reference/commandline/docker/

镜像命令

docker images 查看所有本地的主机上的镜像

[root@banmajio /]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    bf756fb1ae65   11 months ago   13.3kB
# 解释
REPOSITORY 	镜像的仓库源
TAG			镜像的标签
IMAGE ID	镜像的id
CREATED		镜像的创建时间
SIZE		镜像的大小

# 可选项
-a, --all          	# 列出所有镜像
-q, --quiet         # 只显示镜像的id 

docker search 搜索镜像

[root@banmajio ~]# docker search mysql
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   10312     [OK]       
mariadb                           MariaDB is a community-developed fork of MyS…   3822      [OK]       
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   754                  [OK]

# 可选项 通过收藏来过滤
-- filter==STARS=3000 # 搜索出来的镜像就是STARS大于3000的

docker pull 下载镜像

# 下载镜像 docker pull 镜像名[:tag]
[root@banmajio ~]# docker pull mysql
Using default tag: latest  # 如果不屑tag, 默认就是latest
 latest: Pulling from library/mysql
6ec7b7d162b2: Pull complete # 分层下载,docker image的核心 联合文件系统
fedd960d3481: Pull complete 
7ab947313861: Pull complete 
64f92f19e638: Pull complete 
3e80b17bff96: Pull complete 
014e976799f9: Pull complete 
59ae84fee1b3: Pull complete 
ffe10de703ea: Pull complete 
657af6d90c83: Pull complete 
98bfb480322c: Pull complete 
6aa3859c4789: Pull complete 
1ed875d851ef: Pull complete 
Digest: sha256:78800e6d3f1b230e35275145e657b82c3fb02a27b2d8e76aac2f5e90c1c30873 # 签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest  # 真实地址

# 等价命令
docker pull mysql
docker pull docker.io/library/mysql:latest

# 下载指定版本
[root@banmajio ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
6ec7b7d162b2: Already exists 
fedd960d3481: Already exists 
7ab947313861: Already exists 
64f92f19e638: Already exists 
3e80b17bff96: Already exists 
014e976799f9: Already exists 
59ae84fee1b3: Already exists 
7d1da2a18e2e: Pull complete 
301a28b700b9: Pull complete 
529dc8dbeaf3: Pull complete 
bc9d021dc13f: Pull complete 
Digest: sha256:c3a567d3e3ad8b05dfce401ed08f0f6bf3f3b64cc17694979d5f2e5d78e10173
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

在这里插入图片描述

docker rmi 删除镜像

[root@banmajio ~]# docker rmi -f 容器id 				  # 删除指定的容器
[root@banmajio ~]# docker rmi -f 容器id 容器id 			 # 删除指定的多个 容器
[root@banmajio ~]# docker rmi -f $(docker images -aq)  # 删除全部的容器

容器命令

说明: 有了镜像才可以创建容器,下载一个centos镜像来测试学习

docker pull centos 

新建容器并启动

docker run [可选参数] image
# 参数说明
--name="NAME" 	容器名字 用来区分容器
-d            	后台方式运行
-it 		  	使用交互方式运行,进入容器查看内容
-p				指定容器的端口 -p 8080:8080
	-p ip:主机端口:容器端口  (主机端口映射容器端口)
	-p 主机端口:容器端口 (常用)
	-p 容器端口
-P				指定随机端口

# 启动并进入容器  (/bin/bash 控制台)
[root@banmajio ~]# docker run -it centos /bin/bash 
[root@480014f42cc4 /]# ls	#  查看容器内decentos 基础版本,很多命令是不完善的!
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

# 从容器退回到主机
[root@480014f42cc4 /]# exit
exit

列出所有的运行的容器

# docker ps 命令
		# 列出当前正在运行的容器
-a		# 列出当前正在运行的容器+带出历史运行过的容器
-n=?    # 显示最近创建的容器
-q		# 只显示容器的编号
[root@banmajio /]# docker ps		# 列出正在运行的容器
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@banmajio /]# docker ps -a		# 列出
CONTAINER ID   IMAGE          COMMAND       CREATED         STATUS                     PORTS     NAMES
480014f42cc4   centos         "/bin/bash"   9 minutes ago   Exited (0) 7 minutes ago             youthful_lamport
6264cc560613   bf756fb1ae65   "/hello"      2 days ago      Exited (0) 2 days ago                relaxed_wilbur
9313993ed93e   bf756fb1ae65   "/hello"      2 days ago      Exited (0) 2 days ago                adoring_joliot

退出容器

exit	# 直接停止容器并退出
Ctrl + P + Q #容器不停止退出
docker attach 527ed60e6909  # 进入运行的容器中(生产环境不适用)

删除容器

docker rm 容器id 					# 删除指定容器 不能删除正在运行的容器,如果摇强制删除rm -f
docker rm -f $(docker ps -aq)	 # 删除所有容器
docker ps -a -q|xargs docker rm  # 删除所有容器

启动和停止容器的操作

docker start 容器id		# 启动容器
docker restart 容器id		# 重启容器
docker stop 容器id		# 停止当前正在运行的容器
docker kill 容器			# 强制停止当前容器

常用其他命令

后台启动容器

# 命令 docker run -d 镜像名
[root@banmajio ~]# docker run -d centos

# 问题docker ps 发现centos停止了

# 常见的坑: docker 容器使用后台运行,就必须要有一个前台进程,docker发现没有应用就会停止

查看日志

docker logs -f -t --tail 条数 容器id 

# 自己编写一段shell脚本
[root@banmajio ~]# docker run -d centos /bin/sh -c "while true;do echo banmajio;sleep 1;done"

[root@banmajio ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
4a66fd07ac08   centos    "/bin/sh -c 'while t…"   3 seconds ago   Up 2 seconds             blissful_taussig

# 显示日志
-tf  # 显示日志 t是时间戳 f是format
--tail number # 要显示日志的条数
[root@banmajio ~]# docker logs -f -t --tail 10 4a66fd07ac08

查看容器中的进程信息

# 命令 docker top 容器id
[root@banmajio ~]# docker top ce4fb8e672d9
UID                 PID                 PPID                C                   STIME               TTY                 TIME     
root                2195                2176                0                   21:42               ?                   00:00:00

查看镜像源数据

# 命令 
docker inspect 容器id
# 测试
[root@banmajio ~]# docker inspect 4a66fd07ac08
[
    {
        "Id": "4a66fd07ac08afcf204e11a3b33538918f11d20b5276caca907d4e9db53c1bd5",
        "Created": "2020-12-27T13:30:43.876900992Z",
        "Path": "/bin/sh",
        "Args": [
            "-c",
            "while true;do echo banmajio;sleep 1;done"
        ],
        "State": {
            "Status": "exited",
            "Running": false,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 0,
            "ExitCode": 137,
            "Error": "",
            "StartedAt": "2020-12-27T13:30:44.218766184Z",
            "FinishedAt": "2020-12-27T13:35:49.01556222Z"
        },
        "Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
        "ResolvConfPath": "/var/lib/docker/containers/4a66fd07ac08afcf204e11a3b33538918f11d20b5276caca907d4e9db53c1bd5/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/4a66fd07ac08afcf204e11a3b33538918f11d20b5276caca907d4e9db53c1bd5/hostname",
        "HostsPath": "/var/lib/docker/containers/4a66fd07ac08afcf204e11a3b33538918f11d20b5276caca907d4e9db53c1bd5/hosts",
        "LogPath": "/var/lib/docker/containers/4a66fd07ac08afcf204e11a3b33538918f11d20b5276caca907d4e9db53c1bd5/4a66fd07ac08afcf204e11a3b33538918f11d20b5276caca907d4e9db53c1bd5-json.log",
        "Name": "/blissful_taussig",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "CgroupnsMode": "host",
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "private",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": [],
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DeviceRequests": null,
            "KernelMemory": 0,
            "KernelMemoryTCP": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": null,
            "OomKillDisable": false,
            "PidsLimit": null,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0,
            "MaskedPaths": [
                "/proc/asound",
                "/proc/acpi",
                "/proc/kcore",
                "/proc/keys",
                "/proc/latency_stats",
                "/proc/timer_list",
                "/proc/timer_stats",
                "/proc/sched_debug",
                "/proc/scsi",
                "/sys/firmware"
            ],
            "ReadonlyPaths": [
                "/proc/bus",
                "/proc/fs",
                "/proc/irq",
                "/proc/sys",
                "/proc/sysrq-trigger"
            ]
        },
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/64a528b5f8d8ffdde26b897e2e173275a1bc926d158d8f913d93afcdec386ef1-init/diff:/var/lib/docker/overlay2/4255cf21711b71698d5b8761a4f393c271e3c3d63d5a9a2dcabf55b21da3a871/diff",
                "MergedDir": "/var/lib/docker/overlay2/64a528b5f8d8ffdde26b897e2e173275a1bc926d158d8f913d93afcdec386ef1/merged",
                "UpperDir": "/var/lib/docker/overlay2/64a528b5f8d8ffdde26b897e2e173275a1bc926d158d8f913d93afcdec386ef1/diff",
                "WorkDir": "/var/lib/docker/overlay2/64a528b5f8d8ffdde26b897e2e173275a1bc926d158d8f913d93afcdec386ef1/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "4a66fd07ac08",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "while true;do echo banmajio;sleep 1;done"
            ],
            "Image": "centos",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20201204",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS"
            }
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "af3352599d5f4a22baac15d88d2f297f8a2d68468f5d7e6606aabc39d7936f56",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/af3352599d5f",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "21e5615b9ffc90f762ac5fede60f08c589f385ae895d89ffa5ebcc598ca2e61a",
                    "EndpointID": "",
                    "Gateway": "",
                    "IPAddress": "",
                    "IPPrefixLen": 0,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "",
                    "DriverOpts": null
                }
            }
        }
    }
]

进入当前正在运行的容器

# 我们通常容器都是使用后台方式运行的,如果需要进入容器修改一些配置

# 命令
docker exec -it 容器id bashShell
 
[root@banmajio ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED        STATUS              PORTS     NAMES
4a66fd07ac08   centos    "/bin/sh -c 'while t…"   24 hours ago   Up About a minute             blissful_taussig
[root@banmajio ~]# docker exec -it 4a66fd07ac08 /bin/bash
[root@4a66fd07ac08 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

# 方式二
docker attach 容器id  

# docker exec    # 进入容器后开启一个新的终端,可以在里面操作(常用)
# docker attach  # 进入容器正在执行的终端,不会启动新的进程!

从容器内拷贝文件到主机上

# 命令  docker cp 容器id:容器内的路径 目的的主机路径

[root@banmajio home]# docker cp 3f56100a88c3:/home/banmajio.java /home
[root@banmajio home]# ls
banmajio.java  git  minio  test  www

# 拷贝是一个手动过程,未来我们使用 -V 卷的技术,可以实现自动同步。

小结

在这里插入图片描述

docker 命令是十分多的,上面学习的那些哦都是最常用的容器和镜像的命令。

作业练习

作业一:Docker 安装Nginx

# 搜索镜像 建议dockerhub搜索
[root@banmajio home]# docker search nginx

# 安装镜像
[root@banmajio home]# docker pull nginx

# 启动镜像
[root@banmajio home]# docker run -d --name nginx01 -p 3344:80 nginx  
# 以后台启动一个nginx镜像(-d:后台启动),设置别名为“nginx01” 指定主机端口3344映射到nginx镜像的80端口

# 运行测试
[root@banmajio home]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED              STATUS              PORTS                  NAMES
ea2aefddbcd2   nginx     "/docker-entrypoint.…"   About a minute ago   Up About a minute   0.0.0.0:3344->80/tcp   nginx01
[root@banmajio home]# curl localhost:3344

# 进入容器
[root@banmajio home]# docker exec -it nginx01 /bin/bash
root@ea2aefddbcd2:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@ea2aefddbcd2:/# cd /etc/nginx 
root@ea2aefddbcd2:/etc/nginx# ls
conf.d	fastcgi_params	koi-utf  koi-win  mime.types  modules  nginx.conf  scgi_params	uwsgi_params  win-utf


端口暴露的概念:
在这里插入图片描述

作业二:使用docker 来装一个tomcat

# 官方的使用
docker run -it --rm tomcat:9.0

# 我们之前的启动都是后台,停止了容器之后,容器还是可以查到。 docker run -it --rm一般是用来测试,用完就删除

# 启动镜像
[root@banmajio home]# docker run -d -p 3355:8080 --name tomcat01 tomcat

# 测试访问没有问题

# 进入容器
[root@banmajio home]# docker exec -it tomcat01 /bin/bash

# 发现问题:1、linux命令缺少。2、没有webapps。阿里云镜像的原因,默认是最小的镜像,把所有不必要的都剔除掉。
# 保证最小可运行环境

# 将webapps.dist内的内容拷贝到webapps目录下
root@98c5e1bc26e8:/usr/local/tomcat# cp webapps.dist/* webapps
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

banmajio

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值