Docker Tutorial 01

1. Docker

KuangStudy

Docker Learning Video: Crazy God Says Docker Tutorial

KuangStudy
https://www.kuangstudy.com/course

Docker学习视频(狂神说):
https://www.bilibili.com/video/BV1og4y1q7M4?p=1

阿里云服务器:
https://account.aliyun.com/

Docker官网:
https://www.docker.com/

Docker官网文档:
https://docs.docker.com/engine/install/centos/

Docker运行参考:
https://docs.docker.com/engine/reference/run/

Docker Hub:
https://hub.docker.com/

1642733718478

1. Overview of Docker

1. Why did Docker appear?

One product: development – ​​launch two environments! Application environment, application configuration!

Dev-Ops. Question: I am allowed on my computer! The version is updated, causing the service to be unavailable! Is it very challenging for operation and maintenance?

The environment configuration is very troublesome, and each one must deploy the environment (cluster Redis, ES, Hadoop...)! It takes a lot of time and effort.

Publish a project ( jar + (Redis MySQL JDK ES) ), can the project be installed and packaged with the environment!

Before configuring an application environment on the server Redis MySQL JDK ES Hadoop configuration is super troublesome and cannot be cross-platform.

The development environment Windows, and finally released to Linux!

传统:开发jar,运维来做!

现在:开发打包部署上线,一套流程做完!

安卓流程:java — apk —发布(应用商店)一 张三使用apk一安装即可用!

docker流程: java-jar(环境) — 打包项目帯上环境(镜像) — ( Docker仓库:商店)-----

Docker has proposed a solution to the above problems!
1642422784655

Docker的思想就来自于集装箱!

JRE – 多个应用(端口冲突) – 原来都是交叉的!
隔离:Docker核心思想!打包装箱!每个箱子是互相隔离的。

Docker通过隔离机制,可以将服务器利用到极致!

本质:所有的技术都是因为出现了一些问题,我们需要去解决,才去学习!

2. History of Docker

In 2010, several young people established a company in the United Statesdotcloud

Do some pass cloud computing services! Container technology related to LXC (Linux Container container)!

Linux Container容器是一种内核虚拟化技术,可以提供轻量级的虚拟化,以便隔离进程和资源。

They named their technology (containerization technology) as Docker
when Docker was just born, and it did not attract the attention of the industry! dotCloud, just can't survive!
In 2013, Docker was open sourced!

More and more people are discovering the advantages of docker! Fire. Docker will update a version every month!

On April 9, 2014, Docker 1.0 was released!

Why is docker so popular? Very lightweight!

Before container technology came out, we all used virtual machine technology!

Virtual machine: Install a VMware in the window, through this software we can virtualize one or more computers! bulky!

Virtual machines also belong to virtualization technology, and Docker container technology is also a virtualization technology!

1、vm : linux centos 原生镜像(一个电脑!) 隔离、需要开启多个虚拟机! 几个G 几分钟
2、docker: 隔离,镜像(最核心的环境 4m + jdk + mysql)十分的小巧,运行镜像就可以了!小巧! 几个M 秒级启动!1\

3. Talk about Docker

Docker is developed based on the Go language! Open source project!

Docker official website: https://www.docker.com/

Docker documentation: https://docs.docker.com/

docker repository: https://hub.docker.com/

4. What can Docker do?

1. The previous virtual machine technology!

1642428334662

Disadvantages of virtual machine technology :

1. It takes up a lot of resources

2. Many redundant steps

3. It starts very slowly!

2. Container technology

Containerization is not simulating a complete operating system

1642428377977

Compare the difference between Docker and virtual machine technology:

  • A traditional virtual machine virtualizes a piece of hardware, runs a complete operating system, and then installs and runs software on this system
  • The application in the container runs directly on the content of the host machine. The container does not have its own kernel, nor does it virtualize our hardware, so it is portable
  • Each container is isolated from each other, and each container has its own file system, which does not affect each other

3. DevOps (development, operation and maintenance)

1. Faster delivery and deployment of applications

Traditional: a pair of help documents, installer.

Docker: One-click operation of packaged image release test.

2. More convenient upgrade and expansion

After using Docker, we deploy applications just like building blocks.
The project is packaged as a mirror image, and server A is expanded! Server B

3. Easier system operation and maintenance
After containerization, our development and test environments are highly consistent

4. More efficient utilization of computing resources

Docker is kernel-level virtualization, which can run many container instances on a physical machine! The performance of the server can be squeezed to the extreme.

Two, Docker installation

1. The basic composition of Docker

1642428680454

1. Mirror (image) :

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

2. Container :

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

3. Warehouse (repository) :

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

2. Install Docker

1. Check the environment

#系统内核是3.10以上的
[root@Jin ~]# uname -r
3.10.0-1127.19.1.el7.x86_64

#系统版本
[root@Jin ~]# 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"

2. Install Docker

official website

1. Click Docs in Develpoers to jump to the doc page

1642649222632

2. Click Guides

1642655768584

3. Click Get Docker, and then bring you a few Docker for Linux, jump to the page

1642655824484

4. Click Install on CentOS

1642656032580

5. Uninstall the old version according to the documentation
#1、卸载旧版本
[root@Jin /]# yum remove docker \
>                   docker-client \
>                   docker-client-latest \
>                   docker-common \
>                   docker-latest \
>                   docker-latest-logrotate \
>                   docker-logrotate \
>                   docker-engine
Loaded plugins: fastestmirror
No Match for argument: docker
No Match for argument: docker-client
No Match for argument: docker-client-latest
No Match for argument: docker-common
No Match for argument: docker-latest
No Match for argument: docker-latest-logrotate
No Match for argument: docker-logrotate
No Match for argument: docker-engine
No Packages marked for removal

1642656156858

1642656226008

6. Install the required installation package according to the documentation
#2、安装需要的安装包
[root@Jin /]# yum install -y yum-utils

1642656727305

1642656841325

7. Set up the mirror warehouse according to the documentation
#3、按照文档说明设置镜像仓库(文档上是国外的,可以找阿里云的镜像)
yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo
[root@Jin /]# yum-config-manager \
>     --add-repo \
>     https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

1642657065487

1642657047173

8. According to the latest version of docker engine (docker-ce community edition, docker-ee enterprise edition)
#4、按照之前更新也下索引yum
[root@Jin /]# yum makecache fast
#5、按照docker引擎(docker-ce 社区版,docker-ee企业版)
[root@Jin /]# yum install docker-ce docker-ce-cli containerd.io

1642657358361

1642657506763

9. Start docker
#6、启动docker
[root@Jin /]# systemctl start docker
#7、查看docker版本
[root@Jin /]# docker version

1642657558772

1642657679406

10. Test hello-world
#8、测试hello-world(证明docker下载成功)
[root@Jin /]# docker run hello-world

1642657779270

1642657984385

11. View the hello-world image
#9、查看hello-world镜像
[root@Jin /]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    feb5d9fea6a5   3 months ago   13.3kB

1642658295497

12. Learn about uninstalling docker
#1、停止运行docker
systemctl stop docker
#2、卸载 Docker 引擎、CLI 和 Containerd 软件包:
yum remove docker-ce docker-ce-cli containerd.io
#3、主机上的映像、容器、卷或自定义配置文件不会自动删除。要删除所有映像、容器和卷:
rm -rf /var/lib/docker
rm -rf /var/lib/containerd

1642658395804

3. Aliyun image acceleration

1. Click on Products and Services

1642658738044

2. Find Container Mirroring Service in Elastic Computing in Products and Services, and click

1642658801364

3. Find the mirror accelerator and click CentOS

1642658967933

4. Configuration and use
[root@Jin /]# sudo mkdir -p /etc/docker
[root@Jin /]# sudo tee /etc/docker/daemon.json <<-'EOF'
> {
    
    
>   "registry-mirrors": ["https://k143aj4e.mirror.aliyuncs.com"]
> }
> EOF
{
    
    
  "registry-mirrors": ["https://k143aj4e.mirror.aliyuncs.com"]
}
[root@Jin /]# sudo systemctl daemon-reload
[root@Jin /]# sudo systemctl restart docker
[root@Jin /]# docker run hello-world

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/

1642659267637

4. Review the process of running a mirror image

1642659390554

5. The underlying principle

1. How does Docker work ?

Docker is a system of Client-Server structure, and Docker's daemon process runs on the host. Access from the client through Socket!

Docker-Server will execute this command after receiving the command from Docker-Client!

1642659768943

2. Why Docker is faster than Vm (virtual machine)

1. Docker has fewer abstraction layers than virtual machines. Since docker does not need Hypervisor to realize hardware resource virtualization, the programs running on the docker container directly use the hardware resources of the actual physical machine. Therefore, docker will have obvious advantages in efficiency in terms of CPU and memory utilization.
2. docker uses the kernel of the host machine without the need for a Guest OS.

GuestOS: VM(虚拟机)里的的系统(OS);

HostOS:物理机里的系统(OS);

1642659815347

Therefore, when creating a new container, docker does not need to reload an operating system kernel like a virtual machine. Still avoid the process of booting and loading the operating system kernel, which is time-consuming and resource-consuming. When creating a new virtual machine, the virtual machine software needs to load the GuestOS. This new process is at the minute level. However, docker omits this complicated process because it directly uses the host's operating system, so it only takes a few seconds to create a docker container.

1642660191452

3. Common commands of Docker

1. Help command

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

Help document address: https://docs.docker.com/reference/

1642660503538

2. Mirroring command

docker images #查看所有本地主机上的镜像 可以使用docker image ls代替
docker search #搜索镜像
docker pull   #下载镜像 docker image pull
docker rmi    #删除镜像 docker image rm

1、docker images

[root@Jin /]# docker images --help

Usage:  docker images [OPTIONS] [REPOSITORY[:TAG]]

List images

Options:
  -a, --all             Show all images (default hides intermediate images)
      --digests         Show digests
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print images using a Go template
      --no-trunc        Don't truncate output
  -q, --quiet           Only show image IDs
#查看所有本地主机上的镜像 可以使用docker image ls代替
[root@Jin /]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    feb5d9fea6a5   3 months ago   13.3kB
# 解释
#REPOSITORY			# 镜像的仓库源
#TAG				# 镜像的标签
#IMAGE ID			# 镜像的id
#CREATED			# 镜像的创建时间
#SIZE				# 镜像的大小

[root@Jin /]# docker images -a		#查看docker的所有镜像
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    feb5d9fea6a5   3 months ago   13.3kB
[root@Jin /]# docker images -q		#查看docker的镜像id
feb5d9fea6a5
[root@Jin /]# docker images -aq		#查看docker的所有镜像id
feb5d9fea6a5

2、docker search

[root@Jin /]# docker search --help

Usage:  docker search [OPTIONS] TERM

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
#搜索镜像
[root@Jin /]# docker search mysql
#下方图片

# --filter=STARS=3000 #搜索出来的镜像就是STARS大于3000的
[root@Jin /]# docker search mysql --filter=STARS=3000
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   11995     [OK]       
mariadb   MariaDB Server is a high performing open sou…   4591      [OK]   

1642661551763

3、docker pull

[root@Jin /]# 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)
      --platform string         Set platform if server is multi-platform capable
  -q, --quiet                   Suppress verbose output

#下载镜像 docker image pull
#下载镜像 docker pull 镜像名[:tag]
[root@Jin /]# docker pull mysql
Using default tag: latest				#如果不写tag,默认就是latest
latest: Pulling from library/mysql		 #分层下载: docker image 的核心 联合文件系统
72a69066d2fe: Pull complete 
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 pull mysql:5.7		#等价于		#docker pull docker.io/library/mysql:5.7

#指定版本下载
[root@Jin /]# docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Already exists 	#docker里的高明之处,节省内存,共用
93619dbc5b36: Already exists 
99da31dd6142: Already exists 
626033c43d70: Already exists 
37d5d7efb64e: Already exists 
ac563158d721: Already exists 
d2ba16033dad: Already exists 
0ceb82207cd7: Pull complete 	#docker这里只下载需要的
37f2405cae96: Pull complete 
e2482e017e53: Pull complete 
70deed891d42: Pull complete 
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

1642662913020

4. docker rmi image id

[root@Jin /]# docker rmi --help

Usage:  docker rmi [OPTIONS] IMAGE [IMAGE...]

Remove one or more images

Options:
  -f, --force      Force removal of the image
      --no-prune   Do not delete untagged parents
#删除镜像 docker image rm
docker rmi -f 镜像id 						#删除指定的镜像
docker rmi -f 镜像id 镜像id 镜像id 镜像id	 #删除指定的镜像
docker rmi -f $(docker images -aq) 		   #递归删除全部的镜像

1642663329461

3. Container command

docker run 镜像id 				#新建容器并启动
docker ps 						 #列出所有运行的容器 docker container list
exit						 	#退出容器
docker rm 容器id 					#删除指定容器
docker start 容器id 				#启动容器
docker restart 容器id 			#重启容器
docker stop 容器id 				#停止当前正在运行的容器
docker kill 容器id 				#强制停止当前容器

1. Download a centos to practice, test and learn

[root@Jin /]# 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
docker.io/library/centos:latest

2. docker run image id

#新建容器并启动
[root@Jin /]# docker run --help

Usage:  docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Run a command in a new container

Options:
      --add-host list                  Add a custom host-to-IP mapping (host:ip)
  -a, --attach list                    Attach to STDIN, STDOUT or STDERR
      --blkio-weight uint16            Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
      --blkio-weight-device list       Block IO weight (relative device weight) (default [])
      --cap-add list                   Add Linux capabilities
      --cap-drop list                  Drop Linux capabilities
      --cgroup-parent string           Optional parent cgroup for the container
      --cgroupns string                Cgroup namespace to use (host|private)
                                       'host':    Run the container in the Docker host's cgroup namespace
                                       'private': Run the container in its own private cgroup namespace
                                       '':        Use the cgroup namespace as configured by the
                                                  default-cgroupns-mode option on the daemon (default)
      --cidfile string                 Write the container ID to the file
      --cpu-period int                 Limit CPU CFS (Completely Fair Scheduler) period
      --cpu-quota int                  Limit CPU CFS (Completely Fair Scheduler) quota
      --cpu-rt-period int              Limit CPU real-time period in microseconds
      --cpu-rt-runtime int             Limit CPU real-time runtime in microseconds
  -c, --cpu-shares int                 CPU shares (relative weight)
      --cpus decimal                   Number of CPUs
      --cpuset-cpus string             CPUs in which to allow execution (0-3, 0,1)
      --cpuset-mems string             MEMs in which to allow execution (0-3, 0,1)
  -d, --detach                         Run container in background and print container ID
      --detach-keys string             Override the key sequence for detaching a container
      --device list                    Add a host device to the container
      --device-cgroup-rule list        Add a rule to the cgroup allowed devices list
      --device-read-bps list           Limit read rate (bytes per second) from a device (default [])
      --device-read-iops list          Limit read rate (IO per second) from a device (default [])
      --device-write-bps list          Limit write rate (bytes per second) to a device (default [])
      --device-write-iops list         Limit write rate (IO per second) to a device (default [])
      --disable-content-trust          Skip image verification (default true)
      --dns list                       Set custom DNS servers
      --dns-option list                Set DNS options
      --dns-search list                Set custom DNS search domains
      --domainname string              Container NIS domain name
      --entrypoint string              Overwrite the default ENTRYPOINT of the image
  -e, --env list                       Set environment variables
      --env-file list                  Read in a file of environment variables
      --expose list                    Expose a port or a range of ports
      --gpus gpu-request               GPU devices to add to the container ('all' to pass all GPUs)
      --group-add list                 Add additional groups to join
      --health-cmd string              Command to run to check health
      --health-interval duration       Time between running the check (ms|s|m|h) (default 0s)
      --health-retries int             Consecutive failures needed to report unhealthy
      --health-start-period duration   Start period for the container to initialize before starting health-retries countdown (ms|s|m|h)
                                       (default 0s)
      --health-timeout duration        Maximum time to allow one check to run (ms|s|m|h) (default 0s)
      --help                           Print usage
  -h, --hostname string                Container host name
      --init                           Run an init inside the container that forwards signals and reaps processes
  -i, --interactive                    Keep STDIN open even if not attached
      --ip string                      IPv4 address (e.g., 172.30.100.104)
      --ip6 string                     IPv6 address (e.g., 2001:db8::33)
      --ipc string                     IPC mode to use
      --isolation string               Container isolation technology
      --kernel-memory bytes            Kernel memory limit
  -l, --label list                     Set meta data on a container
      --label-file list                Read in a line delimited file of labels
      --link list                      Add link to another container
      --link-local-ip list             Container IPv4/IPv6 link-local addresses
      --log-driver string              Logging driver for the container
      --log-opt list                   Log driver options
      --mac-address string             Container MAC address (e.g., 92:d0:c6:0a:29:33)
  -m, --memory bytes                   Memory limit
      --memory-reservation bytes       Memory soft limit
      --memory-swap bytes              Swap limit equal to memory plus swap: '-1' to enable unlimited swap
      --memory-swappiness int          Tune container memory swappiness (0 to 100) (default -1)
      --mount mount                    Attach a filesystem mount to the container
      --name string                    Assign a name to the container
      --network network                Connect a container to a network
      --network-alias list             Add network-scoped alias for the container
      --no-healthcheck                 Disable any container-specified HEALTHCHECK
      --oom-kill-disable               Disable OOM Killer
      --oom-score-adj int              Tune host's OOM preferences (-1000 to 1000)
      --pid string                     PID namespace to use
      --pids-limit int                 Tune container pids limit (set -1 for unlimited)
      --platform string                Set platform if server is multi-platform capable
      --privileged                     Give extended privileges to this container
  -p, --publish list                   Publish a container's port(s) to the host
  -P, --publish-all                    Publish all exposed ports to random ports
      --pull string                    Pull image before running ("always"|"missing"|"never") (default "missing")
      --read-only                      Mount the container's root filesystem as read only
      --restart string                 Restart policy to apply when a container exits (default "no")
      --rm                             Automatically remove the container when it exits
      --runtime string                 Runtime to use for this container
      --security-opt list              Security Options
      --shm-size bytes                 Size of /dev/shm
      --sig-proxy                      Proxy received signals to the process (default true)
      --stop-signal string             Signal to stop a container (default "SIGTERM")
      --stop-timeout int               Timeout (in seconds) to stop a container
      --storage-opt list               Storage driver options for the container
      --sysctl map                     Sysctl options (default map[])
      --tmpfs list                     Mount a tmpfs directory
  -t, --tty                            Allocate a pseudo-TTY
      --ulimit ulimit                  Ulimit options (default [])
  -u, --user string                    Username or UID (format: <name|uid>[:<group|gid>])
      --userns string                  User namespace to use
      --uts string                     UTS namespace to use
  -v, --volume list                    Bind mount a volume
      --volume-driver string           Optional volume driver for the container
      --volumes-from list              Mount volumes from the specified container(s)
  -w, --workdir string                 Working directory inside the container
#参书说明
docker run [可选参数] image | docker container run [可选参数] image 
--name="Name"		容器名字 tomcat01 tomcat02 用来区分容器
-d					后台方式运行
-it 				使用交互方式运行,进入容器查看内容
-p(小写)					指定容器的端口 -p 8080(宿主机):8080(容器)
		-p ip:主机端口:容器端口
		-p 主机端口:容器端口(常用)
		-p 容器端口
		容器端口
-P(大写) 				随机指定端口

#测试,启动并进入容器
[root@Jin /]# docker images							#查看docker里的centos镜像
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
centos       latest    5d0da3dc9764   4 months ago   231MB
[root@Jin /]# docker run -it centos /bin/bash		  #使用交互方式运行启动并进入容器
[root@e50fd4a491b5 /]# ls							#查看docker内centos容器的目录(内部命令不完善)
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@e50fd4a491b5 /]# exit							#从docker的centos容器退回主机(退出容器)
exit
[root@Jin /]# ls								   #查看主机目录
bin  boot  dev  etc  home  install.sh  lib  lib64  lost+found  media  mnt  opt  patch  proc  root  run  sbin  srv  sys  tmp  usr  var  www

3、docker ps

[root@Jin /]# docker ps --help

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 container IDs
  -s, --size            Display total file sizes
 #列出所有运行的容器 docker container list
 [root@Jin /]# docker ps			#列出当前正在运行的容器
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@Jin /]# docker ps -a			#列出当前正在运行的容器+带出历史运行过的容器
CONTAINER ID   IMAGE          COMMAND       CREATED         STATUS                     PORTS     NAMES
e50fd4a491b5   centos         "/bin/bash"   6 minutes ago   Exited (0) 3 minutes ago             competent_maxwell
669777e459f5   feb5d9fea6a5   "/hello"      2 hours ago     Exited (0) 2 hours ago               mystifying_murdock
f1acebcbbae2   feb5d9fea6a5   "/hello"      2 hours ago     Exited (0) 2 hours ago               vigorous_williams
[root@Jin /]# docker ps -a -n=1		 #显示最近创建的1个容器
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS                     PORTS     NAMES
e50fd4a491b5   centos    "/bin/bash"   7 minutes ago   Exited (0) 4 minutes ago             competent_maxwell
[root@Jin /]# docker ps -aq			 #只显示容器的编号
e50fd4a491b5
669777e459f5
f1acebcbbae2

#docker ps 命令
		#列出当前正在运行的容器
-a  	#列出当前正在运行的容器+带出历史运行过的容器
-n=?	#显示最近创建的?个容器
-q		#只显示容器的编号

4、exit

exit 		#容器直接退出
ctrl +P +Q 	#容器不停止退出

1642666763512

1642666079604

4, docker rm container id

[root@Jin /]# docker rm --help

Usage:  docker rm [OPTIONS] CONTAINER [CONTAINER...]

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 anonymous volumes associated with the container
#删除指定容器
docker rm 容器id   				 #删除指定的容器,不能删除正在运行的容器,如果要强制删除 rm -f
docker rm -f $(docker ps -aq)  		#删除指定的容器
docker ps -a -q|xargs docker rm  	#删除所有的容器

1642667078296

5. docker start container id

[root@Jin /]# docker start --help

Usage:  docker start [OPTIONS] CONTAINER [CONTAINER...]

Start one or more stopped containers

Options:
  -a, --attach               Attach STDOUT/STDERR and forward signals
      --detach-keys string   Override the key sequence for detaching a container
  -i, --interactive          Attach container's STDIN
