Docker基础指令
Docker概述Docker为什么出现解决问题:1.在我的电脑上可以运行,但是到别人的电脑上就不能运行了2.出现版本更新,导致服务不能用3.环境配置麻烦,不是有的项目可能不支持跨平台传统: 开发打jar包,运维来部署现在:开发打包部署上线,一套流程做完拿app应用商店距离java -apk发布 ——用户使用apk ——安装即可以使用java -jar(环境)—— docker:应用商店 ——直接运
Docker概述
Docker为什么出现
解决问题:
1.在我的电脑上可以运行,但是到别人的电脑上就不能运行了
2.出现版本更新,导致服务不能用
3.环境配置麻烦,不是有的项目可能不支持跨平台
传统: 开发打jar包,运维来部署
现在:开发打包部署上线,一套流程做完
拿app应用商店距离
java -apk发布 ——用户使用apk ——安装即可以使用
java -jar(环境)—— docker:应用商店 ——直接运行即可用
docker思想:
dockere的思想来源于集装箱, 采用隔离思想,集装箱内物品有一个损坏,并不影响另外一个集装箱的物品
Docker的历史
2010年docker的前身dotCloud
在美国成立了
dotCloud主要是基于PaaS(Platform as a Serivce),在PaaS平台,所有服务都应经配置好了,开发者只需要选择服务类型,上传代码,就可以对外服务,但是因为PaaS市场还在培育阶段,dotCloud
表现的不温不火
Docker开源
:
Docker为什么火,因为Docker比较小巧
虚拟机: 在windows装一个VMware,通过这个软件我们可以虚拟一台或者多台电脑,比较笨重
docker:使用隔离技术,采用镜像(最核心环境) 小巧
Docker开发
Docker使用go语言开发的
官网地址:https://www.docker.com/
文档地址:https://docs.docker.com/
容器化技术
Docker和传统虚拟机的区别
- 传统虚拟机: 虚拟一条硬件,运行完整的操作系统,然后在这个系统安装和运行软件
- 容器直接运行宿主机的内容,容器没有内核,也没有我们虚拟的硬件,十分小巧
- 容器之间相互隔离,容器都有一个属于自己的文件系统,互不影响
DevOps (开发 运维)
1.应用可以迅速交付和部署
传统:需要有很多帮助文档
Docker:直接打包镜像发布测试
2.更便捷的升级和扩容缩容
项目打包为一个镜像,直接部署到另外一个服务器
3.更简单的系统运维
4.更高效的资源利用
Docker的安装
Docker的基本组成
这里的镜像和容器的关系,就像Java里面的Class和对象的关系,我们根据镜像去生成容器
镜像(image):
docker镜像好比一个模板,我们通过这个模板进行容器创建,通过一个镜像可以创建多个容器
容器(container):
Docker通过容器技术,可以运行一个或一组应用
仓库(repository):
仓库:存放镜像的地方
仓库分为公有仓库和私有仓库‘’
安装Docker
环境准备
- Linux基础
- CentOS 7
- Xshell软件
- 购买一个服务器
环境查看
#系统版本需要是3.10以上
[root@iZ2ze9103odozq16xbkmr6Z /]# uname -r
3.10.0-1160.53.1.el7.x86_64
#系统的环境
[root@iZ2ze9103odozq16xbkmr6Z /]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"
安装Docker
看Docker的帮助文档
#1.删除旧版本的Docker
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
#2.添加需要安装包
yum install -y yum-utils
#3.设置镜像仓库
yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
#上面这个是国外的镜像,十分慢
yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo #推荐使用阿里云镜像
#在第4步之前需要更新yum 的索引
yum makecache fast
#4.安装docker相关的 docker-ce社区版 ee企业版
yum install docker-ce docker-ce-cli containerd.io
#5.启动docker
systemctl start docker
#6. 使用docker version查看是否安装成功下面是启动成功的标识
[root@iZ2ze9103odozq16xbkmr6Z /]# docker version
Client: Docker Engine - Community
Version: 20.10.14
API version: 1.41
Go version: go1.16.15
Git commit: a224086
Built: Thu Mar 24 01:49:57 2022
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.14
API version: 1.41 (minimum version 1.12)
Go version: go1.16.15
Git commit: 87a90dc
Built: Thu Mar 24 01:48:24 2022
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.5.11
GitCommit: 3df54a852345ae127d1fa3092b95168e4a88e2f8
runc:
Version: 1.0.3
GitCommit: v1.0.3-0-gf46b6ba
docker-init:
Version: 0.19.0
GitCommit: de40ad0
#7.docker run
docker run hello-world
[root@iZ2ze9103odozq16xbkmr6Z /]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:10d7d58d5ebd2a652f4d93fdd86da8f265f5318c6a73cc5b6a9798ff6d2b2e67
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
#8.查看下载的hello-world镜像
docker images
[root@iZ2ze9103odozq16xbkmr6Z /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 7 months ago 13.3kB
卸载Docker
#1.卸载运行依赖
yum remove docker-ce docker-ce-cli contained.io
#2.删除Docker
rm -rf /var/lib.docker
# /var/lib/docker Docker的默认工作路径
阿里云镜像加速
1.找到阿里云镜像
2.找到镜像加速地址
3.配置使用
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://6v8m03rx.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
底层原理
Docker如何工作
Docker是一个Server-Client结果系统,Docker的守护进程运行在主机上,通过Socket从客户端进行访问
Docker Server收到Docker Client指令就会执行命令
Docker为什么比VM快
1.Docker比虚拟机有更少的抽象层
2.Docker用的是宿主机的内核,vm需要Guest OS
Docker常用命令
帮助命令
docker version #查看Docker的版本信息
docker info #查看Docker的系统信息,包括镜像和容器的数量
docker 命令 --help #Docker的帮助命令
帮助文档的地址: https://docs.docker.com/reference/
镜像命令
不知道命令,进行找命令的两种方法
1.去官方文档 找Reference 找到 Command-line Reference
2.直接docker+这个命令+ --help
docker images
查看所有主机本地的镜像
[root@iZ2ze9103odozq16xbkmr6Z /]# docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 7 months ago 13.3kB
#解释
Repository 镜像的仓库源
TAG 镜像的标签
IMAGEID 镜像的id
CREATED 镜像的创建时间
SIZE 镜像的大小
#可选项
-a, --all #列出所有镜像
-q, --quiet #只显示镜像的ID
docker search 搜索镜像
[root@iZ2ze9103odozq16xbkmr6Z /]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 12465 [OK]
mariadb MariaDB Server is a high performing open sou… 4801 [OK]
#可选项 通过搜索来过滤
--filter #进行过滤的操作
#例如 --filter=STARS=3000会找所有STARS大于3000的操作
[root@iZ2ze9103odozq16xbkmr6Z /]# docker search mysql --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 12465 [OK]
mariadb MariaDB Server is a high performing open sou… 4801 [OK]
docker pull 下载镜像
#下载镜像 docker pull mysql 镜像名[:tag]
[root@iZ2ze9103odozq16xbkmr6Z /]# docker pull mysql
Using default tag: latest #如果不写tag就是最新版
latest: Pulling from library/mysql
72a69066d2fe: Pull complete #分层下载,docker image的核心
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
688ba7d5c01a: Pull complete
00e060b6d11d: Pull complete
1c04857f594f: Pull complete
4d7cfa90e6ea: Pull complete
e0431212d27d: Pull complete
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709 #签名,防伪标志
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest #真实地址
docker rmi 删除镜像
docker rmi -f #删除所有镜像
docker rmi -f + id #删除id为这个的镜像
docker rmi -f $(docker images -aq) #首先找出所有的镜像,然后一点一点进行递归删除
docker rm #这个是删除容器的命令
容器命令
有了镜像才可以创建容器,镜像可以看做模板,容器可以看做根据模板创建的实体
docker pull centos
新建容器并且启动
docker run [可选参数] image
#参数说明
-
--name="Name" #容器名字 tomcat01 tomcat02 用来区分容器
-d #后台方式运行
-it #交互方式运行,进入容器查看内容
-p #指定容器的端口运行 8080:8080
-p ip:主机端口:容器端口
-p 主机端口:容器端口(常用)
-p 容器端口
容器端口
-P #随机指定端口运行
#测试 启动并且进入容器
[root@iZ2ze9103odozq16xbkmr6Z /]# docker run -it centos /bin/bash
查看所有运行的容器
#docker ps 命令
-a #查看历史运行过的容器
-n=?#显示最近n个创建的容器
-q #显示当前容器的编号
#docker ps 查看正在运行的容器
[root@iZ2ze9103odozq16xbkmr6Z /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
#docker ps -a 查看已经运行过的容器
[root@iZ2ze9103odozq16xbkmr6Z /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
56adb477c1f6 centos "/bin/bash" 3 minutes ago Exited (130) 2 minutes ago nice_dijkstra
76d663de83c0 hello-world "/hello" 18 hours ago Exited (0) 18 hours ago awesome_swanson
2ac516be86b1 hello-world "/hello" 39 hours ago Exited (0) 39 hours ago optimistic_shannon
退出容器
exit #直接容器停止并且退出
Ctrl + P + Q #退出不关闭容器
[root@iZ2ze9103odozq16xbkmr6Z /]# docker run -it centos
[root@9318f4c7d61b /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@9318f4c7d61b /]
# [root@iZ2ze9103odozq16xbkmr6Z /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9318f4c7d61b centos "/bin/bash" 12 seconds ago Up 11 seconds focused_sammet
删除容器
docker rm 容器id #直接删除容器id,不能删除正在运行的容器
docker rm -f $(docker ps -aq) #删除所有的容器
docker ps -aq|xargs docker rm #删除所有的容器
启动和停止容器的操作
docker start 容器id #启动容器
docker restart 容器id #重启容器
docker stop 容器id #停止正在运行的容器
docker kill 容器id #kill掉正在运行的容器
#test
[root@iZ2ze9103odozq16xbkmr6Z /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@iZ2ze9103odozq16xbkmr6Z /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ae20d59eb6b2 centos "/bin/bash" 38 seconds ago Exited (0) 36 seconds ago naughty_golick
[root@iZ2ze9103odozq16xbkmr6Z /]# docker start ae20d59eb6b2
ae20d59eb6b2
[root@iZ2ze9103odozq16xbkmr6Z /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ae20d59eb6b2 centos "/bin/bash" 55 seconds ago Up 3 seconds naughty_golick
[root@iZ2ze9103odozq16xbkmr6Z /]# docker stop ae20d59eb6b2
ae20d59eb6b2
[root@iZ2ze9103odozq16xbkmr6Z /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
常用其他命令
后台启动容器
docker run -d centos
# docker ps 发现容器已经停止
# 常见的坑,docker 容器后台进行必须有一个前台进程
查看日志
docker logs -f -t --tail #容器,没有日志
#自己编写一个shell脚本
"while true; do echo nydj; sleep 1; done"
[root@iZ2ze9103odozq16xbkmr6Z /]# docker run -d centos /bin/sh -c "while true;do echo nydj; sleep 1; done"
4c8ec7c7ed776b4112b33a67c1533d5a6c38660aeaaa549ab478cc9c937837b0
#docker ps
[root@iZ2ze9103odozq16xbkmr6Z /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4c8ec7c7ed77 centos "/bin/sh -c 'while t…" 6 seconds ago Up 5 seconds inspiring_robinson
#显示日志
-tf #显示日志
--tail number 显示的条数
docker logs -f -t --tail
[root@iZ2ze9103odozq16xbkmr6Z /]# docker logs -tf --tail 10 4c8ec7c7ed77
2022-04-26T01:46:01.423640053Z nydj
2022-04-26T01:46:02.425890496Z nydj
2022-04-26T01:46:03.428120406Z nydj
2022-04-26T01:46:04.430293653Z nydj
2022-04-26T01:46:05.432438622Z nydj
2022-04-26T01:46:06.434612956Z nydj
2022-04-26T01:46:07.436766806Z nydj
2022-04-26T01:46:08.438845567Z nydj
查看容器中的进程信息
#命令 docker top 容器id
[root@iZ2ze9103odozq16xbkmr6Z /]# docker top aa88ca71214e
UID PID PPID C STIME TTY TIME CMD
root 5384 5364 0 09:51 pts/0 00:00:00 /bin/bash
查看镜像的元数据
[root@iZ2ze9103odozq16xbkmr6Z /]# docker inspect aa88ca71214e #查看元数据
[
{
"Id": "aa88ca71214e1c00e965e3ba9cd89054e211230b61db987067b14635e7043f22",
"Created": "2022-04-26T01:51:33.302882561Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 5384,
"ExitCode": 0,
"Error": "",
"StartedAt": "2022-04-26T01:51:33.591410753Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/aa88ca71214e1c00e965e3ba9cd89054e211230b61db987067b14635e7043f22/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/aa88ca71214e1c00e965e3ba9cd89054e211230b61db987067b14635e7043f22/hostname",
"HostsPath": "/var/lib/docker/containers/aa88ca71214e1c00e965e3ba9cd89054e211230b61db987067b14635e7043f22/hosts",
"LogPath": "/var/lib/docker/containers/aa88ca71214e1c00e965e3ba9cd89054e211230b61db987067b14635e7043f22/aa88ca71214e1c00e965e3ba9cd89054e211230b61db987067b14635e7043f22-json.log",
"Name": "/trusting_elgamal",
"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/36d5c7c81555220dfefd6ea4f012c7d69160ba6f17f3e40bf578ac2a3c24fdd8-init/diff:/var/lib/docker/overlay2/4e237525b4ff033dd6a363c221e5d9532e9ecb226db8d1ca9b7e3f395a90fdbc/diff",
"MergedDir": "/var/lib/docker/overlay2/36d5c7c81555220dfefd6ea4f012c7d69160ba6f17f3e40bf578ac2a3c24fdd8/merged",
"UpperDir": "/var/lib/docker/overlay2/36d5c7c81555220dfefd6ea4f012c7d69160ba6f17f3e40bf578ac2a3c24fdd8/diff",
"WorkDir": "/var/lib/docker/overlay2/36d5c7c81555220dfefd6ea4f012c7d69160ba6f17f3e40bf578ac2a3c24fdd8/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "aa88ca71214e",
"Domainname": "",
"User": "",
"AttachStdin": true,
"AttachStdout": true,
"AttachStderr": true,
"Tty": true,
"OpenStdin": true,
"StdinOnce": true,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20210915",
"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": "fc73bbd114b9394ed4158fe6c8eeb0994ed668d76714896652ab9d059f6a8e30",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/fc73bbd114b9",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "57a9a393bcec6b1916597c410ab5d202d1d27120d88730b30de9047fa7c3149f",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "2d2b2a45eba7af6d3cb90470329a48b29bdf199bca88641f77ea7926180b2c88",
"EndpointID": "57a9a393bcec6b1916597c410ab5d202d1d27120d88730b30de9047fa7c3149f",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
}
}
]
进入当前正在运行的容器
#我们通常容器是使用后台方式运行的 需要进入容器 配置一些配置
#命令
docker exec -it 容器id bashShell
docker attach 容器id
#docker exec #进入容器后开启一个新的终端,可以在里面操作
#docker attach #进入容器正在执行的终端不会开启新的进程
从容器拷贝文件到主机上
注意:从容器中将文件拷贝到主机上
#docker cp 容器id:容器内的路径 目的的主机路径
#我下载个centos然后在里面进入 /home建立test.java文件,需要上传到docker容器外面的 /home文件夹
#1.查看当前主机目录
[root@iZ2ze9103odozq16xbkmr6Z home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8cd44bada347 centos "/bin/bash" About a minute ago Up About a minute nostalgic_williamson
#2.进入docker容器内部
[root@iZ2ze9103odozq16xbkmr6Z home]# docker attach 8cd44bada347
[root@8cd44bada347 /]# cd /home
[root@8cd44bada347 home]# ls
#3.在容器内部新建1一个文件
[root@8cd44bada347 home]# touch test.java
[root@8cd44bada347 home]# ls
test.java
[root@8cd44bada347 home]# exit
exit
[root@iZ2ze9103odozq16xbkmr6Z home]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8cd44bada347 centos "/bin/bash" 2 minutes ago Exited (0) 7 seconds ago nostalgic_williamson
732589cf7466 centos "-it /bin/bash" 2 minutes ago Created practical_cartwright
[root@iZ2ze9103odozq16xbkmr6Z home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
#4.将这个文件从容器内部拷贝到容器外部
[root@iZ2ze9103odozq16xbkmr6Z home]# docker cp 8cd44bada347:/home/test.java /home
[root@iZ2ze9103odozq16xbkmr6Z home]# ls
beifeng.java test.java
小结
port # 查看映射端口对应的容器内部源端口
pause # 暂停容器
ps # 猎户容器列表
pull # 从docker镜像源服务器拉取指定镜像或者库镜像
push # 推送指定镜像或者库镜像至docker源服务器
restart # 重启运行的容器
rm # 移除一个或多个容器
rmi # 移除一个或多个镜像 (无容器使用该镜像才可删除,否则需要删除相关容器才可继续或 -f 强制删除)
run # 创建一个新的容器并运行一个命令
save # 保存一个镜像为一个 tar 包【对应 load】
search # 在 docker hub 中搜索镜像
start # 启动容器
stop # 停止容器
tag # 给源中镜像打标签
top # 查看容器中运行的进程信息
unpause # 取消暂停容器
version # 查看 docker版本号
wait # 截取容器停止时的退出状态值
作业练习
Docker安装Nginx
# 1.搜索镜像
docker search nginx
# 2.下载镜像
docker pull nginx
#3.运行镜像
[root@iZ2ze9103odozq16xbkmr6Z home]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 3 months ago 141MB
mysql latest 3218b38490ce 4 months ago 516MB
hello-world latest feb5d9fea6a5 7 months ago 13.3kB
centos latest 5d0da3dc9764 7 months ago 231MB
[root@iZ2ze9103odozq16xbkmr6Z home]# docker run -d --name:nginx1 -p3344:80 nginx
unknown flag: --name:nginx1
See 'docker run --help'.
[root@iZ2ze9103odozq16xbkmr6Z home]# docker run -d --name nginx1 -p3344:80 nginx
7781e736b2cf62288e25709c81e5d935e0a7c23dffcc9ac1894abe4262729e45
[root@iZ2ze9103odozq16xbkmr6Z home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7781e736b2cf nginx "/docker-entrypoint.…" 4 seconds ago Up 3 seconds 0.0.0.0:3344->80/tcp, :::3344->80/tcp nginx1
# 4.测试
[root@iZ2ze9103odozq16xbkmr6Z home]# curl localhost:3344
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
#进入容器
[root@iZ2ze9103odozq16xbkmr6Z home]# docker exec -it nginx1 /bin/bash
root@7781e736b2cf:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@7781e736b2cf:/# cd /etc/nginx
root@7781e736b2cf:/etc/nginx# ls
conf.d fastcgi_params mime.types modules nginx.conf scgi_params uwsgi_params
root@7781e736b2cf:/etc/nginx#
Docker装Tomcat
# 官方使用 用完即删除
docker run -it --rm tomcat:9.0 #该指令一般用作测试,用完之后就会删除
#下载然后启动
docker pull tomcat:9.0
#启动运行
docker run -d -p 3355:8080 -name tomcat1 tomcat
# -d 后台运行
# -p 3355:8080 外部访问的3355端口和容器内部的8080端口做一个映射链接
# -name指定名字为tomcat1而不是让他生成一个id
# tomcat指的是运行tomcat镜像
#测试访问没有问题
#进入容器
[root@iZ2ze9103odozq16xbkmr6Z ~]# docker exec -it tomcat1 /bin/bash
root@079f019540de:/usr/local/tomcat# ls
BUILDING.txt LICENSE README.md RUNNING.txt conf logs temp webapps.dist
CONTRIBUTING.md NOTICE RELEASE-NOTES bin lib native-jni-lib webapps work
root@079f019540de:/usr/local/tomcat# ll
bash: ll: command not found
root@079f019540de:/usr/local/tomcat# cd webapps
root@079f019540de:/usr/local/tomcat/webapps# ls
root@079f019540de:/usr/local/tomcat/webapps#
# 命令少了 而且没有webapps
# 保证最小可运行环境
#我们发现webapps需要的文件在webapps.dist目录下
#我们做一次cp操作,将webapps.dist的文件粘贴到webapps目录下就可以
root@079f019540de:/usr/local/tomcat# cp -r webapps.dist/* webapps
root@079f019540de:/usr/local/tomcat# cd webapps
root@079f019540de:/usr/local/tomcat/webapps# ls
ROOT docs examples host-manager manager
更多推荐
所有评论(0)