前言:

推荐免费Docker基础讲解视频:【狂神说Java】Docker最新超详细版教程通俗易懂_哔哩哔哩_bilibili

常用其他命令

后台启动容器

1
2
3
4
# docker run -d 镜像名
[root@jokerdig ~]# docker run -d centos
6819932fe4b4e58575ac1fb7b0e4046730ef606a886019753c17ab9a954b3fd0
# nginx 容器启动后,发现自己没有提供服务,就会立刻停止;

查看日志

1
2
# 查看日志
docker logs [OPTIONS] CONTAINER

描述(OPTIONS)

名称 默认 描述
--details 显示提供给日志的额外详细信息
--follow , -f 跟踪日志输出
--since 显示自时间戳以来的日志
--tail , -n all 从日志末尾开始显示的行数
--timestamps , -t 显示时间戳
--until 在时间戳之前显示日志

测试

1
[root@jokerdig ~]# docker logs -f -t 5d0da3dc9764

查看容器中的进程信息

1
2
# 显示容器的运行进程
docker top CONTAINER [ps OPTIONS]

测试

1
[root@jokerdig ~]# docker top 5d0da3dc9764

查看镜像元数据

1
2
# 返回Docker对象的元数据
docker inspect [OPTIONS] NAME|ID [NAME|ID...]

选项(OPTIONS)

名称 默认 描述
--format , -f 使用给定的Go模板格式化输出
--size , -s 如果类型为容器,则显示总文件大小
--type 返回指定类型的JSON

测试

1
[root@jokerdig ~]# docker inspect 5d0da3dc97

进入当前正在运行的容器

1
2
3
4
5
# 进入当前正在运行的容器
# 方式一
docker exec -it 容器ID /bin/bash
# 方式二
docker attach 容器ID /bin/bash

二者的区别:

  • docker exec:进入容器后开启一个新的终端,可以在里面操作(常用);

  • docker attach:进入容器正在执行的终端,不会启动新的进程;

从容器内拷贝文件到主机

1
2
# 在容器和本地文件系统之间复制文件/文件夹
docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-

选项(OPTIONS)

名称 默认 描述
--archive , -a 存档模式(复制所有uid/gid信息)
--follow-link , -L 始终遵循SRC_PATH中的符号链接

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
# 启动容器
[root@jokerdig ~]# docker run -it centos /bin/bash
[root@b0e02a550063 /]# cd /home
[root@b0e02a550063 home]# ls
# 在容器的home目录创建test.java
[root@b0e02a550063 home]# touch test.java
[root@b0e02a550063 home]# exit
exit
# 从容器拷贝test.java到主机
[root@jokerdig home]# docker cp b0e02a550063:/home/test.java /home
[root@jokerdig home]# ls
test.java
[root@jokerdig home]#

部署Nginx