#启动容器
[root@Jin /]# docker ps							#列出当前正在运行的容器,为空
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@Jin /]# docker ps -a						#列出当前正在运行的容器+带出历史运行过的容器,历史为空
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@Jin /]# docker run -it centos /bin/bash	  #运行一个centos容器
[root@cdf5f1835e5a /]# exit						#退出centos容器,让它停止运行
exit
[root@Jin /]# docker ps							#查看当前正在运行的容器,为空
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@Jin /]# docker ps -a						#查看当前正在运行的容器+带出历史运行过的容器,历史运行1个
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                      PORTS     NAMES
cdf5f1835e5a   centos    "/bin/bash"   19 seconds ago   Exited (0) 15 seconds ago             nifty_booth
[root@Jin /]# docker start cdf5f1835e5a			  #启动该历史容器
cdf5f1835e5a
[root@Jin /]# docker ps						     #查看当前正在运行的容器,可以看到历史容器已经启动运行
CONTAINER ID   IMAGE     COMMAND       CREATED              STATUS         PORTS     NAMES
cdf5f1835e5a   centos    "/bin/bash"   About a minute ago   Up 6 seconds             nifty_booth
[root@Jin /]# docker ps -a						#查看当前正在运行的容器+带出历史运行过的容器,历史运行1个
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
cdf5f1835e5a   centos    "/bin/bash"   10 minutes ago   Up 34 seconds             nifty_booth

1642667634971

6. docker stop container id

[root@Jin /]# docker stop --help

Usage:  docker stop [OPTIONS] CONTAINER [CONTAINER...]

Stop one or more running containers

Options:
  -t, --time int   Seconds to wait for stop before killing it (default 10)
#停止当前正在运行的容器
[root@Jin /]# docker stop cdf5f1835e5a			#停止当前正在运行的容器
cdf5f1835e5a
[root@Jin /]# docker ps						   #查看当前正在运行的容器,为空
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@Jin /]# docker ps -a					   #查看当前正在运行的容器+带出历史运行过的容器,已成历史
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS                     PORTS     NAMES
cdf5f1835e5a   centos    "/bin/bash"   4 minutes ago   Exited (0) 9 seconds ago             nifty_booth

1642667551126

7. docker restart container id

[root@Jin /]# docker restart --help

Usage:  docker restart [OPTIONS] CONTAINER [CONTAINER...]

Restart one or more containers

Options:
  -t, --time int   Seconds to wait for stop before killing the container (default 10)
#重启容器
[root@Jin /]# docker restart cdf5f1835e5a			#重启当前容器
cdf5f1835e5a
[root@Jin /]# docker ps							  #查看当前正在运行的容器,可以看到历史容器已经启动运行
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS         PORTS     NAMES
cdf5f1835e5a   centos    "/bin/bash"   10 minutes ago   Up 3 seconds             nifty_booth
[root@Jin /]# docker ps -a						#查看当前正在运行的容器+带出历史运行过的容器,历史运行1个
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
cdf5f1835e5a   centos    "/bin/bash"   10 minutes ago   Up 34 seconds             nifty_booth

1642667907695

8, docker kill container id

[root@Jin /]# docker kill --help

Usage:  docker kill [OPTIONS] CONTAINER [CONTAINER...]

Kill one or more running containers

Options:
  -s, --signal string   Signal to send to the container (default "KILL")
#强制停止当前容器
root@Jin /]# docker kill cdf5f1835e5a				#强制停止当前容器
cdf5f1835e5a
[root@Jin /]# docker ps							   #查看当前正在运行的容器,为空
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@Jin /]# docker ps -a						   #查看当前正在运行的容器+带出历史运行过的容器,已成历史
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                       PORTS     NAMES
cdf5f1835e5a   centos    "/bin/bash"   15 minutes ago   Exited (137) 6 seconds ago             nifty_booth

1642668245056

4. Other commonly used commands

1、docker run -d 镜像名				    	   #后台启动容器
2、docker logs 容器id		 			 	    #查看日志
3、docker top 容器id						    #查看容器中进程信息 ps
4、docker inspect 容器id						#查看镜像的元数据
5、docker exec -it 容器id bashShell	  	 	 #进入容器后开启一个新的终端,可以在里面操作(常用)
6、docker attach 容器id				 		#进入容器正在执行的终端,不会启动新的进程
7、docker cp 容器id:容器内路劲 目的的主机路劲		#从容器内拷贝文件到主机上

1. docker run -d image name

#后台启动容器
[root@Jin /]# docker run -d centos			#后台启动centos容器
804ff690c53fa2206cfcc486b76ce375f8d4b9659dbf8d1622babf8521f03bc8
[root@Jin /]# docker ps					   #列出当前正在运行的容器
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@Jin /]# docker ps -a				    #列出当前正在运行的容器+带出历史运行过的容器
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS                     PORTS     NAMES
804ff690c53f   centos    "/bin/bash"   7 seconds ago   Exited (0) 6 seconds ago             amazing_curie
# 问题docker ps. 发现centos 停止了
# 常见的坑,docker容器使用后台运行,就必须要有要一个前台进程,docker发现没有应用,就会自动停止
# nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了

2. docker logs container id

[root@Jin /]# 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:37Z) or relative (e.g. 42m for 42 minutes)
  -n, --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:37Z) or relative (e.g. 42m for 42 minutes)
#查看日志
[root@Jin /]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@Jin /]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                      PORTS     NAMES
804ff690c53f   centos    "/bin/bash"   18 minutes ago   Exited (0) 18 minutes ago             amazing_curie
[root@Jin /]# docker run -d centos /bin/sh -c "while true;do echo Jin;sleep 1;done"
147cf5252b46ed8190faca361e7c60f5c2e07f36931261c2299dcec1ce839496
[root@Jin /]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
147cf5252b46   centos    "/bin/sh -c 'while t…"   4 seconds ago   Up 3 seconds             hopeful_blackburn
[root@Jin /]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS                      PORTS     NAMES
147cf5252b46   centos    "/bin/sh -c 'while t…"   11 seconds ago   Up 10 seconds                         hopeful_blackburn
804ff690c53f   centos    "/bin/bash"              22 minutes ago   Exited (0) 22 minutes ago             amazing_curie
[root@Jin /]# docker logs -tf --tail 10 147cf5252b46
2022-01-20T09:28:12.152771473Z Jin
2022-01-20T09:28:13.154941667Z Jin
2022-01-20T09:28:14.157061463Z Jin
2022-01-20T09:28:15.159186164Z Jin
2022-01-20T09:28:16.161556094Z Jin
2022-01-20T09:28:17.163631824Z Jin
2022-01-20T09:28:18.165699169Z Jin
2022-01-20T09:28:19.167913722Z Jin
2022-01-20T09:28:20.170283854Z Jin
2022-01-20T09:28:21.172387225Z Jin

#显示日志
-tf		#显示日志信息(一直更新)
--tail number #需要显示日志条数
docker logs -t --tail n 容器id #查看n行日志
docker logs -ft 容器id #跟着日志

1642671046636

3. docker top container id

#查看容器中进程信息
[root@Jin /]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
147cf5252b46   centos    "/bin/sh -c 'while t…"   11 minutes ago   Up 11 minutes             hopeful_blackburn
[root@Jin /]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
147cf5252b46   centos    "/bin/sh -c 'while t…"   11 minutes ago   Up 11 minutes             hopeful_blackburn
[root@Jin /]# docker top 147cf5252b46
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                28423               28405               0                   17:27               ?                   00:00:00            /bin/sh -c while true;do echo Jin;sleep 1;done
root                30493               28423               0                   17:39               ?                   00:00:00            /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1

1642671590069

4, docker inspect container id

#查看镜像的元数据
[root@Jin /]# 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
#查看镜像的元数据
[root@Jin /]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
147cf5252b46   centos    "/bin/sh -c 'while t…"   33 minutes ago   Up 33 minutes             hopeful_blackburn
[root@Jin /]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
147cf5252b46   centos    "/bin/sh -c 'while t…"   33 minutes ago   Up 33 minutes             hopeful_blackburn
[root@Jin /]# docker inspect 147cf5252b46
[
    {
    
    
        "Id": "147cf5252b46ed8190faca361e7c60f5c2e07f36931261c2299dcec1ce839496",
        "Created": "2022-01-20T09:27:41.742911618Z",
        "Path": "/bin/sh",
        "Args": [
            "-c",
            "while true;do echo Jin;sleep 1;done"
        ],
        "State": {
    
    
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 28423,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2022-01-20T09:27:42.086397147Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
        "ResolvConfPath": "/var/lib/docker/containers/147cf5252b46ed8190faca361e7c60f5c2e07f36931261c2299dcec1ce839496/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/147cf5252b46ed8190faca361e7c60f5c2e07f36931261c2299dcec1ce839496/hostname",
        "HostsPath": "/var/lib/docker/containers/147cf5252b46ed8190faca361e7c60f5c2e07f36931261c2299dcec1ce839496/hosts",
        "LogPath": "/var/lib/docker/containers/147cf5252b46ed8190faca361e7c60f5c2e07f36931261c2299dcec1ce839496/147cf5252b46ed8190faca361e7c60f5c2e07f36931261c2299dcec1ce839496-json.log",
        "Name": "/hopeful_blackburn",
        "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/ab96a59f4d2ff35c697f844ba02421b67fb39aa8d371b9092bc0ae2d021d86c5-init/diff:/var/lib/docker/overlay2/062d86c5e366ca0ececd1c38ff44dee264283b594189692994d190dd67fee589/diff",
                "MergedDir": "/var/lib/docker/overlay2/ab96a59f4d2ff35c697f844ba02421b67fb39aa8d371b9092bc0ae2d021d86c5/merged",
                "UpperDir": "/var/lib/docker/overlay2/ab96a59f4d2ff35c697f844ba02421b67fb39aa8d371b9092bc0ae2d021d86c5/diff",
                "WorkDir": "/var/lib/docker/overlay2/ab96a59f4d2ff35c697f844ba02421b67fb39aa8d371b9092bc0ae2d021d86c5/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
    
    
            "Hostname": "147cf5252b46",
            "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 Jin;sleep 1;done"
            ],
            "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": "abd457c1d26b17b9ed9dc337dc51332ddcf8040730c279e9a74a1a965b476204",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
    
    },
            "SandboxKey": "/var/run/docker/netns/abd457c1d26b",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "236ee46e9c4e2db362215c37fcaee68912266fb73b4553098c6f35e104c01775",
            "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": "48b70a1d71f00b2fc81e5cc7552d7baf620ad160932904552f0e5df1c62caaa1",
                    "EndpointID": "236ee46e9c4e2db362215c37fcaee68912266fb73b4553098c6f35e104c01775",
                    "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
                }
            }
        }
    }
]

5, docker exec -it container id bashShell

#进入当前正在运行的容器
#进入容器后开启一个新的终端,可以在里面操作(常用)
[root@Jin /]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED        STATUS        PORTS     NAMES
147cf5252b46   centos    "/bin/sh -c 'while t…"   17 hours ago   Up 17 hours             hopeful_blackburn
[root@Jin /]# docker exec -it 147cf5252b46 /bin/bash		#进入当前正在运行的容器
[root@147cf5252b46 /]# ps -ef							  #
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 Jan20 ?        00:00:20 /bin/sh -c while true;do echo Jin;sleep 1;done
root     27421     0  0 02:08 pts/0    00:00:00 /bin/bash
root     27444     1  0 02:08 ?        00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
root     27445 27421  0 02:08 pts/0    00:00:00 ps -ef
[root@147cf5252b46 /]# exit
exit
[root@Jin /]#

6, docker attach container id

#进入当前正在运行的容器
#进入容器正在执行的终端,不会启动新的进程
[root@Jin /]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED        STATUS        PORTS     NAMES
147cf5252b46   centos    "/bin/sh -c 'while t…"   17 hours ago   Up 17 hours             hopeful_blackburn
[root@Jin /]# docker attach 147cf5252b46
Jin
Jin
Jin
Jin
Jin

7. docker cp container id: the host path of the path destination in the container

#从容器内拷贝文件到主机上
[root@Jin /]# cd home							#查看root下的/home目录下文件
[root@Jin home]# ls
Jin  JinTest  Jin.txt  redis  test  www
[root@Jin home]# cd /
[root@Jin /]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@Jin /]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@Jin /]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
centos       latest    5d0da3dc9764   4 months ago   231MB
[root@Jin /]# docker run -it centos /bin/bash		#交互运行centos镜像,并进入该容器
[root@25a7baa32d3b /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@25a7baa32d3b /]# cd /home
[root@25a7baa32d3b home]# ls
[root@25a7baa32d3b home]# touch copy.java			#在容器的/home目录下创建一个copu.java文件
[root@25a7baa32d3b home]# ls
copy.java
[root@25a7baa32d3b home]# exit						#exit退出容器,容器停止运行
exit
[root@Jin /]# docker ps								
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@Jin /]# docker ps -a							#但只要容器还在(历史上,没有被rm -f),数据就还在
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                      PORTS     NAMES
25a7baa32d3b   centos    "/bin/bash"   55 seconds ago   Exited (0) 14 seconds ago             dazzling_thompson
[root@Jin /]# docker cp 25a7baa32d3b:/home/copy.java /home   #将容器/home下的copy.java文件copy到/home下
[root@Jin /]# ls
bin  boot  dev  etc  home  install.sh  lib  lib64  lost+found  media  mnt  opt  patch  proc  root  run  sbin  srv  sys  tmp  usr  var  www
[root@Jin /]# cd /home
[root@Jin home]# ls
copy.java  Jin  JinTest  Jin.txt  redis  test  www

1642732791465

V. Summary

[root@Jin /]# docker --help

Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default "/root/.docker")
  -c, --context string     Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set
                           with "docker context use")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket(s) to connect to
  -l, --log-level string   Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default "/root/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/root/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/root/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

Management Commands:
  app*        Docker App (Docker Inc., v0.9.1-beta3)
  builder     Manage builds
  buildx*     Docker Buildx (Docker Inc., v0.7.1-docker)
  config      Manage Docker configs
  container   Manage containers
  context     Manage contexts
  image       Manage images
  manifest    Manage Docker image manifests and manifest lists
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  scan*       Docker Scan (Docker Inc., v0.12.0)
  secret      Manage Docker secrets
  service     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  build       Build an image from a 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
  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
  history     Show the history of an image
  images      List images
  import      Import the contents from a tarball to create a filesystem image
  info        Display system-wide information
  inspect     Return low-level information on Docker objects
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  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

Run 'docker COMMAND --help' for more information on a command.

1642733718478

1642734152727

1642734193751

Fourth, docker deployment application

Dockerhub address: https://hub.docker.com/

1. The server adds a security group development port:

1642742523706

2. Pagoda panel release port

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-xqASnPMe-1679562589551) (https://gitee.com/happy_sad/typora-image/raw/master/images /1642742542354.png)]

1. Deploy Nginx

1. Xshell executes deployment and startup

# -d 		后台运行
# --name	给容器命名
# -p		宿主机端口:
[root@Jin /]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
centos       latest    5d0da3dc9764   4 months ago   231MB
[root@Jin /]# docker search nginx
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
nginx                             Official build of Nginx.                        16161     [OK]       
jwilder/nginx-proxy               Automated Nginx reverse proxy for docker con…   2109                 [OK]
richarvey/nginx-php-fpm           Container running Nginx + PHP-FPM capable of…   821                  [OK]
jc21/nginx-proxy-manager          Docker container for managing Nginx proxy ho…   314                  
linuxserver/nginx                 An Nginx container, brought to you by LinuxS…   160                  
tiangolo/nginx-rtmp               Docker image with Nginx using the nginx-rtmp…   151                  [OK]
jlesage/nginx-proxy-manager       Docker container for Nginx Proxy Manager        150                  [OK]
alfg/nginx-rtmp                   NGINX, nginx-rtmp-module and FFmpeg from sou…   114                  [OK]
nginxdemos/hello                  NGINX webserver that serves a simple page co…   82                   [OK]
privatebin/nginx-fpm-alpine       PrivateBin running on an Nginx, php-fpm & Al…   62                   [OK]
nginx/nginx-ingress               NGINX and  NGINX Plus Ingress Controllers fo…   59                   
nginxinc/nginx-unprivileged       Unprivileged NGINX Dockerfiles                  57                   
nginxproxy/nginx-proxy            Automated Nginx reverse proxy for docker con…   34                   
staticfloat/nginx-certbot         Opinionated setup for automatic TLS certs lo…   25                   [OK]
nginx/nginx-prometheus-exporter   NGINX Prometheus Exporter for NGINX and NGIN…   23                   
schmunk42/nginx-redirect          A very simple container to redirect HTTP tra…   19                   [OK]
centos/nginx-112-centos7          Platform for running nginx 1.12 or building …   16                   
centos/nginx-18-centos7           Platform for running nginx 1.8 or building n…   13                   
flashspys/nginx-static            Super Lightweight Nginx Image                   12                   [OK]
bitwarden/nginx                   The Bitwarden nginx web server acting as a r…   12                   
mailu/nginx                       Mailu nginx frontend                            10                   [OK]
sophos/nginx-vts-exporter         Simple server that scrapes Nginx vts stats a…   7                    [OK]
ansibleplaybookbundle/nginx-apb   An APB to deploy NGINX                          3                    [OK]
wodby/nginx                       Generic nginx                                   1                    [OK]
arnau/nginx-gate                  Docker image with Nginx with Lua enabled on …   1                    [OK]
[root@Jin /]# 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
docker.io/library/nginx:latest
[root@Jin /]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
nginx        latest    605c77e624dd   3 weeks ago    141MB
centos       latest    5d0da3dc9764   4 months ago   231MB
[root@Jin /]# docker ps 
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@Jin /]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@Jin /]# docker run -d --name nginx01 -p 39000:80 nginx
17faa6fdbd46c73ec29cc88aa9a34967ac2224c75a66b79e1f55303aa1d61af2
[root@Jin /]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS         PORTS                                     NAMES
17faa6fdbd46   nginx     "/docker-entrypoint.…"   10 seconds ago   Up 9 seconds   0.0.0.0:39000->80/tcp, :::39000->80/tcp   nginx01
[root@Jin /]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                                     NAMES
17faa6fdbd46   nginx     "/docker-entrypoint.…"   14 seconds ago   Up 13 seconds   0.0.0.0:39000->80/tcp, :::39000->80/tcp   nginx01
[root@Jin /]# curl localhost:39000
<!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>

1642743220012

2. -p port mapping

1642743467481

3. View the running effect

1642743544122

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-XC08S8IE-1679562589553) (https://gitee.com/happy_sad/typora-image/raw/master/images /1642743588404.png)]

4. Enter and stop the container

[root@Jin /]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                                     NAMES
17faa6fdbd46   nginx     "/docker-entrypoint.…"   31 minutes ago   Up 31 minutes   0.0.0.0:39000->80/tcp, :::39000->80/tcp   nginx01
[root@Jin /]# docker exec -it nginx01 /bin/bash
root@17faa6fdbd46:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@17faa6fdbd46:/# cd /etc/nginx
root@17faa6fdbd46:/etc/nginx# ls
conf.d	fastcgi_params	mime.types  modules  nginx.conf  scgi_params  uwsgi_params
root@17faa6fdbd46:/etc/nginx# exit
exit
[root@Jin /]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                                     NAMES
17faa6fdbd46   nginx     "/docker-entrypoint.…"   33 minutes ago   Up 33 minutes   0.0.0.0:39000->80/tcp, :::39000->80/tcp   nginx01
[root@Jin /]# docker stop 17faa6fdbd46
17faa6fdbd46
[root@Jin /]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@Jin /]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS                      PORTS     NAMES
17faa6fdbd46   nginx     "/docker-entrypoint.…"   34 minutes ago   Exited (0) 26 seconds ago             nginx01

1642745070401

2. Deploy Tomcat

1. Xshell executes deployment and startup

[root@Jin /]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
nginx        latest    605c77e624dd   3 weeks ago    141MB
centos       latest    5d0da3dc9764   4 months ago   231MB
[root@Jin /]# docker search tomcat
NAME                          DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
tomcat                        Apache Tomcat is an open source implementati…   3232      [OK]       
tomee                         Apache TomEE is an all-Apache Java EE certif…   94        [OK]       
dordoka/tomcat                Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 base…   57                   [OK]
kubeguide/tomcat-app          Tomcat image for Chapter 1                      33                   
consol/tomcat-7.0             Tomcat 7.0.57, 8080, "admin/admin"              18                   [OK]
cloudesire/tomcat             Tomcat server, 6/7/8                            15                   [OK]
aallam/tomcat-mysql           Debian, Oracle JDK, Tomcat & MySQL              12                   [OK]
arm32v7/tomcat                Apache Tomcat is an open source implementati…   11                   
andreptb/tomcat               Debian Jessie based image with Apache Tomcat…   10                   [OK]
rightctrl/tomcat              CentOS , Oracle Java, tomcat application ssl…   7                    [OK]
arm64v8/tomcat                Apache Tomcat is an open source implementati…   7                    
unidata/tomcat-docker         Security-hardened Tomcat Docker container.      5                    [OK]
jelastic/tomcat               An image of the Tomcat Java application serv…   3                    
amd64/tomcat                  Apache Tomcat is an open source implementati…   3                    
fabric8/tomcat-8              Fabric8 Tomcat 8 Image                          2                    [OK]
cfje/tomcat-resource          Tomcat Concourse Resource                       2                    
oobsri/tomcat8                Testing CI Jobs with different names.           2                    
camptocamp/tomcat-logback     Docker image for tomcat with logback integra…   1                    [OK]
picoded/tomcat7               tomcat7 with jre8 and MANAGER_USER / MANAGER…   1                    [OK]
ppc64le/tomcat                Apache Tomcat is an open source implementati…   1                    
99taxis/tomcat7               Tomcat7                                         1                    [OK]
chenyufeng/tomcat-centos      tomcat基于centos6的镜像                              1                    [OK]
secoresearch/tomcat-varnish   Tomcat and Varnish 5.0                          0                    [OK]
s390x/tomcat                  Apache Tomcat is an open source implementati…   0                    
softwareplant/tomcat          Tomcat images for jira-cloud testing            0                    [OK]
[root@Jin /]# docker pull tomcat
Using default tag: latest
latest: Pulling from library/tomcat
0e29546d541c: Pull complete 
9b829c73b52b: Pull complete 
cb5b7ae36172: Pull complete 
6494e4811622: Pull complete 
668f6fcc5fa5: Pull complete 
dc120c3e0290: Pull complete 
8f7c0eebb7b1: Pull complete 
77b694f83996: Pull complete 
0f611256ec3a: Pull complete 
4f25def12f23: Pull complete 
Digest: sha256:9dee185c3b161cdfede1f5e35e8b56ebc9de88ed3a79526939701f3537a52324
Status: Downloaded newer image for tomcat:latest
docker.io/library/tomcat:latest
[root@Jin /]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
nginx        latest    605c77e624dd   3 weeks ago    141MB
tomcat       latest    fb5657adc892   4 weeks ago    680MB
centos       latest    5d0da3dc9764   4 months ago   231MB
[root@Jin /]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@Jin /]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED             STATUS                      PORTS     NAMES
17faa6fdbd46   nginx     "/docker-entrypoint.…"   About an hour ago   Exited (0) 33 minutes ago             nginx01
[root@Jin /]# docker run -d --name tomcat01 -p 39001:8080 tomcat
d5845263bcba21bafc7e1969db91bd9e6a6c96df5a5e4d1bf294f72d7bd176f6
[root@Jin /]# docker ps
CONTAINER ID   IMAGE     COMMAND             CREATED         STATUS         PORTS                                         NAMES
d5845263bcba   tomcat    "catalina.sh run"   4 seconds ago   Up 3 seconds   0.0.0.0:39001->8080/tcp, :::39001->8080/tcp   tomcat01
[root@Jin /]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED             STATUS                      PORTS                                         NAMES
d5845263bcba   tomcat    "catalina.sh run"        7 seconds ago       Up 6 seconds                0.0.0.0:39001->8080/tcp, :::39001->8080/tcp   tomcat01
17faa6fdbd46   nginx     "/docker-entrypoint.…"   About an hour ago   Exited (0) 38 minutes ago                                                 nginx01
[root@Jin /]# curl localhost:39001
<!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">body {
    
    font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {
    
    color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 – Not Found</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p><hr class="line" /><h3>Apache Tomcat/10.0.14</h3></body></html>[root@Jin /]# 

1642747702600

2. Deal with the problem

[root@Jin /]# docker exec -it tomcat01 /bin/bash
root@d5845263bcba:/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@d5845263bcba:/usr/local/tomcat# cd webapps
root@d5845263bcba:/usr/local/tomcat/webapps# ls
root@d5845263bcba:/usr/local/tomcat/webapps# cd ..
root@d5845263bcba:/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@d5845263bcba:/usr/local/tomcat# cd webapps.dist
root@d5845263bcba:/usr/local/tomcat/webapps.dist# ls
ROOT  docs  examples  host-manager  manager
root@d5845263bcba:/usr/local/tomcat/webapps.dist# cd ..
root@d5845263bcba:/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@d5845263bcba:/usr/local/tomcat# cp -r webapps.dist/* webapps
root@d5845263bcba:/usr/local/tomcat# cd webapps
root@d5845263bcba:/usr/local/tomcat/webapps# ls
ROOT  docs  examples  host-manager  manager
root@d5845263bcba:/usr/local/tomcat/webapps# exit
exit
[root@Jin /]# docker ps
CONTAINER ID   IMAGE     COMMAND             CREATED          STATUS         PORTS                                         NAMES
d5845263bcba   tomcat    "catalina.sh run"   49 minutes ago   Up 2 seconds   0.0.0.0:39001->8080/tcp, :::39001->8080/tcp   tomcat01
[root@Jin /]# docker stop d5845263bcba
d5845263bcba
[root@Jin /]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@Jin /]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS                         PORTS     NAMES
d5845263bcba   tomcat    "catalina.sh run"        50 minutes ago   Exited (143) 9 seconds ago               tomcat01
17faa6fdbd46   nginx     "/docker-entrypoint.…"   2 hours ago      Exited (0) About an hour ago             nginx01

