2020-06-08 docker-2 实践

安装tomcat

  • 官方方式: --rm 用完就删.
docker run -it --rm tomcat:9.0
  • 下载
docker pull tomcat:9.0
  • 后台运行,暴露端口
docker run -d -p 3355:8080 --name tomcat01 tomcat
  • 进入docker
docker exec -it tomcat01 /bin/bash
# 没有webapps,默认是最小镜像. 不必要的都剔除掉. linux命令少了.
# 保证最小可运行环境.
  • 容器内复制webapps.dist
cp -rf ./* /usr/local/tomcat/webapps/
  • 希望在外部操作,同步到内部.

14 部署ES+kibana

  • 启动
docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.6.2 
  • 启动来 linux就很卡, docker status 查看cpu状态.
CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
a355588d89c8        elasticsearch       95.30%              590.3MiB / 972.6MiB   60.70%              656B / 0B           0B / 0B             22
44608694a619        tomcat01            0.13%               5.582MiB / 972.6MiB   0.57%               10.1kB / 63.3kB     0B / 0B             30
  • 访问9200
{
    "name": "a355588d89c8",
    "cluster_name": "docker-cluster",
    "cluster_uuid": "WYTqvf2fTiGevHs1HTO5YA",
    "version": {
        "number": "7.6.2",
        "build_flavor": "default",
        "build_type": "docker",
        "build_hash": "ef48eb35cf30adf4db14086e8aabd07ef6fb113f",
        "build_date": "2020-03-26T06:34:37.794943Z",
        "build_snapshot": false,
        "lucene_version": "8.4.0",
        "minimum_wire_compatibility_version": "6.8.0",
        "minimum_index_compatibility_version": "6.0.0-beta1"
    },
    "tagline": "You Know, for Search"
}
  • 增加配置.启动ES
docker run -d --name elasticsearch01 -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node"  -e ES_JAVA_OPTS="Xms64m -Xmx512m" elasticsearch:7.6.2 

[root@localhost ~]# curl localhost:9200
{
  "name" : "a6dd36f326e3",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "nDA8UV-OQuOsDI6OhpQjqQ",
  "version" : {
    "number" : "7.6.2",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "ef48eb35cf30adf4db14086e8aabd07ef6fb113f",
    "build_date" : "2020-03-26T06:34:37.794943Z",
    "build_snapshot" : false,
    "lucene_version" : "8.4.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

17 Portainer可视化面板安装.

  • portainer
docker run -d -p 8088:9000 --restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer 
  • Rancher(CI/CD可用)

什么是portainer?

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

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

访问测试: 外网: http://172.16.165.131:8088

18.镜像原理之联合文件系统.

20. commit 镜像

  • 提交镜像命令
docker  commit -m="提交的描述信息" -a="作者"  容器id 目标镜像名: [tag]

21. 容器数据卷使用

[root@localhost ~]# docker run -it -v /home/ceshi:/home centos /bin/bash


# 目录挂载
        "Mounts": [
            {
                "Type": "bind",
                "Source": "/home/ceshi",
                "Destination": "/home",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }
  • docker 操作文件,linux就同步.

22. 实战: Mysql同步数据

docker pull mysql
# -e 配置环境
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
# 启动带密码 -d后台运行 -p 端口映射 -v 数据卷挂载 -e 环境配置.--name 容器名字.
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=123456 --name mysql01 mysql

挂载到本地.

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=123456 --name mysql01 mysql:5.7

23. 具名挂载和匿名挂载

匿名挂载

-v 容器内路径
-P 大写

[root@localhost data]# docker run -d -P --name nginx01 -v /etc/nginx nginx 
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
8559a31e96f4: Already exists 
8d69e59170f7: Pull complete 
3f9f1ec1d262: Pull complete 
d1f5ff4f210d: Pull complete 
1e22bfa8652e: Pull complete 
Digest: sha256:21f32f6c08406306d822a0e6e8b7dc81f53f336570e852e25fbe1e3e3d0d0133
Status: Downloaded newer image for nginx:latest
WARNING: IPv4 forwarding is disabled. Networking will not work.
6dee02275e577583a3742e4e212bd76339e02d8b6900dd5f19cba4367ad848e7
[root@localhost data]# docker volume ls
DRIVER              VOLUME NAME
local               90082dee8994000431b93045d6451e5bb22c906843f5f3079d0b1891d0d31f1b
local               ef0c4170034b7b84ac586e5ca4519ed1c8242ec7f1a651171e2c01e9437468b4
local               f1840696c2e9d8bdab29f97557362105b3ab43641c4ac8ea4df588324d2a2aea
  • 具名挂载
[root@localhost data]# docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx nginx
WARNING: IPv4 forwarding is disabled. Networking will not work.
9d9350f2b3c9d9eacdd3ecfac4df0dca6d1b255af6f7b41546d659350ad1beb1
[root@localhost data]# docker volume ls
DRIVER              VOLUME NAME
local               90082dee8994000431b93045d6451e5bb22c906843f5f3079d0b1891d0d31f1b
local               ef0c4170034b7b84ac586e5ca4519ed1c8242ec7f1a651171e2c01e9437468b4
local               f1840696c2e9d8bdab29f97557362105b3ab43641c4ac8ea4df588324d2a2aea
local               juming-nginx

通过-v 卷名: 容器内路径

[root@localhost data]# docker volume inspect juming-nginx
[
    {
        "CreatedAt": "2020-06-10T19:59:49+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/juming-nginx/_data",
        "Name": "juming-nginx",
        "Options": null,
        "Scope": "local"
    }
]
  • 所有的docker容器内的卷, 没有指定目录的情况下都是在/var/lib/docker/volumes/xxx/_data
    我们通过具名挂载二块方便的找到我们的一个卷,大多数情况在使用 具名挂载.
# 挂载参数
-v 容器内路径   # 匿名挂载
-v 卷名: 容器内路径  #具名挂载
-v /宿主机路径::容器内路径 # 指定路径挂载!

拓展:

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

24. 初识Dockerfile

  • Docker 就是用来构建docker 镜像的构建文件!命令脚本!先体验一下.
    指令需要大写.
FROM centos

VOLUME ["volume01", "volume02"]

CMD echo "----end----"
CMD /bin/bash
[root@localhost home]# docker build -f /home/dockerfil -t lvyang/centos:1.0 .                   
unable to prepare context: unable to evaluate symlinks in Dockerfile path: lstat /home/dockerfil: no such file or directory
[root@localhost home]# docker build -f /home/dockerfile -t lvyang/centos:1.0 .
Sending build context to Docker daemon  180.1MB
Step 1/4 : FROM centos
 ---> 470671670cac
Step 2/4 : VOLUME ["volume01", "volume02"]
 ---> [Warning] IPv4 forwarding is disabled. Networking will not work.
 ---> Running in ab234415790b
Removing intermediate container ab234415790b
 ---> e9571b2ff91f
Step 3/4 : CMD echo "----end----"
 ---> [Warning] IPv4 forwarding is disabled. Networking will not work.
 ---> Running in 533d675f572a
Removing intermediate container 533d675f572a
 ---> b36be0ef82a8
Step 4/4 : CMD /bin/bash
 ---> [Warning] IPv4 forwarding is disabled. Networking will not work.
 ---> Running in 4efa2d8d8735
Removing intermediate container 4efa2d8d8735
 ---> b8a01d973fc0
Successfully built b8a01d973fc0
Successfully tagged lvyang/centos:1.0
  • 启动自己的镜像
[root@localhost home]# docker run -it b8a01d973fc0
WARNING: IPv4 forwarding is disabled. Networking will not work.
[root@c5a96b674a38 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var  volume01  volume02
[root@c5a96b674a38 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var  volume01  volume02
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                               NAMES
c5a96b674a38        b8a01d973fc0          "/bin/sh -c /bin/bash"   3 minutes ago       Up 3 minutes                                            vigorous_einstein
9d9350f2b3c9        nginx                 "/docker-entrypoint.…"   About an hour ago   Up About an hour    0.0.0.0:32769->80/tcp               nginx02
6dee02275e57        nginx                 "/docker-entrypoint.…"   About an hour ago   Up About an hour    0.0.0.0:32768->80/tcp               nginx01
2149ca869695        mysql:5.7             "docker-entrypoint.s…"   23 hours ago        Up 23 hours         33060/tcp, 0.0.0.0:3310->3306/tcp   mysql01
5f136e097791        portainer/portainer   "/portainer"             25 hours ago        Up 25 hours         0.0.0.0:8088->9000/tcp              eager_wright
[root@localhost ~]# docker inspect c5a96b674a38
 "Mounts": [
            {
                "Type": "volume",
                "Name": "eaffe03828e0e012bdfcf184f8e6eb42cbeafb485c94e24d0baf2a94f851b985",
                "Source": "/var/lib/docker/volumes/eaffe03828e0e012bdfcf184f8e6eb42cbeafb485c94e24d0baf2a94f851b985/_data",
                "Destination": "volume01",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            },
[root@localhost ~]# cd /var/lib/docker/volumes/eaffe03828e0e012bdfcf184f8e6eb42cbeafb485c94e24d0baf2a94f851b985/_data
[root@localhost _data]# ls
test
[root@localhost _data]# 

27.DockerFile指令说明

你可能感兴趣的:(2020-06-08 docker-2 实践)