Docker安装Nginx

  1. 搜索镜像

    1
    2
    3
    [root@jokerdig ~]# docker search nginx
    NAME DESCRIPTION STARS OFFICIAL AUTOMATED
    nginx Official build of Nginx. 17422 [OK]
  2. 下载镜像

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    [root@jokerdig ~]# docker pull nginx
    Using default tag: latest
    latest: Pulling from library/nginx
    31b3f1ad4ce1: Pull complete
    fd42b079d0f8: Pull complete
    30585fbbebc6: Pull complete
    18f4ffdd25f4: Pull complete
    9dc932c8fba2: Pull complete
    600c24b8ba39: Pull complete
    Digest: sha256:0b970013351304af46f322da1263516b188318682b2ab1091862497591189ff1
    Status: Downloaded newer image for nginx:latest
    docker.io/library/nginx:latest
  3. 启动nginx

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    # 启动容器                       
    # -name 容器命名
    # -p 服务器端口:容器内端口(前提是3355端口安全组开放)
    [root@jokerdig ~]# docker run -d --name nginx001 -p 3355:80 nginx
    50a74618450cec874acf12411dd0972ead390458dd188b1d8865a9e6ce033518
    # 查看容器是否启动
    [root@jokerdig ~]# docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    50a74618450c nginx "/docker-entrypoint.…" 6 seconds ago Up 6 seconds 0.0.0.0:3355->80/tcp, :::3355->80/tcp nginx001
    [root@jokerdig ~]#
  4. 运行测试

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    [root@jokerdig ~]# curl localhost:3355
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    <style>
    html { color-scheme: light dark; }
    body { width: 35em; margin: 0 auto;
    font-family: Tahoma, Verdana, Arial, sans-serif; }
    </style>
    </head>
    <body>
    <h1>Welcome to nginx!</h1>
    <p>If you see this page, the nginx web server is successfully installed and
    working. Further configuration is required.</p>

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

    <p><em>Thank you for using nginx.</em></p>
    </body>
    </html>
    # 进入容器
    [root@jokerdig ~]# docker exec -it nginx001 /bin/bash
    root@50a74618450c:/# whereis nginx
    nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
    root@50a74618450c:/#

    端口暴露理解图

    image-20220922114216684

  5. 停止容器

    1
    2
    3
    4
    5
    6
    7
    8
    9
    # 退出
    root@50a74618450c:/# exit
    exit
    [root@jokerdig ~]# docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    50a74618450c nginx "/docker-entrypoint.…" 13 minutes ago Up 13 minutes 0.0.0.0:3355->80/tcp, :::3355->80/tcp nginx001
    # 停止nginx容器
    [root@jokerdig ~]# docker stop 50a74618450c
    50a74618450c

部署Tomcat

Docker安装Tomcat

  1. 运行Tomcat

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    # 运行tomcat服务(运行的时候会自动帮我们拉去到本地)
    # --rm 用完后直接删掉
    # docker run -it --rm tomcat:8.5
    # 运行
    [root@jokerdig ~]# docker run -it --rm tomcat:8.5
    # 开始从Dockerhub拉取tomcat
    Unable to find image 'tomcat:8.5' locally
    8.5: Pulling from library/tomcat
    2b55860d4c66: Pull complete
    49a58ffb4a94: Pull complete
    8889343dc9d4: Pull complete
    5c321d92dfdb: Pull complete
    65e12e19b4c9: Pull complete
    62a7c4782c2f: Pull complete
    e04f1c28dfdd: Pull complete
    Digest: sha256:eee599390437683dc87463aa66fd7b46dbc25e39178df1e89a9f3382a77c3a58
    Status: Downloaded newer image for tomcat:8.5
    # 拉取完成后直接启动了Tomcat
    Using CATALINA_BASE: /usr/local/tomcat
    Using CATALINA_HOME: /usr/local/tomcat
    Using CATALINA_TMPDIR: /usr/local/tomcat/temp
    Using JRE_HOME: /opt/java/openjdk
    Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
    Using CATALINA_OPTS:
    # .........
    # 中间内容省略
    22-Sep-2022 03:52:41.199 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
    22-Sep-2022 03:52:41.212 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 64 ms
    # 停掉tomcat后, 容器就被删除了(镜像没有被删除)
    [root@jokerdig ~]# docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  2. 手动启动

    1
    2
    3
    [root@jokerdig ~]# docker run -d -p 3344:8080 --name tomcat01 tomcat
    dcb362c29a523e57ced30b8619e2b74fa50d43e6320349097db990b64b09b91a
    # 访问发现是404,因为docker官方的tomcat版本是阉割过的版本
  3. 进入Tomcat镜像

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    [root@jokerdig ~]# docker exec -it tomcat01 /bin/bash
    root@dcb362c29a52:/usr/local/tomcat# ls
    bin logs temp
    BUILDING.txt native-jni-lib webapps
    conf NOTICE webapps.dist
    CONTRIBUTING.md README.md work
    lib RELEASE-NOTES
    LICENSE RUNNING.txt
    root@dcb362c29a52:/usr/local/tomcat# cd webapps
    root@dcb362c29a52:/usr/local/tomcat/webapps# ls
    # webapps里是空的
    root@dcb362c29a52:/usr/local/tomcat/webapps#
    # 官方的镜像移除了不必要的内容,保证了最小的可运行环境;
  4. 找到root,复制到webapps

    1
    2
    3
    4
    5
    6
    7
    8
    9
    root@dcb362c29a52:/usr/local/tomcat# cd webapps.dist
    root@dcb362c29a52:/usr/local/tomcat/webapps.dist# ls
    docs examples host-manager manager ROOT
    root@dcb362c29a52:/usr/local/tomcat/webapps.dist# cd ..
    root@dcb362c29a52:/usr/local/tomcat# cp -r webapps.dist/* webapps
    root@dcb362c29a52:/usr/local/tomcat# cd webapps
    root@dcb362c29a52:/usr/local/tomcat/webapps# ls
    docs examples host-manager manager ROOT
    root@dcb362c29a52:/usr/local/tomcat/webapps#
  5. 重新访问

    image-20220922132733854