1642749782557

3. View the running results

1642749482273

3. Deploy ES and kibana

1. Xshell executes deployment and startup

# 启动 elasticsearch
[root@Jin /]# docker run -d --name elasticsearch01 -p 39002:9200 -p 39003:9300 -e "discovery.type=single-node" elasticsearch:7.6.2
Unable to find image 'elasticsearch:7.6.2' locally
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
6b475296fdf0543cd8d5327fbc9fb79dc15e72d02e8dde507e10fff18785e866

[root@Jin /]# docker ps
[root@Jin /]# docker ps -a
# 查看内存占用情况
[root@Jin /]# docker stats
[root@Jin /]# docker stop es的容器id
[root@Jin /]# docker ps
[root@Jin /]# docker ps -a

1642750800016

[root@Jin /]# docker images
REPOSITORY      TAG       IMAGE ID       CREATED         SIZE
nginx           latest    605c77e624dd   3 weeks ago     141MB
tomcat          latest    fb5657adc892   4 weeks ago     680MB
centos          latest    5d0da3dc9764   4 months ago    231MB
elasticsearch   7.6.2     f29a1ee41030   22 months ago   791MB

1642753466834

2. Optimization method

[root@Jin /]# docker run -d --name elasticsearch02 -p 39001:9200 -p 39002:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xmx512m" elasticsearch:7.6.2
90cb39473aff8f9a57557e05f6fa210afbad48b3782e22e29e1be7e804063e0a
[root@Jin /]# docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED         STATUS         PORTS                                                                                      NAMES
90cb39473aff   elasticsearch:7.6.2   "/usr/local/bin/dock…"   6 seconds ago   Up 5 seconds   0.0.0.0:39001->9200/tcp, :::39001->9200/tcp, 0.0.0.0:39002->9300/tcp, :::39002->9300/tcp   elasticsearch02
[root@Jin /]# docker ps -a
CONTAINER ID   IMAGE                 COMMAND                  CREATED          STATUS                        PORTS                                                                                      NAMES
90cb39473aff   elasticsearch:7.6.2   "/usr/local/bin/dock…"   10 seconds ago   Up 10 seconds                 0.0.0.0:39001->9200/tcp, :::39001->9200/tcp, 0.0.0.0:39002->9300/tcp, :::39002->9300/tcp   elasticsearch02
cf8ebbd2dd64   feb5d9fea6a5          "/hello"                 7 minutes ago    Exited (0) 7 minutes ago                                                                                                 ecstatic_ramanujan
6b475296fdf0   elasticsearch:7.6.2   "/usr/local/bin/dock…"   50 minutes ago   Exited (255) 8 minutes ago    0.0.0.0:39002->9200/tcp, :::39002->9200/tcp, 0.0.0.0:39003->9300/tcp, :::39003->9300/tcp   elasticsearch01
d5845263bcba   tomcat                "catalina.sh run"        2 hours ago      Exited (143) 57 minutes ago                                                                                              tomcat01
17faa6fdbd46   nginx                 "/docker-entrypoint.…"   3 hours ago      Exited (0) 2 hours ago                                                                                                   nginx01
[root@Jin /]# docker stats

CONTAINER ID   NAME              CPU %     MEM USAGE / LIMIT     MEM %     NET I/O     BLOCK I/O       PIDS
90cb39473aff   elasticsearch02   2.91%     348.2MiB / 1.795GiB   18.95%    656B / 0B   177MB / 246kB   23

CONTAINER ID   NAME              CPU %     MEM USAGE / LIMIT     MEM %     NET I/O     BLOCK I/O       PIDS
90cb39473aff   elasticsearch02   98.91%    355.1MiB / 1.795GiB   19.32%    656B / 0B   180MB / 246kB   26

CONTAINER ID   NAME              CPU %     MEM USAGE / LIMIT     MEM %     NET I/O     BLOCK I/O       PIDS
90cb39473aff   elasticsearch02   98.91%    355.1MiB / 1.795GiB   19.32%    656B / 0B   180MB / 246kB   26

CONTAINER ID   NAME              CPU %     MEM USAGE / LIMIT     MEM %     NET I/O     BLOCK I/O       PIDS
90cb39473aff   elasticsearch02   98.91%    355.1MiB / 1.795GiB   19.32%    656B / 0B   180MB / 246kB   26

CONTAINER ID   NAME              CPU %     MEM USAGE / LIMIT     MEM %     NET I/O     BLOCK I/O       PIDS
90cb39473aff   elasticsearch02   96.20%    364.1MiB / 1.795GiB   19.81%    656B / 0B   181MB / 246kB   26

CONTAINER ID   NAME              CPU %     MEM USAGE / LIMIT     MEM %     NET I/O     BLOCK I/O       PIDS
90cb39473aff   elasticsearch02   96.20%    364.1MiB / 1.795GiB   19.81%    656B / 0B   181MB / 246kB   26

CONTAINER ID   NAME              CPU %     MEM USAGE / LIMIT   MEM %     NET I/O     BLOCK I/O       PIDS
90cb39473aff   elasticsearch02   97.76%    373MiB / 1.795GiB   20.30%    656B / 0B   183MB / 246kB   26
^C
[root@Jin /]# docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED          STATUS          PORTS                                                                                      NAMES
90cb39473aff   elasticsearch:7.6.2   "/usr/local/bin/dock…"   46 seconds ago   Up 45 seconds   0.0.0.0:39001->9200/tcp, :::39001->9200/tcp, 0.0.0.0:39002->9300/tcp, :::39002->9300/tcp   elasticsearch02
[root@Jin /]# docker stop 90cb39473aff
90cb39473aff
[root@Jin /]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@Jin /]# docker images
REPOSITORY      TAG       IMAGE ID       CREATED         SIZE
nginx           latest    605c77e624dd   3 weeks ago     141MB
tomcat          latest    fb5657adc892   4 weeks ago     680MB
centos          latest    5d0da3dc9764   4 months ago    231MB
elasticsearch   7.6.2     f29a1ee41030   22 months ago   791MB
[root@Jin /]# 

1642753960612

5. Visualization panel

1. Deploy the visualization panel

docker run -d -p 39000:9000 --restart=always -v /var/run/docker.sock:/var/run/docker --privileged=true portainer/portainer
[root@Jin /]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
nginx                 latest    605c77e624dd   3 weeks ago     141MB
tomcat                latest    fb5657adc892   4 weeks ago     680MB
centos                latest    5d0da3dc9764   4 months ago    231MB
portainer/portainer   latest    580c0e4e98b0   10 months ago   79.1MB
elasticsearch         7.6.2     f29a1ee41030   22 months ago   791MB
[root@Jin /]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@Jin /]# docker ps -a
CONTAINER ID   IMAGE                 COMMAND                  CREATED          STATUS                        PORTS                                                                                      NAMES
90cb39473aff   elasticsearch:7.6.2   "/usr/local/bin/dock…"   44 minutes ago   Exited (143) 43 minutes ago                                                                                              elasticsearch02
6b475296fdf0   elasticsearch:7.6.2   "/usr/local/bin/dock…"   2 hours ago      Exited (255) 53 minutes ago   0.0.0.0:39002->9200/tcp, :::39002->9200/tcp, 0.0.0.0:39003->9300/tcp, :::39003->9300/tcp   elasticsearch01
d5845263bcba   tomcat                "catalina.sh run"        3 hours ago      Exited (143) 2 hours ago                                                                                                 tomcat01
17faa6fdbd46   nginx                 "/docker-entrypoint.…"   4 hours ago      Exited (0) 3 hours ago                                                                                                   nginx01
[root@Jin /]# docker run -d -p 39000:9000 --restart=always -v /var/run/docker.sock:/var/run/docker --privileged=true portainer/portainer
492115c699358c0d9dcf1bbf4dcd11ae9ad43842777a41bbecd3f2138bd2afac
[root@Jin /]# docker ps
CONTAINER ID   IMAGE                 COMMAND        CREATED          STATUS          PORTS                                         NAMES
492115c69935   portainer/portainer   "/portainer"   17 seconds ago   Up 16 seconds   0.0.0.0:39000->9000/tcp, :::39000->9000/tcp   charming_mclean
[root@Jin /]# docker ps -a
CONTAINER ID   IMAGE                 COMMAND                  CREATED          STATUS                        PORTS                                                                                      NAMES
492115c69935   portainer/portainer   "/portainer"             21 seconds ago   Up 20 seconds                 0.0.0.0:39000->9000/tcp, :::39000->9000/tcp                                                charming_mclean
90cb39473aff   elasticsearch:7.6.2   "/usr/local/bin/dock…"   45 minutes ago   Exited (143) 44 minutes ago                                                                                              elasticsearch02
6b475296fdf0   elasticsearch:7.6.2   "/usr/local/bin/dock…"   2 hours ago      Exited (255) 53 minutes ago   0.0.0.0:39002->9200/tcp, :::39002->9200/tcp, 0.0.0.0:39003->9300/tcp, :::39003->9300/tcp   elasticsearch01
d5845263bcba   tomcat                "catalina.sh run"        3 hours ago      Exited (143) 2 hours ago                                                                                                 tomcat01
17faa6fdbd46   nginx                 "/docker-entrypoint.…"   4 hours ago      Exited (0) 3 hours ago                                                                                                   nginx01

1642756596754

2. Page display

username:admin
password:123456789
confirm password:123456789

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-dcUT6pGA-1679562589556) (https://gitee.com/happy_sad/typora-image/raw/master/images /1642756721109.png)]

1642756801427

6. Docker image

1. What is a mirror image

A mirror is a lightweight, executable independent software package, used to package the software operating environment and software developed based on the operating environment. It contains everything needed to run a certain software, including code, runtime libraries, and environment variables. and configuration file

2. Docker image loading principle

1. UnionFS joint file system

UnionFs(联合文件系统):Union文件系统(UnionFs)是一种分层、轻量级并且高性能的文件系统,他支持对文件系统的修改作为一次提交来一层层的叠加,同时可以将不同目录挂载到同一个虚拟文件系统下( unite several directories into a single virtual filesystem)。Union文件系统是 Docker镜像的基础。镜像可以通过分层来进行继承,基于基础镜像(没有父镜像),可以制作各种具体的应用镜像
特性:一次同时加载多个文件系统,但从外面看起来,只能看到一个文件系统,联合加载会把各层文件系统叠加起来,这样最终的文件系统会包含所有底层的文件和目录

2. Docker image loading principle

docker的镜像实际上由一层一层的文件系统组成,这种层级的文件系统UnionFS。
boots(boot file system)主要包含 bootloader和 Kernel, bootloader主要是引导加 kernel, Linux刚启动时会加bootfs文件系统,在 Docker镜像的最底层是 boots。这一层与我们典型的Linux/Unix系统是一样的,包含boot加載器和内核。当boot加载完成之后整个内核就都在内存中了,此时内存的使用权已由 bootfs转交给内核,此时系统也会卸载bootfs。
rootfs(root file system),在 bootfs之上。包含的就是典型 Linux系统中的/dev,/proc,/bin,/etc等标准目录和文件。 rootfs就是各种不同的操作系统发行版,比如 Ubuntu, Centos等等。

1642757221038

3. Usually, the CentOS we install into the virtual machine is several G, why is it only 200M here for Docker?

1642757555818

For a streamlined OS, the rootfs can be very small. It only needs to include the most basic commands, tools and program libraries. Because the underlying layer directly uses the host's kernel, you only need to provide the rootfs. It can be seen that for different Linux distributions, the boots are basically the same, but the rootfs will be different, so different distributions can share the bootfs.

The virtual machine is at the minute level, and the container is at the second level

3. Hierarchical understanding

1. Why does the Docker image adopt this layered structure?

  1. The biggest benefit, I think, is resource sharing! For example, if multiple images are built from the same Base image, then the host only needs to keep one base image on disk, and only one base image needs to be loaded in memory, so that all containers can be served , and each layer of the image can be shared.
  2. You can view the image layering method through the docker image inspect command

2. Download a redis image

Pay attention to the log output of the download, you can see that it is downloading layer by layer

[root@Jin /]# docker search redis
NAME                             DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
redis                            Redis is an open source key-value store that…   10459     [OK]       
grokzen/redis-cluster            Redis cluster 3.0, 3.2, 4.0, 5.0, 6.0, 6.2      83                   
sameersbn/redis                                                                  83                   [OK]
rediscommander/redis-commander   Alpine image for redis-commander - Redis man…   73                   [OK]
redislabs/redisearch             Redis With the RedisSearch module pre-loaded…   48                   
redislabs/rejson                 RedisJSON - Enhanced JSON data type processi…   40                   
redislabs/redisinsight           RedisInsight - The GUI for Redis                35                   
redislabs/redis                  Clustered in-memory database engine compatib…   32                   
oliver006/redis_exporter          Prometheus Exporter for Redis Metrics. Supp…   31                   
arm32v7/redis                    Redis is an open source key-value store that…   24                   
arm64v8/redis                    Redis is an open source key-value store that…   19                   
redislabs/rebloom                A probablistic datatypes module for Redis       18                   [OK]
redislabs/redisgraph             A graph database module for Redis               17                   [OK]
redislabs/redismod               An automated build of redismod - latest Redi…   17                   [OK]
webhippie/redis                  Docker image for redis                          11                   [OK]
s7anley/redis-sentinel-docker    Redis Sentinel                                  10                   [OK]
redislabs/redistimeseries        A time series database module for Redis         10                   
insready/redis-stat              Docker image for the real-time Redis monitor…   10                   [OK]
goodsmileduck/redis-cli          redis-cli on alpine                             9                    [OK]
centos/redis-32-centos7          Redis in-memory data structure store, used a…   6                    
clearlinux/redis                 Redis key-value data structure server with t…   3                    
tiredofit/redis                  Redis Server w/ Zabbix monitoring and S6 Ove…   1                    [OK]
wodby/redis                      Redis container image with orchestration        1                    [OK]
xetamus/redis-resource           forked redis-resource                           0                    [OK]
flant/redis-sentinel-proxy       Redis sentinel proxy by enriclluelles writte…   0                    [OK]
[root@Jin /]# docker pull redis
Using default tag: latest
latest: Pulling from library/redis
a2abf6c4d29d: Already exists 
c7a4e4382001: Pull complete 
4044b9ba67c9: Pull complete 
c8388a79482f: Pull complete 
413c8bb60be2: Pull complete 
1abfd3011519: Pull complete 
Digest: sha256:db485f2e245b5b3329fdc7eff4eb00f913e09d8feb9ca720788059fdc2ed8339
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest
[root@Jin /]# docker image inspect redis
[
    {
    
    
        "Id": "sha256:7614ae9453d1d87e740a2056257a6de7135c84037c367e1fffa92ae922784631",
        "RepoTags": [
            "redis:latest"
        ],
        "RepoDigests": [
            "redis@sha256:db485f2e245b5b3329fdc7eff4eb00f913e09d8feb9ca720788059fdc2ed8339"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2021-12-21T12:42:49.755107412Z",
        "Container": "13d25f53410417c5220c8dfe8bd49f06abdbcd69faa62a9b877de02464bb04a3",
        "ContainerConfig": {
    
    
            "Hostname": "13d25f534104",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
    
    
                "6379/tcp": {
    
    }
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "GOSU_VERSION=1.12",
                "REDIS_VERSION=6.2.6",
                "REDIS_DOWNLOAD_URL=http://download.redis.io/releases/redis-6.2.6.tar.gz",
                "REDIS_DOWNLOAD_SHA=5b2b8b7a50111ef395bf1c1d5be11e6e167ac018125055daa8b5c2317ae131ab"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"redis-server\"]"
            ],
            "Image": "sha256:e093f59d716c95cfce82c676f099b960cc700432ab531388fcedf79932fc81ec",
            "Volumes": {
    
    
                "/data": {
    
    }
            },
            "WorkingDir": "/data",
            "Entrypoint": [
                "docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {
    
    }
        },
        "DockerVersion": "20.10.7",
        "Author": "",
        "Config": {
    
    
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
    
    
                "6379/tcp": {
    
    }
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "GOSU_VERSION=1.12",
                "REDIS_VERSION=6.2.6",
                "REDIS_DOWNLOAD_URL=http://download.redis.io/releases/redis-6.2.6.tar.gz",
                "REDIS_DOWNLOAD_SHA=5b2b8b7a50111ef395bf1c1d5be11e6e167ac018125055daa8b5c2317ae131ab"
            ],
            "Cmd": [
                "redis-server"
            ],
            "Image": "sha256:e093f59d716c95cfce82c676f099b960cc700432ab531388fcedf79932fc81ec",
            "Volumes": {
    
    
                "/data": {
    
    }
            },
            "WorkingDir": "/data",
            "Entrypoint": [
                "docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": null
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 112691373,
        "VirtualSize": 112691373,
        "GraphDriver": {
    
    
            "Data": {
    
    
                "LowerDir": "/var/lib/docker/overlay2/d11a7ad84c40c6d666f556ce80024890a138b0a45170c37b9ae389e025a02d84/diff:/var/lib/docker/overlay2/a1589372955ff65774a113cd71249d06c71aa7185369346fcdf0dded5040d61f/diff:/var/lib/docker/overlay2/b5d49d3d8933b9a0997465e9479541ac8ddfa1c829e45a449ea0414850a49986/diff:/var/lib/docker/overlay2/e12127840416a40f62641daa073920654819b790343f8dd15d95afec3ddc7a9b/diff:/var/lib/docker/overlay2/987e1c9a9c3d72e8928a2c04d8592c5feffaee8d65ab89b4acb490b976d30e08/diff",
                "MergedDir": "/var/lib/docker/overlay2/25288ea7595fa789a2aaf22f460d544b6caf92de18383c80f7007b0fe6ba2703/merged",
                "UpperDir": "/var/lib/docker/overlay2/25288ea7595fa789a2aaf22f460d544b6caf92de18383c80f7007b0fe6ba2703/diff",
                "WorkDir": "/var/lib/docker/overlay2/25288ea7595fa789a2aaf22f460d544b6caf92de18383c80f7007b0fe6ba2703/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
    
    
            "Type": "layers",
            "Layers": [
                "sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f",
                "sha256:9b24afeb7c2f21e50a686ead025823cd2c6e9730c013ca77ad5f115c079b57cb",
                "sha256:4b8e2801e0f956a4220c32e2c8b0a590e6f9bd2420ec65453685246b82766ea1",
                "sha256:529cdb636f61e95ab91a62a51526a84fd7314d6aab0d414040796150b4522372",
                "sha256:9975392591f2777d6bf4d9919ad1b2c9afa12f9a9b4d260f45025ec3cc9b18ed",
                "sha256:8e5669d8329116b8444b9bbb1663dda568ede12d3dbcce950199b582f6e94952"
            ]
        },
        "Metadata": {
    
    
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]

Note:

bootfs:boot file system

rootfs:root file system

所有的 Docker镜像都起始于一个基础镜像层,当进行修改或培加新的内容时,就会在当前镜像层之上,创建新的镜像层。

举一个简单的例子,假如基于 Ubuntu Linux16.04创建一个新的镜像,这就是新镜像的第一层;如果在该镜像中添加 Python包,
就会在基础镜像层之上创建第二个镜像层;如果继续添加一个安全补丁,就会创健第三个镜像层该像当前已经包含3个镜像层,如下图所示(这只是一个用于演示的很简单的例子)。

1642757240020

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-5s7oDRG8-1679562589558) (https://gitee.com/happy_sad/typora-image/raw/master/images /1642757252646.png)]

It is important to understand that while adding additional image layers, the image always remains a combination of all current images. A simple example is shown in the figure below, each image layer contains 3 files, and the image contains 6 files from two image layers.

1642757265822

The image layer in the above image is slightly different from the previous image, the main purpose is to facilitate the display of
files File 7 in is an updated version of File 5

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-d9D3jzjQ-1679562589559) (https://gitee.com/happy_sad/typora-image/raw/master/images /1642757277766.png)]

In this case, the files in the upper mirror layer overwrite the files in the lower mirror layer. This causes the updated version of the file to be added to the image as a new image layer

Docker implements the image layer stack through the storage engine (the new version adopts the snapshot mechanism), and ensures that multiple image layers are displayed as a unified file system to the outside world

The storage drivers available on Linux are AUFS, Overlay2, Device Mapper, Btrfs, and ZFS. As the name implies, each storage engine is based on the corresponding
hardware system or block device technology in Linux, and each storage engine has its unique performance characteristics.

Docker only supports windowsfilter, a storage engine on Windows, which implements layering and CoW [1] based on the NTFS file system.

The figure below shows the same three-layer mirror as the system displays. All mirror layer stacks are merged to provide a unified view to the outside world
1642758144514

3. Features

Docker images are all read-only. When the container starts, a new writable layer is added to the top of the image. This layer is what we usually call the container layer, and everything below the container layer is called the image layer.

1642757297877

docker -pull 应用  为镜像层
docker _____ 	  为容器层
docker run ____	  将镜像层和容器层打包为一个新的容器

4. docker commit

1. docker commit submit image

docker commit						 # 提交容器成为一个新的副本
docker commit -m="提交的描述信息" -a="作者" 容器id 目标镜像名:[TAG]
# 1、启动一个默认的tomcat镜像
# 2、发现这个tomcat是没有webapps应用的,官方的镜像默认webapps下面没有文件,需要从webapps.dist文件只能拷贝过来
# 3、将webapps.dist文件中的文件拷贝到webapps中
# 4、通过docker commit将此容器提交为一个副本(即新的镜像,自己修改了的镜像)

2. [Port 1]

[root@Jin ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@Jin ~]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
nginx                 latest    605c77e624dd   3 weeks ago     141MB
tomcat                latest    fb5657adc892   4 weeks ago     680MB
redis                 latest    7614ae9453d1   4 weeks ago     113MB
centos                latest    5d0da3dc9764   4 months ago    231MB
portainer/portainer   latest    580c0e4e98b0   10 months ago   79.1MB
elasticsearch         7.6.2     f29a1ee41030   22 months ago   791MB
[root@Jin ~]# docker run -it -p 39004:8080 tomcat
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/openjdk-11
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
NOTE: Picked up JDK_JAVA_OPTIONS:  --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
23-Jan-2022 07:52:34.298 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name:   Apache Tomcat/10.0.14
23-Jan-2022 07:52:34.334 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Dec 2 2021 22:01:36 UTC
23-Jan-2022 07:52:34.335 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version number: 10.0.14.0
23-Jan-2022 07:52:34.335 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Linux
23-Jan-2022 07:52:34.335 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:            3.10.0-1127.19.1.el7.x86_64
23-Jan-2022 07:52:34.335 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:          amd64
23-Jan-2022 07:52:34.336 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             /usr/local/openjdk-11
23-Jan-2022 07:52:34.336 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           11.0.13+8
23-Jan-2022 07:52:34.336 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            Oracle Corporation
23-Jan-2022 07:52:34.336 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         /usr/local/tomcat
23-Jan-2022 07:52:34.337 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         /usr/local/tomcat
23-Jan-2022 07:52:34.378 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.lang=ALL-UNNAMED
23-Jan-2022 07:52:34.379 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.io=ALL-UNNAMED
23-Jan-2022 07:52:34.379 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.util=ALL-UNNAMED
23-Jan-2022 07:52:34.379 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.util.concurrent=ALL-UNNAMED
23-Jan-2022 07:52:34.379 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
23-Jan-2022 07:52:34.380 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties
23-Jan-2022 07:52:34.380 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
23-Jan-2022 07:52:34.380 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
23-Jan-2022 07:52:34.380 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
23-Jan-2022 07:52:34.381 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dorg.apache.catalina.security.SecurityListener.UMASK=0027
23-Jan-2022 07:52:34.383 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
23-Jan-2022 07:52:34.384 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat
23-Jan-2022 07:52:34.384 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat
23-Jan-2022 07:52:34.384 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp
23-Jan-2022 07:52:34.397 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded Apache Tomcat Native library [1.2.31] using APR version [1.7.0].
23-Jan-2022 07:52:34.398 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true], UDS [true].
23-Jan-2022 07:52:34.406 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized [OpenSSL 1.1.1k  25 Mar 2021]
23-Jan-2022 07:52:35.939 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
23-Jan-2022 07:52:36.059 INFO [main] org.apache.catalina.startup.Catalina.load Server initialization in [2832] milliseconds
23-Jan-2022 07:52:36.420 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
23-Jan-2022 07:52:36.421 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet engine: [Apache Tomcat/10.0.14]
23-Jan-2022 07:52:36.473 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
23-Jan-2022 07:52:36.515 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [456] milliseconds
23-Jan-2022 07:55:06.510 INFO [Catalina-utility-2] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/docs]
23-Jan-2022 07:55:07.374 INFO [Catalina-utility-2] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/docs] has finished in [865] ms
23-Jan-2022 07:55:07.375 INFO [Catalina-utility-2] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/ROOT]
23-Jan-2022 07:55:07.443 INFO [Catalina-utility-2] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/ROOT] has finished in [68] ms
23-Jan-2022 07:55:07.444 INFO [Catalina-utility-2] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/host-manager]
23-Jan-2022 07:55:07.549 INFO [Catalina-utility-2] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/host-manager] has finished in [105] ms
23-Jan-2022 07:55:07.549 INFO [Catalina-utility-2] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/manager]
23-Jan-2022 07:55:07.651 INFO [Catalina-utility-2] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/manager] has finished in [102] ms
23-Jan-2022 07:55:07.651 INFO [Catalina-utility-2] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/examples]
23-Jan-2022 07:55:08.309 INFO [Catalina-utility-2] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/examples] has finished in [658] ms

