Docker 中有非常重要的三个基本概念,理解了这三个概念,就理解了 Docker 的整个生命周期。
Docker镜像常用命令
Docker创建容器可选参数
Docker 容器限制参数
Docker基本指令
Docker 版本、系统信息(包含镜像和容器的数量信息)和帮助指令
docker version #查看docker的版本信息
docker info #查看docker的系统信息,包括镜像和容器的数量
docker 命令 --help #帮助命令(可查看可选的参数)
docker COMMAND --help
MobaXterm 实际操作
[root@localhost etc]# docker version
Client:
Version: 17.12.0-ce
API version: 1.35
Go version: go1.9.2
Git commit: c97c6d6
Built: Wed Dec 27 20:10:14 2017
OS/Arch: linux/amd64
Server:
Engine:
Version: 17.12.0-ce
API version: 1.35 (minimum version 1.12)
Go version: go1.9.2
Git commit: c97c6d6
Built: Wed Dec 27 20:12:46 2017
OS/Arch: linux/amd64
Experimental: false
[root@localhost etc]# docker info
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 17.12.0-ce
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 89623f28b87a6004d4b785663257362d1658a729
runc version: b2567b37d7b75eb4cf325b77297b140ea686ce8f
init version: 949e6fa
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-1160.66.1.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 1.777GiB
Name: localhost.localdomain
ID: KG26:QA7R:ZVE7:ITKF:7I6Z:U2XM:HSGR:ZHS6:EPCM:DUOZ:IA4P:LYJS
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://oalqrqxl.mirror.aliyuncs.com/
Live Restore Enabled: false
[root@localhost etc]# docker pull --help
Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Pull an image or a repository from a registry
Options:
-a, --all-tags Download all tagged images in the repository
--disable-content-trust Skip image verification (default true)
[root@localhost etc]#
MobaXterm 实际操作
[root@localhost etc]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@localhost etc]#
列表参数介绍:
# 字段解释:
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的id
CREATED 镜像的创建时间
SIZE 镜像的大小
image指令可选参数:
# 可选参数
-a/--all 列出所有镜像
-q/--quiet 只显示镜像的id
MobaXterm 实际操作
[root@localhost etc]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 12729 [OK]
mariadb MariaDB Server is a high performing open sou… 4882 [OK]
percona Percona Server is a fork of the MySQL relati… 579 [OK]
phpmyadmin phpMyAdmin - A web interface for MySQL and M… 555 [OK]
bitnami/mysql Bitnami MySQL Docker Image 71 [OK]
linuxserver/mysql-workbench 36
linuxserver/mysql A Mysql container, brought to you by LinuxSe… 35
ubuntu/mysql MySQL open source fast, stable, multi-thread… 34
circleci/mysql MySQL is a widely used, open-source relation… 26
google/mysql MySQL server for Google Compute Engine 21 [OK]
rapidfort/mysql RapidFort optimized, hardened image for MySQL 13
vmware/harbor-db Mysql container for Harbor 10
bitnami/mysqld-exporter 3
ibmcom/mysql-s390x Docker image for mysql-s390x 2
nasqueron/mysql 1 [OK]
vitess/mysqlctld vitess/mysqlctld 1 [OK]
newrelic/mysql-plugin New Relic Plugin for monitoring MySQL databa… 1 [OK]
mirantis/mysql 0
docksal/mysql MySQL service images for Docksal - https://d… 0
drud/mysql-local-57 ddev mysql local container 0
drud/mysql 0
drud/mysql-docker-local-57 This repo has been deprecated, new tags are … 0
drud/mysql-docker-local docker containers for local womysql rk 0 [OK]
cimg/mysql 0
silintl/mysql-backup-restore Simple docker image to perform mysql backups… 0 [OK]
[root@localhost etc]#
search指令可选参数:
Search the Docker Hub for images
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
检索收藏数大于3000的MySQL镜像且返回一条记录
[root@localhost etc]# docker search mysql --filter=STARS=3000 --limit 1
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 12729 [OK]
MobaXterm 实际操作:
[root@localhost etc]# docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Pull complete
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
0ceb82207cd7: Pull complete
37f2405cae96: Pull complete
e2482e017e53: Pull complete
70deed891d42: Pull complete
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
[root@localhost etc]#
#1.删除指定的镜像id
[root@localhost /]# docker rmi -f 镜像id
#2.删除多个镜像id
[root@localhost /]# docker rmi -f 镜像id 镜像id 镜像id
#3.删除全部的镜像id
[root@localhost /]# docker rmi -f $(docker images -aq)
run指令可选参数:
#参数说明
--name="名字" 指定容器名字
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-p 指定容器的端口
( -p ip:主机端口:容器端口 配置主机端口映射到容器端口
-p 主机端口:容器端口
-p 容器端口)
-P 随机指定端口(大写的P)
MobaXterm 实际操作:
[root@localhost /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 c20987f18b13 5 months ago 448MB
[root@localhost /]# docker run c20987f18b13
2022-06-15 03:01:14+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.36-1debian10 started.
2022-06-15 03:01:14+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2022-06-15 03:01:14+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.36-1debian10 started.
2022-06-15 03:01:14+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified
You need to specify one of the following:
- MYSQL_ROOT_PASSWORD
- MYSQL_ALLOW_EMPTY_PASSWORD
- MYSQL_RANDOM_ROOT_PASSWORD
[root@localhost /]#
MobaXterm 实际操作:
[root@localhost /]# docker run -it c20987f18b13 /bin/bash
MobaXterm 实际操作:
#exit 停止并退出容器(后台方式运行则仅退出)
#Ctrl+P+Q 不停止容器退出
root@38f095df58c1:/# exit
exit
[root@localhost /]#
MobaXterm 实际操作:
[root@localhost /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost /]#
ps 指令可选参数:
Usage: docker ps [OPTIONS]
List containers
Options:
-a, --all Show all containers (default shows just running) 显示全部运行(默认显示正在运行)
-f, --filter filter Filter output based on conditions provided 基于给定条件进行过滤
--format string Pretty-print containers using a Go template
-n, --last int Show n last created containers (includes all states) (default -1) (显示最近创建容器记录数)
-l, --latest Show the latest created container (includes all states)
--no-trunc Don't truncate output
-q, --quiet Only display numeric IDs (显示容器编码)
-s, --size Display total file sizes (显示容器文件大小)
# 显示容器运行全部记录
[root@localhost /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
38f095df58c1 c20987f18b13 "docker-entrypoint.s…" 9 minutes ago Exited (0) 7 minutes ago youthful_clarke
a7963718f711 c20987f18b13 "docker-entrypoint.s…" 13 minutes ago Exited (1) 13 minutes ago friendly_kepler
[root@localhost /]#
rm 可选参数:
Remove one or more containers
Options:
-f, --force Force the removal of a running container (uses SIGKILL) (移除正在的容器)
-l, --link Remove the specified link (移除指定连接)
-v, --volumes Remove the volumes associated with the container (移除容器关联数据卷)
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 容器id #强制停止当前容器
#命令 docker run -d 镜像ID
[root@localhost /]# docker run -d c20987f18b13
c587f4387b92560788b2591d6d4fcc83c5d2149b1d9d5362544aadfd42a89017
[root@localhost /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost /]#
#问题docker ps,发现MySQL停止了
#常见的坑,docker容器使用后台运行,就必须要有一个前台进程,docker发现没有应用了,就会自动停止
#nginx,容器启动后发现自己没有提供服务,就会立刻停止,就是没有程序了
logs 可选参数:
[root@localhost /]# docker logs --help
Usage: docker logs [OPTIONS] CONTAINER
Fetch the logs of a container
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)
[root@localhost /]#
docker logs -tf 容器id
docker logs --tail number 容器id #nummber为要显示的日志条数
docker logs -n number 容器id #number为要显示的日志条数
MobaXterm 实际操作:
[root@localhost /]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
[root@localhost /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 c20987f18b13 5 months ago 448MB
centos latest 5d0da3dc9764 9 months ago 231MB
#docker容器后台运行,必须要有一个前台的进程,否则会自动停止
#编写shell脚本循环执行,使得centos容器保持运行状态
[root@localhost /]# docker run -d centos /bin/sh -c "while true;do echo hi;sleep 5;done"
6c94b90f2ca7fcd77b77ada2974ec9582a130315a43d107d968926840bc1c6ef
[root@localhost /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6c94b90f2ca7 centos "/bin/sh -c 'while t…" 6 seconds ago Up 5 seconds friendly_noyce
[root@localhost /]# docker logs -tf --tail 10 6c94b90f2ca7fcd
2022-06-15T03:39:56.014839022Z hi
2022-06-15T03:40:01.019979705Z hi
2022-06-15T03:40:06.025769245Z hi
2022-06-15T03:40:11.030432112Z hi
2022-06-15T03:40:16.036156839Z hi
2022-06-15T03:40:21.041203308Z hi
2022-06-15T03:40:26.048672225Z hi
2022-06-15T03:40:31.058255127Z hi
2022-06-15T03:40:36.061984758Z hi
2022-06-15T03:40:41.075577602Z hi
2022-06-15T03:40:46.083983779Z hi
2022-06-15T03:40:51.096404381Z hi
2022-06-15T03:40:56.109763375Z hi
2022-06-15T03:41:01.118786583Z hi
2022-06-15T03:41:06.122710210Z hi
2022-06-15T03:41:11.131146109Z hi
2022-06-15T03:41:16.136816858Z hi
2022-06-15T03:41:21.140617379Z hi
# 命令 docker top 容器id
[root@localhost /]# docker top 6c94b90f2ca7
UID PID PPID C STIME TTY TIME CMD
root 54382 54367 0 11:39 ? 00:00:00 /bin/sh -c while true;do echo hi;sleep 5;done
root 55986 54382 0 11:44 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 5
[root@localhost /]#
$ docker inspect 容器id
#docker inspect : 获取容器/镜像的元数据。
#语法
docker inspect [OPTIONS] NAME|ID [NAME|ID...]
inspect 可选参数:
[root@localhost ~]# docker inspect --help
Usage: docker inspect [OPTIONS] NAME|ID [NAME|ID...]
Return low-level information on Docker objects
Options:
-f, --format string Format the output using the given Go template (指定返回值的模板文件。)
-s, --size Display total file sizes if the type is container (显示总的文件大小。)
--type string Return JSON for specified type (为指定类型返回JSON。)
[root@localhost ~]#
MobaXterm 实际操作:
[root@localhost ~]# docker inspect 6c94b90f2ca7
[
{
"Id": "6c94b90f2ca7fcd77b77ada2974ec9582a130315a43d107d968926840bc1c6ef",
"Created": "2022-06-15T03:39:55.433118825Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo hi;sleep 5;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 54382,
"ExitCode": 0,
"Error": "",
"StartedAt": "2022-06-15T03:39:56.031108092Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/6c94b90f2ca7fcd77b77ada2974ec9582a130315a43d107d968926840bc1c6ef/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/6c94b90f2ca7fcd77b77ada2974ec9582a130315a43d107d968926840bc1c6ef/hostname",
"HostsPath": "/var/lib/docker/containers/6c94b90f2ca7fcd77b77ada2974ec9582a130315a43d107d968926840bc1c6ef/hosts",
"LogPath": "/var/lib/docker/containers/6c94b90f2ca7fcd77b77ada2974ec9582a130315a43d107d968926840bc1c6ef/6c94b90f2ca7fcd77b77ada2974ec9582a130315a43d107d968926840bc1c6ef-json.log",
产生的原因:容器使用后台方式运行时, 需要进入容器,修改一些配置。
方式一:通过 docker exex[可选参数] 容器ID Command指令
#语法
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
#exec 可选参数:
Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Run a command in a running container
Options:
-d, --detach Detached mode: run command in the background (分离模式: 在后台运行)
--detach-keys string Override the key sequence for detaching a container
-e, --env list Set environment variables(设置环境变量)
-i, --interactive Keep STDIN open even if not attached (即使没有附加也保持STDIN 打开)
--privileged Give extended privileges to the command
-t, --tty Allocate a pseudo-TTY (分配一个伪终端)
-u, --user string Username or UID (format: [:])
-w, --workdir string Working directory inside the container
MobaXterm 实际操作:
[root@localhost ~]# docker exec -it 6c94b90f2ca7 /bin/bash
[root@6c94b90f2ca7 /]# pwd
/
[root@6c94b90f2ca7 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 05:32 ? 00:00:00 /bin/sh -c while true;do echo
root 385 0 0 06:04 pts/0 00:00:00 /bin/bash
root 400 1 0 06:04 ? 00:00:00 /usr/bin/coreutils --coreutils
root 401 385 0 06:04 pts/0 00:00:00 ps -ef
[root@6c94b90f2ca7 /]#
方式二:通过 docker attach[可选参数] 容器ID (不推荐使用)
#语法
docker attach[OPTIONS] CONTAINER
#attach可选参数:
Usage: docker attach [OPTIONS] CONTAINER
Attach local standard input, output, and error streams to a running container
Options:
--detach-keys string Override the key sequence for detaching a container
--no-stdin Do not attach STDIN
--sig-proxy Proxy all received signals to the process (default true)
MobaXterm 实际操作:
[root@localhost ~]# docker attach 6c94b90f2ca7
hi
hi
hi
hi
docker cp 容器id:容器内路径 目的主机路径
MobaXterm 实际操作:
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6c94b90f2ca7 centos "/bin/sh -c 'while t…" 3 hours ago Up 3 hours friendly_noyce
[root@localhost ~]# docker exec -it 6c94b90f2ca7 /bin/bash
[root@6c94b90f2ca7 /]# cd /home/
[root@6c94b90f2ca7 home]# ls -a
. ..
# 在指定容器内的/home 文件夹下创建test.java 文件
[root@6c94b90f2ca7 home]# vi test.java
[root@6c94b90f2ca7 home]# docker cp 6c94b90f2ca7:/home/test.java /home
bash: docker: command not found
# 退出指定容器
[root@6c94b90f2ca7 home]# exit
exit
# 执行容器文件拷贝至宿主主机(/home 文件夹下)
[root@localhost ~]# docker cp 6c94b90f2ca7:/home/test.java /home
[root@localhost ~]# cd /home
[root@localhost home]# ls -a
. .. test.java
[root@localhost home]#
运行命令:
docker search dockerui
docker pull abh1nav/dockerui
docker run -d --privileged --name dockerui -p 9000:9000 -v /var/run/docker.sock:/var/run/docker.sock abh1nav/dockerui
#放开物理机的9000端口对应Docker容器的9000端口
MobaXterm 实际操作:
[root@localhost home]# docker search dockerui
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
abh1nav/dockerui An updated version of crosbymichael/dockerui… 99 [OK]
kevan/dockerui Deprecated: Use uifd/ui-for-docker 15 [OK]
microbox/dockerui Trusted Automated dockerui image (16MB size) 9
madhavkobal/dockerui Docker Updated Version having Search, Pull, … 7
mgtsai/dockerui.base-xpra Provide base docker images for X application… 2
elegoev/dockerui dockerui image based on crosbymichael/docker… 1 [OK]
rediceli/dockerui Dockerui with nginx for basic auth 1
navionics/dockerui Docker UI 1 [OK]
winking/dockerui The latest DockerUI build, see https://githu… 0
******
[root@localhost home]# docker pull abh1nav/dockerui
Using default tag: latest
latest: Pulling from abh1nav/dockerui
a3ed95caeb02: Pull complete
5d3df020ecd3: Pull complete
bebf5a3b4dfb: Pull complete
e4452c0fe72b: Pull complete
6167d9726b07: Pull complete
53ebae19a314: Pull complete
Digest: sha256:a9c6c5393f561a0f42f41cfa80572b666e745d9b419569c42bac1e5cf9ceda32
Status: Downloaded newer image for abh1nav/dockerui:latest
[root@localhost home]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 c20987f18b13 5 months ago 448MB
centos latest 5d0da3dc9764 9 months ago 231MB
abh1nav/dockerui latest 6e4d05915b2a 7 years ago 470MB
[root@localhost home]# docker run -d --privileged --name dockerui -p 9000:9000 -v /var/run/docker.sock:/var/run/docker.sock abh1nav/dockerui
WARNING: IPv4 forwarding is disabled. Networking will not work.
97f024c55d667e6b2ea6efa0e019a1118358f396f3bc112adf2d77a7fc17a95a
访问地址:http://宿主主机ip地址:9000
界面截图:
MobaXterm 实际操作:
[root@localhost etc]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 c20987f18b13 5 months ago 448MB
centos latest 5d0da3dc9764 9 months ago 231MB
abh1nav/dockerui latest 6e4d05915b2a 7 years ago 470MB
# 查找nginx 镜像服务
[root@localhost etc]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 16950 [OK]
linuxserver/nginx An Nginx container, brought to you by LinuxS… 169
bitnami/nginx Bitnami nginx Docker Image 131 [OK]
ubuntu/nginx Nginx, a high-performance reverse proxy & we… 51
bitnami/nginx-ingress-controller Bitnami Docker Image for NGINX Ingress Contr… 18 [OK]
******
# 下载nginx 镜像
[root@localhost etc]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a2abf6c4d29d: Pull complete
a9edb18cadd1: Pull complete
589b7251471a: Pull complete
186b1aaa4aa6: Pull complete
b4df32aa5a72: Pull complete
a0bcbecc962e: Pull complete
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for nginx:latest
[root@localhost etc]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 5 months ago 141MB
mysql 5.7 c20987f18b13 5 months ago 448MB
centos latest 5d0da3dc9764 9 months ago 231MB
abh1nav/dockerui latest 6e4d05915b2a 7 years ago 470MB
# 运行nginx
[root@localhost etc]# docker run -d --name nginx -p 3344:80 nginx
a3eb2528b46c0e1a5742429e45877c3f7ab601c3b77b17977772732c901a7e29
# 进入正在运行nginx 容器后台
[root@localhost etc]# docker exec -it nginx /bin/bash
root@a3eb2528b46c:/# where is nginx
bash: where: command not found
root@a3eb2528b46c:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@a3eb2528b46c:/# cd /etc/nginx
root@a3eb2528b46c:/etc/nginx# ls -a
. conf.d mime.types nginx.conf uwsgi_params
.. fastcgi_params modules scgi_params
root@a3eb2528b46c:/etc/nginx# cat nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
******
# 退出容器
root@a3eb2528b46c:/etc/nginx# exit
[root@localhost etc]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a3eb2528b46c nginx "/docker-entrypoint.…" 7 minutes ago Up 7 minutes 0.0.0.0:3344->80/tcp nginx
97f024c55d66 abh1nav/dockerui "./dockerui" About an hour ago Up About an hour 0.0.0.0:9000->9000/tcp dockerui
# 停止容器
[root@localhost etc]# docker stop a3eb2528b46c
a3eb2528b46c
测试访问:
核心指令:
# 查找nginx 镜像服务
[root@localhost etc]# docker search nginx
# 下载nginx 镜像
[root@localhost etc]# docker pull nginx
# 运行nginx
[root@localhost etc]# docker run -d --name nginx -p 3344:80 nginx
# 备注
# -d 后台运行
# --name 给容器命名
# -p 3334:80 将宿主机的端口3334映射到该容器的80端口
MobaXterm 实际操作:
[root@localhost etc]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 5 months ago 141MB
mysql 5.7 c20987f18b13 5 months ago 448MB
centos latest 5d0da3dc9764 9 months ago 231MB
abh1nav/dockerui latest 6e4d05915b2a 7 years ago 470MB
# 查账tomcat 镜像
[root@localhost etc]# docker search tomcat
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
tomcat Apache Tomcat is an open source implementati… 3341 [OK]
tomee Apache TomEE is an all-Apache Java EE certif… 97 [OK]
******
# 下载tomcat 指定版本8.0
[root@localhost etc]# docker pull tomcat:8.0
8.0: Pulling from library/tomcat
f189db1b88b3: Pull complete
3d06cf2f1b5e: Pull complete
edd0da9e3091: Pull complete
eb7768aae14e: Pull complete
e2780f585e0f: Pull complete
e5ed720afeba: Pull complete
d9e134700cfc: Pull complete
e4804b33d02a: Pull complete
b9df0c24315e: Pull complete
49fdae8eaa20: Pull complete
1aea3d9a32e6: Pull complete
Digest: sha256:8ecb10948deb32c34aeadf7bf95d12a93fbd3527911fa629c1a3e7823b89ce6f
Status: Downloaded newer image for tomcat:8.0
[root@localhost etc]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 5 months ago 141MB
tomcat latest fb5657adc892 5 months ago 680MB
mysql 5.7 c20987f18b13 5 months ago 448MB
centos latest 5d0da3dc9764 9 months ago 231MB
tomcat 8.0 ef6a7c98d192 3 years ago 356MB
abh1nav/dockerui latest 6e4d05915b2a 7 years ago 470MB
# 启动tomcat 指定版本8.0
[root@localhost etc]# docker run -d -p 3355:8080 --name tomcat ef6a7c98d192
5722a9fd9fbb0be727954399b939d2a89e43b84791a792e04216c0dcd9d65d2c
# 进入容器
[root@localhost etc]# docker exec -it tomcat /bin/bash
root@65b4405c78c7:/usr/local/tomcat# whereis tomcat
tomcat: /usr/local/tomcat
root@65b4405c78c7:/usr/local/tomcat# pwd
/usr/local/tomcat
root@65b4405c78c7:/usr/local/tomcat# ls -a
. LICENSE RUNNING.txt logs webapps.dist
.. NOTICE bin native-jni-lib work
BUILDING.txt README.md conf temp
CONTRIBUTING.md RELEASE-NOTES lib webapps
root@65b4405c78c7:/usr/local/tomcat# cd webapps
root@65b4405c78c7:/usr/local/tomcat/webapps# ls -a
. ..
root@65b4405c78c7:/usr/local/tomcat/webapps# cp -r webapps.dist/* webapp
cp: cannot stat 'webapps.dist/*': No such file or directory
root@65b4405c78c7:/usr/local/tomcat/webapps# cp -r webapps.dist/* webapps
cp: cannot stat 'webapps.dist/*': No such file or directory
root@65b4405c78c7:/usr/local/tomcat/webapps# ls -a
. ..
root@65b4405c78c7:/usr/local/tomcat/webapps# cd ..
root@65b4405c78c7:/usr/local/tomcat# ls -a
. LICENSE RUNNING.txt logs webapps.dist
.. NOTICE bin native-jni-lib work
BUILDING.txt README.md conf temp
CONTRIBUTING.md RELEASE-NOTES lib webapps
# 发现问题:1.linux命令少了, 2. webapps下内容为空,阿里云默认是最小的镜像,所有不必要的都剔除了
# 保证最小可运行环境即可
root@65b4405c78c7:/usr/local/tomcat# cp -r webapps.dist/* webapps
# 网站恢复访问
root@65b4405c78c7:/usr/local/tomcat# exit
exit
测试访问:
核心指令:
# 查账tomcat 镜像
[root@localhost etc]# docker search tomcat
# 下载tomcat 指定版本8.0
[root@localhost etc]# docker pull tomcat:8.0
# 启动tomcat 指定版本8.0
[root@localhost etc]# docker run -d -p 3355:8080 --name tomcat ef6a7c98d192
# 进入容器
[root@localhost etc]# docker exec -it tomcat /bin/bash
# 发现问题:1.linux命令少了, 2. webapps下内容为空,阿里云默认是最小的镜像,所有不必要的都剔除了
# 保证最小可运行环境即可
root@65b4405c78c7:/usr/local/tomcat# cp -r webapps.dist/* webapps
# 网站恢复访问
重点提示:由于Elasticsearch 占用虚拟机内存严重,需要添加’-e ES_JAVA_OPTS=“-Xms128m -Xmx512m” ‘ 配置ElasticSearch的虚拟机占用的内存大小。
MobaXterm 实际操作:
[root@localhost etc]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 5 months ago 141MB
tomcat latest fb5657adc892 5 months ago 680MB
mysql 5.7 c20987f18b13 5 months ago 448MB
centos latest 5d0da3dc9764 9 months ago 231MB
tomcat 8.0 ef6a7c98d192 3 years ago 356MB
abh1nav/dockerui latest 6e4d05915b2a 7 years ago 470MB
# 查询ElasticSearch 镜像
[root@localhost etc]# docker search elasticsearch
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
elasticsearch Elasticsearch is a powerful open source sear… 5657 [OK]
kibana Kibana gives shape to any kind of data — str… 2416 [OK]
bitnami/elasticsearch Bitnami Docker Image for Elasticsearch 53 [OK]
bitnami/elasticsearch-exporter Bitnami Elasticsearch Exporter Docker Image 4 [OK]
******
# 下载Elasticsearch指定版本7.6.2
[root@localhost etc]# docker pull elasticsearch:7.6.2
7.6.2: Pulling from library/elasticsearch
ab5ef0e58194: Pull complete
c4d1ca5c8a25: Pull complete
941a3cc8e7b8: Pull complete
43ec483d9618: Pull complete
c486fd200684: Pull complete
1b960df074b2: Pull complete
1719d48d6823: Pull complete
Digest: sha256:1b09dbd93085a1e7bca34830e77d2981521a7210e11f11eda997add1c12711fa
Status: Downloaded newer image for elasticsearch:7.6.2
[root@localhost etc]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 5 months ago 141MB
tomcat latest fb5657adc892 5 months ago 680MB
mysql 5.7 c20987f18b13 5 months ago 448MB
centos latest 5d0da3dc9764 9 months ago 231MB
elasticsearch 7.6.2 f29a1ee41030 2 years ago 791MB
tomcat 8.0 ef6a7c98d192 3 years ago 356MB
abh1nav/dockerui latest 6e4d05915b2a 7 years ago 470MB
# 运行Elasticsearch
[root@localhost etc]# docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xmx512m" elasticsearch:7.6.2
测试访问:
核心指令:
# 查询ElasticSearch 镜像
[root@localhost etc]# docker search elasticsearch
# 下载Elasticsearch指定版本7.6.2
[root@localhost etc]# docker pull elasticsearch:7.6.2
# 直接启动Elasticsearch7.6.2
[root@localhost etc]# docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.6.2
# 启动后linux就卡住了,docker stats 查看cpu状态
[root@localhost etc]# docker stats
# es 暴露的端口很多
# es 十分的耗内存
# es 的数据一般需要放置到安全目录! 挂载
# --net somenetwork ? 网络配置
# 增加内存限制,修改配置文件 -e 环境配置修改
[root@localhost etc]# docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xmx512m" elasticsearch:7.6.2
Docker镜像是一种轻量级、可执行的独立软件包,用来打包软件运行环境和基于运行环境开发的软件,它包含某个软件所需的所有内容,包括代码、库、环境变量、配置文件、运行时环境等。
所有的应用,直接打包成Docker镜像,然后通过镜像创建出容器,然后就可以直接跑起来。
如何得到镜像:
通过对原有的镜像创建的容器进行一些修改(也可以不修改),然后通过Commit命令提交一个新的镜像。
docker镜像实际上是由一层层的文件系统组成,这种层级文件系统就是联合文件系统。
我们平时安装的虚拟机centos镜像好几个G,Docker安装的才200多M,因为对于一个精简的OS,rootfs可以很小,只需包含最基本的命令,工具和程序库就行了,因为底层直接使用宿主机的内核,自己只需提供rootfs(相当于操作内核的客户端)就可以,由此可见不同发行版的bootfs基本是一致的,roorfs有差别,因此不同的发行版可以公有bootfs。虚拟机是分钟级别,容器是秒级。
第一个图仅仅是bootfs+rootfs,然后如果要制作一个emacs环境的镜像,就在这个基础上新加一层emacs镜像,如图二。如果要在添加一个Apache环境,那就再图二基础上加一个apache镜像。如图三。图中的每一层镜像都能进行复用。
[root@localhost etc]# docker pull elasticsearch:7.6.2
7.6.2: Pulling from library/elasticsearch
ab5ef0e58194: Pull complete
c4d1ca5c8a25: Pull complete
941a3cc8e7b8: Pull complete
43ec483d9618: Pull complete
c486fd200684: Pull complete
1b960df074b2: Pull complete
1719d48d6823: Pull complete
Digest: sha256:1b09dbd93085a1e7bca34830e77d2981521a7210e11f11eda997add1c12711fa
Status: Downloaded newer image for elasticsearch:7.6.2
比如:上面的elasticsearch镜像。使用docker inspect elasticsearch镜像的ID 命令查看镜像的元信息,找到layer信息。
由上图可以看到下载的Elasticsearch镜像是由7个镜像一层层组成的。
这些镜像都是一个个独立可复用的镜像,如果下载其他镜像是,某一层镜像是已经存在本地的了,就不用在下载,直接复用该镜像,节省空间。比如上面下载redis镜像时,提示某个镜像已经存在。
温馨提示:Docker镜像都是只读的,用镜像创建容器启动时,实际上是在原本的镜像上新建了一层可写层到原本镜像的顶部,这一层我们叫作容器层,容器层之下的叫作镜像层。
如上图,使用ElasticSearch镜像创建容器后,会在ElasticSearch镜像的基础上新建一个可写层,容器的写入是在可写层进行记录,然后使用commit命令把该容器创建一个新的镜像,实际上新的镜像是ElasticSearch镜像+可写层镜像,以ElasticSearch镜像为基础。通过下面介绍使用容器构建镜像,可以更好地理解。
常用命令:
docker commit 提交容器成为一个新的副本
docker commit -m="描述信息,类似git提交的信息" -a="作者" 容器id 目标镜像名:[TAG]$ 编辑容器后提交容器成为一个新镜像a
MobaXterm 实际操作:
#1.启动一个默认的tomcat
docker run -it -p 8080:8080 tomcat
#2.发现这个默认的tomcat是没有webapps的应用,镜像的原因,官方的镜像默认webapps下面是没有文件的
#3.自己拷贝进去基本的文件
cp -r webapps.dist/* webapps
#4.将修改后的容器通过commit提交为新的镜像~我们以后就使用我们自己制作的镜像了
docker commit =a="zzg" -m="add webapps app" 容器ID tomcat02:1.0
为了实现数据持久化,使容器之间可以共享数据。可以将容器内的目录,挂载到宿主机上或其他容器内,实现同步和共享的操作。即使将容器删除,挂载到本地的数据卷也不会丢失。
Docker理念
将应用和环境打包成一个镜像
数据如果都在容器中,那么我们容器一旦删除,数据就会丢失 需求:数据可以持久化
MySQL,容器删了,删库跑路 需求:MySQL数据可以存储在本地
容器之间可以有一个数据共享的技术,Docker容器中产生的数据同步到本地
这就是卷技术,目录的挂载将我们容器内的目录挂载到Linux上面
容器的持久化和同步操作,容器间也是可以做数据共享的。
方式一:直接使用命令来挂载 -v
语法:
docker run -it -v 主机目录:容器内目录
MobaXterm 实际操作:
[root@localhost etc]# pwd
/etc
[root@localhost etc]# cd /home
# 查询/home 目录下相关文件
[root@localhost home]# ls -a
. .. test.java
# 容器卷挂载
[root@localhost ceshi]# docker run -dit -v /home/ceshi:/home centos /bin/sh -c "while true;do echo hi;sleep 5;done"
# 查看容器状态
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fe52a516f65e centos "/bin/sh -c 'while t…" 47 seconds ago Up 46 seconds reverent_bassi
2688accce5f9 elasticsearch:7.6.2 "/usr/local/bin/dock…" About an hour ago Up About an hour 0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp elasticsearch
5722a9fd9fbb ef6a7c98d192 "catalina.sh run" 2 hours ago Up 2 hours 0.0.0.0:3355->8080/tcp tomcat
97f024c55d66 abh1nav/dockerui "./dockerui" 4 hours ago Up 3 hours 0.0.0.0:9000->9000/tcp dockerui
# 进入正在运行的容器后台
[root@localhost ~]# docker exec -it fe52a516f65e /bin/bash
[root@fe52a516f65e /]# cd /home
[root@fe52a516f65e home]# ls -a
. ..
# 在/home 文件目录新增volumn.txt 文件
[root@fe52a516f65e home]# vi volumn.txt
[root@fe52a516f65e home]# exit
exit
# 查看宿主主机上的/home/ceshi 文件夹下是否有新增volumn.txt文件
[root@localhost ~]# cd /home/ceshi
[root@localhost ceshi]# ls -a
. .. volumn.txt
#查看挂载情况
[root@localhost ceshi]# docker inspect fe52a516f65e
1.安装 Mysql 5.7 docker镜像
docker pull mysql:5.7
2.查看镜像库
docker images
3.创建mysql容器
3.1 在本地创建mysql的映射目录
mkdir -p /root/mysql/data /root/mysql/logs /root/mysql/conf
在/root/mysql/conf中创建 *.cnf 文件(叫什么都行)
touch my.cnf
3.2 创建容器,将数据,日志,配置文件映射到本机
docker run -p 3306:3306 --name mysql -v /root/mysql/conf:/etc/mysql/conf.d -v /root/mysql/logs:/logs -v /root/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7
-d: 后台运行容器
-p 将容器的端口映射到本机的端口
-v 将主机目录挂载到容器的目录
-e 设置参数
3.3 查看/root/mysql/data目录是否有数据文件
MobaXterm 实际操作:
[root@localhost ceshi]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 5 months ago 141MB
tomcat latest fb5657adc892 5 months ago 680MB
mysql 5.7 c20987f18b13 5 months ago 448MB
centos latest 5d0da3dc9764 9 months ago 231MB
elasticsearch 7.6.2 f29a1ee41030 2 years ago 791MB
tomcat 8.0 ef6a7c98d192 3 years ago 356MB
abh1nav/dockerui latest 6e4d05915b2a 7 years ago 470MB
[root@localhost ceshi]# cd /
[root@localhost /]# mkdir -p /root/mysql/data /root/mysql/logs /root/mysql/conf
[root@localhost /]# cd /root/mysql/conf
[root@localhost conf]# touch my.cnf
[root@localhost conf]# docker run -p 3306:3306 --name mysql -v /root/mysql/conf:/etc/mysql/conf.d -v /root/mysql/logs:/logs -v /root/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7
dacc4ce950337e1a3e7ec39451340a2428d75b5b36deb08ba9d6f0fb3cf386cd
[root@localhost conf]# cd /root/mysql/data
[root@localhost data]# ls -a
. auto.cnf ca.pem client-key.pem ibdata1 ib_logfile1 mysql private_key.pem server-cert.pem sys
.. ca-key.pem client-cert.pem ib_buffer_pool ib_logfile0 ibtmp1 performance_schema public_key.pem server-key.pem
[root@localhost data]#
使用Native For MySQL工具连接测试:
#匿名挂载
-v 容器内挂载
docker run -d -P --name nginx01 -v /etc/nginx nginx $ -P 随机指定端口
#查看所有的volume的情况
$ docker volume ls
DRIVER VOLUME NAME
local 8026aa409bf4851f9056cc3092e27493db1ee654b461b1ea05c3bedfafed3e32
#具名挂载
$ docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx nginx
72bf479b190b74d53256311e626bab459630af2028b9c9e1a83064c2ff745d9e
$ docker volume ls
DRIVER VOLUME NAME
local 8026aa409bf4851f9056cc3092e27493db1ee654b461b1ea05c3bedfafed3e32
local 04862392ad0fcff6ecabd65989ae57e109a9963de0d517f4e8b305b44700795a
local d7111fd00a6d3704ec4bd5419eee07ba6c4bac699abe227b908f6d385188c472
local jumping-nginx
# 通过-v 卷名:容器内的路径
# 查看一下这个卷
# docker volume inspect 卷名
$ docker volume inspect juming-nginx
[
{
"CreatedAt": "2022-04-19T14:52:24+08:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/juming-nginx/_data",
"Name": "jumping-nginx",
"Options": null,
"Scope": "local"
}
]
知识点:
#如何确定是具名挂载还是匿名挂载,还是指定路径挂载
-v 容器内路径 #匿名挂载
-v 卷名:容器内路径 #具名挂载
-v /宿主机路径::容器内路径 #指定路径挂载
拓展:
#通过-v容器内路径,ro rw改变读写权限
ro read only #只读
rw read write #可读可写
#一旦这个设置了容器权限,容器对我们挂载出来的内容就有限定了
$ docker run -d -P --name nginx02 -v jumping-nginx:/etc/nginx:ro nginx
$ docker run -d -P --name nginx02 -v jumping-nginx:/etc/nginx:rw nginx
#ro 只要看到ro就说明这个路径是只能通过宿主机来操作,容器内部是无法操作
Dockerfile是用来构建docker镜像的文件,命令参数脚本
构建步骤:
查看一下官方是怎么做的
很多官方镜像都是基础包,我们通常会自己搭建自己的镜像。
官方既然可以制作镜像,那我们也可以自己构建。
基础知识:
每个保留关键字(指令)都必须是大写字母
执行顺序从上到下
#表示注释
每一个指令都会创建提交一个新的镜像层,并提交!
FROM # 基础镜像
MAINTAINER # 镜像谁写的,姓名+邮箱
RUN # 镜像构建的时候需要运行的命令
ADD # 步骤:centos镜像,centos压缩包!添加内容
WORKDIR # 镜像的工作目录
VOLUME # 挂载的目录
EXPOSE # 暴露端口配置
CMD # 指定这个容器启动的时候运行的命令,只有最后一个会生效,可以被替代
ENTRYPOINT # 指定这个容器启动的时候要运行的命令,可以追加命令
ONBUILD # 当构建一个被继承 DockerFile 这个时候就会运行 ONBUILD 的指令。触发指令
COPY # 类似ADD,将我们文件拷贝到镜像中
ENV # 构建的时候设置环境变量
Docker Hub中99%镜像都是从这个基础镜像过来的FROM scratch
,然后配置需要的软件和配置来进行构建。
MobaXterm 实际操作:
# 编辑dockerfile-centos 文件
[root@localhost ~]# vim dockerfile-centos
[root@localhost ~]# ls -a
. .. anaconda-ks.cfg .bash_history .bash_logout .bash_profile .bashrc .cshrc dockerfile-centos mysql .pki .tcshrc .viminfo
# 编译dockerfile-centos 文件
[root@localhost ~]# docker build -f dockerfile-centos -t mycentos:0.1 .
Sending build context to Docker daemon 219.8MB
Step 1/8 : FROM centos:latest
---> 5d0da3dc9764
Step 2/8 : MAINTAINER zhouzhiwengang
---> Using cache
---> c7877d372938
Step 3/8 : ENV MYPATH /user/local
---> Using cache
---> d6232b2fbf27
Step 4/8 : WORKDIR $MYPATH
---> Using cache
---> 1f73cff95efe
Step 5/8 : EXPOSE 80
---> Running in d90f6378290a
Removing intermediate container d90f6378290a
---> 397d16dc87d1
Step 6/8 : CMD echo $MYPATH
---> Running in 815375f27354
Removing intermediate container 815375f27354
---> 7c6fa3907394
Step 7/8 : CMD echo "-----end-----"
---> Running in 300078bc3e8c
Removing intermediate container 300078bc3e8c
---> ccb4425fb27a
Step 8/8 : CMD /bin/bash
---> Running in 58cb10d51d54
Removing intermediate container 58cb10d51d54
---> d42edc289e9c
Successfully built d42edc289e9c
Successfully tagged mycentos:0.1
# 查询镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos 0.1 d42edc289e9c 7 seconds ago 231MB
nginx latest 605c77e624dd 5 months ago 141MB
tomcat latest fb5657adc892 5 months ago 680MB
mysql 5.7 c20987f18b13 5 months ago 448MB
centos latest 5d0da3dc9764 9 months ago 231MB
elasticsearch 7.6.2 f29a1ee41030 2 years ago 791MB
tomcat 8.0 ef6a7c98d192 3 years ago 356MB
abh1nav/dockerui latest 6e4d05915b2a 7 years ago 470MB
# 运行自定义镜像文件
[root@localhost ~]# docker run -it mycentos:0.1
[root@6c55f06e8eea local]# pwd
/user/local
[root@6c55f06e8eea local]# exit
编辑dockerfile-centos 内容:
[root@localhost ~]# cat dockerfile-centos
FROM centos:latest
MAINTAINER zhouzhiwengang
ENV MYPATH /user/local
WORKDIR $MYPATH
EXPOSE 80
CMD echo $MYPATH
CMD echo "-----end-----"
CMD /bin/bash
[root@localhost ~]#
CMD # 指定这个容器启动的时候运行的命令,只有最后一个会生效,可以被替代
ENTRYPOINT # 指定这个容器启动的时候要运行的命令,可以追加命令
从Oracle JDK 官网找到JDK8版本。
从Tomcat 8 官网找到Tomcat8版本。
将下载好的apache-tomcat-8.5.81.tar.gz 和 jdk-8u333-linux-x64.tar.gz 上传CentOS 服务器的/usr/local目录下。
2.编写dockerfile文件,我这里将它明明为:dockerfile-tomcat
MobaXterm 实际操作:
# 编辑dockerfile-tomcat文件
[root@localhost local]# vim dockerfile-tomcat
# 编译dockerfile-tomcat 文件
[root@localhost local]# docker build -f dockerfile-tomcat -t mytocmat:0.1 .
Sending build context to Docker daemon 543.9MB
Step 1/14 : FROM centos:7
7: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987
Status: Downloaded newer image for centos:7
---> eeb6ee3f44bd
Step 2/14 : MAINTAINER zhouzhiwengang
---> Running in 28155bdd30d5
Removing intermediate container 28155bdd30d5
---> 112ca76a8227
Step 3/14 : ADD jdk-8u333-linux-x64.tar.gz /usr/local
---> f1f6b9c1b86a
Step 4/14 : ADD apache-tomcat-8.5.81.tar.gz /usr/local
---> 8ca2bbb1b2fc
Step 5/14 : RUN yum -y install vim
---> Running in ecca98c27761
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.ustc.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package vim-enhanced.x86_64 2:7.4.629-8.el7_9 will be installed
--> Processing Dependency: vim-common = 2:7.4.629-8.el7_9 for package: 2:vim-enhanced-7.4.629-8.el7_9.x86_64
--> Processing Dependency: which for package: 2:vim-enhanced-7.4.629-8.el7_9.x86_64
--> Processing Dependency: perl(:MODULE_COMPAT_5.16.3) for package: 2:vim-enhanced-7.4.629-8.el7_9.x86_64
--> Processing Dependency: libperl.so()(64bit) for package: 2:vim-enhanced-7.4.629-8.el7_9.x86_64
--> Processing Dependency: libgpm.so.2()(64bit) for package: 2:vim-enhanced-7.4.629-8.el7_9.x86_64
--> Running transaction check
---> Package gpm-libs.x86_64 0:1.20.7-6.el7 will be installed
---> Package perl.x86_64 4:5.16.3-299.el7_9 will be installed
--> Processing Dependency: perl(Socket) >= 1.3 for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Scalar::Util) >= 1.10 for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl-macros for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(threads::shared) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(threads) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(constant) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Time::Local) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Time::HiRes) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Storable) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Socket) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Scalar::Util) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Pod::Simple::XHTML) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Pod::Simple::Search) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Getopt::Long) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Filter::Util::Call) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(File::Temp) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(File::Spec::Unix) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(File::Spec::Functions) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(File::Spec) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(File::Path) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Exporter) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Cwd) for package: 4:perl-5.16.3-299.el7_9.x86_64
--> Processing Dependency: perl(Carp) for package: 4:perl-5.16.3-299.el7_9.x86_64
---> Package perl-libs.x86_64 4:5.16.3-299.el7_9 will be installed
---> Package vim-common.x86_64 2:7.4.629-8.el7_9 will be installed
--> Processing Dependency: vim-filesystem for package: 2:vim-common-7.4.629-8.el7_9.x86_64
---> Package which.x86_64 0:2.20-7.el7 will be installed
--> Running transaction check
---> Package perl-Carp.noarch 0:1.26-244.el7 will be installed
---> Package perl-Exporter.noarch 0:5.68-3.el7 will be installed
---> Package perl-File-Path.noarch 0:2.09-2.el7 will be installed
---> Package perl-File-Temp.noarch 0:0.23.01-3.el7 will be installed
---> Package perl-Filter.x86_64 0:1.49-3.el7 will be installed
---> Package perl-Getopt-Long.noarch 0:2.40-3.el7 will be installed
--> Processing Dependency: perl(Pod::Usage) >= 1.14 for package: perl-Getopt-Long-2.40-3.el7.noarch
--> Processing Dependency: perl(Text::ParseWords) for package: perl-Getopt-Long-2.40-3.el7.noarch
---> Package perl-PathTools.x86_64 0:3.40-5.el7 will be installed
---> Package perl-Pod-Simple.noarch 1:3.28-4.el7 will be installed
--> Processing Dependency: perl(Pod::Escapes) >= 1.04 for package: 1:perl-Pod-Simple-3.28-4.el7.noarch
--> Processing Dependency: perl(Encode) for package: 1:perl-Pod-Simple-3.28-4.el7.noarch
---> Package perl-Scalar-List-Utils.x86_64 0:1.27-248.el7 will be installed
---> Package perl-Socket.x86_64 0:2.010-5.el7 will be installed
---> Package perl-Storable.x86_64 0:2.45-3.el7 will be installed
---> Package perl-Time-HiRes.x86_64 4:1.9725-3.el7 will be installed
---> Package perl-Time-Local.noarch 0:1.2300-2.el7 will be installed
---> Package perl-constant.noarch 0:1.27-2.el7 will be installed
---> Package perl-macros.x86_64 4:5.16.3-299.el7_9 will be installed
---> Package perl-threads.x86_64 0:1.87-4.el7 will be installed
---> Package perl-threads-shared.x86_64 0:1.43-6.el7 will be installed
---> Package vim-filesystem.x86_64 2:7.4.629-8.el7_9 will be installed
--> Running transaction check
---> Package perl-Encode.x86_64 0:2.51-7.el7 will be installed
---> Package perl-Pod-Escapes.noarch 1:1.04-299.el7_9 will be installed
---> Package perl-Pod-Usage.noarch 0:1.63-3.el7 will be installed
--> Processing Dependency: perl(Pod::Text) >= 3.15 for package: perl-Pod-Usage-1.63-3.el7.noarch
--> Processing Dependency: perl-Pod-Perldoc for package: perl-Pod-Usage-1.63-3.el7.noarch
---> Package perl-Text-ParseWords.noarch 0:3.29-4.el7 will be installed
--> Running transaction check
---> Package perl-Pod-Perldoc.noarch 0:3.20-4.el7 will be installed
--> Processing Dependency: perl(parent) for package: perl-Pod-Perldoc-3.20-4.el7.noarch
--> Processing Dependency: perl(HTTP::Tiny) for package: perl-Pod-Perldoc-3.20-4.el7.noarch
--> Processing Dependency: groff-base for package: perl-Pod-Perldoc-3.20-4.el7.noarch
---> Package perl-podlators.noarch 0:2.5.1-3.el7 will be installed
--> Running transaction check
---> Package groff-base.x86_64 0:1.22.2-8.el7 will be installed
---> Package perl-HTTP-Tiny.noarch 0:0.033-3.el7 will be installed
---> Package perl-parent.noarch 1:0.225-244.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
vim-enhanced x86_64 2:7.4.629-8.el7_9 updates 1.1 M
Installing for dependencies:
gpm-libs x86_64 1.20.7-6.el7 base 32 k
groff-base x86_64 1.22.2-8.el7 base 942 k
perl x86_64 4:5.16.3-299.el7_9 updates 8.0 M
perl-Carp noarch 1.26-244.el7 base 19 k
perl-Encode x86_64 2.51-7.el7 base 1.5 M
perl-Exporter noarch 5.68-3.el7 base 28 k
perl-File-Path noarch 2.09-2.el7 base 26 k
perl-File-Temp noarch 0.23.01-3.el7 base 56 k
perl-Filter x86_64 1.49-3.el7 base 76 k
perl-Getopt-Long noarch 2.40-3.el7 base 56 k
perl-HTTP-Tiny noarch 0.033-3.el7 base 38 k
perl-PathTools x86_64 3.40-5.el7 base 82 k
perl-Pod-Escapes noarch 1:1.04-299.el7_9 updates 52 k
perl-Pod-Perldoc noarch 3.20-4.el7 base 87 k
perl-Pod-Simple noarch 1:3.28-4.el7 base 216 k
perl-Pod-Usage noarch 1.63-3.el7 base 27 k
perl-Scalar-List-Utils x86_64 1.27-248.el7 base 36 k
perl-Socket x86_64 2.010-5.el7 base 49 k
perl-Storable x86_64 2.45-3.el7 base 77 k
perl-Text-ParseWords noarch 3.29-4.el7 base 14 k
perl-Time-HiRes x86_64 4:1.9725-3.el7 base 45 k
perl-Time-Local noarch 1.2300-2.el7 base 24 k
perl-constant noarch 1.27-2.el7 base 19 k
perl-libs x86_64 4:5.16.3-299.el7_9 updates 690 k
perl-macros x86_64 4:5.16.3-299.el7_9 updates 44 k
perl-parent noarch 1:0.225-244.el7 base 12 k
perl-podlators noarch 2.5.1-3.el7 base 112 k
perl-threads x86_64 1.87-4.el7 base 49 k
perl-threads-shared x86_64 1.43-6.el7 base 39 k
vim-common x86_64 2:7.4.629-8.el7_9 updates 5.9 M
vim-filesystem x86_64 2:7.4.629-8.el7_9 updates 11 k
which x86_64 2.20-7.el7 base 41 k
Transaction Summary
================================================================================
Install 1 Package (+32 Dependent packages)
******
Complete!
Removing intermediate container ecca98c27761
---> e470ac38e590
Step 6/14 : ENV MYPATH /usr/local
---> Running in 7d1e3bc9e4f4
Removing intermediate container 7d1e3bc9e4f4
---> 3f3efa08035c
Step 7/14 : WORKDIR $MYPATH
Removing intermediate container 00093ee1ed27
---> 91a8012d7140
Step 8/14 : ENV JAVA_HOME /usr/local/jdk1.8.0_333
---> Running in 40676f5ebd78
Removing intermediate container 40676f5ebd78
---> 27ff7f09493c
Step 9/14 : ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
---> Running in 6eec410ad4fd
Removing intermediate container 6eec410ad4fd
---> b11a97bf86f8
Step 10/14 : ENV CATALINA_HOME /usr/local/apache-tomcat-8.5.81
---> Running in dbbbaa2403bd
Removing intermediate container dbbbaa2403bd
---> 137839506864
Step 11/14 : ENV CATALINA_BASH /usr/local/apache-tomcat-8.5.81
---> Running in 9da924344e23
Removing intermediate container 9da924344e23
---> b265479462d2
Step 12/14 : ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
---> Running in ac85df4cc141
Removing intermediate container ac85df4cc141
---> 69c5e9ddc8f8
Step 13/14 : EXPOSE 8080
---> Running in f64c87fa722a
Removing intermediate container f64c87fa722a
---> ff6b10cda8d6
Step 14/14 : CMD /usr/local/apache-tomcat-8.5.81/bin/startup.sh && tail -F /usr/local/apache-tomcat-8.5.81/bin/logs/catalina.out
---> Running in 2ffa3ae58b6e
Removing intermediate container 2ffa3ae58b6e
---> bdb80b135844
Successfully built bdb80b135844
Successfully tagged mytocmat:0.1
# 查看文件镜像文件
[root@localhost local]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mytocmat 0.1 bdb80b135844 About a minute ago 814MB
mycentos 0.1 d42edc289e9c 2 hours ago 231MB
nginx latest 605c77e624dd 5 months ago 141MB
tomcat latest fb5657adc892 5 months ago 680MB
mysql 5.7 c20987f18b13 5 months ago 448MB
centos 7 eeb6ee3f44bd 9 months ago 204MB
centos latest 5d0da3dc9764 9 months ago 231MB
elasticsearch 7.6.2 f29a1ee41030 2 years ago 791MB
tomcat 8.0 ef6a7c98d192 3 years ago 356MB
abh1nav/dockerui latest 6e4d05915b2a 7 years ago 470MB
# 运行mytocmat 文件
[root@localhost home]# docker run -d -p 9090:8080 --name mytomcat -v /home/build/tomcat/test:/usr/local/apache-tomcat-8.5.81/webapps/test -v /home/build/tomcat/logs:/usr/local/apache-tomcat-8.5.81/logs mytocmat:0.1
32b038bc7c74ddc2bc5c2efe53b2c30a388bdd05231555f510935eac3bd1e37f
# 查看正在运行的容器
[root@localhost home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
32b038bc7c74 mytocmat:0.1 "/bin/sh -c '/usr/lo…" 8 seconds ago Up 6 seconds 0.0.0.0:9090->8080/tcp mytomcat
[root@localhost home]#
dockerfile-tomcat 文件核心内容:
FROM centos:7
MAINTAINER zhouzhiwengang
ADD jdk-8u333-linux-x64.tar.gz /usr/local
ADD apache-tomcat-8.5.81.tar.gz /usr/local
RUN yum -y install vim
ENV MYPATH /usr/local
WORKDIR $MYPATH
ENV JAVA_HOME /usr/local/jdk1.8.0_333
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-8.5.81
ENV CATALINA_BASH /usr/local/apache-tomcat-8.5.81
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
EXPOSE 8080
CMD /usr/local/apache-tomcat-8.5.81/bin/startup.sh && tail -F /usr/local/apache-tomcat-8.5.81/bin/logs/catalina.out
访问:http://192.168.254.128:9090/
3、发布项目(由于做了卷挂载,我们直接在本地编写项目就可以发布了)
在tomcat的test文件夹下建立文件夹WEB-INF(挂载卷主机地址:/home/build/tomcat/test/) ,在其中创建web.xml
FirstWebFontEnd
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
在test文件夹下(挂载卷主机地址:/home/build/tomcat/test/) 创建index.jsp文件
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
首页
Hello World!
My blog is lxz9.com!
访问页面http://IP地址:9090/test/
待解决问题:中文乱码问题。