部署ES+Kibana

elasticsearch暴露端口多,而且十分耗内存,且要把数据放置到安全目录(挂载)

  1. 运行镜像

    1
    2
    3
    4
    5
    6
    7
    # --net 网络配置
    #下载并启动elasticsearch
    docker run -d --name elasticsearch --net somenetwork -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.17.6
    # 下载启动后,发现elasticsearch使用非常卡顿
    # 如果elasticsearch启动成功,就先关闭elasticsearch
    # 查看docker cpu状态
    docker stats
  2. 增加内存限制

    1
    2
    # -e ES_JAVA_POTS="-Xms128m -Xmx128m"
    docker run -d --name elasticsearch02 -p 9200:9200 -p 9300:9300 -e ES_JAVA_POTS="-Xms64m -Xmx128m" -e "discovery.type=single-node" --name elastic_search elasticsearch:7.17.6

    运行测试

    1
    2
    3
    4
    5
    6
    [root@jokerdig ~]# docker run -d --name elasticsearch02 -p 9200:9200 -p 9300:9300 -e ES_JAVA_POTS="-Xms64m -Xmx128m" -e "discovery.type=single-node" --name elastic_search elasticsearch:7.17.6
    845a223848a27749ea954335aad392f125235ed4507379676b3acbc8c3f11d04
    # 查看容器进程
    [root@jokerdig ~]# docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    845a223848a2 elasticsearch:7.17.6 "/bin/tini -- /usr/l…" 6 seconds ago Up 4 seconds 0.0.0.0:9200->9200/tcp, :::9200->9200/tcp, 0.0.0.0:9300->9300/tcp, :::9300->9300/tcp elastic_search

kibana连接elasticsearch

需要学习Docker网络原理

image-20220922143004272

Portainer可视化面板安装

先使用Portainer可视化面板,但不是最优选择;

什么是Portainer

Docker的图形化界面管理工具,提供一个后台面板供我们操作;

安装Portainer

安装

1
2
3
# 安装 (服务器安全组开启对应端口)
docker run -d -p 8088:9000 \
--restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer

运行结果

1
2
3
4
5
6
7
8
9
10
[root@jokerdig ~]# docker run -d -p 8088:9000 \
> --restart=always -v /var/run/docker.sock:/var/run/docker
Unable to find image 'portainer/portainer:latest' locally
latest: Pulling from portainer/portainer
b890dbc4eb27: Pull complete
81378af8dad0: Pull complete
Digest: sha256:958a8e5c814e2610fb1946179a3db598f9b5c15ed90d92b42d94aa99f039f30b
Status: Downloaded newer image for portainer/portainer:latest
f49be8654a13b892a64a53ba0b7b4f1b8665801d503ac5efcd27d355388a7b4d
[root@jokerdig ~]#

访问测试

http://你服务器外网IP:8088 (第一次访问比较慢)

登录界面

设置号账号密码,进行登录

image-20220922144058241

选择Docker

image-20220922144257706

可视化控制面板

image-20220922144356699