1642924693727

3. [Port 2]

[root@Jin ~]# docker ps
CONTAINER ID   IMAGE     COMMAND             CREATED          STATUS          PORTS                                         NAMES
532697cee847   tomcat    "catalina.sh run"   35 seconds ago   Up 34 seconds   0.0.0.0:39004->8080/tcp, :::39004->8080/tcp   wizardly_shamir
[root@Jin ~]# docker exec -it 532697cee847 /bin/bash
root@532697cee847:/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@532697cee847:/usr/local/tomcat# cd webapps
root@532697cee847:/usr/local/tomcat/webapps# ls
root@532697cee847:/usr/local/tomcat/webapps# cd ..
root@532697cee847:/usr/local/tomcat# cd webapps.dist/
root@532697cee847:/usr/local/tomcat/webapps.dist# ls
ROOT  docs  examples  host-manager  manager
root@532697cee847:/usr/local/tomcat/webapps.dist# cd ..
root@532697cee847:/usr/local/tomcat# cp -r webapps.dist/* webapps
root@532697cee847:/usr/local/tomcat# cd webapps
root@532697cee847:/usr/local/tomcat/webapps# ls
ROOT  docs  examples  host-manager  manager
root@532697cee847:/usr/local/tomcat/webapps# 

1642924802670

4. View the running results

1642947890584

5、docker commit

docker commit -a="Jin" -m="add webapps app" 当前容器的id tomcat02:1.0
root@532697cee847:/usr/local/tomcat/webapps# exit
exit
[root@Jin ~]# docker ps
CONTAINER ID   IMAGE     COMMAND             CREATED          STATUS          PORTS                                         NAMES
532697cee847   tomcat    "catalina.sh run"   13 minutes ago   Up 13 minutes   0.0.0.0:39004->8080/tcp, :::39004->8080/tcp   wizardly_shamir
[root@Jin ~]# docker commit -a="Jin" -m="add webapps app" 532697cee847 tomcat02:1.0
sha256:c2ae9e171b978d04816f1e697781b21c81c3c90be5a30ef4dcb77b57bf2d795d
[root@Jin ~]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
tomcat02              1.0       c2ae9e171b97   4 minutes ago   684MB
nginx                 latest    605c77e624dd   3 weeks ago     141MB
tomcat                latest    fb5657adc892   4 weeks ago     680MB
redis                 latest    7614ae9453d1   4 weeks ago     113MB
centos                latest    5d0da3dc9764   4 months ago    231MB
portainer/portainer   latest    580c0e4e98b0   10 months ago   79.1MB
elasticsearch         7.6.2     f29a1ee41030   22 months ago   791MB

1642925428018

1642925524449

6. docker inspect image id

[root@Jin ~]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED          SIZE
tomcat02              1.0       c2ae9e171b97   26 minutes ago   684MB
nginx                 latest    605c77e624dd   3 weeks ago      141MB
tomcat                latest    fb5657adc892   4 weeks ago      680MB
redis                 latest    7614ae9453d1   4 weeks ago      113MB
centos                latest    5d0da3dc9764   4 months ago     231MB
portainer/portainer   latest    580c0e4e98b0   10 months ago    79.1MB
elasticsearch         7.6.2     f29a1ee41030   22 months ago    791MB
[root@Jin ~]# docker inspect fb5657adc892		#tomcat
[
    {
    
    
        "Id": "sha256:fb5657adc892ed15910445588404c798b57f741e9921ff3c1f1abe01dbb56906",
        "RepoTags": [
            "tomcat:latest"
        ],
        "RepoDigests": [
            "tomcat@sha256:9dee185c3b161cdfede1f5e35e8b56ebc9de88ed3a79526939701f3537a52324"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2021-12-22T17:07:13.333084424Z",
        "Container": "de0900b3a6caf902ccdaa1c7871d244e29978119ad8a1cce799cf47f1717b679",
        "ContainerConfig": {
    
    
            "Hostname": "de0900b3a6ca",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
    
    
                "8080/tcp": {
    
    }
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/tomcat/bin:/usr/local/openjdk-11/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "JAVA_HOME=/usr/local/openjdk-11",
                "LANG=C.UTF-8",
                "JAVA_VERSION=11.0.13",
                "CATALINA_HOME=/usr/local/tomcat",
                "TOMCAT_NATIVE_LIBDIR=/usr/local/tomcat/native-jni-lib",
                "LD_LIBRARY_PATH=/usr/local/tomcat/native-jni-lib",
                "GPG_KEYS=A9C5DF4D22E99998D9875A5110C01C5A2F6059E7",
                "TOMCAT_MAJOR=10",
                "TOMCAT_VERSION=10.0.14",
                "TOMCAT_SHA512=c2d2ad5ed17f7284e3aac5415774a8ef35434f14dbd9a87bc7230d8bfdbe9aa1258b97a59fa5c4030e4c973e4d93d29d20e40b6254347dbb66fae269ff4a61a5"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"catalina.sh\" \"run\"]"
            ],
            "Image": "sha256:6e2683bf6f13f0050833b6807871b4980142835747139a2c2ae91b274787e399",
            "Volumes": null,
            "WorkingDir": "/usr/local/tomcat",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
    
    }
        },
        "DockerVersion": "20.10.7",
        "Author": "",
        "Config": {
    
    
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
    
    
                "8080/tcp": {
    
    }
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/tomcat/bin:/usr/local/openjdk-11/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "JAVA_HOME=/usr/local/openjdk-11",
                "LANG=C.UTF-8",
                "JAVA_VERSION=11.0.13",
                "CATALINA_HOME=/usr/local/tomcat",
                "TOMCAT_NATIVE_LIBDIR=/usr/local/tomcat/native-jni-lib",
                "LD_LIBRARY_PATH=/usr/local/tomcat/native-jni-lib",
                "GPG_KEYS=A9C5DF4D22E99998D9875A5110C01C5A2F6059E7",
                "TOMCAT_MAJOR=10",
                "TOMCAT_VERSION=10.0.14",
                "TOMCAT_SHA512=c2d2ad5ed17f7284e3aac5415774a8ef35434f14dbd9a87bc7230d8bfdbe9aa1258b97a59fa5c4030e4c973e4d93d29d20e40b6254347dbb66fae269ff4a61a5"
            ],
            "Cmd": [
                "catalina.sh",
                "run"
            ],
            "Image": "sha256:6e2683bf6f13f0050833b6807871b4980142835747139a2c2ae91b274787e399",
            "Volumes": null,
            "WorkingDir": "/usr/local/tomcat",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": null
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 679618222,
        "VirtualSize": 679618222,
        "GraphDriver": {
    
    
            "Data": {
    
    
                "LowerDir": "/var/lib/docker/overlay2/ea19cccea66a231080bcb1a8a32712a8e33202c5ec1df3f975c6da41292d45e6/diff:/var/lib/docker/overlay2/61611fd029553a2f2dec395504676ea654216c51c2aa64c8a3f55ea2ed3c5e0d/diff:/var/lib/docker/overlay2/d8b1dd83653d99129d569fd4cbd85d98c716b1434a562d9594ba97b491e80508/diff:/var/lib/docker/overlay2/4a1da536dfcd3200c8ea0a2db832cb6bcc7d936725bcfaa9942e343eebc53758/diff:/var/lib/docker/overlay2/89b1b263dd5b26f98488fa81427f8ae0989bd62d67ca19c3b5f2e62f6500e0c9/diff:/var/lib/docker/overlay2/240fc4c132167f5e02e00fa3917b16de1c77c7db39c3d30b1b66939f3b6560be/diff:/var/lib/docker/overlay2/0ebf344344cb49b98d9abd76c612b8b726699eb669b82a2719561272d640eaef/diff:/var/lib/docker/overlay2/e950bce470bf7ebe00f928c0fb4818d1677e610323c50c90e31a252bb29c2dc4/diff:/var/lib/docker/overlay2/39f2fa82d180fa4aa66544371b5e38a7d31e1335c50285f64616adf58fd4481b/diff",
                "MergedDir": "/var/lib/docker/overlay2/308d4f1c743b796a9961ab56fecdba5fc5c803e6b31385553c94ff66d7775b9f/merged",
                "UpperDir": "/var/lib/docker/overlay2/308d4f1c743b796a9961ab56fecdba5fc5c803e6b31385553c94ff66d7775b9f/diff",
                "WorkDir": "/var/lib/docker/overlay2/308d4f1c743b796a9961ab56fecdba5fc5c803e6b31385553c94ff66d7775b9f/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
    
    
            "Type": "layers",
            "Layers": [
                "sha256:11936051f93baf5a4fb090a8fa0999309b8173556f7826598e235e8a82127bce",
                "sha256:31892cc314cb1993ba1b8eb5f3002c4e9f099a9237af0d03d1893c6fcc559aab",
                "sha256:8bf42db0de72f74f4ef0c1d1743f5d54efc3491ee38f4af6d914a6032148b78e",
                "sha256:26a504e63be4c63395f216d70b1b8af52263a5289908df8e96a0e7c840813adc",
                "sha256:f9e18e59a5651609a1503ac17dcfc05856b5bea21e41595828471f02ad56a225",
                "sha256:832e177bb5008934e2f5ed723247c04e1dd220d59a90ce32000b7c22bd9d9b54",
                "sha256:3bb5258f46d2a511ddca2a4ec8f9091d676a116830a7f336815f02c4b34dbb23",
                "sha256:59c516e5b6fafa2e6b63d76492702371ca008ade6e37d931089fe368385041a0",
                "sha256:bd2befca2f7ef51f03b757caab549cc040a36143f3b7e3dab94fb308322f2953",
                "sha256:3e2ed6847c7a081bd90ab8805efcb39a2933a807627eb7a4016728f881430f5f"
            ]
        },
        "Metadata": {
    
    
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]
[root@Jin ~]# docker inspect c2ae9e171b97	#tomcat02
[
    {
    
    
        "Id": "sha256:c2ae9e171b978d04816f1e697781b21c81c3c90be5a30ef4dcb77b57bf2d795d",
        "RepoTags": [
            "tomcat02:1.0"
        ],
        "RepoDigests": [],
        "Parent": "sha256:fb5657adc892ed15910445588404c798b57f741e9921ff3c1f1abe01dbb56906",
        "Comment": "add webapps app",
        "Created": "2022-01-23T08:07:15.760213309Z",
        "Container": "532697cee8479431ad985335faed7506a019312ceb4e47792cf61008209236a4",
        "ContainerConfig": {
    
    
            "Hostname": "532697cee847",
            "Domainname": "",
            "User": "",
            "AttachStdin": true,
            "AttachStdout": true,
            "AttachStderr": true,
            "ExposedPorts": {
    
    
                "8080/tcp": {
    
    }
            },
            "Tty": true,
            "OpenStdin": true,
            "StdinOnce": true,
            "Env": [
                "PATH=/usr/local/tomcat/bin:/usr/local/openjdk-11/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "JAVA_HOME=/usr/local/openjdk-11",
                "LANG=C.UTF-8",
                "JAVA_VERSION=11.0.13",
                "CATALINA_HOME=/usr/local/tomcat",
                "TOMCAT_NATIVE_LIBDIR=/usr/local/tomcat/native-jni-lib",
                "LD_LIBRARY_PATH=/usr/local/tomcat/native-jni-lib",
                "GPG_KEYS=A9C5DF4D22E99998D9875A5110C01C5A2F6059E7",
                "TOMCAT_MAJOR=10",
                "TOMCAT_VERSION=10.0.14",
                "TOMCAT_SHA512=c2d2ad5ed17f7284e3aac5415774a8ef35434f14dbd9a87bc7230d8bfdbe9aa1258b97a59fa5c4030e4c973e4d93d29d20e40b6254347dbb66fae269ff4a61a5"
            ],
            "Cmd": [
                "catalina.sh",
                "run"
            ],
            "Image": "tomcat",
            "Volumes": null,
            "WorkingDir": "/usr/local/tomcat",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
    
    }
        },
        "DockerVersion": "20.10.12",
        "Author": "Jin",
        "Config": {
    
    
            "Hostname": "532697cee847",
            "Domainname": "",
            "User": "",
            "AttachStdin": true,
            "AttachStdout": true,
            "AttachStderr": true,
            "ExposedPorts": {
    
    
                "8080/tcp": {
    
    }
            },
            "Tty": true,
            "OpenStdin": true,
            "StdinOnce": true,
            "Env": [
                "PATH=/usr/local/tomcat/bin:/usr/local/openjdk-11/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "JAVA_HOME=/usr/local/openjdk-11",
                "LANG=C.UTF-8",
                "JAVA_VERSION=11.0.13",
                "CATALINA_HOME=/usr/local/tomcat",
                "TOMCAT_NATIVE_LIBDIR=/usr/local/tomcat/native-jni-lib",
                "LD_LIBRARY_PATH=/usr/local/tomcat/native-jni-lib",
                "GPG_KEYS=A9C5DF4D22E99998D9875A5110C01C5A2F6059E7",
                "TOMCAT_MAJOR=10",
                "TOMCAT_VERSION=10.0.14",
                "TOMCAT_SHA512=c2d2ad5ed17f7284e3aac5415774a8ef35434f14dbd9a87bc7230d8bfdbe9aa1258b97a59fa5c4030e4c973e4d93d29d20e40b6254347dbb66fae269ff4a61a5"
            ],
            "Cmd": [
                "catalina.sh",
                "run"
            ],
            "Image": "tomcat",
            "Volumes": null,
            "WorkingDir": "/usr/local/tomcat",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
    
    }
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 684000041,
        "VirtualSize": 684000041,
        "GraphDriver": {
    
    
            "Data": {
    
    
                "LowerDir": "/var/lib/docker/overlay2/308d4f1c743b796a9961ab56fecdba5fc5c803e6b31385553c94ff66d7775b9f/diff:/var/lib/docker/overlay2/ea19cccea66a231080bcb1a8a32712a8e33202c5ec1df3f975c6da41292d45e6/diff:/var/lib/docker/overlay2/61611fd029553a2f2dec395504676ea654216c51c2aa64c8a3f55ea2ed3c5e0d/diff:/var/lib/docker/overlay2/d8b1dd83653d99129d569fd4cbd85d98c716b1434a562d9594ba97b491e80508/diff:/var/lib/docker/overlay2/4a1da536dfcd3200c8ea0a2db832cb6bcc7d936725bcfaa9942e343eebc53758/diff:/var/lib/docker/overlay2/89b1b263dd5b26f98488fa81427f8ae0989bd62d67ca19c3b5f2e62f6500e0c9/diff:/var/lib/docker/overlay2/240fc4c132167f5e02e00fa3917b16de1c77c7db39c3d30b1b66939f3b6560be/diff:/var/lib/docker/overlay2/0ebf344344cb49b98d9abd76c612b8b726699eb669b82a2719561272d640eaef/diff:/var/lib/docker/overlay2/e950bce470bf7ebe00f928c0fb4818d1677e610323c50c90e31a252bb29c2dc4/diff:/var/lib/docker/overlay2/39f2fa82d180fa4aa66544371b5e38a7d31e1335c50285f64616adf58fd4481b/diff",
                "MergedDir": "/var/lib/docker/overlay2/d3798394809b1327003f6925b277affd6291c1f531b523b0acae8351d186b2a2/merged",
                "UpperDir": "/var/lib/docker/overlay2/d3798394809b1327003f6925b277affd6291c1f531b523b0acae8351d186b2a2/diff",
                "WorkDir": "/var/lib/docker/overlay2/d3798394809b1327003f6925b277affd6291c1f531b523b0acae8351d186b2a2/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
    
    
            "Type": "layers",
            "Layers": [
                "sha256:11936051f93baf5a4fb090a8fa0999309b8173556f7826598e235e8a82127bce",
                "sha256:31892cc314cb1993ba1b8eb5f3002c4e9f099a9237af0d03d1893c6fcc559aab",
                "sha256:8bf42db0de72f74f4ef0c1d1743f5d54efc3491ee38f4af6d914a6032148b78e",
                "sha256:26a504e63be4c63395f216d70b1b8af52263a5289908df8e96a0e7c840813adc",
                "sha256:f9e18e59a5651609a1503ac17dcfc05856b5bea21e41595828471f02ad56a225",
                "sha256:832e177bb5008934e2f5ed723247c04e1dd220d59a90ce32000b7c22bd9d9b54",
                "sha256:3bb5258f46d2a511ddca2a4ec8f9091d676a116830a7f336815f02c4b34dbb23",
                "sha256:59c516e5b6fafa2e6b63d76492702371ca008ade6e37d931089fe368385041a0",
                "sha256:bd2befca2f7ef51f03b757caab549cc040a36143f3b7e3dab94fb308322f2953",
                "sha256:3e2ed6847c7a081bd90ab8805efcb39a2933a807627eb7a4016728f881430f5f",
                "sha256:652437f00c8a01ecfe92a2a120b65fc5ca27dcabf416547d46a7f99c85691e3f"  #多了这一层
            ]
        },
        "Metadata": {
    
    
            "LastTagTime": "2022-01-23T16:07:15.769405777+08:00"
        }
    }
]

7. Container data volume

1. What is a data volume

Docker is to package the application and environment into a mirror image

In this way, the data should not be in the container, otherwise the container will be deleted, and the data will be lost. This is to delete the library and run away

Therefore, there must be a data sharing technology between containers

The data generated in the Docker container is synchronized locally, which is the volume technology

It is essentially a directory mount, which mounts the directory in the container to the virtual machine

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-wYFokO2w-1679562589563) (https://gitee.com/happy_sad/typora-image/raw/master/images /1642928686464.png)]

Purpose: Persistence and synchronization of containers! Data sharing between containers

2. Using data volumes

1. Use the command to mount -v

1. Mount the centos container

[Port 1] Mount the centos container: mount the /home directory of the centos container with the /home/docker of the host

[root@Jin /]# cd home
[root@Jin home]# ls
copy.java  Jin  JinTest  Jin.txt  redis  test  www
[root@Jin home]# mkdir docker			    #创建一个docker文件给docker挂载东西
[root@Jin home]# ls
copy.java  docker  Jin  JinTest  Jin.txt  redis  test  www
[root@Jin home]# docker run -it -v /home/docker:/home/ centos /bin/bash
[root@aa3358059bca /]# cd /home				#容器里的/home目录下为空
[root@aa3358059bca home]# ls

【Port 2】View mount information

[root@Jin /]# cd home
[root@Jin home]# ls
copy.java  docker  Jin  JinTest  Jin.txt  redis  test  www
[root@Jin home]# cd docker					#主机下的/home/docker也为空
[root@Jin docker]# ls
[root@Jin docker]# docker ps				#挂载的centos容器运行
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
aa3358059bca   centos    "/bin/bash"   2 minutes ago   Up 2 minutes             trusting_black
[root@Jin docker]# docker inspect aa3358059bca
[
    {
    
    
        "Id": "aa3358059bca419f0bfe6ba6a9909bb074236f1abfb3361f2e189d14a848564f",
        "Created": "2022-01-23T14:28:15.050024448Z",
        "Path": "/bin/bash",
        "Args": [],
        "State": {
    
    
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 6978,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2022-01-23T14:28:15.688296627Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
        "ResolvConfPath": "/var/lib/docker/containers/aa3358059bca419f0bfe6ba6a9909bb074236f1abfb3361f2e189d14a848564f/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/aa3358059bca419f0bfe6ba6a9909bb074236f1abfb3361f2e189d14a848564f/hostname",
        "HostsPath": "/var/lib/docker/containers/aa3358059bca419f0bfe6ba6a9909bb074236f1abfb3361f2e189d14a848564f/hosts",
        "LogPath": "/var/lib/docker/containers/aa3358059bca419f0bfe6ba6a9909bb074236f1abfb3361f2e189d14a848564f/aa3358059bca419f0bfe6ba6a9909bb074236f1abfb3361f2e189d14a848564f-json.log",
        "Name": "/trusting_black",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
    
    
            "Binds": [
                "/home/docker:/home/"
            ],
            "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/da4c11981095cdded80fa3d49dffe0d04afb9060c7231cc3ccad65c6a89b56f0-init/diff:/var/lib/docker/overlay2/062d86c5e366ca0ececd1c38ff44dee264283b594189692994d190dd67fee589/diff",
                "MergedDir": "/var/lib/docker/overlay2/da4c11981095cdded80fa3d49dffe0d04afb9060c7231cc3ccad65c6a89b56f0/merged",
                "UpperDir": "/var/lib/docker/overlay2/da4c11981095cdded80fa3d49dffe0d04afb9060c7231cc3ccad65c6a89b56f0/diff",
                "WorkDir": "/var/lib/docker/overlay2/da4c11981095cdded80fa3d49dffe0d04afb9060c7231cc3ccad65c6a89b56f0/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
    
    
                "Type": "bind",
                "Source": "/home/docker",
                "Destination": "/home",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }
        ],
        "Config": {
    
    
            "Hostname": "aa3358059bca",
            "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": "09bf2cb9cefa443acadcc1fb65d967c317a9e93c37359e21328a53f5b3298461",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
    
    },
            "SandboxKey": "/var/run/docker/netns/09bf2cb9cefa",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "444ccac85bb2960fb015ae90e45e8b7527c25083f6375d2ee33eb94e97a94962",
            "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": "7b8a815e7cd106aefd34943f8f9a8cdb55febac4620a4f820874412a6ccc74ec",
                    "EndpointID": "444ccac85bb2960fb015ae90e45e8b7527c25083f6375d2ee33eb94e97a94962",
                    "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
                }
            }
        }
    }
]

