1. Docker入门

官网:https://www.docker.com/
文档:
仓库:https://hub.docker.com/

1.1 Docker基本组成

镜像(image):docker镜像就好比是一个目标,可以通过这个目标来创建容器服务,tomcat镜像==>run==>容器(提供服务器),通过这个镜像可以创建多个容器(最终服务运行或者项目运行就是在容器中的)。

容器(container):Docker利用容器技术,独立运行一个或者一组应用,通过镜像来创建的.启动,停止,删除,基本命令目前就可以把这个容器理解为就是一个简易的 Linux系统。

仓库(repository):仓库就是存放镜像的地方!
仓库分为公有仓库和私有仓库。(很类似git)
Docker Hub是国外的。
阿里云…都有容器服务器(配置镜像加速!)

1.2 安装、卸载Docker

帮助文档:https://docs.docker.com/engine/install/

环境准备:
Linux要求内核3.0以上
CentOS 7

  1. [root@iz2zeak7sgj6i7hrb2g862z ~]# uname -r
  2. 3.10.0-514.26.2.el7.x86_64 # 要求3.0以上
  3. [root@iz2zeak7sgj6i7hrb2g862z ~]# cat /etc/os-release
  4. NAME="CentOS Linux"
  5. VERSION="7 (Core)"
  6. ID="centos"
  7. ID_LIKE="rhel fedora"
  8. VERSION_ID="7"
  9. PRETTY_NAME="CentOS Linux 7 (Core)"
  10. ANSI_COLOR="0;31"
  11. CPE_NAME="cpe:/o:centos:centos:7"
  12. HOME_URL="https://www.centos.org/"
  13. BUG_REPORT_URL="https://bugs.centos.org/"
  14. CENTOS_MANTISBT_PROJECT="CentOS-7"
  15. CENTOS_MANTISBT_PROJECT_VERSION="7"
  16. REDHAT_SUPPORT_PRODUCT="centos"
  17. REDHAT_SUPPORT_PRODUCT_VERSION="7"


安装

#1.卸载旧版本
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 \
https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

#更新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查看是否按照成功
docker version

#7. 测试
docker run hello-world

