Docker getting started process

Basic docker basic commands

docker start command

[root@localhost ~]# systemctl start docker

Query docker command help

[root@localhost ~]# docker --help

View image

[root@localhost ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
mysql         5.7       c20987f18b13   5 months ago   448MB
mysql         latest    3218b38490ce   5 months ago   516MB
hello-world   latest    feb5d9fea6a5   7 months ago   13.3kB

docker search command (it will go to the image address to search)

[root@localhost ~]# docker search mysql
NAME                           DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                          MySQL is a widely used, open-source relation…   12606     [OK]       
mariadb                        MariaDB Server is a high performing open sou…   4843      [OK]       
percona                        Percona Server is a fork of the MySQL relati…   576       [OK]       
phpmyadmin                     phpMyAdmin - A web interface for MySQL and M…   542       [OK]       
bitnami/mysql                  Bitnami MySQL Docker Image                      71                   [OK]
linuxserver/mysql-workbench                                                    36                   
linuxserver/mysql              A Mysql container, brought to you by LinuxSe…   35                   
ubuntu/mysql                   MySQL open source fast, stable, multi-thread…   33                   
circleci/mysql                 MySQL is a widely used, open-source relation…   26                   
google/mysql                   MySQL server for Google Compute Engine          21                   [OK]
rapidfort/mysql                RapidFort optimized, hardened image for mysql   12                   
vmware/harbor-db               Mysql container for Harbor                      10                   
bitnami/mysqld-exporter                                                        3                    
ibmcom/mysql-s390x             Docker image for mysql-s390x                    2                    
nasqueron/mysql                                                                1                    [OK]
newrelic/mysql-plugin          New Relic Plugin for monitoring MySQL databa…   1                    [OK]
vitess/mysqlctld               vitess/mysqlctld                                1                    [OK]
cimg/mysql                                                                     0                    
mirantis/mysql                                                                 0                    
drud/mysql-local-57            ddev mysql local container                      0                    
drud/mysql-docker-local-57     This repo has been deprecated, new tags are …   0                    
drud/mysql                                                                     0                    
drud/mysql-docker-local        docker containers for local womysql rk          0                    [OK]
docksal/mysql                  MySQL service images for Docksal - https://d…   0                    
silintl/mysql-backup-restore   Simple docker image to perform mysql backups…   0                    [OK]

docker pull download image

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

# 指定版本下载
[root@localhost ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Already exists     #上面已经下载过的mysql镜像,这一次可以共用,所以表示已存在
93619dbc5b36: Already exists 
99da31dd6142: Already exists 
626033c43d70: Already exists 
37d5d7efb64e: Already exists 
ac563158d721: Already exists 
d2ba16033dad: Already exists 
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

docker rmi delete image

# 查看已有的镜像
[root@localhost ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
mysql         5.7       c20987f18b13   5 months ago   448MB
mysql         latest    3218b38490ce   5 months ago   516MB
hello-world   latest    feb5d9fea6a5   7 months ago   13.3kB

 # 根据镜像的id进行删除
[root@localhost ~]# docker rmi -f c20987f18b13  
Untagged: mysql:5.7
Untagged: mysql@sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Deleted: sha256:c20987f18b130f9d144c9828df630417e2a9523148930dc3963e9d0dab302a76
Deleted: sha256:6567396b065ee734fb2dbb80c8923324a778426dfd01969f091f1ab2d52c7989

# 查看镜像,删除成功
[root@localhost ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
mysql         latest    3218b38490ce   5 months ago   516MB
hello-world   latest    feb5d9fea6a5   7 months ago   13.3kB

# 查询出所有镜像后进行递归删除
[root@localhost ~]# docker rmi -f $(docker images -aq)
Untagged: mysql:latest
Untagged: mysql@sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Deleted: sha256:3218b38490cec8d31976a40b92e09d61377359eab878db49f025e5d464367f3b
Untagged: hello-world:latest
Untagged: hello-world@sha256:80f31da1ac7b312ba29d65080fddf797dd76acfb870e677f390d5acba9741b17
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412

Container commands

Note: Only when we have an image can we create a container. For Linux, download a centos image to test and learn.

[root@localhost ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos

# 查看镜像, 镜像已经下载成功
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
centos       latest    5d0da3dc9764   8 months ago   231MB
1. Create a new container and start it
docker run [可选参数] image    # 可选参数使用 docker run --help  命令查看有哪些

# 参数说明
--name="name"	容器名字 tomcat01   tomcat02  用来区分容器
-d 			后台方式运行 
-it			使用交互方式运行,进入容器查看内容
-p				指定容器的端口 -p 8080:8080\
	-p 主机端口:容器端口(常用)
	-p 容器端口
-p	随机指定端口

	 		
test:
Start the container and give it the name it
[root@localhost ~]# docker run -it centos /bin/bash  	
[root@317beaac0681 /]# 					
Indicates that it has entered the container

Looking inside the started container, you can see that the contents of this container are no different from ordinary containers.
[root@317beaac0681 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var 
Exit container
exit   # 容器停止运行并退出
ctrl + p + q  # 容器不停止退出

[root@317beaac0681 /]# exit    # 容器停止运行并退出

# ctrl + P + Q  # 容器不停止退出 (在键盘的大写模式下按这几个键)
[root@4cac2196aa2e /]# [root@localhost ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND       CREATED              STATUS              PORTS     NAMES
4cac2196aa2e   centos    "/bin/bash"   13 seconds ago       Up 10 seconds                 peaceful_carver
64efab6e12e5   centos    "/bin/bash"   About a minute ago   Up About a minute             objective_jackson
List all running containers
# docker ps 命令 
-a			# 列出当前正在运行的容器(默认容器),顺带带出历史运行过的容器
-n=?		# 只显示最近创建的容器
-q			# 只显示容器的编号

[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS                      PORTS     NAMES
3bd0aed6e189   centos         "/bin/bash"   45 seconds ago   Exited (0) 17 seconds ago             hopeful_williams
598addfa8720   centos         "/bin/bash"   5 hours ago      Exited (0) 5 hours ago                quirky_agnesi
317beaac0681   centos         "/bin/bash"   5 hours ago      Exited (130) 5 hours ago              fervent_lalande
83c174518303   feb5d9fea6a5   "/hello"      46 hours ago     Exited (0) 46 hours ago               thirsty_davinci
204a0f5e6da0   feb5d9fea6a5   "/hello"      46 hours ago     Exited (0) 46 hours ago               clever_archimedes		

 docker ps 
CONTAINER ID   IMAGE     COMMAND       CREATED              STATUS              PORTS     NAMES
4cac2196aa2e   centos    "/bin/bash"   13 seconds ago       Up 10 seconds                 peaceful_carver
64efab6e12e5   centos    "/bin/bash"   About a minute ago   Up About a minute             objective_jackson
Delete container
docker rm 容器id		# 根据容器id删除容器(不能删除正在运行的容器,要强制删除利用 rm -f)
docker rm -f $(docker ps -aq)	# 删除所有容器
docker ps -a -q|xargs docker rm	# 删除所有容器
下面是列子:

# 由于rm 容器id 不能删除正在运行的容器而导致报错
[root@localhost ~]# docker rm 4cac2196aa2e
Error response from daemon: You cannot remove a running container 4cac2196aa2e1df5d16f987c560d3dd81dea7cfa1bdb362cc4e4150856421d0b. Stop the container before attempting removal or force remove
[root@localhost ~]# docker ps -aq
4cac2196aa2e
64efab6e12e5
3bd0aed6e189
598addfa8720
317beaac0681
83c174518303
204a0f5e6da0

# rm -f 即可删除正在运行的容器 $(docker ps -aq): 查询出所有正在运行的容器
[root@localhost ~]# docker rm -f $(docker ps -aq)
4cac2196aa2e
64efab6e12e5
3bd0aed6e189
598addfa8720
317beaac0681
83c174518303
204a0f5e6da0
[root@localhost ~]# docker ps -aq		
[root@localhost ~]# 				# 可以看到,容器都被删除掉了
Starting and stopping container operations
docker start 容器id		# 启动容器
docker restart 容器id	# 重启容器
docker stop 容器id		# 停止 当前正在运行的容器
docker kill 			# 强制停止当前容器

下面是例子(需要有历史运行过的容器,才可以使用以上四条命令):

# 先运行一个容器(这里的目的为了能找到历史运行容器id,否则无法使用上面的方式启动容器)
[root@localhost ~]# docker run -it centos /bin/bash   

# 把该容器退出
[root@00faae8b4bf2 /]# exit
exit

# 查看当前正在运行的容器(为空)
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

# 查看历史运行过的容器(可以看到 容器id=00faae8b4bf2。有了该容器id 就可以用以上命令启动或者停止容器了)
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED              STATUS                          PORTS     NAMES
00faae8b4bf2   centos    "/bin/bash"   19 seconds ago       Exited (127) 8 seconds ago                suspicious_meninsky
1581a1d0cf0a   centos    "/bin/bash"   About a minute ago   Exited (0) About a minute ago             serene_bouman

# 使用docker start 容器id 命令启动容器
[root@localhost ~]# docker start 00faae8b4bf2
00faae8b4bf2
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS         PORTS     NAMES
00faae8b4bf2   centos    "/bin/bash"   45 seconds ago   Up 4 seconds             suspicious_meninsky

# 使用docker stop 容器id 停止容器
[root@localhost ~]# docker stop 00faae8b4bf2
00faae8b4bf2
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost ~]# 

Other commonly used docker commands

Start container in background

Note: After starting the container with this command, use docker ps to check and you will find that the container has stopped.
Reason: If the docker container runs in the background, there must be a foreground process. If docker finds that there is no application, it will automatically stop
the container. After installing an nginx, after the container is started, it finds that it does not provide services, and it will stop immediately, that is, there is no program.

# docker run -d 镜像名
View log
docker logs -f -t --tail [要显示的日志条数] 容器id 
# 自己编写一段shell脚本,循环打印一句话(为了查看日志)
[root@localhost ~]# docker run -d centos /bin/sh -c "while true;do echo chenxiansheng;sleep 1;done"
3926294a2cca4431540e8c5aced519f22c0999f9e6466bd96e767cea548c53ae
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
3926294a2cca   centos    "/bin/sh -c 'while t…"   5 seconds ago   Up 4 seconds             fervent_torvalds

# 查看日志命令
[root@localhost ~]# docker logs -t -f --tail 5 3926294a2cca
2022-05-22T06:56:16.970217803Z chenxiansheng
2022-05-22T06:56:17.983250772Z chenxiansheng
2022-05-22T06:56:18.994815474Z chenxiansheng
2022-05-22T06:56:19.998974466Z chenxiansheng
2022-05-22T06:56:21.003155944Z chenxiansheng
^C
[root@localhost ~]# 
View container running process information
docker top 容器id

# 例子:
[root@localhost ~]# docker top 3926294a2cca
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                1967                1947                0                   14:54               ?                   00:00:00            /bin/sh -c while true;do echo chenxiansheng;sleep 1;done
root                2845                1967                0                   15:08               ?                   00:00:00            /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
View image metadata
docker inspect  

# docker inspect 能使用的命令
[root@localhost ~]# docker inspect --help

Usage:  docker inspect [OPTIONS] NAME|ID [NAME|ID...]

Return low-level information on Docker objects

Options:
  -f, --format string   Format the output using the given Go template
  -s, --size            Display total file sizes if the type is container
      --type string     Return JSON for specified type


下面是使用例子:
# 可以看到镜像id 是该进程id的前缀
[root@localhost ~]# docker inspect 3926294a2cca
[
    {
    
    
        "Id": "3926294a2cca4431540e8c5aced519f22c0999f9e6466bd96e767cea548c53ae",
        "Created": "2022-05-22T06:54:51.055490626Z",
        "Path": "/bin/sh",
        "Args": [
            "-c",
            "while true;do echo chenxiansheng;sleep 1;done"
        ],
        "State": {
    
    
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 1967,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2022-05-22T06:54:52.152158173Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
        "ResolvConfPath": "/var/lib/docker/containers/3926294a2cca4431540e8c5aced519f22c0999f9e6466bd96e767cea548c53ae/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/3926294a2cca4431540e8c5aced519f22c0999f9e6466bd96e767cea548c53ae/hostname",
        "HostsPath": "/var/lib/docker/containers/3926294a2cca4431540e8c5aced519f22c0999f9e6466bd96e767cea548c53ae/hosts",
        "LogPath": "/var/lib/docker/containers/3926294a2cca4431540e8c5aced519f22c0999f9e6466bd96e767cea548c53ae/3926294a2cca4431540e8c5aced519f22c0999f9e6466bd96e767cea548c53ae-json.log",
        "Name": "/fervent_torvalds",
        "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/e564fca82acaf0d594fea6217e190503e5674ff9909a45429ec75cb0d8ab2d72-init/diff:/var/lib/docker/overlay2/8da0b0258a4c475bbdf2c544b058908a98b9e203ccddd77c36dcad7dc096162a/diff",
                "MergedDir": "/var/lib/docker/overlay2/e564fca82acaf0d594fea6217e190503e5674ff9909a45429ec75cb0d8ab2d72/merged",
                "UpperDir": "/var/lib/docker/overlay2/e564fca82acaf0d594fea6217e190503e5674ff9909a45429ec75cb0d8ab2d72/diff",
                "WorkDir": "/var/lib/docker/overlay2/e564fca82acaf0d594fea6217e190503e5674ff9909a45429ec75cb0d8ab2d72/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
    
    
            "Hostname": "3926294a2cca",
            "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 chenxiansheng;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": "d80dfcd35589cac557f556bf5f47895d0d7dc9eabf461985c703b240daed0f33",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
    
    },
            "SandboxKey": "/var/run/docker/netns/d80dfcd35589",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "41234855c20dd7890acb5e3a48217c14e61a1e3ab876d01f79ed03b88d543b88",
            "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": "5702770d4ed5c8618623571e5e7d4fa386b63c152a28bc01c40830bb8e389d21",
                    "EndpointID": "41234855c20dd7890acb5e3a48217c14e61a1e3ab876d01f79ed03b88d543b88",
                    "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
                }
            }
        }
    }
]
Enter the currently running container
docker exec -it 容器id bashShell			# 进入容器后开启一个新的终端,可以再里面操作
docker attach 容器id						# 进入容器正在执行的终端,不会开启新的进程

-it    # 表示使用交互模式

下面是例子
[root@localhost ~]# docker exec -it 3926294a2cca /bin/bash
[root@3926294a2cca /]# 
Copy files from the container to the host
docker cp 容器id:容器内路径 目的主机路径

下面是例子: 首先进入到容器home文件夹建立一个文件,随后退出到本机,再利用docker复制命令把容器的文件复制到本机的home里面来
[root@localhost ~]# docker exec -it 3926294a2cca /bin/bash
[root@3926294a2cca /]# cd home
[root@3926294a2cca home]# 
[root@3926294a2cca home]# touch chenxiansheng222.java 
[root@3926294a2cca home]# ls
chenxiansheng222.java
[root@3926294a2cca home]# exit
exit
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS                        PORTS     NAMES
3926294a2cca   centos    "/bin/sh -c 'while t…"   35 minutes ago   Up 35 minutes                           fervent_torvalds
47d1e769d463   centos    "/bin/sh -c 'while t…"   36 minutes ago   Exited (137) 35 minutes ago             great_wing
00faae8b4bf2   centos    "/bin/bash"              18 hours ago     Exited (0) 18 hours ago                 suspicious_meninsky
1581a1d0cf0a   centos    "/bin/bash"              18 hours ago     Exited (0) 18 hours ago                 serene_bouman


# 使用复制命令将容器的文件复制到本机的linux上来
[root@localhost ~]# docker cp 3926294a2cca:/home/chenxiansheng222.java /home 
[root@3926294a2cca home]# exit
exit
[root@localhost /]# cd home/
[root@localhost home]# ls
chenmaolin  chenxiansheng222.java				# 可以看到文件已经正确复制到本机上了

Practical application

Docker install Nginx

1. Download the nginx image
2. Start nginx
3. Test
docker run -d --name nginx01 -p 3344:80 nginx

  • -d # Set up background operation
  • –name nginx01 # Give the nginx to be started a name, name it nginx01
  • -p 3344:80 # 3344 refers to the port number of linux, 80 refers to the default port of the nginx container, change the container's
# 下载nginx 镜像
[root@localhost ~]# 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@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
nginx        latest    605c77e624dd   4 months ago   141MB
centos       latest    5d0da3dc9764   8 months ago   231MB

# 运行nginx镜像,参数说明:

 - -d  					# 设置后台运行
 - --name nginx01		# 给要启动的nginx一个名字,取名为nginx01	
 - -p 3344:80			# 3344指的是linux的端口号,80指的是nginx容器的默认端口,将容器的端口映射到linux端口
 - nginx 				#指的是容器名称REPOSITORY   
[root@localhost ~]# docker run -d --name nginx01 -p 3344:80 nginx
e87309bccecb4163d75b0cf6d87ebadc330e64b6de14e7c31fd0a76da19f23d8

# 查看运行的nginx ,可以看到容器运行成功
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                                   NAMES
e87309bccecb   nginx     "/docker-entrypoint.…"   26 seconds ago   Up 23 seconds   0.0.0.0:3344->80/tcp, :::3344->80/tcp   nginx01

# 测试nginx,可以看到 welcome to nginx ,测试成功
[root@localhost ~]# curl localhost:3344
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html {
    
     color-scheme: light dark; }
body {
    
     width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

You can also directly access the Linux server and add the port started above to test whether nginx runs successfully.
This 192.168.239.128 is the address of my Linux server.
Insert image description here

# 进入到nginx容器查看
[root@localhost ~]# docker exec -it e87309bccecb /bin/bash
root@e87309bccecb:/# ls
bin   dev		   docker-entrypoint.sh  home  lib64  mnt  proc  run   srv  tmp  var
boot  docker-entrypoint.d  etc			 lib   media  opt  root  sbin  sys  usr
root@e87309bccecb:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@e87309bccecb:/# 

docker install tomcat
docker  run -it  --rm  tomcat:9.0			

 - -rm  指的是运行容器停止后,在docker ps -a 下会搜不到历史运行的该容器的id,也就是停止容器后,会立马把该容器的历史删除掉

# 首先下载tomcat镜像
[root@localhost ~]# docker pull tomcat:9.0
9.0: 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 
7662046c36cb: Pull complete 
b93639122cb4: Pull complete 
Digest: sha256:cd96d4f7d3f5fc4d3bc1622ec678207087b8215d55021a607ecaefba80b403ea
Status: Downloaded newer image for tomcat:9.0
docker.io/library/tomcat:9.0

# 运行tomcat ,可以正常看到输出的tomcat的启动日志
[root@localhost ~]# docker run -it --rm tomcat:9.0
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
22-May-2022 13:44:17.732 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name:   Apache Tomcat/9.0.56
22-May-2022 13:44:17.736 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Dec 2 2021 14:30:07 UTC
22-May-2022 13:44:17.743 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         /usr/local/tomcat
22-May-2022 13:44:17.817 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties
22-May-2022 13:44:17.818 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
22-May-2022 13:44:17.818 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
22-May-2022 13:44:17.819 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
22-May-2022 13:44:17.819 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dorg.apache.catalina.security.SecurityListener.UMASK=0027
22-May-2022 13:44:17.819 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
22-May-2022 13:44:17.819 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat
22-May-2022 13:44:17.819 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat
22-May-2022 13:44:17.820 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp
22-May-2022 13:44:17.866 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded Apache Tomcat Native library [1.2.31] using APR version [1.7.0].
22-May-2022 13:44:17.866 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true], UDS [true].
22-May-2022 13:44:17.866 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
22-May-2022 13:44:17.889 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized [OpenSSL 1.1.1k  25 Mar 2021]
22-May-2022 13:44:19.883 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
22-May-2022 13:44:20.074 INFO [main] org.apache.catalina.startup.Catalina.load Server initialization in [3403] milliseconds
22-May-2022 13:44:20.392 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
22-May-2022 13:44:20.402 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet engine: [Apache Tomcat/9.0.56]
22-May-2022 13:44:20.446 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
22-May-2022 13:44:20.516 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [441] milliseconds


As you can see here, after the tomcat container is started, the container is also deleted. This is the role of the –rm command.
Insert image description here

# 继续测试,利用linux的地址,进行访问tomcat(将tomcat8080的默认端口映射到本机的3355端口)
[root@localhost ~]# docker run -d -p 3355:8080 --name tomcat01 tomcat 
77a49b3e9292c5e9bd7acd8bf9f85db7e07005493819fd6773f8158c02c1e670

# 测试访问linux:3355地址,会发现报404,原因是因为该tomcat是阉割版,webapps文件是空的
[root@localhost ~]# curl localhost:3355
<!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@localhost ~]# ^C

Use linux address: 3355 to access the testInsert image description here

Then why does the access to tomcat fail when using Linux address: 3355? The container has obviously started normally.
The reason is that the tomcat is a castrated version, and the corresponding webapps folder is empty.

# 进入到tomcat容器里,ls查看目录
[root@localhost ~]# docker exec -it 77a49b3e9292 /bin/bash
root@77a49b3e9292:/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

# 进入到webapps目录,发现是空目录
root@77a49b3e9292:/usr/local/tomcat# cd webapps
root@77a49b3e9292:/usr/local/tomcat/webapps# ls
root@77a49b3e9292:/usr/local/tomcat/webapps# 

However, it can be seen that although the webapps directory of the castrated version of tomcat is empty, the corresponding webapps.dist directory has content, and the content inside is exactly what the webapps file needs.

root@77a49b3e9292:/usr/local/tomcat# cd webapps.dist/
root@77a49b3e9292:/usr/local/tomcat/webapps.dist# ll		# 由于是阉割版本,所有ll命令都没有
bash: ll: command not found

# 可以看到,webapps.dist 目录下的文件都是webapps所需要的,把它复制到webapps 目录下
root@77a49b3e9292:/usr/local/tomcat/webapps.dist# ls
ROOT  docs  examples  host-manager  manager					

Use the cp command to copy the files in the webapps.dist directory to the webapps directory. Use the local address plus the 3355 port number to successfully access tomcat.

root@77a49b3e9292:/usr/local/tomcat# cp -r  webapps.dist/* webapps
root@77a49b3e9292:/usr/local/tomcat# cd webapps
root@77a49b3e9292:/usr/local/tomcat/webapps# ls
ROOT  docs  examples  host-manager  manager				# webapps文件有内容了

Reuse the liunx address +3355 (when starting the tomcat container earlier, you specified the 3355 port mapped to linux) port number access: the
Insert image description here
familiar tomcat page, the access is successful! ! !

docker graphical installation

Install Portainer

portainer: Docker graphical interface management tool! Provide a background panel for us to operate

Use the following command to install portainer and map the port to Linux port 8088 (so that you can access it with the Linux address + port number)

docker run -d -p 8088:9000
–restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer

Installation is successful.
Insert image description here
Set account: root Password: rootroot

After logging in, choose to connect locally to view existing images (not the point, generally not used, just understand it):
Insert image description here

The meaning of layering of mirrors (to be added)

commit image

docker commit

提交镜像命令:docker commit -a="chenxiansheng" -m="add webapps app" 77a49b3e9292 chentomcat:1.0
参数说明:
 - -a="chenxiansheng"				# 提交的人的标识
 - -m="add webapps app"    			#提交的描述
 -  77a49b3e9292 					#容器id
 - chentomcat:1.0					#设定镜像名称,并设定版本为1.0

步骤:
# 1、首先利用上面下载好的tomcat镜像,启动一个tomcat容器
# 2、然而这个默认的tomcat是没有webapps应用的,镜像的原因,官方的镜像默认webapps下面是没有文件的
# 3、把基本文件拷贝进webapps里面
# 4、将上面操作过的容器通过commit 提交为一个新的镜像,以后就可以使用自己的镜像,可以直接访问tomcat页面成功。

If you don’t understand the steps mentioned above, you can check the above docker installation tomcat. Since the official tomcat is a castrated version, the tomcat page cannot be accessed (the reason is that there are no files in the webapps directory), we will optimize it here, that is, The files in the webapps directory are complete.

# 使用cp命令,将webapps.dist 目录下的文件拷贝到webapps 目录下,使用本机地址加3355端口号就可以成功访问tomcat了
root@77a49b3e9292:/usr/local/tomcat# cp -r  webapps.dist/* webapps
root@77a49b3e9292:/usr/local/tomcat# cd webapps
root@77a49b3e9292:/usr/local/tomcat/webapps# ls
ROOT  docs  examples  host-manager  manager				# webapps文件有内容了

Test it
Insert image description here
. There is no problem in the test. Now submit this running container as our own image. In the future, we will use our own tomcat image for running. We do not need to copy the files in the webapps.dict directory to the webapps directory again before we can view the tomcat page.

# 从刚刚启动的容器中退出来
root@77a49b3e9292:/usr/local/tomcat/webapps# exit
exit

# 使用提交镜像命令
[root@localhost ~]# docker commit -a="chenxiansheng" -m="add webapps app" 77a49b3e9292 chentomcat:1.0
sha256:8c3fc729e0f1c139d2b7b1cbe7acb2c3f12cb5c8e3cdf307dc5813fb8258c613

You can see that we have generated our own image chentomcat

[root@localhost ~]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED              SIZE
chentomcat            1.0       8c3fc729e0f1   About a minute ago   684MB
nginx                 latest    605c77e624dd   4 months ago         141MB
tomcat                9.0       b8e65a4d736d   5 months ago         680MB
tomcat                latest    fb5657adc892   5 months ago         680MB
centos                latest    5d0da3dc9764   8 months ago         231MB
portainer/portainer   latest    580c0e4e98b0   14 months ago        79.1MB

Container data volume

Implement the data volume function by adding the -v parameter

In order to persist the data of the container. This prevents the data in the container from being lost after deleting the container.
Function: Mount a directory on the host.
Think of the container as a separate system, or computer, and your host directory is a USB flash drive. After mounting, you put files in the directory on the host, then pass This file can be accessed from the corresponding directory in the container. You can add and modify certain files "outside the container" without regenerating the container. For example, I use the Mythri tool to detect smart contract vulnerabilities and use the following command in the docker container: docker run -v $(pwd):/tmp mythril/myth analyze /tmp/test.sol, mount the current directory of the host to the tmp directory of the container, then the tmp directory is the current directory when operating in the container, create a new smart directory in the current directory contract or change the content of an old contract such as test.sol, and then detect the contract through the analyze command. There is no need to regenerate the container, nor do you need to know the absolute path of the container to copy the new contract into it. You only need to copy it to the current directory. Can.

命令:docker run -it -v 主机目录:容器目录 容器名称

# 首先确认下本机linux目录下是没有其他目录的
[root@localhost /]# cd home/
[root@localhost home]#

1. We first create a new directory named ceshi in the home directory of the local machine, and then we mount the home/ceshi directory of the host to the home directory of the container. When either one of these two directories is changed, the other directory can also be changed simultaneously.

# 启动centos容器,并且将主机的home/ceshi目录挂载到容器的home目录,并且成功进入到容器里面
[root@localhost home]# docker run -it -v /home/ceshi:/home centos
[root@9df6ac86d312 /]# 

2. To further view the process information of the container that has opened the data volume, use the inspect command.

[root@localhost ceshi]# docker inspect 9df6ac86d312

Insert image description here

3. Open a new session and enter the home/ceshi directory of Linux. You can see that there are no files in the ceshi directory.

[root@localhost home]# cd ceshi/
[root@localhost ceshi]# ls
[root@localhost ceshi]# 

4. Start testing, add files to the home directory of the container, and create a new test.java file.

[root@9df6ac86d312 /]# cd home/
[root@9df6ac86d312 home]# touch test.java
[root@9df6ac86d312 home]# ls
test.java
[root@9df6ac86d312 home]# 

5. Go to the home/ceshi directory of Linux to see if there is a new test.java file.

[root@localhost ceshi]# ls
test.java

Result: You can see that the host directory has been successfully mounted to the container directory. If there are any changes to the home directory in the container, the home/ceshi directory under Linux will also be synchronized.

Now let's change the home/ceshi/test.java file in Linux. Will the test.java file in the container's home directory also be changed?

1. Here, I entered a line of text into the linux test.java file, :wq, saved and exited.

[root@localhost ceshi]# vi test.java
[root@localhost ceshi]# cat test.java 
hello,chenxiansheng
[root@localhost ceshi]#

2. Now check the test.java file of the container. You can see that the synchronization is also successful.

[root@9df6ac86d312 home]# cat test.java 
hello,chenxiansheng
[root@9df6ac86d312 home]# 

Based on the above we draw conclusions. The container data volume function is to add the -v parameter when starting the container and write the directory to be mounted and the directory to be bound, so that these two directories that are not at the same level have a two-way binding function. Data Synchronization can be achieved (even if the container has stopped running, the two-way binding is still effective).

Install mysql for practical testing of data volumes
# 下载mysql镜像
[root@localhost ~]# 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
# 查看镜像
[root@localhost ~]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
chentomcat            1.0       8c3fc729e0f1   47 hours ago    684MB
nginx                 latest    605c77e624dd   4 months ago    141MB
tomcat                9.0       b8e65a4d736d   5 months ago    680MB
tomcat                latest    fb5657adc892   5 months ago    680MB
mysql                 5.7       c20987f18b13   5 months ago    448MB
centos                latest    5d0da3dc9764   8 months ago    231MB
portainer/portainer   latest    580c0e4e98b0   14 months ago   79.1MB

# 运行mysql容器。 
参数说明:
[root@localhost ~]# docker run -d -p 3310:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root --name mysqlO1 mysql:5.7
6a2caa469f09e9ff2417d31634fb50a4624118d1724f236ca0962a62989bb2f8
[root@localhost ~]# 

Test connection:
Here I use the Navicat Premium tool to connect and test.
192.168.239.128 is my Linux address. You can see that the connection is successful.
Insert image description here
Check the home/mysql folder in Linux:
Insert image description here
Then we use the Navicat Premium tool to create a new test database in the mysql. :

Insert image description here
Re-check the home/mysql/data directory of this machine. You can see that the test folder has been added.
Insert image description here

The above conclusion is that we created a new database in the container's mysql. We can find the newly created test database in the container on Linux. These two folders have achieved two-way binding. If we delete the container, the data will not be lost, realizing the data persistence function.

Named and anonymous mounts
Use docker volume to view the folder information of the data volume
docker run -d -p --name nginx01 -v /etc/nginx nginx
-P  随机指定端口

[root@localhost /]# docker run -d -P --name nginx02 -v /etc/nginx nginx
5035f960746ba9a373fdeeec59e837f77793465055ade7e46e49d11d3e820a6d

# 使用docker volume 命令查看数据卷具体信息
[root@localhost /]# docker volume --help

Usage:  docker volume COMMAND

Manage volumes

Commands:
  create      Create a volume
  inspect     Display detailed information on one or more volumes
  ls          List volumes
  prune       Remove all unused local volumes
  rm          Remove one or more volumes

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

# docker volume ls 命令列出该数据卷文件夹
[root@localhost /]# docker volume ls
DRIVER    VOLUME NAME
local     5935c0c061619752d5939c6c0567a588feacb171ec5bbe15be327e1952a32aef
local     d8bd26ce1c9d58e224d378446283b9166f53f1f3d9bb2ae3419d0dd67667cb20

Because the above command mounts anonymously, that is, it does not specify the host directory, it will generate a directory by itself. I have entered it here and it will mount the 5935c0c061619752d5939c6c0567a588feacb171ec5bbe15be327e1952a32aef directory into the container's /etc/nginx directory.
Next we continue to use the docker volume command to view the specific path of the data volume folder:

[root@localhost home]# docker volume inspect 5935c0c061619752d5939c6c0567a588feacb171ec5bbe15be327e1952a32aef
[
    {
    
    
        "CreatedAt": "2022-05-25T21:55:11+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/5935c0c061619752d5939c6c0567a588feacb171ec5bbe15be327e1952a32aef/_data",
        "Name": "5935c0c061619752d5939c6c0567a588feacb171ec5bbe15be327e1952a32aef",
        "Options": null,
        "Scope": "local"
    }
]

As can be seen from the above, the folder of the data volume is placed in the /var/lib/docker/volumes/ directory. We go directly to this directory to see if there are nginx files.

[root@localhost home]# cd /var/lib/docker/volumes/5935c0c061619752d5939c6c0567a588feacb171ec5bbe15be327e1952a32aef/
[root@localhost 5935c0c061619752d5939c6c0567a588feacb171ec5bbe15be327e1952a32aef]# ls
_data
[root@localhost 5935c0c061619752d5939c6c0567a588feacb171ec5bbe15be327e1952a32aef]# cd _data/
[root@localhost _data]# ls
conf.d  fastcgi_params  mime.types  modules  nginx.conf  scgi_params  uwsgi_params
[root@localhost _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;
}
[root@localhost _data]# 

As you can see, the nginx files are also synchronized correctly. The disadvantage of anonymous mounting is that the name of the generated mounting directory is uncertain, so we don't know which directory is actually mounted and can only find it one by one.

Named mount

Similarly, we start an nginx container. This time we specify the host directory juming (not placed on a relative path, no need to add "/" in front). At this time, use the docker volume command to view the path of the directory.

[root@localhost /]# docker run -d -P --name nginx04 -v  juming:/etc/nginx nginx
d68b6d085d0304d43e205c5627c1fc790aea3391b09bf7b6304b2c71cafc8376
[root@localhost /]# docker volume ls
DRIVER    VOLUME NAME
local     5935c0c061619752d5939c6c0567a588feacb171ec5bbe15be327e1952a32aef
local     d8bd26ce1c9d58e224d378446283b9166f53f1f3d9bb2ae3419d0dd67667cb20
local     juming

# 使用 docker volume inspect 查看挂载目录的路径
[root@localhost _data]# docker volume inspect juming
[
    {
    
    
        "CreatedAt": "2022-05-25T22:29:57+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/juming/_data",
        "Name": "juming",
        "Options": null,
        "Scope": "local"
    }
]


# 进入到该挂载目录的_data目录,可以看到nginx.conf  也是存在的,说明具名挂载也是成功的
[root@localhost /]# cd var/lib/docker/volumes/juming/_data/
[root@localhost _data]# ls
conf.d  fastcgi_params  mime.types  modules  nginx.conf  scgi_params  uwsgi_params
[root@localhost _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;
}
[root@localhost _data]# 

From the above anonymous mounting and named mounting, we can draw the conclusion that
the difference is: Anonymous mounting means that you do not actively specify the host directory to be mounted when mounting. At this time, a mounting directory will be automatically generated, but what is its specific name? We don't know, we can only use the "docker inspect container id" command to find out the name of the mounted local directory from the mounts of the process information. Named mounting means to manually specify a directory to be mounted when mounting. At this time, we can easily find the corresponding host directory with the "docker volume ls" command.
Similarity: The host directories for named mounts and anonymous mounts are both stored on the "/var/lib/docker/volumes/" path

expand:

# 通过 -v 容器内路径:ro  /rw 改变读写权限
ro:				# 只读
rw:				# 可读可写
 docker run -d -P --name nginx03 -v  /home/juming:/etc/nginx:ro nginx
 docker run -d -P --name nginx03 -v  /home/juming:/etc/nginx:rw nginx

#ro As long as you see ro, it means that this path can only be operated through the host and cannot be operated inside the container! (Test: look at the two pictures below)

Add the test.java file to the host (Figure 1). You can see that the container also has a test.java file (Figure 2). However, when you add the test2.java file to the container, it only reports read-only permission (Figure 2).
figure 1
figure 1

figure 2
figure 2

First introduction to Dockerfile
Build dockerfile command: docker build

Dockerfile is the build file and command script used to build docker images!
Through this script, you can generate an image. The image is layer by layer and the script is command by command. Every command is a layer.

Build your own image

# 创建一个dockerfile文件,名字可以随机,建议叫:dockerfile
# 文件中的内容 指令(大写)	参数

# 第一步,在本机的home目录新建一个docker-test-volume 目录
[root@localhost home]# mkdir docker-test-volume
[root@localhost home]# ls
ceshi  chenmaolin  chenxiansheng222.java  docker-test-volume  juming  mysql

# 第二部,进入到该目录,新建一个名为dockerfile1的文件,并写上内容
[root@localhost home]# cd docker-test-volume/
[root@localhost docker-test-volume]# vi dockerfile1
[root@localhost docker-test-volume]# cat dockerfile1
FROM centos

VOLUME ["volume01","volume02"]

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

# 第三步,使用构建镜像的命,docker build
-f 		# 指定要构建的文件
-t 		# 要生成的镜像
.		# 表示当前目录
[root@localhost docker-test-volume]# docker build -f /home/docker-test-volume/dockerfile1 -t chenxiansheng/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 de2d516d27b4
Removing intermediate container de2d516d27b4
 ---> ab91177ccd23
Step 3/4 : CMD echo "-------end------"
 ---> Running in da532b3a90af
Removing intermediate container da532b3a90af
 ---> 3dfd8b7d5bde
Step 4/4 : CMD /bin/bash
 ---> Running in df8f5c472181
Removing intermediate container df8f5c472181
 ---> b0b6b03694f5
Successfully built b0b6b03694f5
Successfully tagged chenxiansheng/centos:1.0

# 第四步,查看镜像,可以看到自己的镜像构建成功,chenxiansheng/centos
[root@localhost docker-test-volume]# docker images
REPOSITORY             TAG       IMAGE ID       CREATED         SIZE
chenxiansheng/centos   1.0       b0b6b03694f5   8 seconds ago   231MB
chentomcat             1.0       8c3fc729e0f1   3 days ago      684MB
nginx                  latest    605c77e624dd   4 months ago    141MB
tomcat                 9.0       b8e65a4d736d   5 months ago    680MB
tomcat                 latest    fb5657adc892   5 months ago    680MB
mysql                  5.7       c20987f18b13   5 months ago    448MB
centos                 latest    5d0da3dc9764   8 months ago    231MB
portainer/portainer    latest    580c0e4e98b0   14 months ago   79.1MB

As you can see, building the image here is divided into four steps, and the output sentence we wrote earlier is also output normally
-----end------
Insert image description here

Start your own image for testing

Insert image description here

But there is a problem with the above mounting. That is, we did not specify the local directory, but only the directories volume01 and volume02 in the mounted container. This is an anonymous mount. So the local directory it generates is a very long directory name represented in hexadecimal.

Use the docker inspect container id command to view the process information of the container, and you can get the process information of the container. You can find the mounted local directory of the container from the process information.

[root@localhost /]# docker inspect d0ea1e9cf82e

From the process information, find mounts, and you can see that the host directory
Insert image description here
is the same as the previous data volume, and the files are also bound in both directions. If the volume01 directory in the container changes, the local /var/lib/docker/volumes/656e215f074529196d4b1b8df2e9339286b81bba49beebeb03ea2c483d25e902/_data will also be synchronized accordingly. The same goes for volume02.

The above method of building dockerfile will be used a lot in the future. Because we usually build our own images!

data volume container

To share data between multiple containers, use the –volumes-from command.

The first step is to start a docker01 container, using the image we built earlier.

[root@localhost docker-test-volume]# docker run -it --name docker01 chenxiansheng/centos:1.0
[root@cf7f334a2601 /]# ls -l
total 0
lrwxrwxrwx.   1 root root   7 Nov  3  2020 bin -> usr/bin
drwxr-xr-x.   5 root root 360 May 26 15:04 dev
drwxr-xr-x.   1 root root  66 May 26 15:04 etc
drwxr-xr-x.   2 root root   6 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   6 Sep 15  2021 lost+found
drwxr-xr-x.   2 root root   6 Nov  3  2020 media
drwxr-xr-x.   2 root root   6 Nov  3  2020 mnt
drwxr-xr-x.   2 root root   6 Nov  3  2020 opt
dr-xr-xr-x. 126 root root   0 May 26 15:04 proc
dr-xr-x---.   2 root root 162 Sep 15  2021 root
drwxr-xr-x.  11 root root 163 Sep 15  2021 run
lrwxrwxrwx.   1 root root   8 Nov  3  2020 sbin -> usr/sbin
drwxr-xr-x.   2 root root   6 Nov  3  2020 srv
dr-xr-xr-x.  13 root root   0 May 26 11:41 sys
drwxrwxrwt.   7 root root 171 Sep 15  2021 tmp
drwxr-xr-x.  12 root root 144 Sep 15  2021 usr
drwxr-xr-x.  20 root root 262 Sep 15  2021 var
drwxr-xr-x.   2 root root   6 May 26 15:04 volume01
drwxr-xr-x.   2 root root   6 May 26 15:04 volume02

# 随后使用ctrl+P+Q ,后台运行并退出
[root@cf7f334a2601 /]# [root@localhost docker-test-volume]#

The second step is to inherit the docker01 image, start a docker02 container, and use the –volumes-from parameter.

[root@localhost docker-test-volume]# docker run -it --name docker02 --volumes-from docker01 chenxiansheng/centos:1.0
[root@f0cc63e30a17 /]# ls -l
total 0
lrwxrwxrwx.   1 root root   7 Nov  3  2020 bin -> usr/bin
drwxr-xr-x.   5 root root 360 May 26 15:07 dev
drwxr-xr-x.   1 root root  66 May 26 15:07 etc
drwxr-xr-x.   2 root root   6 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   6 Sep 15  2021 lost+found
drwxr-xr-x.   2 root root   6 Nov  3  2020 media
drwxr-xr-x.   2 root root   6 Nov  3  2020 mnt
drwxr-xr-x.   2 root root   6 Nov  3  2020 opt
dr-xr-xr-x. 125 root root   0 May 26 15:07 proc
dr-xr-x---.   2 root root 162 Sep 15  2021 root
drwxr-xr-x.  11 root root 163 Sep 15  2021 run
lrwxrwxrwx.   1 root root   8 Nov  3  2020 sbin -> usr/sbin
drwxr-xr-x.   2 root root   6 Nov  3  2020 srv
dr-xr-xr-x.  13 root root   0 May 26 11:41 sys
drwxrwxrwt.   7 root root 171 Sep 15  2021 tmp
drwxr-xr-x.  12 root root 144 Sep 15  2021 usr
drwxr-xr-x.  20 root root 262 Sep 15  2021 var
drwxr-xr-x.   2 root root   6 May 26 15:04 volume01
drwxr-xr-x.   2 root root   6 May 26 15:04 volume02

As you can see, docker01 and docker02 both have volume01 and volume02 directories. These two directories synchronize data. Let's test it:

# 我们在docker01 容器的volume01 目录新建一个 test.java 
[root@cf7f334a2601 /]# cd volume01/
[root@cf7f334a2601 volume01]# ls
[root@cf7f334a2601 volume01]# touch test.java
[root@cf7f334a2601 volume01]# ls
test.java
[root@cf7f334a2601 volume01]# 

At this time, go to the docker02 container and view the volume01 file. The test.java culture also exists, which realizes the synchronization function of the data volume container.

[root@f0cc63e30a17 /]# cd volume01
[root@f0cc63e30a17 volume01]# ls
test.java
[root@f0cc63e30a17 volume01]# 

So, what if I want to synchronize container 3 and container 4? Let’s continue testing here. We inherit docker02 and start docker03.

[root@localhost ~]# docker run -it --name docker03 --volumes-from docker02 chenxiansheng/centos:1.0
[root@97396699c735 /]# ls -l
total 0
lrwxrwxrwx.   1 root root   7 Nov  3  2020 bin -> usr/bin
drwxr-xr-x.   5 root root 360 May 26 15:18 dev
drwxr-xr-x.   1 root root  66 May 26 15:17 etc
drwxr-xr-x.   2 root root   6 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   6 Sep 15  2021 lost+found
drwxr-xr-x.   2 root root   6 Nov  3  2020 media
drwxr-xr-x.   2 root root   6 Nov  3  2020 mnt
drwxr-xr-x.   2 root root   6 Nov  3  2020 opt
dr-xr-xr-x. 130 root root   0 May 26 15:17 proc
dr-xr-x---.   2 root root 162 Sep 15  2021 root
drwxr-xr-x.  11 root root 163 Sep 15  2021 run
lrwxrwxrwx.   1 root root   8 Nov  3  2020 sbin -> usr/sbin
drwxr-xr-x.   2 root root   6 Nov  3  2020 srv
dr-xr-xr-x.  13 root root   0 May 26 11:41 sys
drwxrwxrwt.   7 root root 171 Sep 15  2021 tmp
drwxr-xr-x.  12 root root 144 Sep 15  2021 usr
drwxr-xr-x.  20 root root 262 Sep 15  2021 var
drwxr-xr-x.   2 root root  23 May 26 15:11 volume01
drwxr-xr-x.   2 root root   6 May 26 15:04 volume02
[root@97396699c735 /]# cd volume01
[root@97396699c735 volume01]# ls
test.java
[root@97396699c735 volume01]# 

As you can see, synchronization is still effective.
Is it possible if docker03 inherits docker01 startup? The answer is yes, and data sharing can also be achieved! !
After deleting docker 01, docker02 and docker03 can still share data. It has nothing to do with whether the container is started or not!
Therefore, it is concluded that to achieve data sharing between containers, you only need to add the –volumes-from parameter parent class container name in docker run to achieve data sharing between containers. As long as they have a common parent class.

Conclusion: the transfer of configuration information between containers, the life cycle of the data volume container continues until no container is used. However,
once persisted to the local, the local data will not be deleted at this time, even if the container is no longer used.

DockerFile

dockerfIle is the file used to build docker images!

Build steps:

  1. Write a dockerfile
  2. docker build builds into a mirror
  3. docker run runs the image
  4. docker push releases images (DockerHub, Alibaba Cloud Image Warehouse, etc.)

Many official images are basic packages and do not have many features. We usually build our own images ourselves.

DockerFile build process

Basic knowledge:
  1. Each reserved keyword (command) must be capitalized
  2. Execute sequentially from top to bottom
  3. # represents comments
  4. Each command will create and submit a new image layer and submit it!
    Similar to what we wrote above, dockerfile is a layer-by-layer
    Insert image description hereInsert image description here
    dockerfile for development. When we release projects and make mirrors in the future, we need to write a dockerfile. This file is very simple!
    Docker images are increasingly becoming the standard for enterprise delivery.
    Steps: Development, deployment, operation and maintenance
    Dockerfile: Build file defines all steps and source code.
    DockerImages: Build the generated image through Dockerfile, and finally publish and run the product!
    Docker container: A container is an image that runs to provide a server

Dockerfile instructions

Here are the meanings of the various commands:
Insert image description here

CMD 		# 指定这个容器启动的时候要运行的命令,只有最后一个会生效,可被替代
ENTRYPOINT	# 指定这个容器启动的时候要运行的命令,可以追加命令
ONBUILD		# 当构建一个被继承DockerFile 这个时候就会运行ONBUILD 的指令,触发指令
COPY		# 类似ADD,将我们文件拷贝到镜像中
ENY			# 构建的时候设置环境变量!
Practical test

99% of the images in Docker Hub are FROM scratch from this basic image, and then configure the required software and configuration to be built.

We first start centos and use the vim command and ifconfig command in the container. We find that this command is not supported, indicating that the official centos container is a castrated version. Then we will build a centos ourselves so that it can support vim and ifconfig. Order
Insert image description here

Build your own centos

Create a directory, create a new mydockerfilecentos file in the directory, and write the image instructions to be built.

[root@localhost home]# mkdir firstdockerfile
[root@localhost home]# ls
ceshi  chenmaolin  chenxiansheng222.java  docker-test-volume  firstdockerfile  juming  mysql
[root@localhost home]# cd firstdockerfile/

# 1 编写Dockerfile 文件
[root@localhost firstdockerfile]# vi mydockerfilecentos
[root@localhost firstdockerfile]# cat mydockerfilecentos 
FROM centos:7										# 指定了centos 为基础镜像,因为本机的centos版本就是7.9,所以这里也要下7,都则会下载不了
MAINTAINER chenxiansheng<1050480930@qq.com>		# 维护者西你想

ENV MYPATH /usr/local							# 设置环境变量
WORKDIR $MYPATH									# 设置当前工作目录

RUN  yum -y install vim 						# 默认下载vim指令
RUN  yum -y insatll net-tools					# 默认下载网络工具包

EXPOSE 80										# 指定对外的端口

CMD echo $MYPATH								# 容器启动时,要运行的命令,将工作目录打印出来
CMD echo "----end------"						# 容器启动时,要运行的命令
CMD /bin/bash 									# 容器启动时,要启动的命令行

# 2、通过这个文件构建镜像
# 命令	docker build -f dockerfile文件路径
[root@localhost firstdockerfile]# docker build -f mydockerfilecentos -t mycentos:0.1 .

Build successful
Insert image description here

Pay attention to check whether the centos version in the docker container is 8.0. The local centos is 7.9. In this case, an error will be reported when the build encounters the yum command. You need to specify in the dockerfile file to download the 7 version of centos to solve the problem.

# 3、 测试运行
[root@localhost firstdockerfile]# docker run -it mycentos:0.1

# 这里可以看到,容器默认就是进入到/usr/local,这就是我们前面设置的工作目录
[root@547054def8e6 local]# pwd
/usr/local

# 可以看到我们构建的镜像,可以使用ifconifg、vim命令了
[root@547054def8e6 local]# ifconfig  
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.4  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:04  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

[root@547054def8e6 local]#vim  
[root@547054def8e6 local]#   

Use docker history image id to view dockerfile change history

We usually get an image and study how it is done.

[root@localhost firstdockerfile]# docker images
REPOSITORY             TAG       IMAGE ID       CREATED          SIZE
mycentos               0.1       c80a46b4bab7   12 minutes ago   601MB

# 使用镜像id查看变更历史(也就是可以查看这些镜像的dockerfile是怎么写的)
[root@localhost firstdockerfile]# docker history c80a46b4bab7
IMAGE          CREATED          CREATED BY                                      SIZE      COMMENT
c80a46b4bab7   13 minutes ago   /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "/bin…   0B        
522954b80b85   13 minutes ago   /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "echo…   0B        
dfcfc1df60c5   13 minutes ago   /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "echo…   0B        
a04211a9bee4   13 minutes ago   /bin/sh -c #(nop)  EXPOSE 80                    0B        
536afb4c9003   13 minutes ago   /bin/sh -c yum -y install net-tools             171MB     
497262217236   13 minutes ago   /bin/sh -c yum -y install vim                   226MB     
f4d69a0c750d   20 minutes ago   /bin/sh -c #(nop) WORKDIR /usr/local            0B        
098b9103d5fb   20 minutes ago   /bin/sh -c #(nop)  ENV MYPATH=/usr/local        0B        
722b4df5b913   20 minutes ago   /bin/sh -c #(nop)  MAINTAINER chenxiansheng<…   0B        
eeb6ee3f44bd   8 months ago     /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B        
<missing>      8 months ago     /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B        
<missing>      8 months ago     /bin/sh -c #(nop) ADD file:b3ebbe8bd304723d4…   204MB     
[root@localhost firstdockerfile]# 

The difference between CMD and ENTRYPOINT

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

testcmd

# 构建dockerfile镜像
[root@localhost firstdockerfile]# vi dockerfile-cmd-test

# 这里的镜像就只运行 ls -a 命令
[root@localhost firstdockerfile]# cat dockerfile-cmd-test 
FROM centos
CMD ["ls","-a"]

# 构建cmd镜像
[root@localhost firstdockerfile]# docker build -f dockerfile-cmd-test -t cmdtest .
Sending build context to Docker daemon  3.072kB
Step 1/2 : FROM centos
 ---> 5d0da3dc9764
Step 2/2 : CMD ["ls","-a"]
 ---> Running in 82f7e399a1f0
Removing intermediate container 82f7e399a1f0
 ---> 0b2e5b872f1c
Successfully built 0b2e5b872f1c
Successfully tagged cmdtest:latest

# 运行cmd镜像
[root@localhost firstdockerfile]# docker run 0b2e5b872f1c
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var

# 上面可以看到,正确执行了 ls -a 命令,下面我们想要追加一个命令,变成 ls -l 
[root@localhost firstdockerfile]# docker run 0b2e5b872f1c -l
docker: Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "-l": executable file not found in $PATH: unknown.

# cmd 的清理下,会用-l 替换 ["ls","-a"] 命令, 因为-l 不是命令,所以报错了

Test ENTRYPOINT

# 构建dockerfile镜像
[root@localhost firstdockerfile]# vi docker-entrypoint-test

# 这里和上面一样,该镜像只执行ls -a 命令
[root@localhost firstdockerfile]# cat docker-entrypoint-test 
FROM centos
ENTRYPOINT ["ls","-a"]

# 构建entrypoint 镜像
[root@localhost firstdockerfile]# docker build -f docker-entrypoint-test -t entrypointtest .
Sending build context to Docker daemon  4.096kB
Step 1/2 : FROM centos
 ---> 5d0da3dc9764
Step 2/2 : ENTRYPOINT ["ls","-a"]
 ---> Running in 221b27881ee1
Removing intermediate container 221b27881ee1
 ---> 3cdb2e38bcd8
Successfully built 3cdb2e38bcd8
Successfully tagged entrypointtest:latest

# 运行entrypoint 镜像,可以发现,运行成功
[root@localhost firstdockerfile]# docker run 3cdb2e38bcd8
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var

# 继续追加命令,直接在后面加 -l 
[root@localhost firstdockerfile]# docker run 3cdb2e38bcd8 -l
total 0
drwxr-xr-x.   1 root root   6 May 30 14:45 .
drwxr-xr-x.   1 root root   6 May 30 14:45 ..
-rwxr-xr-x.   1 root root   0 May 30 14:45 .dockerenv
lrwxrwxrwx.   1 root root   7 Nov  3  2020 bin -> usr/bin
drwxr-xr-x.   5 root root 340 May 30 14:45 dev
drwxr-xr-x.   1 root root  66 May 30 14:45 etc
drwxr-xr-x.   2 root root   6 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   6 Sep 15  2021 lost+found
drwxr-xr-x.   2 root root   6 Nov  3  2020 media
drwxr-xr-x.   2 root root   6 Nov  3  2020 mnt
drwxr-xr-x.   2 root root   6 Nov  3  2020 opt
dr-xr-xr-x. 135 root root   0 May 30 14:45 proc
dr-xr-x---.   2 root root 162 Sep 15  2021 root
drwxr-xr-x.  11 root root 163 Sep 15  2021 run
lrwxrwxrwx.   1 root root   8 Nov  3  2020 sbin -> usr/sbin
drwxr-xr-x.   2 root root   6 Nov  3  2020 srv
dr-xr-xr-x.  13 root root   0 May 30 11:07 sys
drwxrwxrwt.   7 root root 171 Sep 15  2021 tmp
drwxr-xr-x.  12 root root 144 Sep 15  2021 usr
drwxr-xr-x.  20 root root 262 Sep 15  2021 var

# 结果:-l 命令执行成功了,是因为 他只是替换了-a ,变成了 ls -l 了。所以成功运行

Practical combat: Tomcat mirror

1. Prepare the image file, tomcat compressed package, jdk compressed package
Insert image description here
2. Create a new readme.txt, Dockerfile file, and fill in the dockerfile instructions (here we are going to build a tomcat and jdk image), and then build the written dockerfile file

[root@localhost tomcat]# touch readme.txt
[root@localhost tomcat]# vi Dockerfile

# 填写dockerfile指令,这里我们是要建一个tomcat 和jdk的镜像
[root@localhost tomcat]# cat Dockerfile 
FROM centos:7
MAINTAINER chenxiansheng<1050480930@qq.com>

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

ADD jdk-8u60-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-9.0.63.tar.gz /usr/local/

RUN yum -y install vim 

ENV MYPATH /usr/local
WORKDIR $MYPATH

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


EXPOSE 8080

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


# 开始构建dockerfile(这里的“.” 代表构建当前目录下的Dockfile文件)
[root@localhost tomcat]# docker build -t diytomcat .
Sending build context to Docker daemon  192.8MB
Step 1/15 : FROM centos:7
 ---> eeb6ee3f44bd
Step 2/15 : MAINTAINER chenxiansheng<1050480930@qq.com>
 ---> Using cache
 ---> 722b4df5b913
#....... 省略构建日志
Successfully built 7c5296e1e145
Successfully tagged diytomcat:latest

# 查看镜像,可以发现,我们的diytomcat 构建成功
[root@localhost tomcat]# docker images
REPOSITORY             TAG       IMAGE ID       CREATED         SIZE
diytomcat              latest    7c5296e1e145   2 minutes ago   811MB

Step 3. Start the image

[root@localhost tomcat]# docker run -d -p 9090:8080 --name chenxianshengtomcat -v /home/tomcat/test:/usr/local/apache-tomcat-9.0.63/webapps/test -v /home/tomcat/tomcatlogs/:/usr/local/apache-tomcat-9.0.63/logs diytomcat
49b22d1db8763cee40876a75a4ed11cebdc51462153f981253d1a04582cca618

Mirror startup parameter description:
Insert image description here

Step 4: Start testing to see if tomcat is successful

Directly access port 9090 of the Linux address and check if there is a tomcat page:
Insert image description here
you can see that tomcat is running normally.

the fifth step. Continue to test tomcat, because when we start the diytomcat image we made, we bind the /home/tomcat/test directory of the local machine to the /usr/local/apache-tomcat-9.0.63/webapps/test in the container. Therefore, we go directly to the /home/tomcat/test directory of the local machine and add index.jsp. Then the apache-tomcat-9.0.63/webapps/test directory of the container will also have an index.jsp file. Go visit it at this time. Local address: 9090/test address will recognize the index.jsp file and parse the content into a web page.

1) Enter the /home/tomcat/test directory of the local machine, create a new index.jsp, and fill in the content:

[root@localhost tomcat]# cd test/
[root@localhost test]# vi index.jsp
[root@localhost 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>chenxiansheng(runoob.com)</title>
</head>
<body>
Hello World,chenxiansheng!<br/>
<%
out.println("--------my test web logs-------");
%>
</body>
</html>

# 查看该容器是否也同步了index.jsp文件,可以看到同步成功
[root@localhost tomcat]# docker exec -it 49b22d1db8763 /bin/bash
[root@49b22d1db876 local]# cd /usr/local/apache-tomcat-9.0.63/webapps/test/
[root@49b22d1db876 test]# ls
index.jsp

2) Directly access the local address: 9090/test path.
Insert image description here
You can see that the tomcat in the container correctly parsed the index.jsp file and generated the web page. And we can go to the local machine to view the logs in the container (this is also bound when starting the image)

Insert image description here

Conclusion 1: This diytomcat image was created by ourselves and realizes the functions that a tomcat image should have.

Conclusion 2: Through this two-way binding method, our Linux machine does not need to install the tomcat image. We only need to bind one of the directories to the docker container with tomcat, and we can upload the web on the machine. Project, the function that allows docker containers to parse.

Publish your own image

1. Register your own account at https://hub.docker.com/
2. Make sure this account can log in
3. Submit your own image on our server

# 使用docker login 命令 
[root@localhost tomcatlogs]# docker login -u morningchan
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@localhost tomcatlogs]# 

After successful login, you need to modify the tag, that is, give your image a version number, use the docker tag command

[root@localhost tomcatlogs]# docker tag  7c5296e1e145 morningchan/diytomcat:1.0

Push your own image to docker hub, use docker push command

[root@localhost tomcatlogs]# docker push morning/diytomcat:1.0

Note: When modifying the tag, it is best to modify it to the form of your own dockerhub user name/image name. If the previous user names his own docker hub image, then the subsequent push will be rejected. My docker hub username here is morningchan.

As you can see, the push is successful (the speed here is too slow, after all, it is a foreign website)
Insert image description here

Guess you like

Origin blog.csdn.net/chenmaolin928/article/details/124890653