1642948671637

2. The container creates a file synchronization host

[Port 1] Create a docker_v.java file in the /home directory of the centos container

[root@aa3358059bca /]# cd /home
[root@aa3358059bca home]# ls
[root@aa3358059bca home]# touch docker_v.java
[root@aa3358059bca home]# ls
docker_v.java

[Port 2] View the files in the mounted /home/docker file directory

[root@Jin docker]# cd ..
[root@Jin home]# ls
copy.java  docker  Jin  JinTest  Jin.txt  redis  test  www
[root@Jin home]# cd docker
[root@Jin docker]# ls
[root@Jin docker]# ls
docker_v.java

1642948905349

3. The host modifies the file synchronization container

[Port 2] Modify the docker_v.java file in the host /home/docker directory, and add a docker_v_v.java file

[root@Jin docker]# vim docker_v.java
[root@Jin docker]# touch docker_v_v.java
[root@Jin docker]# ls
docker_v.java  docker_v_v.java

[Port 1] View the docker_v.java file in the /home directory in the centos container and view all files

[root@aa3358059bca home]# ls
docker_v.java  docker_v_v.java
[root@aa3358059bca home]# cat docker_v.java 
hello,数据卷挂载!

1642949332872

1642949210712

4. Even if the container is stopped, the address added configuration mounted in the host is still synchronized

[Port 1] Stop the container

[root@Jin docker]# exit
exit
[root@Jin docker]# docker ps				#挂载的centos容器停止运行
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES

[Port 2] Modify the docker_v_v.java file in the host /home/docker directory

[root@Jin docker]# vim docker_v_v.java 

[Port 1] Start the container and enter the container to view, the docker_v_v.java file in the /home directory

[root@Jin home]# docker start aa3358059bca
aa3358059bca
[root@Jin home]# docker attach aa3358059bca
[root@aa3358059bca /]# cd /home
[root@aa3358059bca home]# ls
docker_v.java  docker_v_v.java
[root@aa3358059bca home]# cat docker_v_v.java 
hello,只要使用了docker -v挂载了,容器没有被强制删除,主机在挂载的地址添加配置,容器仍然同步。

1642949790594

1642949987842

2. MySQL synchronizes data

1. Download and install mysql: 5.7
[root@Jin /]# docker search mysql:5.7
NAME                                      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
kiratalent/mysql                          Pinned copy of mysql:5.7.24                     1                    
variu/hyperledger-explorer-mysql          mysql:5.7.23 for hyperledger explorer           0                    
migs/mysql-5.7                            mysql:5.7 plus some basic enhancements          0                    [OK]
stsiwo/sts-blog-db                        base image mysql:5.7.28                         0                    
xiao806852034/mysql57                     基于 mysql:5.7                                    0                    
metatronx/mysql                           mysql:5.7 + druid account & database            0                    
idyci/mysql                               mysql:5.7.25                                    0                    
michaelzx/docker_msyql                    without ONLY_FULL_GROUP_BY base on mysql:5.7…   0                    [OK]
denghui/mysql                             mysql:5.7-tz.8                                  0                    [OK]
framgiaciteam/mysql-256                   FROM mysql:5.7  CMD ["--block_encryption_mod…   0                    
llasuka/mysql                             mysql:5.7                                       0                    
beyondray/kbengine-mysql                  kbengine auto-build env from mysql:5.7          0                    [OK]
liuzugang/mysql                           mysql:5.7                                       0                    
ahmedelshfie/spring-boot-mysql-rest-api   mysql:5.7                                       0                    
alecchyi/mysql                            mysql:5.7 for demo                              0                    
zhjs/mysql                                mysql:5.7                                       0                    
crbanman/mysql-tmpfs                      A docker image based off of mysql:5.7 that m…   0                    
alexmbarbosa/mysql5.7                     Based on official mysql:5.7 docker image        0                    
vigasin/mysql                             Clone of official mysql:5.7 image, that allo…   0                    
zeq9069/mysql_service                     mysql:5.7                                       0                    
arturgoldyn/mysql_5-7_inmemory            Based on mysql:5.7 image, run MySQL in /dev/…   0                    [OK]
kamatimaru/mysql57-ja                     Added Japanese support settings to mysql:5.7…   0                    
doctorva/mysql_fede                       Based on Mysql:5.7 and add new configuration…   0                    
liyupei/mysql                             Mysql:5.7                                       0                    
zhangruihuai/mysql-node-nginx-python      based on mysql:5.7  MySQL 5.7 NodeJS v14.18.…   0                    
[root@Jin /]# 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
docker.io/library/mysql:5.7

1642988661015

2. Data volume mount mysql: 5.7
-d 后台运行							(不会进行运行时界面)
-p 端口映射							(映射端口,将默认端口映射到其他端口打开)
-v 卷挂载							 (挂载,将主机的设定目录与容器想要挂载的目录挂载)
-e 环境配置 安装启动mysql需要配置密码	   (mysql配置密码)
--name 容器名字						 (给容器拟一个名字)

【Port 1】

[root@Jin /]# docker run -d -p 39005:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7
461f9060e5e9e19acdf7d57968a13a6db0df43c0edd2e77ea75342d9c7c89573

【Port 2】

[root@Jin home]# ls
copy.java  docker  Jin  JinTest  Jin.txt  redis  test  www
[root@Jin home]# ls
copy.java  Jin      Jin.txt  redis  www
docker     JinTest  mysql    test
[root@Jin home]# cd mysql
[root@Jin mysql]# ls
conf  data
[root@Jin mysql]# cd data
[root@Jin data]# ls
auto.cnf         ibdata1             private_key.pem
ca-key.pem       ib_logfile0         public_key.pem
ca.pem           ib_logfile1         server-cert.pem
client-cert.pem  ibtmp1              server-key.pem
client-key.pem   mysql               sys
ib_buffer_pool   performance_schema

1642990320881

3. Local test connection and creation

1. Database connection test

1642989206866

2. Create a database in the mysql01 connection

1642990843300

3. [Port 2] View port 2 file changes

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-yVww3k7j-1679562589568) (https://gitee.com/happy_sad/typora-image/raw/master/images /1642990913075.png)]

4. The container is deleted or stopped, but the data is still there

Implement container persistence

1642991125270

3. Anonymous, named, specific path mount

1. Anonymous mount

All the volumes in the docker container are in ==/var/lib/docker/volumes/xxx/_data== if no directory is specified

[root@Jin /]# docker run -d -P --name niming-nginx -v /etc/nginx nginx
faf9c51c6676113544e129e13f2808e9cbcd22dd6c68c7b7fba8cef760a7fbc8
[root@Jin /]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                                     NAMES
faf9c51c6676   nginx     "/docker-entrypoint.…"   4 seconds ago   Up 3 seconds   0.0.0.0:49154->80/tcp, :::49154->80/tcp   niming-nginx
[root@Jin /]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS                    PORTS                                     NAMES
faf9c51c6676   nginx     "/docker-entrypoint.…"   7 seconds ago   Up 6 seconds              0.0.0.0:49154->80/tcp, :::49154->80/tcp   niming-nginx
aa3358059bca   centos    "/bin/bash"              12 hours ago    Exited (0) 12 hours ago                                             trusting_black
[root@Jin /]# docker volume ls
DRIVER    VOLUME NAME
local     1a382afa251c9035f90255d5ceddc7fbf5cc1febbb16b6b25356aad962b630bf
local     8de45fa0f3619b7bfe78e5e5484a6c79bdae54aca2305213db2f742aef5c8fab
local     a4aa3a7209d6ccf0f4a415cd340bfcf0ce170aa9731e6a0fe9f0eb1211a7eab5
local     c44a66786e2558401adb4216aa0f1b3a9d8d252a14524f2411b31bcc61cda201

1642992216050

2. Named mount

[root@Jin /]# docker run -d -P --name juming-nginx -v juming-nginx:/etc/nginx nginx
9346743f602c6ed3d2e8cbf8de1e6ff93acdeafbd158069261f5cf27dff5232f
[root@Jin /]# docker volume ls
DRIVER    VOLUME NAME
local     1a382afa251c9035f90255d5ceddc7fbf5cc1febbb16b6b25356aad962b630bf
local     8de45fa0f3619b7bfe78e5e5484a6c79bdae54aca2305213db2f742aef5c8fab
local     a4aa3a7209d6ccf0f4a415cd340bfcf0ce170aa9731e6a0fe9f0eb1211a7eab5
local     c44a66786e2558401adb4216aa0f1b3a9d8d252a14524f2411b31bcc61cda201
local     juming-nginx
[root@Jin /]# docker volume inspect juming-nginx
[
    {
    
    
        "CreatedAt": "2022-01-24T10:50:45+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/juming-nginx/_data",
        "Name": "juming-nginx",
        "Options": null,
        "Scope": "local"
    }
]

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-Gx2MjpIU-1679562589570) (https://gitee.com/happy_sad/typora-image/raw/master/images /1642992811835.png)]

3. Specific path to mount

[root@Jin /]# cd home
[root@Jin home]# ls
copy.java  docker  Jin  JinTest  Jin.txt  mysql  redis  test  www
[root@Jin home]# mkdir nginx
[root@Jin home]# ls
copy.java  docker  Jin  JinTest  Jin.txt  mysql  nginx  redis  test  www
[root@Jin home]# cd nginx
[root@Jin nginx]# touch nginx.config
[root@Jin nginx]# ls
nginx.config
[root@Jin nginx]# cd /
[root@Jin /]# docker run -d -P --name jutilujing-nginx -v /home/nginx/nginx.config:/etc/nginx/nginx.config nginx
eaee05f7dec261adfcfddd91711f094d4bcf8ac40da42d0e3c107c396b5a8a5b
[root@Jin /]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                                     NAMES
eaee05f7dec2   nginx     "/docker-entrypoint.…"   8 seconds ago   Up 7 seconds   0.0.0.0:49168->80/tcp, :::49168->80/tcp   jutilujing-nginx
9346743f602c   nginx     "/docker-entrypoint.…"   3 hours ago     Up 3 hours     0.0.0.0:49155->80/tcp, :::49155->80/tcp   juming-nginx
faf9c51c6676   nginx     "/docker-entrypoint.…"   3 hours ago     Up 3 hours     0.0.0.0:49154->80/tcp, :::49154->80/tcp   niming-nginx
[root@Jin /]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS                    PORTS                                     NAMES
eaee05f7dec2   nginx     "/docker-entrypoint.…"   12 seconds ago   Up 12 seconds             0.0.0.0:49168->80/tcp, :::49168->80/tcp   jutilujing-nginx
9346743f602c   nginx     "/docker-entrypoint.…"   3 hours ago      Up 3 hours                0.0.0.0:49155->80/tcp, :::49155->80/tcp   juming-nginx
faf9c51c6676   nginx     "/docker-entrypoint.…"   3 hours ago      Up 3 hours                0.0.0.0:49154->80/tcp, :::49154->80/tcp   niming-nginx
aa3358059bca   centos    "/bin/bash"              15 hours ago     Exited (0) 15 hours ago                                             trusting_black
[root@Jin /]# docker volume ls
DRIVER    VOLUME NAME
local     1a382afa251c9035f90255d5ceddc7fbf5cc1febbb16b6b25356aad962b630bf
local     8de45fa0f3619b7bfe78e5e5484a6c79bdae54aca2305213db2f742aef5c8fab
local     a4aa3a7209d6ccf0f4a415cd340bfcf0ce170aa9731e6a0fe9f0eb1211a7eab5
local     c44a66786e2558401adb4216aa0f1b3a9d8d252a14524f2411b31bcc61cda201
local     juming-nginx
[root@Jin /]# docker volume inspect jutilujing-nginx
[]
Error: No such volume: jutilujing-nginx

1643003029070

4. Mount the stored directory

All the volumes in the docker container are in ==/var/lib/docker/volumes/xxx/_data== if no directory is specified

[root@Jin /]# cd /var/lib/docker
[root@Jin docker]# ls
buildkit  containers  image  network  overlay2  plugins  runtimes  swarm  tmp  trust  volumes
[root@Jin docker]# cd volumes/
[root@Jin volumes]# ls
1a382afa251c9035f90255d5ceddc7fbf5cc1febbb16b6b25356aad962b630bf  c44a66786e2558401adb4216aa0f1b3a9d8d252a14524f2411b31bcc61cda201
8de45fa0f3619b7bfe78e5e5484a6c79bdae54aca2305213db2f742aef5c8fab  juming-nginx
a4aa3a7209d6ccf0f4a415cd340bfcf0ce170aa9731e6a0fe9f0eb1211a7eab5  metadata.db
backingFsBlockDev
[root@Jin volumes]# cd juming-nginx/
[root@Jin juming-nginx]# ls
_data
[root@Jin juming-nginx]# cd _data/
[root@Jin _data]# ls
conf.d  fastcgi_params  mime.types  modules  nginx.conf  scgi_params  uwsgi_params
[root@Jin _data]# 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;
}