#8.查看已经下载的镜像(从这里可以查看已有镜像的id)
[root@iz2zeak7sgj6i7hrb2g862z ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 4 months ago 13.3kB

卸载

#1. 卸载依赖
yum remove docker-ce docker-ce-cli containerd.io
#2. 删除资源
rm -rf /var/lib/docker
# /var/lib/docker 是docker的默认工作路径!

1.3阿里云镜像加速

(1)登录阿里云找到容器服务
(2)找到镜像加速器
(3)选择linux版本
(4)配置使用
#1.创建一个目录
sudo mkdir -p /etc/docker

#2.编写配置文件
sudo tee /etc/docker/daemon.json <<-‘EOF’
{
“registry-mirrors”: [“https://t2wwyxhb.mirror.aliyuncs.com“]
}
EOF

#3.重启服务
sudo systemctl daemon-reload
sudo systemctl restart docker

1.4 Docker常用命令

1.4.1 帮助命令

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

1.4.2 镜像命令

docker images #查看所有本地主机上的镜像 可以使用docker image ls代替

docker search #搜索镜像

docker pull #下载镜像 docker image pull

docker rmi #删除镜像 docker image rm

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

[root@iz2zeak7sgj6i7hrb2g862z ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 4 months ago 13.3kB
mysql 5.7 b84d68d0a7db 6 days ago 448MB

# 解释
#REPOSITORY # 镜像的仓库源
#TAG # 镜像的标签(版本) —-lastest 表示最新版本
#IMAGE ID # 镜像的id
#CREATED # 镜像的创建时间
#SIZE # 镜像的大小

# 可选项
Options:
-a, —all Show all images (default hides intermediate images) #列出所有镜像
-q, —quiet Only show numeric IDs # 只显示镜像的id

[root@iz2zeak7sgj6i7hrb2g862z ~]# docker images -a #列出所有镜像详细信息
[root@iz2zeak7sgj6i7hrb2g862z ~]# docker images -aq #列出所有镜像的id
d5f28a0bb0d0
f19c56ce92a8
1b6b1fe7261e
1b6b1fe7261e

(2)docker search 搜索镜像

[root@iz2zeak7sgj6i7hrb2g862z ~]# docker search mysql

# —filter=STARS=3000 #过滤,搜索出来的镜像收藏STARS数量大于3000的
Options:
-f, —filter filter Filter output based on conditions provided
—format string Pretty-print search using a Go template
—limit int Max number of search results (default 25)
—no-trunc Don’t truncate output

[root@iz2zeak7sgj6i7hrb2g862z ~]# docker search mysql —filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL IS … 9520 [OK]
mariadb MariaDB IS … 3456 [OK]

(3)docker pull 下载镜像

# 下载镜像 docker pull 镜像名[:tag]
[root@iz2zeak7sgj6i7hrb2g862z ~]# docker pull tomcat:8
8: Pulling from library/tomcat #如果不写tag,默认就是latest
90fe46dd8199: Already exists #分层下载: docker image 的核心 联合文件系统
35a4f1977689: Already exists
bbc37f14aded: Already exists
74e27dc593d4: Already exists
93a01fbfad7f: Already exists
1478df405869: Pull complete
64f0dd11682b: Pull complete
68ff4e050d11: Pull complete
f576086003cf: Pull complete
3b72593ce10e: Pull complete
Digest: sha256:0c6234e7ec9d10ab32c06423ab829b32e3183ba5bf2620ee66de866df #签名防伪
Status: Downloaded newer image for tomcat:8
docker.io/library/tomcat:8 #真实地址

#等价于
docker pull tomcat:8
docker pull docker.io/library/tomcat:8

(4)docker rmi 删除镜像

docker rmi -f 镜像id #删除指定id的镜像
[root@iz2zeak7sgj6i7hrb2g862z ~]# docker rmi -f f19c56ce92a8

docker rmi -f $(docker images -aq) #删除全部的镜像
[root@iz2zeak7sgj6i7hrb2g862z ~]# docker stop $(docker ps -a -q)

1.4.3 容器命令

说明:我们有了镜像才可以创建容器,Linux,下载centos镜像来学习

镜像下载

#docker中下载centos
docker pull centos

docker run 镜像id #新建容器并启动

docker ps 列出所有运行的容器 docker container list
[root@iz2zeak7sgj6i7hrb2g862z ~]# docker container list # h和docker ps相同

docker rm 容器id #删除指定容器

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

新建容器并启动

docker run [可选参数] image | docker container run [可选参数] image
#参书说明
—name=”Name” #容器名字 tomcat01 tomcat02 用来区分容器
-d #后台方式运行
-it #使用交互方式运行,进入容器查看内容
-p #指定容器的端口 -p 8080(宿主机):8080(容器)
-p ip:主机端口:容器端口
-p 主机端口:容器端口(常用)
-p 容器端口
容器端口
-P(大写) #随机指定端口

# 测试、启动并进入容器
[root@iz2zeak7sgj6i7hrb2g
[root@iz2zeak7sgj6i7hrb2g862z ~]# docker run -it centos /bin/bash
[root@241b5abce65e /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@241b5abce65e /]# exit #从容器退回主机
exit

列出所有运行的容器

docker ps 命令 #列出当前正在运行的容器
-a, —all #列出当前正在运行的容器 + 带出历史运行过的容器
-n=?, —last int #列出最近创建的?个容器 ?为1则只列出最近创建的一个容器,为2则列出2个
-q, —quiet #只列出容器的编号

退出容器

exit #容器直接退出
ctrl +P +Q #容器不停止退出 —-注意:这个很有用的操作

删除容器

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

启动和停止容器的操作

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

1.4.4 常见其他命令

后台启动命令

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

[root@iz2zeak7sgj6i7hrb2g862z ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# 问题docker ps. 发现centos 停止了
# 常见的坑,docker容器使用后台运行,就必须要有要一个前台进程,docker发现没有应用(前台进程),就会自动停止
# nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了

查看日志

docker logs —help
Options:
—details Show extra details provided to logs
-f, —follow Follow log output
—since string Show logs since timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
—tail string Number of lines to show from the end of the logs (default “all”)
* -t, —timestamps Show timestamps
—until string Show logs before a timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
➜ ~ docker run -d centos /bin/sh -c “while true;do echo 6666;sleep 1;done” #模拟日志
#显示日志
-tf #显示日志信息(一直更新)
—tail number #需要显示日志条数
docker logs -t —tail n 容器id #查看n行日志
docker logs -ft 容器id #跟着日志

查看容器中的进程信息

# 命令 docker top 容器id

查看镜像的元数据

命令
docker inspect 容器id

#测试
➜ ~ docker inspect 55321bcae33d
[
{
“Id”: “55321bcae33d15da8280bcac1d2bc1141d213bcc8f8e792edfd832ff61ae5066”,
“Created”: “2020-05-15T05:22:05.515909071Z”,
“Path”: “/bin/sh”,
“Args”: [
“-c”,
“while true;do echo 6666;sleep 1;done”
],
“State”: {
“Status”: “running”,
“Running”: true,
“Paused”: false,
“Restarting”: false,
“OOMKilled”: false,
“Dead”: false,
“Pid”: 22973,
“ExitCode”: 0,
“Error”: “”,
“StartedAt”: “2020-05-15T05:22:06.165904633Z”,
“FinishedAt”: “0001-01-01T00:00:00Z”
},
“Image”: “sha256:470671670cac686c7cf0081e0b37da2e9f4f768ddc5f6a26102ccd1c6954c1ee”,
“ResolvConfPath”: “/var/lib/docker/containers/55321bcae33d15da8280bcac1d2bc1141d213bcc8f8e792edfd832ff61ae5066/resolv.conf”,
“HostnamePath”: “/var/lib/docker/containers/55321bcae33d15da8280bcac1d2bc1141d213bcc8f8e792edfd832ff61ae5066/hostname”,
“HostsPath”: “/var/lib/docker/containers/55321bcae33d15da8280bcac1d2bc1141d213bcc8f8e792edfd832ff61ae5066/hosts”,
“LogPath”: “/var/lib/docker/containers/55321bcae33d15da8280bcac1d2bc1141d213bcc8f8e792edfd832ff61ae5066/55321bcae33d15da8280bcac1d2bc1141d213bcc8f8e792edfd832ff61ae5066-json.log”,
“Name”: “/bold_bell”,
“RestartCount”: 0,
“Driver”: “overlay2”,
“Platform”: “linux”,
“MountLabel”: “”,
“ProcessLabel”: “”,
“AppArmorProfile”: “docker-default”,
“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,
“Capabilities”: null,
“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/1f347949ba49c4dbee70cea9ff3af39a14e602bc8fac8331c46347bf6708757a-init/diff:/var/lib/docker/overlay2/5afcd8220c51854a847a36f52775b4ed0acb16fe6cfaec3bd2e5df59863835ba/diff”,
“MergedDir”: “/var/lib/docker/overlay2/1f347949ba49c4dbee70cea9ff3af39a14e602bc8fac8331c46347bf6708757a/merged”,
“UpperDir”: “/var/lib/docker/overlay2/1f347949ba49c4dbee70cea9ff3af39a14e602bc8fac8331c46347bf6708757a/diff”,
“WorkDir”: “/var/lib/docker/overlay2/1f347949ba49c4dbee70cea9ff3af39a14e602bc8fac8331c46347bf6708757a/work”
},
“Name”: “overlay2”
},
“Mounts”: [],
“Config”: {
“Hostname”: “55321bcae33d”,
“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 6666;sleep 1;done”
],
“Image”: “centos”,
“Volumes”: null,
“WorkingDir”: “”,
“Entrypoint”: null,
“OnBuild”: null,
“Labels”: {
“org.label-schema.build-date”: “20200114”,
“org.label-schema.license”: “GPLv2”,
“org.label-schema.name”: “CentOS Base Image”,
“org.label-schema.schema-version”: “1.0”,
“org.label-schema.vendor”: “CentOS”,
“org.opencontainers.image.created”: “2020-01-14 00:00:00-08:00”,
“org.opencontainers.image.licenses”: “GPL-2.0-only”,
“org.opencontainers.image.title”: “CentOS Base Image”,
“org.opencontainers.image.vendor”: “CentOS”
}
},
“NetworkSettings”: {
“Bridge”: “”,
“SandboxID”: “63ed0c837f35c12453bae9661859f37a08541a0749afb86e881869bf6fd9031b”,
“HairpinMode”: false,
“LinkLocalIPv6Address”: “”,
“LinkLocalIPv6PrefixLen”: 0,
“Ports”: {},
“SandboxKey”: “/var/run/docker/netns/63ed0c837f35”,
“SecondaryIPAddresses”: null,
“SecondaryIPv6Addresses”: null,
“EndpointID”: “b129d9a5a2cbb92722a2101244bd81a9e3d8af034e83f338c13790a1a94552a1”,
“Gateway”: “172.17.0.1”,
“GlobalIPv6Address”: “”,
“GlobalIPv6PrefixLen”: 0,
“IPAddress”: “172.17.0.4”,
“IPPrefixLen”: 16,
“IPv6Gateway”: “”,
“MacAddress”: “02:42:ac:11:00:04”,
“Networks”: {
“bridge”: {
“IPAMConfig”: null,
“Links”: null,
“Aliases”: null,
“NetworkID”: “ad5ada6a106f5ba3dda9ce4bc1475a4bb593bf5f7fbead72196e66515e8ac36a”,
“EndpointID”: “b129d9a5a2cbb92722a2101244bd81a9e3d8af034e83f338c13790a1a94552a1”,
“Gateway”: “172.17.0.1”,
“IPAddress”: “172.17.0.4”,
“IPPrefixLen”: 16,
“IPv6Gateway”: “”,
“GlobalIPv6Address”: “”,
“GlobalIPv6PrefixLen”: 0,
“MacAddress”: “02:42:ac:11:00:04”,
“DriverOpts”: null
}
}
}
}
]

进入当前正在运行的容器

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

命令:
# 方式一
docker exec -it 容器id bashshell

#测试
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
55321bcae33d centos “/bin/sh -c ‘while t…” 10 minutes ago Up 10 minutes bold_bell
a7215824a4db centos “/bin/sh -c ‘while t…” 13 minutes ago Up 13 minutes zen_kepler
55a31b3f8613 centos “/bin/bash” 15 minutes ago Up 15 minutes lucid_clarke
➜ ~ docker exec -it 55321bcae33d /bin/bash
[root@55321bcae33d /]#

# 方式二
docker attach 容器id

#测试
docker attach 55321bcae33d
正在执行当前的代码…

区别
#docker exec # 进入当前容器后开启一个新的终端,可以在里面操作。(常用)
#docker attach # 进入容器正在执行的终端

从容器拷贝到主机上

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

[root@iz2zeak7sgj6i7hrb2g862z ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
56a5583b25b4 centos “/bin/bash” 7seconds ago Up 6 seconds

#1. 进入docker容器内部
[root@iz2zeak7sgj6i7hrb2g862z ~]# docker exec -it 56a5583b25b4 /bin/bash
[root@55321bcae33d /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var

#新建一个文件
[root@55321bcae33d /]# echo “hello” > java.java
[root@55321bcae33d /]# cat hello.java
hello
[root@55321bcae33d /]# exit
exit

#hello.java拷贝到home文件加下
[root@iz2zeak7sgj6i7hrb2g862z /]# docker cp 56a5583b25b4:/hello.java /home
[root@iz2zeak7sgj6i7hrb2g862z /]# cd /home
[root@iz2zeak7sgj6i7hrb2g862z home]# ls -l #可以看见java.java存在
total 8
-rw-r—r— 1 root root 0 May 19 22:09 haust.java
-rw-r—r— 1 root root 6 May 22 11:12 java.java
drwx——— 3 www www 4096 May 8 12:14 www

1.4.5 命令小结

7--Docker容器技术 - 图1

attach Attach local standard input, output, and error streams to a running container
#当前shell下 attach连接指定运行的镜像
build Build an image from a Dockerfile # 通过Dockerfile定制镜像
commit Create a new image from a container’s changes #提交当前容器为新的镜像
cp Copy files/folders between a container and the local filesystem #拷贝文件
create Create a new container #创建一个新的容器
diff Inspect changes to files or directories on a container’s filesystem #查看docker容器的变化
events Get real time events from the server # 从服务获取容器实时时间
exec Run a command in a running container # 在运行中的容器上运行命令
export Export a container’s filesystem as a tar archive #导出容器文件系统作为一个tar归档文件[对应import]
history Show the history of an image # 展示一个镜像形成历史
images List images #列出系统当前的镜像
import Import the contents from a tarball to create a filesystem image #从tar包中导入内容创建一个文件系统镜像
info Display system-wide information # 显示全系统信息
inspect Return low-level information on Docker objects #查看容器详细信息
kill Kill one or more running containers # kill指定docker容器
load Load an image from a tar archive or STDIN #从一个tar包或标准输入中加载一个镜像[对应save]
login Log in to a Docker registry #
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes


1.5 部署

1.5.1 部署Nginx

#1. 搜索镜像 search 建议大家去docker搜索,可以看到帮助文档
[root@iz2zeak7sgj6i7hrb2g862z ~]# docker search nginx

#2. 拉取下载镜像 pull
[root@iz2zeak7sgj6i7hrb2g862z ~]# docker pull nginx

#3. 查看是否下载成功镜像
[root@iz2zeak7sgj6i7hrb2g862z ~]# docker images

#3. 运行测试
# -d 后台运行
# —name 给容器命名
# -p 宿主机端口:容器内部端口
[root@iz2zeak7sgj6i7hrb2g862z ~]# docker run -d —name nginx01 -p 3344:80 nginx
aa664b0c8ed98f532453ce1c599be823bcc1f3c9209e5078615af416ccb454c2

#4. 查看正在启动的镜像
[root@iz2zeak7sgj6i7hrb2g862z ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
75943663c116 nginx “nginx -g ‘daemon of…” 41 seconds ago Up 40 seconds 0.0.0.0:82->80/tcp nginx00

#5. 进入容器
[root@iz2zeak7sgj6i7hrb2g862z ~]# docker exec -it nginx01 /bin/bash #进入
root@aa664b0c8ed9:/# whereis nginx #找到nginx位置
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@aa664b0c8ed9:/# cd /etc/nginx/
root@aa664b0c8ed9:/etc/nginx# ls
conf.d fastcgi_params koi-utf koi-win mime.types modules nginx.conf scgi_params uwsgi_params win-utf

#6. 退出容器
root@aa664b0c8ed9:/etc/nginx# exit
exit

#7. 停止容器
[root@iz2zeak7sgj6i7hrb2g862z ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
aa664b0c8ed9 nginx “nginx -g ‘daemon of…” 10 minutes ago Up 10 minutes 0.0.0.0:3344->80/tcp nginx01
[root@iz2zeak7sgj6i7hrb2g862z ~]# docker stop aa664b0c8ed9

宿主机端口和容器内部端口以及端口暴露
7--Docker容器技术 - 图2

1.5.2 部署Tomcat


# 下载 tomcat9.0
# 之前的启动都是后台,停止了容器,容器还是可以查到, docker run -it —rm 镜像名 一般是用来测试,用完就删除
[root@iz2zeak7sgj6i7hrb2g862z ~]# docker run -it —rm tomcat:9.0

—rm Automatically remove the container when it exits 用完即删

#下载 最新版
[root@iz2zeak7sgj6i7hrb2g862z ~]# docker pull tomcat

#查看下载的镜像
[root@iz2zeak7sgj6i7hrb2g862z ~]# docker images

#以后台方式,暴露端口方式,启动运行
[root@iz2zeak7sgj6i7hrb2g862z ~]# docker run -d -p 8080:8080 —name tomcat01 tomcat

#测试访问有没有问题
curl localhost:8080

#根据容器id进入tomcat容器
[root@iz2zeak7sgj6i7hrb2g862z ~]# docker exec -it 645596565d3f /bin/bash
root@645596565d3f:/usr/local/tomcat#
#查看tomcat容器内部内容:
root@645596565d3f:/usr/local/tomcat# ls -l
total 152
-rw-r—r— 1 root root 18982 May 5 20:40 BUILDING.txt
-rw-r—r— 1 root root 5409 May 5 20:40 CONTRIBUTING.md
-rw-r—r— 1 root root 57092 May 5 20:40 LICENSE
-rw-r—r— 1 root root 2333 May 5 20:40 NOTICE
-rw-r—r— 1 root root 3255 May 5 20:40 README.md
-rw-r—r— 1 root root 6898 May 5 20:40 RELEASE-NOTES
-rw-r—r— 1 root root 16262 May 5 20:40 RUNNING.txt
drwxr-xr-x 2 root root 4096 May 16 12:05 bin
drwxr-xr-x 1 root root 4096 May 21 11:04 conf
drwxr-xr-x 2 root root 4096 May 16 12:05 lib
drwxrwxrwx 1 root root 4096 May 21 11:04 logs
drwxr-xr-x 2 root root 4096 May 16 12:05 native-jni-lib
drwxrwxrwx 2 root root 4096 May 16 12:05 temp
drwxr-xr-x 2 root root 4096 May 16 12:05 webapps
drwxr-xr-x 7 root root 4096 May 5 20:37 webapps.dist
drwxrwxrwx 2 root root 4096 May 5 20:36 work
root@645596565d3f:/usr/local/tomcat#
#进入webapps目录
root@645596565d3f:/usr/local/tomcat# cd webapps
root@645596565d3f:/usr/local/tomcat/webapps# ls
root@645596565d3f:/usr/local/tomcat/webapps#
# 发现问题:1、linux命令少了。 2.webapps目录为空
# 原因:阿里云镜像的原因,阿里云默认是最小的镜像,所以不必要的都剔除掉
# 保证最小可运行的环境!
# 解决方案:
# 将webapps.dist下的文件都拷贝到webapps下即可
root@645596565d3f:/usr/local/tomcat# ls 找到webapps.dist
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@645596565d3f:/usr/local/tomcat# cd webapps.dist/ # 进入webapps.dist
root@645596565d3f:/usr/local/tomcat/webapps.dist# ls # 查看内容
ROOT docs examples host-manager manager

root@645596565d3f:/usr/local/tomcat/webapps.dist# cd ..
root@645596565d3f:/usr/local/tomcat# cp -r webapps.dist/* webapps # 拷贝webapps.dist 内容给webapps
root@645596565d3f:/usr/local/tomcat# cd webapps #进入webapps
root@645596565d3f:/usr/local/tomcat/webapps# ls #查看拷贝结果
ROOT docs examples host-manager manager


1.5.3 部署