http {
    
    
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

1642994433865

5. Distinguish

# 如何确定是具名挂载还是匿名挂载,亦或者是具体路劲挂载
-v 容器内路劲			 	#匿名挂载
-v 卷名:容器内路劲		      #具名挂载
-v /宿主机路劲:容器内路劲		#指定路劲挂载

6. Expansion

# 通过 -v容器内路劲,ro,rw改变读写权限
ro 		readonly 	#只读
rw 		readwrite	#可读可写

# 一旦设置了容器权限,容器对挂载的内容就有了限定
docker run -d -P --name nginx01 -v ro-nginx:/etc/nginx/ro nginx
docker run -d -P --name nginx02 -v rw-nginx:/etc/nginx/ro nginx
# ro说明这个只能通过宿主机来操作,容器内无法操作,只有只读权限。

4. dockerfile

1. Create a dockerfile

FROM centos

VOLUME ["volume01","volume02"]

CMD echo "---end---"
CMD /bin/bash

1643005846379

2. Build dockerfile mount

[root@Jin home]# ls
copy.java  docker  Jin  JinTest  Jin.txt  mysql  nginx  redis  test  www
[root@Jin home]# mkdir docker-test-volume
[root@Jin home]# ls
copy.java  docker  docker-test-volume  Jin  JinTest  Jin.txt  mysql  nginx  redis  test  www
[root@Jin home]# cd docker-test-volume/
[root@Jin docker-test-volume]# vim dockerfile1
[root@Jin docker-test-volume]# cat dockerfile1 			#匿名挂载
FROM centos

VOLUME ["volume01","volume02"]

CMD echo "---end---"
CMD /bin/bash
[root@Jin docker-test-volume]# docker build -f dockerfile1 -t jin/centos:1.0 .
Sending build context to Docker daemon  2.048kB
Step 1/4 : FROM centos
 ---> 5d0da3dc9764
Step 2/4 : VOLUME ["volume01","volume02"]
 ---> Running in 5f2523c06abb
Removing intermediate container 5f2523c06abb
 ---> af6080bf17a8
Step 3/4 : CMD echo "---end---"
 ---> Running in 7acbf869f6a2
Removing intermediate container 7acbf869f6a2
 ---> 71b1e98be068
Step 4/4 : CMD /bin/bash
 ---> Running in b9f17806e002
Removing intermediate container b9f17806e002
 ---> 202c0ab30c5e
Successfully built 202c0ab30c5e
Successfully tagged jin/centos:1.0

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-D8Kfx8xR-1679562589572) (https://gitee.com/happy_sad/typora-image/raw/master/images /1643006144793.png)]

3. Start mirroring

[root@Jin docker-test-volume]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED          SIZE
jin/centos            1.0       202c0ab30c5e   10 minutes ago   231MB
tomcat02              1.0       c2ae9e171b97   23 hours ago     684MB
nginx                 latest    605c77e624dd   3 weeks ago      141MB
tomcat                latest    fb5657adc892   4 weeks ago      680MB
redis                 latest    7614ae9453d1   4 weeks ago      113MB
mysql                 5.7       c20987f18b13   4 weeks ago      448MB
centos                latest    5d0da3dc9764   4 months ago     231MB
portainer/portainer   latest    580c0e4e98b0   10 months ago    79.1MB
elasticsearch         7.6.2     f29a1ee41030   22 months ago    791MB
[root@Jin docker-test-volume]# docker run -it 202c0ab30c5e /bin/bash
[root@27a8f2387ed9 /]# ls -l
total 56
lrwxrwxrwx   1 root root    7 Nov  3  2020 bin -> usr/bin
drwxr-xr-x   5 root root  360 Jan 24 06:45 dev
drwxr-xr-x   1 root root 4096 Jan 24 06:45 etc
drwxr-xr-x   2 root root 4096 Nov  3  2020 home
lrwxrwxrwx   1 root root    7 Nov  3  2020 lib -> usr/lib
lrwxrwxrwx   1 root root    9 Nov  3  2020 lib64 -> usr/lib64
drwx------   2 root root 4096 Sep 15 14:17 lost+found
drwxr-xr-x   2 root root 4096 Nov  3  2020 media
drwxr-xr-x   2 root root 4096 Nov  3  2020 mnt
drwxr-xr-x   2 root root 4096 Nov  3  2020 opt
dr-xr-xr-x 119 root root    0 Jan 24 06:45 proc
dr-xr-x---   2 root root 4096 Sep 15 14:17 root
drwxr-xr-x  11 root root 4096 Sep 15 14:17 run
lrwxrwxrwx   1 root root    8 Nov  3  2020 sbin -> usr/sbin
drwxr-xr-x   2 root root 4096 Nov  3  2020 srv
dr-xr-xr-x  13 root root    0 Jan 24 06:45 sys
drwxrwxrwt   7 root root 4096 Sep 15 14:17 tmp
drwxr-xr-x  12 root root 4096 Sep 15 14:17 usr
drwxr-xr-x  20 root root 4096 Sep 15 14:17 var
drwxr-xr-x   2 root root 4096 Jan 24 06:45 volume01
drwxr-xr-x   2 root root 4096 Jan 24 06:45 volume02

1643006819669

4. Check road strength

【Port 1】

[root@Jin docker-test-volume]# exit
exit
[root@Jin docker-test-volume]# docker run -it 202c0ab30c5e /bin/bash
[root@c63aedb36136 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var  volume01	volume02
[root@c63aedb36136 /]# cd volume01
[root@c63aedb36136 volume01]# touch container.txt
[root@c63aedb36136 volume01]# 

【Port 2】

[root@Jin /]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED          SIZE
jin/centos            1.0       202c0ab30c5e   19 minutes ago   231MB
tomcat02              1.0       c2ae9e171b97   23 hours ago     684MB
nginx                 latest    605c77e624dd   3 weeks ago      141MB
tomcat                latest    fb5657adc892   4 weeks ago      680MB
redis                 latest    7614ae9453d1   4 weeks ago      113MB
mysql                 5.7       c20987f18b13   4 weeks ago      448MB
centos                latest    5d0da3dc9764   4 months ago     231MB
portainer/portainer   latest    580c0e4e98b0   10 months ago    79.1MB
elasticsearch         7.6.2     f29a1ee41030   22 months ago    791MB
[root@Jin /]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED              STATUS              PORTS                                     NAMES
c63aedb36136   202c0ab30c5e   "/bin/bash"              About a minute ago   Up About a minute                                             unruffled_shockley
eaee05f7dec2   nginx          "/docker-entrypoint.…"   About an hour ago    Up About an hour    0.0.0.0:49168->80/tcp, :::49168->80/tcp   jutilujing-nginx
9346743f602c   nginx          "/docker-entrypoint.…"   4 hours ago          Up 4 hours          0.0.0.0:49155->80/tcp, :::49155->80/tcp   juming-nginx
faf9c51c6676   nginx          "/docker-entrypoint.…"   4 hours ago          Up 4 hours          0.0.0.0:49154->80/tcp, :::49154->80/tcp   niming-nginx
[root@Jin /]# docker inspect c63aedb36136
[
    {
    
    
        "Id": "c63aedb36136791edf06e52583dd1ee8054a1e8c5204e866c82103586f471185",
        "Created": "2022-01-24T06:51:34.022938064Z",
        "Path": "/bin/bash",
        "Args": [],
        "State": {
    
    
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 20275,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2022-01-24T06:51:34.412266581Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:202c0ab30c5e265b96031de5cf54123897c9df843151b09074d7df150ac38f76",
        "ResolvConfPath": "/var/lib/docker/containers/c63aedb36136791edf06e52583dd1ee8054a1e8c5204e866c82103586f471185/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/c63aedb36136791edf06e52583dd1ee8054a1e8c5204e866c82103586f471185/hostname",
        "HostsPath": "/var/lib/docker/containers/c63aedb36136791edf06e52583dd1ee8054a1e8c5204e866c82103586f471185/hosts",
        "LogPath": "/var/lib/docker/containers/c63aedb36136791edf06e52583dd1ee8054a1e8c5204e866c82103586f471185/c63aedb36136791edf06e52583dd1ee8054a1e8c5204e866c82103586f471185-json.log",
        "Name": "/unruffled_shockley",
        "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/17ee5717bde0d5a152b7675091f16086d6d10a9ebad22c3ef3995e6f8dca1a03-init/diff:/var/lib/docker/overlay2/062d86c5e366ca0ececd1c38ff44dee264283b594189692994d190dd67fee589/diff",
                "MergedDir": "/var/lib/docker/overlay2/17ee5717bde0d5a152b7675091f16086d6d10a9ebad22c3ef3995e6f8dca1a03/merged",
                "UpperDir": "/var/lib/docker/overlay2/17ee5717bde0d5a152b7675091f16086d6d10a9ebad22c3ef3995e6f8dca1a03/diff",
                "WorkDir": "/var/lib/docker/overlay2/17ee5717bde0d5a152b7675091f16086d6d10a9ebad22c3ef3995e6f8dca1a03/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [
            {
    
    
                "Type": "volume",
                "Name": "9020518c96b693ad53a823fb5ceaa66aa295bea0f246242fc1512b99b1dc21bf",
                "Source": "/var/lib/docker/volumes/9020518c96b693ad53a823fb5ceaa66aa295bea0f246242fc1512b99b1dc21bf/_data",
                "Destination": "volume01",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            },
            {
    
    
                "Type": "volume",
                "Name": "4748f366cb24774bec86c3746d4cbb43cc8b0f4940ed11d6719a58c81b5a8846",
                "Source": "/var/lib/docker/volumes/4748f366cb24774bec86c3746d4cbb43cc8b0f4940ed11d6719a58c81b5a8846/_data",
                "Destination": "volume02",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],
        "Config": {
    
    
            "Hostname": "c63aedb36136",
            "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": "202c0ab30c5e",
            "Volumes": {
    
    
                "volume01": {
    
    },
                "volume02": {
    
    }
            },
            "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": "73133fdedfbfd4c5af9ee22fc1ac82cd56bdc1ef1b2e11c921d5375d921df25f",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
    
    },
            "SandboxKey": "/var/run/docker/netns/73133fdedfbf",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "33784f6344e6c71a397438201cc2be3d7ac2df8b4e5b480ab01ac7d1d6dd644f",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.5",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:05",
            "Networks": {
    
    
                "bridge": {
    
    
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "7b8a815e7cd106aefd34943f8f9a8cdb55febac4620a4f820874412a6ccc74ec",
                    "EndpointID": "33784f6344e6c71a397438201cc2be3d7ac2df8b4e5b480ab01ac7d1d6dd644f",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.5",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:05",
                    "DriverOpts": null
                }
            }
        }
    }
]
[root@Jin /]# cd /var/lib/docker/volumes/9020518c96b693ad53a823fb5ceaa66aa295bea0f246242fc1512b99b1dc21bf/_data
[root@Jin _data]# ls
container.txt

1643007668281

5. Data volume container

1643008241045

1. [Port 1] parent container

[root@Jin /]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED          SIZE
jin/centos            1.0       202c0ab30c5e   40 minutes ago   231MB
tomcat02              1.0       c2ae9e171b97   23 hours ago     684MB
nginx                 latest    605c77e624dd   3 weeks ago      141MB
tomcat                latest    fb5657adc892   4 weeks ago      680MB
redis                 latest    7614ae9453d1   4 weeks ago      113MB
mysql                 5.7       c20987f18b13   4 weeks ago      448MB
centos                latest    5d0da3dc9764   4 months ago     231MB
portainer/portainer   latest    580c0e4e98b0   10 months ago    79.1MB
elasticsearch         7.6.2     f29a1ee41030   22 months ago    791MB
[root@Jin /]# docker run -it --name docker01 jin/centos:1.0
[root@d7ad0e34eba3 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var  volume01	volume02
[root@d7ad0e34eba3 /]# cd volume01
[root@d7ad0e34eba3 volume01]# ls
[root@d7ad0e34eba3 volume01]# touch docker01
[root@d7ad0e34eba3 volume01]# ls
docker01  docker02
[root@d7ad0e34eba3 volume01]# ls
docker01  docker02  docker03

1643009339429

2. [Port 2]

[root@Jin /]# docker ps
CONTAINER ID   IMAGE            COMMAND                  CREATED          STATUS          PORTS     NAMES
d7ad0e34eba3   jin/centos:1.0   "/bin/sh -c /bin/bash"   31 seconds ago   Up 30 seconds             docker01
[root@Jin /]# docker run -it --name docker02 --volumes-from docker01 jin/centos:1.0
[root@f13c79d3b59c /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var  volume01	volume02
[root@f13c79d3b59c /]# cd volume01
[root@f13c79d3b59c volume01]# ls
docker01
[root@f13c79d3b59c volume01]# touch docker02
[root@f13c79d3b59c volume01]# ls
docker01  docker02
[root@f13c79d3b59c volume01]# ls
docker01  docker02  docker03

1643009403156

3. [Port 3]

[root@Jin /]# docker ps
CONTAINER ID   IMAGE            COMMAND                  CREATED         STATUS         PORTS     NAMES
f13c79d3b59c   jin/centos:1.0   "/bin/sh -c /bin/bash"   2 minutes ago   Up 2 minutes             docker02
d7ad0e34eba3   jin/centos:1.0   "/bin/sh -c /bin/bash"   3 minutes ago   Up 3 minutes             docker01
[root@Jin /]# docker run -it --name docker03 --volumes-from docker01 jin/centos:1.0
[root@dd6116b88490 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var  volume01	volume02
[root@dd6116b88490 /]# cd volume01
[root@dd6116b88490 volume01]# ls
docker01  docker02
[root@dd6116b88490 volume01]# touch docker03
[root@dd6116b88490 volume01]# ls
docker01  docker02  docker03

1643009460678

4. [Port 4]

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-9fklZe8t-1679562589575) (https://gitee.com/happy_sad/typora-image/raw/master/images /1643009548398.png)]

5 Conclusion:

1、除非所有关联的容器挂了,不然只要有一个在操作,数据就依然存在。
2、容器之间配置信息的传递,数据卷容器的生命周期一直持续到没有容器使用位置。
3、但是如果持久化到了本地,即使所有容器删除了,本地数据是不会删除的。

8. DockerFile

1. Introduction to DockerFile

It is a file used to build a docker image, which can be understood as a command parameter script

Build steps:

  1. Write a dockerfile
  2. docker build builds into a mirror image
  3. docker run run image
  4. docker push release image (DockerHub, Alibaba Cloud mirror warehouse private/shared)

1643010292605

1643010613655

2. Basic knowledge of DockerFile

Docker is development-oriented. If we want to release projects and make images in the future, we need to write a dockerfile. This file is very simple! Docker mirroring has gradually become the delivery standard of enterprises and must be mastered!

  1. Each command must be in capital letters
  2. Execute in order from top to bottom
  3. *#* means comment
  4. Each command will create a new image layer and submit it

1643010683992

Steps: development, deployment, operation and maintenance... all are indispensable!

DockerFile: 构建文件, 定义了一切的步骤,源代码
DockerImages: 通过DockerFile构建生成的镜像, 最终发布和运行的产品!
Docker容器:容器就是镜像运行起来提供服务器

3. DockerFile instruction description

1643010985697

FROM            # 基础镜像,一切从这里开始构建
MAINTAINER      # 镜像是谁写的, 姓名+邮箱
RUN             # 镜像构建的时候需要运行的命令
ADD             # 步骤, tomcat镜像, 这个tomcat压缩包!添加内容
WORKDIR         # 镜像的工作目录
VOLUME          # 挂载的目录
EXPOSE          # 保留端口配置
CMD             # 指定这个容器启动的时候要运行的命令,只有最后一个会生效可被替代
ENTRYPOINT      # 指定这个容器启动的时候要运行的命令, 可以追加命令
ONBUILD         # 当构建一个被继承DockerFile 这个时候就会运行 ONBUILD 的指令,触发指令
COPY            # 类似ADD, 将我们文件拷贝到镜像中
ENV             # 构建的时候设置环境变量!

Fourth, make a centos image

1. [Port 2] There are no official commands by default

[root@Jin /]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
jin/centos            1.0       202c0ab30c5e   2 hours ago     231MB
tomcat02              1.0       c2ae9e171b97   24 hours ago    684MB
nginx                 latest    605c77e624dd   3 weeks ago     141MB
tomcat                latest    fb5657adc892   4 weeks ago     680MB
redis                 latest    7614ae9453d1   4 weeks ago     113MB
mysql                 5.7       c20987f18b13   4 weeks ago     448MB
centos                latest    5d0da3dc9764   4 months ago    231MB
portainer/portainer   latest    580c0e4e98b0   10 months ago   79.1MB
elasticsearch         7.6.2     f29a1ee41030   22 months ago   791MB
[root@Jin /]# docker run -it centos				#运行官方的centos
[root@b5bb3fd852b7 /]# pwd						#默认的目录是跟目录
/
[root@b5bb3fd852b7 /]# vim						#没有vim命令
bash: vim: command not found
[root@b5bb3fd852b7 /]# ifconfig					#没有ifconfig命令
bash: ifconfig: command not found

1643012199074

2. [Port 1] configure dockerfile and build

mydockerfile file content

FROM centos
MAINTAINER jin<879466586@qq.com>

ENV MYPATH /usr/local
WORKDIR $MYPATH

RUN yum -y install vim
RUN yum -y install net-tools

EXPOSE 80

CMD echo $MYPATH
CMD echo "---end---"
CMD /bin/bash

1643012543723

[root@Jin /]# cd /home
[root@Jin home]# ls
copy.java  docker  docker-test-volume  Jin  JinTest  Jin.txt  mysql  nginx  redis  test  www
[root@Jin home]# mkdir dockerfile
[root@Jin home]# ls
copy.java  docker  dockerfile  docker-test-volume  Jin  JinTest  Jin.txt  mysql  nginx  redis  test  www
[root@Jin home]# cd dockerfile
[root@Jin dockerfile]# vim mydockerfile-centos
[root@Jin dockerfile]# cat mydockerfile-centos 
FROM centos
MAINTAINER jin<879466586@qq.com>

ENV MYPATH /usr/local
WORKDIR $MYPATH

RUN yum -y install vim
RUN yum -y install net-tools

EXPOSE 80

CMD echo $MYPATH
CMD echo "---end---"
CMD /bin/bash
[root@Jin dockerfile]# docker build -f mydockerfile-centos -t mycentos:0.1 .
Sending build context to Docker daemon  2.048kB
Step 1/10 : FROM centos
 ---> 5d0da3dc9764
Step 2/10 : MAINTAINER jin<879466586@qq.com>
 ---> Running in 21002a5934bc
Removing intermediate container 21002a5934bc
 ---> 01154e6e44df
Step 3/10 : ENV MYPATH /usr/local
 ---> Running in 96a741e2883f
Removing intermediate container 96a741e2883f
 ---> ce71e94b6557
Step 4/10 : WORKDIR $MYPATH
 ---> Running in 230ee9abfb01
Removing intermediate container 230ee9abfb01
 ---> 9c111c5a92f4
Step 5/10 : RUN yum -y install vim
 ---> Running in 580cd1835dc9
CentOS Linux 8 - AppStream                      8.8 MB/s | 8.4 MB     00:00    
CentOS Linux 8 - BaseOS                         3.5 MB/s | 4.6 MB     00:01    
CentOS Linux 8 - Extras                          14 kB/s |  10 kB     00:00    
Dependencies resolved.
================================================================================
 Package             Arch        Version                   Repository      Size
================================================================================
Installing:
 vim-enhanced        x86_64      2:8.0.1763-16.el8         appstream      1.4 M
Installing dependencies:
 gpm-libs            x86_64      1.20.7-17.el8             appstream       39 k
 vim-common          x86_64      2:8.0.1763-16.el8         appstream      6.3 M
 vim-filesystem      noarch      2:8.0.1763-16.el8         appstream       49 k
 which               x86_64      2.21-16.el8               baseos          49 k

Transaction Summary
================================================================================
Install  5 Packages

Total download size: 7.8 M
Installed size: 30 M
Downloading Packages:
(1/5): gpm-libs-1.20.7-17.el8.x86_64.rpm        393 kB/s |  39 kB     00:00    
(2/5): vim-filesystem-8.0.1763-16.el8.noarch.rp 1.3 MB/s |  49 kB     00:00    
(3/5): vim-enhanced-8.0.1763-16.el8.x86_64.rpm  4.7 MB/s | 1.4 MB     00:00    
(4/5): which-2.21-16.el8.x86_64.rpm             243 kB/s |  49 kB     00:00    
(5/5): vim-common-8.0.1763-16.el8.x86_64.rpm    3.8 MB/s | 6.3 MB     00:01    
--------------------------------------------------------------------------------
Total                                           2.9 MB/s | 7.8 MB     00:02     
warning: /var/cache/dnf/appstream-02e86d1c976ab532/packages/gpm-libs-1.20.7-17.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
CentOS Linux 8 - AppStream                       25 kB/s | 1.6 kB     00:00    
Importing GPG key 0x8483C65D:
 Userid     : "CentOS (CentOS Official Signing Key) <[email protected]>"
 Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1 
  Installing       : which-2.21-16.el8.x86_64                               1/5 
  Installing       : vim-filesystem-2:8.0.1763-16.el8.noarch                2/5 
  Installing       : vim-common-2:8.0.1763-16.el8.x86_64                    3/5 
  Installing       : gpm-libs-1.20.7-17.el8.x86_64                          4/5 
  Running scriptlet: gpm-libs-1.20.7-17.el8.x86_64                          4/5 
  Installing       : vim-enhanced-2:8.0.1763-16.el8.x86_64                  5/5 
  Running scriptlet: vim-enhanced-2:8.0.1763-16.el8.x86_64                  5/5 
  Running scriptlet: vim-common-2:8.0.1763-16.el8.x86_64                    5/5 
  Verifying        : gpm-libs-1.20.7-17.el8.x86_64                          1/5 
  Verifying        : vim-common-2:8.0.1763-16.el8.x86_64                    2/5 
  Verifying        : vim-enhanced-2:8.0.1763-16.el8.x86_64                  3/5 
  Verifying        : vim-filesystem-2:8.0.1763-16.el8.noarch                4/5 
  Verifying        : which-2.21-16.el8.x86_64                               5/5 

Installed:
  gpm-libs-1.20.7-17.el8.x86_64         vim-common-2:8.0.1763-16.el8.x86_64    
  vim-enhanced-2:8.0.1763-16.el8.x86_64 vim-filesystem-2:8.0.1763-16.el8.noarch
  which-2.21-16.el8.x86_64             

Complete!
Removing intermediate container 580cd1835dc9
 ---> a3cdf5f0e8c5
Step 6/10 : RUN yum -y install net-tools
 ---> Running in fc88f4c3c810
Last metadata expiration check: 0:00:12 ago on Mon Jan 24 08:28:30 2022.
Dependencies resolved.
================================================================================
 Package         Architecture Version                        Repository    Size
================================================================================
Installing:
 net-tools       x86_64       2.0-0.52.20160912git.el8       baseos       322 k

Transaction Summary
================================================================================
Install  1 Package

Total download size: 322 k
Installed size: 942 k
Downloading Packages:
net-tools-2.0-0.52.20160912git.el8.x86_64.rpm   1.1 MB/s | 322 kB     00:00    
--------------------------------------------------------------------------------
Total                                           441 kB/s | 322 kB     00:00     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1 
  Installing       : net-tools-2.0-0.52.20160912git.el8.x86_64              1/1 
  Running scriptlet: net-tools-2.0-0.52.20160912git.el8.x86_64              1/1 
  Verifying        : net-tools-2.0-0.52.20160912git.el8.x86_64              1/1 

Installed:
  net-tools-2.0-0.52.20160912git.el8.x86_64                                     

Complete!
Removing intermediate container fc88f4c3c810
 ---> 009d3e2f2d13
Step 7/10 : EXPOSE 80
 ---> Running in e1c1ec5aa2f3
Removing intermediate container e1c1ec5aa2f3
 ---> 0dfd4f54cb01
Step 8/10 : CMD echo $MYPATH
 ---> Running in d2e99e671532
Removing intermediate container d2e99e671532
 ---> acbce20a8438
Step 9/10 : CMD echo "---end---"
 ---> Running in 818a2433e464
Removing intermediate container 818a2433e464
 ---> bc984addcf57
Step 10/10 : CMD /bin/bash
 ---> Running in 01c3fd926a57
Removing intermediate container 01c3fd926a57
 ---> 5f23516ae29a
Successfully built 5f23516ae29a
Successfully tagged mycentos:0.1
[root@Jin dockerfile]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED          SIZE
mycentos              0.1       5f23516ae29a   24 seconds ago   326MB
jin/centos            1.0       202c0ab30c5e   2 hours ago      231MB
tomcat02              1.0       c2ae9e171b97   24 hours ago     684MB
nginx                 latest    605c77e624dd   3 weeks ago      141MB
tomcat                latest    fb5657adc892   4 weeks ago      680MB
redis                 latest    7614ae9453d1   4 weeks ago      113MB
mysql                 5.7       c20987f18b13   4 weeks ago      448MB
centos                latest    5d0da3dc9764   4 months ago     231MB
portainer/portainer   latest    580c0e4e98b0   10 months ago    79.1MB
elasticsearch         7.6.2     f29a1ee41030   22 months ago    791MB
[root@Jin dockerfile]# docker run -it mycentos:0.1
[root@bc70bcdf8742 local]# pwd
/usr/local
[root@bc70bcdf8742 local]# vim test
[root@bc70bcdf8742 local]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.3  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:03  txqueuelen 0  (Ethernet)
        RX packets 8  bytes 656 (656.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-2hBoVb30-1679562589578) (https://gitee.com/happy_sad/typora-image/raw/master/images /1643013223008.png)]

3. View the history of dockerfile image construction

[Port 1] docker history image ID

[root@bc70bcdf8742 local]# exit
exit
[root@Jin dockerfile]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED          SIZE
mycentos              0.1       5f23516ae29a   14 minutes ago   326MB
jin/centos            1.0       202c0ab30c5e   2 hours ago      231MB
tomcat02              1.0       c2ae9e171b97   25 hours ago     684MB
nginx                 latest    605c77e624dd   3 weeks ago      141MB
tomcat                latest    fb5657adc892   4 weeks ago      680MB
redis                 latest    7614ae9453d1   4 weeks ago      113MB
mysql                 5.7       c20987f18b13   4 weeks ago      448MB
centos                latest    5d0da3dc9764   4 months ago     231MB
portainer/portainer   latest    580c0e4e98b0   10 months ago    79.1MB
elasticsearch         7.6.2     f29a1ee41030   22 months ago    791MB
[root@Jin dockerfile]# docker history 5f23516ae29a
IMAGE          CREATED          CREATED BY                                      SIZE      COMMENT
5f23516ae29a   14 minutes ago   /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "/bin…   0B        
bc984addcf57   14 minutes ago   /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "echo…   0B        
acbce20a8438   14 minutes ago   /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "echo…   0B        
0dfd4f54cb01   14 minutes ago   /bin/sh -c #(nop)  EXPOSE 80                    0B        
009d3e2f2d13   14 minutes ago   /bin/sh -c yum -y install net-tools             28.4MB    
a3cdf5f0e8c5   14 minutes ago   /bin/sh -c yum -y install vim                   66.3MB    
9c111c5a92f4   14 minutes ago   /bin/sh -c #(nop) WORKDIR /usr/local            0B        
ce71e94b6557   14 minutes ago   /bin/sh -c #(nop)  ENV MYPATH=/usr/local        0B        
01154e6e44df   14 minutes ago   /bin/sh -c #(nop)  MAINTAINER jin<879466586@…   0B        
5d0da3dc9764   4 months ago     /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B        
<missing>      4 months ago     /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B        
<missing>      4 months ago     /bin/sh -c #(nop) ADD file:805cb5e15fb6e0bb0…   231MB    

1643013828905

Five, CMD and ENTRYPOINT

CMD				# 指定这个容器启动的时候运行的命令,只有最后一个回生效,可被替代
ENTRYPOINT		 # 指定这个容器启动的时候要运行的命令,可以追加命令

1、CMD

dockerfile-cmd-test file content

FROM centos
CMD ["ls","-a"]

1643014332299

【Port 1】

[root@Jin /]# cd home
[root@Jin home]# ls
copy.java  docker  dockerfile  docker-test-volume  Jin  JinTest  Jin.txt  mysql  nginx  redis  test  www
[root@Jin home]# cd dockerfile
[root@Jin dockerfile]# ls
mydockerfile-centos
[root@Jin dockerfile]# vim dockerfile-cmd-test		#编写dockerfile文件
[root@Jin dockerfile]# cat dockerfile-cmd-test 
FROM centos
CMD ["ls","-a"]
[root@Jin dockerfile]# docker build -f dockerfile-cmd-test -t cmd-test .	#构建镜像
Sending build context to Docker daemon  3.072kB
Step 1/2 : FROM centos
 ---> 5d0da3dc9764
Step 2/2 : CMD ["ls","-a"]
 ---> Using cache
 ---> e321aea9b23a
Successfully built e321aea9b23a
Successfully tagged cmd-test:latest
[root@Jin dockerfile]# docker run e321aea9b23a				#运行一下,命令生效
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
#cmd的清理下 -l 替换CMD ["ls","-a"]命令,-l不是命令所以报错
[root@Jin dockerfile]# docker run e321aea9b23a -l		#想追加一个命令 -l,	ls -al
docker: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "-l": executable file not found in $PATH: unknown.
[root@Jin dockerfile]# docker run e321aea9b23a ls -al
total 56
drwxr-xr-x   1 root root 4096 Jan 24 09:22 .
drwxr-xr-x   1 root root 4096 Jan 24 09:22 ..
-rwxr-xr-x   1 root root    0 Jan 24 09:22 .dockerenv
lrwxrwxrwx   1 root root    7 Nov  3  2020 bin -> usr/bin
drwxr-xr-x   5 root root  340 Jan 24 09:22 dev
drwxr-xr-x   1 root root 4096 Jan 24 09:22 etc
drwxr-xr-x   2 root root 4096 Nov  3  2020 home
lrwxrwxrwx   1 root root    7 Nov  3  2020 lib -> usr/lib
lrwxrwxrwx   1 root root    9 Nov  3  2020 lib64 -> usr/lib64
drwx------   2 root root 4096 Sep 15 14:17 lost+found
drwxr-xr-x   2 root root 4096 Nov  3  2020 media
drwxr-xr-x   2 root root 4096 Nov  3  2020 mnt
drwxr-xr-x   2 root root 4096 Nov  3  2020 opt
dr-xr-xr-x 111 root root    0 Jan 24 09:22 proc
dr-xr-x---   2 root root 4096 Sep 15 14:17 root
drwxr-xr-x  11 root root 4096 Sep 15 14:17 run
lrwxrwxrwx   1 root root    8 Nov  3  2020 sbin -> usr/sbin
drwxr-xr-x   2 root root 4096 Nov  3  2020 srv
dr-xr-xr-x  13 root root    0 Jan 24 06:45 sys
drwxrwxrwt   7 root root 4096 Sep 15 14:17 tmp
drwxr-xr-x  12 root root 4096 Sep 15 14:17 usr
drwxr-xr-x  20 root root 4096 Sep 15 14:17 var

1643016316090

2、ENTRYPOINT

dockerfile-entrypoint-test file content

FROM centos
ENTRYPOINT ["ls", "-a"]

1643017794198

【Port 2】

[root@Jin /]# cd /home
[root@Jin home]# ls
copy.java  docker  dockerfile  docker-test-volume  Jin  JinTest  Jin.txt  mysql  nginx  redis  test  www
[root@Jin home]# cd dockerfile
[root@Jin dockerfile]# ls
dockerfile-cmd-test  mydockerfile-centos
[root@Jin dockerfile]# vim dockerfile-entrypoint-test
[root@Jin dockerfile]# cat dockerfile-entrypoint-test 
FROM centos
ENTRYPOINT ["ls", "-a"]
[root@Jin dockerfile]# docker build -f dockerfile-entrypoint-test -t entrypoint-test .
Sending build context to Docker daemon  4.096kB
Step 1/2 : FROM centos
 ---> 5d0da3dc9764
Step 2/2 : ENTRYPOINT ["ls", "-a"]
 ---> Running in 84f53a8505f8
Removing intermediate container 84f53a8505f8
 ---> 59b145d99b79
Successfully built 59b145d99b79
Successfully tagged entrypoint-test:latest
[root@Jin dockerfile]# docker run 59b145d99b79
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
[root@Jin dockerfile]# docker run 59b145d99b79 -l	追加命令-l,是直接拼接在ENTRYPOINT命令后面
total 56
drwxr-xr-x   1 root root 4096 Jan 24 09:51 .
drwxr-xr-x   1 root root 4096 Jan 24 09:51 ..
-rwxr-xr-x   1 root root    0 Jan 24 09:51 .dockerenv
lrwxrwxrwx   1 root root    7 Nov  3  2020 bin -> usr/bin
drwxr-xr-x   5 root root  340 Jan 24 09:51 dev
drwxr-xr-x   1 root root 4096 Jan 24 09:51 etc
drwxr-xr-x   2 root root 4096 Nov  3  2020 home
lrwxrwxrwx   1 root root    7 Nov  3  2020 lib -> usr/lib
lrwxrwxrwx   1 root root    9 Nov  3  2020 lib64 -> usr/lib64
drwx------   2 root root 4096 Sep 15 14:17 lost+found
drwxr-xr-x   2 root root 4096 Nov  3  2020 media
drwxr-xr-x   2 root root 4096 Nov  3  2020 mnt
drwxr-xr-x   2 root root 4096 Nov  3  2020 opt
dr-xr-xr-x 111 root root    0 Jan 24 09:51 proc
dr-xr-x---   2 root root 4096 Sep 15 14:17 root
drwxr-xr-x  11 root root 4096 Sep 15 14:17 run
lrwxrwxrwx   1 root root    8 Nov  3  2020 sbin -> usr/sbin
drwxr-xr-x   2 root root 4096 Nov  3  2020 srv
dr-xr-xr-x  13 root root    0 Jan 24 06:45 sys
drwxrwxrwt   7 root root 4096 Sep 15 14:17 tmp
drwxr-xr-x  12 root root 4096 Sep 15 14:17 usr
drwxr-xr-x  20 root root 4096 Sep 15 14:17 var

1643017999666

6. Make tomcat image

1. Create a tomcat file to store the installation package

【Port 1】

[root@Jin /]# cd home
[root@Jin home]# ls
docker  dockerfile  docker-test-volume  Jin  JinTest  mysql  nginx  redis  www
[root@Jin home]# cd Jin
[root@Jin Jin]# ls
nginx
[root@Jin Jin]# mkdir tomcat
[root@Jin Jin]# ls
nginx  tomcat
[root@Jin Jin]# cd tomcat
[root@Jin tomcat]# ls
apache-tomcat-9.0.58.tar.gz  jdk-8u161-linux-x64.tar.gz

1643079542808

1643079604456

2. Write Dockerfile

Dockerfile file (the official default search Dockerfile)

FROM centos
MAINTAINER Jin<879466586@qq.com>

COPY readme.txt /usr/local/readme.txt

ADD jdk-8u161-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-9.0.58.tar.gz /usr/local/

RUN yum -y install vim

ENV MYPATH /usr/local
WORKDIR $MYPATH

ENV JAVA_HOME /usr/local/jdk1.8.0_161
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.58
ENV CATALINA_BASH /usr/local/apache-tomcat-9.0.58
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin

EXPOSE 8080

CMD /usr/local/apache-tomcat-9.0.58/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.58/bin/logs/catalina.out

1643081093463

【Port 1】

[root@Jin tomcat]# vim Dockerfile
[root@Jin tomcat]# cat Dockerfile 
FROM centos
MAINTAINER Jin<879466586@qq.com>

COPY readme.txt /usr/local/readme.txt
 
ADD jdk-8u161-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-9.0.58.tar.gz /usr/local/
 
RUN yum -y install vim
 
ENV MYPATH /usr/local
WORKDIR $MYPATH
 
ENV JAVA_HOME /usr/local/jdk1.8.0_161
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.58
ENV CATALINA_BASH /usr/local/apache-tomcat-9.0.58
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
 
EXPOSE 8080
 
CMD /usr/local/apache-tomcat-9.0.58/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.58/bin/logs/catalina.out

1643087985432

3. Build Dockerfile

【Port 1】

[root@Jin tomcat]# ls
apache-tomcat-9.0.58.tar.gz  Dockerfile  jdk-8u161-linux-x64.tar.gz  readme.txt
[root@Jin tomcat]# docker build -t mytomcat .
Sending build context to Docker daemon  201.3MB
Step 1/15 : FROM centos
 ---> 5d0da3dc9764
Step 2/15 : MAINTAINER Jin<879466586@qq.com>
 ---> Running in 52bea73a3d40
Removing intermediate container 52bea73a3d40
 ---> 148e4f46aa19
Step 3/15 : COPY readme.txt /usr/local/readme.txt
 ---> 3339a157109c
Step 4/15 : ADD jdk-8u161-linux-x64.tar.gz /usr/local/
 ---> 18e9537c22e9
Step 5/15 : ADD apache-tomcat-9.0.58.tar.gz /usr/local/
 ---> 91632d8d07f9
Step 6/15 : RUN yum -y install vim
 ---> Running in 0d4d07bf2de8
CentOS Linux 8 - AppStream                      979 kB/s | 8.4 MB     00:08    
CentOS Linux 8 - BaseOS                         2.1 MB/s | 4.6 MB     00:02    
CentOS Linux 8 - Extras                          15 kB/s |  10 kB     00:00    
Dependencies resolved.
================================================================================
 Package             Arch        Version                   Repository      Size
================================================================================
Installing:
 vim-enhanced        x86_64      2:8.0.1763-16.el8         appstream      1.4 M
Installing dependencies:
 gpm-libs            x86_64      1.20.7-17.el8             appstream       39 k
 vim-common          x86_64      2:8.0.1763-16.el8         appstream      6.3 M
 vim-filesystem      noarch      2:8.0.1763-16.el8         appstream       49 k
 which               x86_64      2.21-16.el8               baseos          49 k

Transaction Summary
================================================================================
Install  5 Packages

Total download size: 7.8 M
Installed size: 30 M
Downloading Packages:
(1/5): gpm-libs-1.20.7-17.el8.x86_64.rpm        190 kB/s |  39 kB     00:00    
(2/5): vim-filesystem-8.0.1763-16.el8.noarch.rp 404 kB/s |  49 kB     00:00    
(3/5): vim-enhanced-8.0.1763-16.el8.x86_64.rpm  3.2 MB/s | 1.4 MB     00:00    
(4/5): which-2.21-16.el8.x86_64.rpm             272 kB/s |  49 kB     00:00    
(5/5): vim-common-8.0.1763-16.el8.x86_64.rpm    9.9 MB/s | 6.3 MB     00:00    
--------------------------------------------------------------------------------
Total                                           875 kB/s | 7.8 MB     00:09     
warning: /var/cache/dnf/appstream-02e86d1c976ab532/packages/gpm-libs-1.20.7-17.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
CentOS Linux 8 - AppStream                      1.0 MB/s | 1.6 kB     00:00    
Importing GPG key 0x8483C65D:
 Userid     : "CentOS (CentOS Official Signing Key) <[email protected]>"
 Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1 
  Installing       : which-2.21-16.el8.x86_64                               1/5 
  Installing       : vim-filesystem-2:8.0.1763-16.el8.noarch                2/5 
  Installing       : vim-common-2:8.0.1763-16.el8.x86_64                    3/5 
  Installing       : gpm-libs-1.20.7-17.el8.x86_64                          4/5 
  Running scriptlet: gpm-libs-1.20.7-17.el8.x86_64                          4/5 
  Installing       : vim-enhanced-2:8.0.1763-16.el8.x86_64                  5/5 
  Running scriptlet: vim-enhanced-2:8.0.1763-16.el8.x86_64                  5/5 
  Running scriptlet: vim-common-2:8.0.1763-16.el8.x86_64                    5/5 
  Verifying        : gpm-libs-1.20.7-17.el8.x86_64                          1/5 
  Verifying        : vim-common-2:8.0.1763-16.el8.x86_64                    2/5 
  Verifying        : vim-enhanced-2:8.0.1763-16.el8.x86_64                  3/5 
  Verifying        : vim-filesystem-2:8.0.1763-16.el8.noarch                4/5 
  Verifying        : which-2.21-16.el8.x86_64                               5/5 

Installed:
  gpm-libs-1.20.7-17.el8.x86_64         vim-common-2:8.0.1763-16.el8.x86_64    
  vim-enhanced-2:8.0.1763-16.el8.x86_64 vim-filesystem-2:8.0.1763-16.el8.noarch
  which-2.21-16.el8.x86_64             

Complete!
Removing intermediate container 0d4d07bf2de8
 ---> 6f172f822fff
Step 7/15 : ENV MYPATH /usr/local
 ---> Running in e774d1a4b4e4
Removing intermediate container e774d1a4b4e4
 ---> 7f15c049205b
Step 8/15 : WORKDIR $MYPATH
 ---> Running in d208b00a0ba6
Removing intermediate container d208b00a0ba6
 ---> 75dfbcfbb3ae
Step 9/15 : ENV JAVA_HOME /usr/local/jdk1.8.0_161
 ---> Running in 64514a8a9aec
Removing intermediate container 64514a8a9aec
 ---> 582e25c060c0
Step 10/15 : ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
 ---> Running in 613b7575e456
Removing intermediate container 613b7575e456
 ---> e37a7b5087f1
Step 11/15 : ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.58
 ---> Running in f68a99b1dae5
Removing intermediate container f68a99b1dae5
 ---> 4ea58d36847b
Step 12/15 : ENV CATALINA_BASH /usr/local/apache-tomcat-9.0.58
 ---> Running in 257e896ff584
Removing intermediate container 257e896ff584
 ---> ab5097155241
Step 13/15 : ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
 ---> Running in 5609e2dda324
Removing intermediate container 5609e2dda324
 ---> 5ac5bc1cc39f
Step 14/15 : EXPOSE 8080
 ---> Running in d71d0cc8f8e2
Removing intermediate container d71d0cc8f8e2
 ---> 2f452a5796ae
Step 15/15 : CMD /usr/local/apache-tomcat-9.0.58/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.58/bin/logs/catalina.out
 ---> Running in cfafa81911e5
Removing intermediate container cfafa81911e5
 ---> d7bdbbd1573b
Successfully built d7bdbbd1573b
Successfully tagged mytomcat:latest
[root@Jin tomcat]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED          SIZE
mytomcat              latest    d7bdbbd1573b   47 seconds ago   698MB
entrypoint-test       latest    59b145d99b79   20 hours ago     231MB
cmd-test              latest    e321aea9b23a   20 hours ago     231MB
mycentos              0.1       5f23516ae29a   21 hours ago     326MB
jin/centos            1.0       202c0ab30c5e   23 hours ago     231MB
tomcat02              1.0       c2ae9e171b97   45 hours ago     684MB
nginx                 latest    605c77e624dd   3 weeks ago      141MB
tomcat                latest    fb5657adc892   4 weeks ago      680MB
redis                 latest    7614ae9453d1   4 weeks ago      113MB
mysql                 5.7       c20987f18b13   5 weeks ago      448MB
centos                latest    5d0da3dc9764   4 months ago     231MB
portainer/portainer   latest    580c0e4e98b0   10 months ago    79.1MB
elasticsearch         7.6.2     f29a1ee41030   22 months ago    791MB

1643089475944

4. Run the mytomcat image

【Port 1】

[root@Jin tomcat]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED          SIZE
mytomcat              latest    d7bdbbd1573b   30 minutes ago   698MB
entrypoint-test       latest    59b145d99b79   20 hours ago     231MB
cmd-test              latest    e321aea9b23a   21 hours ago     231MB
mycentos              0.1       5f23516ae29a   21 hours ago     326MB
jin/centos            1.0       202c0ab30c5e   23 hours ago     231MB
tomcat02              1.0       c2ae9e171b97   46 hours ago     684MB
nginx                 latest    605c77e624dd   3 weeks ago      141MB
tomcat                latest    fb5657adc892   4 weeks ago      680MB
redis                 latest    7614ae9453d1   4 weeks ago      113MB
mysql                 5.7       c20987f18b13   5 weeks ago      448MB
centos                latest    5d0da3dc9764   4 months ago     231MB
portainer/portainer   latest    580c0e4e98b0   10 months ago    79.1MB
elasticsearch         7.6.2     f29a1ee41030   22 months ago    791MB
[root@Jin tomcat]# docker run -d -p 39010:8080 --name mytomcat01 -v /home/Jin/tomcat/test:/usr/local/apache-tomcat-9.0.58/webapps/test -v /home/Jin/tomcat/tomcatlogs/:/usr/local/apache-tomcat-9.0.58/logs mytomcat
63f04f54ae3880b9a6d221aef88bc2e19714fb82effd596b8a28ce2a39d81ccb
[root@Jin tomcat]# docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED          STATUS          PORTS                                         NAMES
63f04f54ae38   mytomcat   "/bin/sh -c '/usr/lo…"   21 seconds ago   Up 20 seconds   0.0.0.0:39010->8080/tcp, :::39010->8080/tcp   mytomcat01
[root@Jin tomcat]# docker exec -it 63f04f54ae38 /bin/bash
[root@63f04f54ae38 local]# ls
aegis  apache-tomcat-9.0.58  bin  etc  games  include  jdk1.8.0_161  lib  lib64  libexec  readme.txt  sbin  share  src
[root@63f04f54ae38 local]# pwd
/usr/local
[root@63f04f54ae38 local]# cd apache-tomcat-9.0.58/
[root@63f04f54ae38 apache-tomcat-9.0.58]# ls
BUILDING.txt  CONTRIBUTING.md  LICENSE	NOTICE	README.md  RELEASE-NOTES  RUNNING.txt  bin  conf  lib  logs  temp  webapps  work

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-CMIOgFrK-1679562589584) (https://gitee.com/happy_sad/typora-image/raw/master/images /1643090312508.png)]

5. Run the test

【Port 2】
[root@Jin /]# curl localhost:39010



<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <title>Apache Tomcat/9.0.58</title>
        <link href="favicon.ico" rel="icon" type="image/x-icon" />
        <link href="tomcat.css" rel="stylesheet" type="text/css" />
    </head>

    <body>
        <div id="wrapper">
            <div id="navigation" class="curved container">
                <span id="nav-home"><a href="https://tomcat.apache.org/">Home</a></span>
                <span id="nav-hosts"><a href="/docs/">Documentation</a></span>
                <span id="nav-config"><a href="/docs/config/">Configuration</a></span>
                <span id="nav-examples"><a href="/examples/">Examples</a></span>
                <span id="nav-wiki"><a href="https://wiki.apache.org/tomcat/FrontPage">Wiki</a></span>
                <span id="nav-lists"><a href="https://tomcat.apache.org/lists.html">Mailing Lists</a></span>
                <span id="nav-help"><a href="https://tomcat.apache.org/findhelp.html">Find Help</a></span>
                <br class="separator" />
            </div>
            <div id="asf-box">
                <h1>Apache Tomcat/9.0.58</h1>
            </div>
            <div id="upper" class="curved container">
                <div id="congrats" class="curved container">
                    <h2>If you're seeing this, you've successfully installed Tomcat. Congratulations!</h2>
                </div>
                <div id="notice">
                    <img id="tomcat-logo" src="tomcat.svg" alt="[tomcat logo]" />
                    <div id="tasks">
                        <h3>Recommended Reading:</h3>
                        <h4><a href="/docs/security-howto.html">Security Considerations How-To</a></h4>
                        <h4><a href="/docs/manager-howto.html">Manager Application How-To</a></h4>
                        <h4><a href="/docs/cluster-howto.html">Clustering/Session Replication How-To</a></h4>
                    </div>
                </div>
                <div id="actions">
                    <div class="button">
                        <a class="container shadow" href="/manager/status"><span>Server Status</span></a>
                    </div>
                    <div class="button">
                        <a class="container shadow" href="/manager/html"><span>Manager App</span></a>
                    </div>
                    <div class="button">
                        <a class="container shadow" href="/host-manager/html"><span>Host Manager</span></a>
                    </div>
                </div>
                <br class="separator" />
            </div>
            <div id="middle" class="curved container">
                <h3>Developer Quick Start</h3>
                <div class="col25">
                    <div class="container">
                        <p><a href="/docs/setup.html">Tomcat Setup</a></p>
                        <p><a href="/docs/appdev/">First Web Application</a></p>
                    </div>
                </div>
                <div class="col25">
                    <div class="container">
                        <p><a href="/docs/realm-howto.html">Realms &amp; AAA</a></p>
                        <p><a href="/docs/jndi-datasource-examples-howto.html">JDBC DataSources</a></p>
                    </div>
                </div>
                <div class="col25">
                    <div class="container">
                        <p><a href="/examples/">Examples</a></p>
                    </div>
                </div>
                <div class="col25">
                    <div class="container">
                        <p><a href="https://wiki.apache.org/tomcat/Specifications">Servlet Specifications</a></p>
                        <p><a href="https://wiki.apache.org/tomcat/TomcatVersions">Tomcat Versions</a></p>
                    </div>
                </div>
                <br class="separator" />
            </div>
            <div id="lower">
                <div id="low-manage" class="">
                    <div class="curved container">
                        <h3>Managing Tomcat</h3>
                        <p>For security, access to the <a href="/manager/html">manager webapp</a> is restricted.
                        Users are defined in:</p>
                        <pre>$CATALINA_HOME/conf/tomcat-users.xml</pre>
                        <p>In Tomcat 9.0 access to the manager application is split between
                           different users. &nbsp; <a href="/docs/manager-howto.html">Read more...</a></p>
                        <br />
                        <h4><a href="/docs/RELEASE-NOTES.txt">Release Notes</a></h4>
                        <h4><a href="/docs/changelog.html">Changelog</a></h4>
                        <h4><a href="https://tomcat.apache.org/migration.html">Migration Guide</a></h4>
                        <h4><a href="https://tomcat.apache.org/security.html">Security Notices</a></h4>
                    </div>
                </div>
                <div id="low-docs" class="">
                    <div class="curved container">
                        <h3>Documentation</h3>
                        <h4><a href="/docs/">Tomcat 9.0 Documentation</a></h4>
                        <h4><a href="/docs/config/">Tomcat 9.0 Configuration</a></h4>
                        <h4><a href="https://wiki.apache.org/tomcat/FrontPage">Tomcat Wiki</a></h4>
                        <p>Find additional important configuration information in:</p>
                        <pre>$CATALINA_HOME/RUNNING.txt</pre>
                        <p>Developers may be interested in:</p>
                        <ul>
                            <li><a href="https://tomcat.apache.org/bugreport.html">Tomcat 9.0 Bug Database</a></li>
                            <li><a href="/docs/api/index.html">Tomcat 9.0 JavaDocs</a></li>
                            <li><a href="https://github.com/apache/tomcat/tree/9.0.x">Tomcat 9.0 Git Repository at GitHub</a></li>
                        </ul>
                    </div>
                </div>
                <div id="low-help" class="">
                    <div class="curved container">
                        <h3>Getting Help</h3>
                        <h4><a href="https://tomcat.apache.org/faq/">FAQ</a> and <a href="https://tomcat.apache.org/lists.html">Mailing Lists</a></h4>
                        <p>The following mailing lists are available:</p>
                        <ul>
                            <li id="list-announce"><strong><a href="https://tomcat.apache.org/lists.html#tomcat-announce">tomcat-announce</a><br />
                                Important announcements, releases, security vulnerability notifications. (Low volume).</strong>
                            </li>
                            <li><a href="https://tomcat.apache.org/lists.html#tomcat-users">tomcat-users</a><br />
                                User support and discussion
                            </li>
                            <li><a href="https://tomcat.apache.org/lists.html#taglibs-user">taglibs-user</a><br />
                                User support and discussion for <a href="https://tomcat.apache.org/taglibs/">Apache Taglibs</a>
                            </li>
                            <li><a href="https://tomcat.apache.org/lists.html#tomcat-dev">tomcat-dev</a><br />
                                Development mailing list, including commit messages
                            </li>
                        </ul>
                    </div>
                </div>
                <br class="separator" />
            </div>
            <div id="footer" class="curved container">
                <div class="col20">
                    <div class="container">
                        <h4>Other Downloads</h4>
                        <ul>
                            <li><a href="https://tomcat.apache.org/download-connectors.cgi">Tomcat Connectors</a></li>
                            <li><a href="https://tomcat.apache.org/download-native.cgi">Tomcat Native</a></li>
                            <li><a href="https://tomcat.apache.org/taglibs/">Taglibs</a></li>
                            <li><a href="/docs/deployer-howto.html">Deployer</a></li>
                        </ul>
                    </div>
                </div>
                <div class="col20">
                    <div class="container">
                        <h4>Other Documentation</h4>
                        <ul>
                            <li><a href="https://tomcat.apache.org/connectors-doc/">Tomcat Connectors</a></li>
                            <li><a href="https://tomcat.apache.org/connectors-doc/">mod_jk Documentation</a></li>
                            <li><a href="https://tomcat.apache.org/native-doc/">Tomcat Native</a></li>
                            <li><a href="/docs/deployer-howto.html">Deployer</a></li>
                        </ul>
                    </div>
                </div>
                <div class="col20">
                    <div class="container">
                        <h4>Get Involved</h4>
                        <ul>
                            <li><a href="https://tomcat.apache.org/getinvolved.html">Overview</a></li>
                            <li><a href="https://tomcat.apache.org/source.html">Source Repositories</a></li>
                            <li><a href="https://tomcat.apache.org/lists.html">Mailing Lists</a></li>
                            <li><a href="https://wiki.apache.org/tomcat/FrontPage">Wiki</a></li>
                        </ul>
                    </div>
                </div>
                <div class="col20">
                    <div class="container">
                        <h4>Miscellaneous</h4>
                        <ul>
                            <li><a href="https://tomcat.apache.org/contact.html">Contact</a></li>
                            <li><a href="https://tomcat.apache.org/legal.html">Legal</a></li>
                            <li><a href="https://www.apache.org/foundation/sponsorship.html">Sponsorship</a></li>
                            <li><a href="https://www.apache.org/foundation/thanks.html">Thanks</a></li>
                        </ul>
                    </div>
                </div>
                <div class="col20">
                    <div class="container">
                        <h4>Apache Software Foundation</h4>
                        <ul>
                            <li><a href="https://tomcat.apache.org/whoweare.html">Who We Are</a></li>
                            <li><a href="https://tomcat.apache.org/heritage.html">Heritage</a></li>
                            <li><a href="https://www.apache.org">Apache Home</a></li>
                            <li><a href="https://tomcat.apache.org/resources.html">Resources</a></li>
                        </ul>
                    </div>
                </div>
                <br class="separator" />
            </div>
            <p class="copyright">Copyright &copy;1999-2022 Apache Software Foundation.  All Rights Reserved</p>
        </div>
    </body>

</html>

1643090815752

Web page access:

http://120.76.157.172:39010/

1643090948185

6. Release the project

Write web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
        
</web-app>

1643092749525

index.jsp page

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>hello. xiaofan</title>
</head>
<body>
Hello World!<br/>
<%
System.out.println("-----my test web logs------");
%>
</body>
</html>

1643093668438

【Port 2】

[root@Jin /]# cd /home/Jin/tomcat
[root@Jin tomcat]# ls
apache-tomcat-9.0.58.tar.gz  Dockerfile  jdk-8u161-linux-x64.tar.gz  readme.txt  test  tomcatlogs
[root@Jin tomcat]# cd test/
[root@Jin test]# mkdir WEB-INF
[root@Jin test]# ls
WEB-INF
[root@Jin test]# cd WEB-INF/
[root@Jin WEB-INF]# vim web.xml
[root@Jin WEB-INF]# cd ..
[root@Jin test]# vim index.jsp
[root@Jin test]# ls
index.jsp  WEB-INF
[root@Jin test]# cat index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>hello. Jin</title>
</head>
<body>
Hello World!<br/>
<%
System.out.println("-----my test web logs------");
%>
</body>
</html>

1643093555424

view log

[root@Jin test]# cd ..
[root@Jin tomcat]# ls
apache-tomcat-9.0.58.tar.gz  Dockerfile  jdk-8u161-linux-x64.tar.gz  readme.txt  test  tomcatlogs
[root@Jin tomcat]# cd tomcatlogs/
[root@Jin tomcatlogs]# ls -ll
total 24
-rw-r----- 1 root root 6708 Jan 25 14:39 catalina.2022-01-25.log
-rw-r----- 1 root root 6736 Jan 25 14:49 catalina.out
-rw-r----- 1 root root    0 Jan 25 13:55 host-manager.2022-01-25.log
-rw-r----- 1 root root  408 Jan 25 13:55 localhost.2022-01-25.log
-rw-r----- 1 root root  989 Jan 25 14:49 localhost_access_log.2022-01-25.txt
-rw-r----- 1 root root    0 Jan 25 13:55 manager.2022-01-25.log
[root@Jin tomcatlogs]# cat catalina.out
25-Jan-2022 05:55:38.398 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name:   Apache Tomcat/9.0.58
25-Jan-2022 05:55:38.515 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Jan 15 2022 14:37:38 UTC
25-Jan-2022 05:55:38.515 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version number: 9.0.58.0
25-Jan-2022 05:55:38.515 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Linux
25-Jan-2022 05:55:38.515 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:            3.10.0-1127.19.1.el7.x86_64
25-Jan-2022 05:55:38.515 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:          amd64
25-Jan-2022 05:55:38.516 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             /usr/local/jdk1.8.0_161/jre
25-Jan-2022 05:55:38.516 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           1.8.0_161-b12
25-Jan-2022 05:55:38.516 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            Oracle Corporation
25-Jan-2022 05:55:38.516 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         /usr/local/apache-tomcat-9.0.58
25-Jan-2022 05:55:38.516 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         /usr/local/apache-tomcat-9.0.58
25-Jan-2022 05:55:38.523 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/apache-tomcat-9.0.58/conf/logging.properties
25-Jan-2022 05:55:38.523 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
25-Jan-2022 05:55:38.523 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
25-Jan-2022 05:55:38.523 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
25-Jan-2022 05:55:38.523 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dorg.apache.catalina.security.SecurityListener.UMASK=0027
25-Jan-2022 05:55:38.523 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
25-Jan-2022 05:55:38.523 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/apache-tomcat-9.0.58
25-Jan-2022 05:55:38.523 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/apache-tomcat-9.0.58
25-Jan-2022 05:55:38.524 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/apache-tomcat-9.0.58/temp
25-Jan-2022 05:55:38.525 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The Apache Tomcat Native library which allows using OpenSSL was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib]
25-Jan-2022 05:55:39.670 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
25-Jan-2022 05:55:39.722 INFO [main] org.apache.catalina.startup.Catalina.load Server initialization in [1740] milliseconds
25-Jan-2022 05:55:39.785 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
25-Jan-2022 05:55:39.785 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet engine: [Apache Tomcat/9.0.58]
25-Jan-2022 05:55:39.805 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/apache-tomcat-9.0.58/webapps/docs]
25-Jan-2022 05:55:40.394 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/apache-tomcat-9.0.58/webapps/docs] has finished in [589] ms
25-Jan-2022 05:55:40.395 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/apache-tomcat-9.0.58/webapps/ROOT]
25-Jan-2022 05:55:40.448 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/apache-tomcat-9.0.58/webapps/ROOT] has finished in [53] ms
25-Jan-2022 05:55:40.448 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/apache-tomcat-9.0.58/webapps/host-manager]
25-Jan-2022 05:55:40.524 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/apache-tomcat-9.0.58/webapps/host-manager] has finished in [76] ms
25-Jan-2022 05:55:40.524 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/apache-tomcat-9.0.58/webapps/manager]
25-Jan-2022 05:55:40.578 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/apache-tomcat-9.0.58/webapps/manager] has finished in [54] ms
25-Jan-2022 05:55:40.578 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/apache-tomcat-9.0.58/webapps/examples]
25-Jan-2022 05:55:41.120 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/apache-tomcat-9.0.58/webapps/examples] has finished in [541] ms
25-Jan-2022 05:55:41.120 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/apache-tomcat-9.0.58/webapps/test]
25-Jan-2022 05:55:41.169 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/apache-tomcat-9.0.58/webapps/test] has finished in [49] ms
25-Jan-2022 05:55:41.178 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
25-Jan-2022 05:55:41.238 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [1516] milliseconds
25-Jan-2022 06:39:51.600 INFO [Catalina-utility-1] org.apache.catalina.startup.HostConfig.reload Reloading context [/test]
25-Jan-2022 06:39:51.601 INFO [Catalina-utility-1] org.apache.catalina.core.StandardContext.reload Reloading Context with name [/test] has started
25-Jan-2022 06:39:51.668 INFO [Catalina-utility-1] org.apache.catalina.core.StandardContext.reload Reloading Context with name [/test] is completed
-----my test web logs------

1643093873761

7. Publish the image

1. Publish the image to DockerHub

1、DockerHub
# 账号:jinki
# 密码:yjj227819

DockerHub official website: https://hub.docker.com/

1643094094531

2. The image name submitted by docker push
# 提交的镜像名 等价于 账号/镜像名:版本号(Username/Image:Tag)
# 步骤:
1、docker tag [制作的镜像名] 账号/镜像名:版本号
2、docker images
2、docker push 账号/镜像名:版本号
[root@Jin /]# docker login --help			#查看登录dockerhub的命令

Usage:  docker login [OPTIONS] [SERVER]

Log in to a Docker registry.
If no server is specified, the default is defined by the daemon.

Options:
  -p, --password string   Password
      --password-stdin    Take the password from stdin
  -u, --username string   Username
[root@Jin /]# docker login -u jinki		   # docker login -u 账户
Password: 								 # 密码
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[root@Jin /]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
mytomcat              latest    d7bdbbd1573b   2 hours ago     698MB
entrypoint-test       latest    59b145d99b79   22 hours ago    231MB
cmd-test              latest    e321aea9b23a   22 hours ago    231MB
mycentos              0.1       5f23516ae29a   23 hours ago    326MB
jin/centos            1.0       202c0ab30c5e   25 hours ago    231MB
tomcat02              1.0       c2ae9e171b97   47 hours ago    684MB
nginx                 latest    605c77e624dd   3 weeks ago     141MB
tomcat                latest    fb5657adc892   4 weeks ago     680MB
redis                 latest    7614ae9453d1   4 weeks ago     113MB
mysql                 5.7       c20987f18b13   5 weeks ago     448MB
centos                latest    5d0da3dc9764   4 months ago    231MB
portainer/portainer   latest    580c0e4e98b0   10 months ago   79.1MB
elasticsearch         7.6.2     f29a1ee41030   22 months ago   791MB
[root@Jin /]# docker push jin/mytomcat:1.0			#以下几种方法都没有成功
The push refers to repository [docker.io/jin/mytomcat]
An image does not exist locally with the tag: jin/mytomcat
[root@Jin /]# docker push jin/mytomcat
Using default tag: latest
The push refers to repository [docker.io/jin/mytomcat]
An image does not exist locally with the tag: jin/mytomcat
[root@Jin /]# docker push jinki/mytomcat
Using default tag: latest
The push refers to repository [docker.io/jinki/mytomcat]
An image does not exist locally with the tag: jinki/mytomcat
[root@Jin /]# docker push mytomcat
Using default tag: latest
The push refers to repository [docker.io/library/mytomcat]
8376280e9e29: Preparing 
652d035abf0b: Preparing 
a80817105c03: Preparing 
34d84c167f88: Preparing 
74ddd0ec08fa: Preparing 
denied: requested access to the resource is denied
[root@Jin /]# docker tag mytomcat jinki/tomcat:1.0	#docker tag [制作的镜像名] 账号/镜像名:版本号
[root@Jin /]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
mytomcat              latest    d7bdbbd1573b   2 hours ago     698MB
jinki/tomcat          1.0       d7bdbbd1573b   2 hours ago     698MB
entrypoint-test       latest    59b145d99b79   22 hours ago    231MB
cmd-test              latest    e321aea9b23a   23 hours ago    231MB
mycentos              0.1       5f23516ae29a   23 hours ago    326MB
jin/centos            1.0       202c0ab30c5e   25 hours ago    231MB
tomcat02              1.0       c2ae9e171b97   47 hours ago    684MB
nginx                 latest    605c77e624dd   3 weeks ago     141MB
tomcat                latest    fb5657adc892   4 weeks ago     680MB
redis                 latest    7614ae9453d1   4 weeks ago     113MB
mysql                 5.7       c20987f18b13   5 weeks ago     448MB
centos                latest    5d0da3dc9764   4 months ago    231MB
portainer/portainer   latest    580c0e4e98b0   10 months ago   79.1MB
elasticsearch         7.6.2     f29a1ee41030   22 months ago   791MB
[root@Jin /]# docker push jinki/tomcat:1.0		#docker push 账号/镜像名:版本号
The push refers to repository [docker.io/jinki/tomcat]
8376280e9e29: Pushed 
652d035abf0b: Pushed 
a80817105c03: Pushed 
34d84c167f88: Pushed 
74ddd0ec08fa: Pushed 
1.0: digest: sha256:007aaaffa7f82cf96d008745ab06f0619cf752c19a7edaef5a9d1bda87a31728 size: 1373

1643099182864

3. Results display

1643100397641

4. The image name submitted by docker rmi and the image name submitted by docker pull
[root@Jin /]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
jinki/tomcat          1.0       d7bdbbd1573b   3 hours ago     698MB
mytomcat              latest    d7bdbbd1573b   3 hours ago     698MB
entrypoint-test       latest    59b145d99b79   23 hours ago    231MB
cmd-test              latest    e321aea9b23a   24 hours ago    231MB
mycentos              0.1       5f23516ae29a   24 hours ago    326MB
jin/centos            1.0       202c0ab30c5e   26 hours ago    231MB
tomcat02              1.0       c2ae9e171b97   2 days ago      684MB
nginx                 latest    605c77e624dd   3 weeks ago     141MB
tomcat                latest    fb5657adc892   4 weeks ago     680MB
redis                 latest    7614ae9453d1   4 weeks ago     113MB
mysql                 5.7       c20987f18b13   5 weeks ago     448MB
centos                latest    5d0da3dc9764   4 months ago    231MB
portainer/portainer   latest    580c0e4e98b0   10 months ago   79.1MB
elasticsearch         7.6.2     f29a1ee41030   22 months ago   791MB
[root@Jin /]# docker rmi jinki/tomcat:1.0		#docker rmi 提交的镜像名
Untagged: jinki/tomcat:1.0
Untagged: jinki/tomcat@sha256:007aaaffa7f82cf96d008745ab06f0619cf752c19a7edaef5a9d1bda87a31728
[root@Jin /]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
mytomcat              latest    d7bdbbd1573b   3 hours ago     698MB
entrypoint-test       latest    59b145d99b79   23 hours ago    231MB
cmd-test              latest    e321aea9b23a   24 hours ago    231MB
mycentos              0.1       5f23516ae29a   24 hours ago    326MB
jin/centos            1.0       202c0ab30c5e   26 hours ago    231MB
tomcat02              1.0       c2ae9e171b97   2 days ago      684MB
nginx                 latest    605c77e624dd   3 weeks ago     141MB
tomcat                latest    fb5657adc892   4 weeks ago     680MB
redis                 latest    7614ae9453d1   4 weeks ago     113MB
mysql                 5.7       c20987f18b13   5 weeks ago     448MB
centos                latest    5d0da3dc9764   4 months ago    231MB
portainer/portainer   latest    580c0e4e98b0   10 months ago   79.1MB
elasticsearch         7.6.2     f29a1ee41030   22 months ago   791MB
[root@Jin /]# docker pull jinki/tomcat:1.0		#docker pull 提交的镜像名
1.0: Pulling from jinki/tomcat
Digest: sha256:007aaaffa7f82cf96d008745ab06f0619cf752c19a7edaef5a9d1bda87a31728
Status: Downloaded newer image for jinki/tomcat:1.0
docker.io/jinki/tomcat:1.0
[root@Jin /]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
jinki/tomcat          1.0       d7bdbbd1573b   3 hours ago     698MB
mytomcat              latest    d7bdbbd1573b   3 hours ago     698MB
entrypoint-test       latest    59b145d99b79   23 hours ago    231MB
cmd-test              latest    e321aea9b23a   24 hours ago    231MB
mycentos              0.1       5f23516ae29a   24 hours ago    326MB
jin/centos            1.0       202c0ab30c5e   26 hours ago    231MB
tomcat02              1.0       c2ae9e171b97   2 days ago      684MB
nginx                 latest    605c77e624dd   3 weeks ago     141MB
tomcat                latest    fb5657adc892   4 weeks ago     680MB
redis                 latest    7614ae9453d1   4 weeks ago     113MB
mysql                 5.7       c20987f18b13   5 weeks ago     448MB
centos                latest    5d0da3dc9764   4 months ago    231MB
portainer/portainer   latest    580c0e4e98b0   10 months ago   79.1MB
elasticsearch         7.6.2     f29a1ee41030   22 months ago   791MB

1643101193346

5. Log out
[root@Jin /]# docker logout
Removing login credentials for https://index.docker.io/v1/

1643103360511

2. Publish the image to the Alibaba Cloud container

1. Alibaba Cloud server

Official website: https://ecs.console.aliyun.com/#/home

1643101989806

Create a personal instance

1643102143132

Set Registry Password

password:*****************

1643102286263

create namespace

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-58tpZJhF-1679562589590) (https://gitee.com/happy_sad/typora-image/raw/master/images /1643102404493.png)]

1643102516248

Create a container image

[External link image transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the image and upload it directly (img-pbTXkJZe-1679562589592) (https://gitee.com/happy_sad/typora-image/raw/master/images /1643102669049.png)]

1643102711949

Result display

2. Alibaba Cloud release image steps

1643103112930

1643103172994

1643103197278

1. 登录阿里云Docker Registry
$ docker login --username=jin进 registry.cn-hangzhou.aliyuncs.com
用于登录的用户名为阿里云账号全名,密码为开通服务时设置的密码。

您可以在访问凭证页面修改凭证密码。

2. 从Registry中拉取镜像
$ docker pull registry.cn-hangzhou.aliyuncs.com/jinki/jin-jinki:[镜像版本号]
3. 将镜像推送到Registry
$ docker login --username=jin进 registry.cn-hangzhou.aliyuncs.com
$ docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/jinki/jin-jinki:[镜像版本号]
$ docker push registry.cn-hangzhou.aliyuncs.com/jinki/jin-jinki:[镜像版本号]
请根据实际镜像信息替换示例中的[ImageId][镜像版本号]参数。

4. 选择合适的镜像仓库地址
从ECS推送镜像时,可以选择使用镜像仓库内网地址。推送速度将得到提升并且将不会损耗您的公网流量。

如果您使用的机器位于VPC网络,请使用 registry-vpc.cn-hangzhou.aliyuncs.com 作为Registry的域名登录。

5. 示例
使用"docker tag"命令重命名镜像,并将它通过专有网络地址推送至Registry。

$ docker images
REPOSITORY                                                         TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
registry.aliyuncs.com/acs/agent                                    0.7-dfb6816         37bb9c63c8b2        7 days ago          37.89 MB
$ docker tag 37bb9c63c8b2 registry-vpc.cn-hangzhou.aliyuncs.com/acs/agent:0.7-dfb6816
使用 "docker push" 命令将该镜像推送至远程。

$ docker push registry-vpc.cn-hangzhou.aliyuncs.com/acs/agent:0.7-dfb6816
3. login, tag, push (strictly regulated Alibaba Cloud)
[root@Jin /]# docker login --username=jin进 registry.cn-hangzhou.aliyuncs.com
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[root@Jin /]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
jinki/tomcat          1.0       d7bdbbd1573b   9 hours ago     698MB
mytomcat              latest    d7bdbbd1573b   9 hours ago     698MB
entrypoint-test       latest    59b145d99b79   29 hours ago    231MB
cmd-test              latest    e321aea9b23a   30 hours ago    231MB
mycentos              0.1       5f23516ae29a   30 hours ago    326MB
jin/centos            1.0       202c0ab30c5e   32 hours ago    231MB
tomcat02              1.0       c2ae9e171b97   2 days ago      684MB
nginx                 latest    605c77e624dd   3 weeks ago     141MB
tomcat                latest    fb5657adc892   4 weeks ago     680MB
redis                 latest    7614ae9453d1   5 weeks ago     113MB
mysql                 5.7       c20987f18b13   5 weeks ago     448MB
centos                latest    5d0da3dc9764   4 months ago    231MB
portainer/portainer   latest    580c0e4e98b0   10 months ago   79.1MB
elasticsearch         7.6.2     f29a1ee41030   22 months ago   791MB
[root@Jin /]# docker tag d7bdbbd1573b registry.cn-hangzhou.aliyuncs.com/jinki/jin-jinki:1.0
[root@Jin /]# docker images
REPOSITORY                                          TAG       IMAGE ID       CREATED         SIZE
registry.cn-hangzhou.aliyuncs.com/jinki/jin-jinki   1.0       d7bdbbd1573b   9 hours ago     698MB
jinki/tomcat                                        1.0       d7bdbbd1573b   9 hours ago     698MB
mytomcat                                            latest    d7bdbbd1573b   9 hours ago     698MB
entrypoint-test                                     latest    59b145d99b79   29 hours ago    231MB
cmd-test                                            latest    e321aea9b23a   30 hours ago    231MB
mycentos                                            0.1       5f23516ae29a   30 hours ago    326MB
jin/centos                                          1.0       202c0ab30c5e   32 hours ago    231MB
tomcat02                                            1.0       c2ae9e171b97   2 days ago      684MB
nginx                                               latest    605c77e624dd   3 weeks ago     141MB
tomcat                                              latest    fb5657adc892   4 weeks ago     680MB
redis                                               latest    7614ae9453d1   5 weeks ago     113MB
mysql                                               5.7       c20987f18b13   5 weeks ago     448MB
centos                                              latest    5d0da3dc9764   4 months ago    231MB
portainer/portainer                                 latest    580c0e4e98b0   10 months ago   79.1MB
elasticsearch                                       7.6.2     f29a1ee41030   22 months ago   791MB
[root@Jin /]# docker push registry.cn-hangzhou.aliyuncs.com/jinki/jin-jinki:1.0
The push refers to repository [registry.cn-hangzhou.aliyuncs.com/jinki/jin-jinki]
8376280e9e29: Pushed 
652d035abf0b: Pushed 
a80817105c03: Pushed 
34d84c167f88: Pushed 
74ddd0ec08fa: Pushed 
1.0: digest: sha256:007aaaffa7f82cf96d008745ab06f0619cf752c19a7edaef5a9d1bda87a31728 size: 1373

1643123724523

1643121103889

4. Results display

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-erPsGSNM-1679562589595) (https://gitee.com/happy_sad/typora-image/raw/master/images /1643158948168.png)]

8. Summary

1643121133592

1642733718478

Guess you like

Origin blog.csdn.net/weixin_45207323/article/details/129735060