docker 学习
视频来源:https://www.bilibili.com/video/BV1og4y1q7M4?p=1 遇见狂神说
介绍
Logo:鲸鱼集装箱
www.docker.com 官网
https://docs.docker.com/ docker官网文档极其详细,安装照着步骤抄就好了
www.dockerhub.com 镜像官网
镜像相关命令
启动docker服务
systemctl start docker
docker search 搜索镜像
[root@localhost ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10937 [OK]
mariadb MariaDB Server is a high performing open sou… 4136 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 812 [OK]
percona Percona Server is a fork of the MySQL relati… 539 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 88
mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 85
centurylink/mysql Image containing mysql. Optimized to be link… 59 [OK]
bitnami/mysql Bitnami MySQL Docker Image 52 [OK]
databack/mysql-backup Back up mysql databases to… anywhere! 43
deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r… 41 [OK]
prom/mysqld-exporter 39 [OK]
tutum/mysql Base docker image to run a MySQL database se… 35
linuxserver/mysql A Mysql container, brought to you by LinuxSe… 30
schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup… 29 [OK]
circleci/mysql MySQL is a widely used, open-source relation… 20
centos/mysql-56-centos7 MySQL 5.6 SQL database server 20
mysql/mysql-router MySQL Router provides transparent routing be… 19
arey/mysql-client Run a MySQL client from a docker container 17 [OK]
fradelg/mysql-cron-backup MySQL/MariaDB database backup using cron tas… 13 [OK]
openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 image… 6
devilbox/mysql Retagged MySQL, MariaDB and PerconaDB offici… 3
ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 2 [OK]
widdpim/mysql-client Dockerized MySQL Client (5.7) including Curl… 1 [OK]
jelastic/mysql An image of the MySQL database server mainta… 1
vitess/mysqlctld vitess/mysqlctld 1 [OK]
docker pull mysql
拉取
[root@localhost ~]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
69692152171a: Already exists
1651b0be3df3: Already exists
951da7386bc8: Already exists
0f86c95aa242: Already exists
37ba2d8bd4fe: Already exists
6d278bb05e94: Already exists
497efbd93a3e: Already exists
f7fddf10c2c2: Pull complete
16415d159dfb: Pull complete
0e530ffc6b73: Pull complete
b0a4a1a77178: Pull complete
cd90f92aa9ef: Pull complete
Digest: sha256:d50098d7fcb25b1fcb24e2d3247cae3fc55815d64fec640dc395840f8fa80969
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest
docker images
查看镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 2c9028880e58 2 weeks ago 447MB
mysql latest c0cdc95609f1 2 weeks ago 556MB
tomcat latest c0e850d7b9bb 5 weeks ago 667MB
nginx latest 62d49f9bab67 6 weeks ago 133MB
centos latest 300e315adb2f 5 months ago 209MB
docker history
学习其他的docker镜像构建过程
[root@localhost ~]# docker history tomcat
IMAGE CREATED CREATED BY SIZE COMMENT
c0e850d7b9bb 4 weeks ago /bin/sh -c #(nop) CMD [“catalina.sh” “run”] 0B
4 weeks ago /bin/sh -c #(nop) EXPOSE 8080 0B
4 weeks ago /bin/sh -c set -eux; nativeLines="
(
c
a
t
a
l
i
n
…
0
B
<
m
i
s
s
i
n
g
>
4
w
e
e
k
s
a
g
o
/
b
i
n
/
s
h
−
c
s
e
t
−
e
u
x
;
s
a
v
e
d
A
p
t
M
a
r
k
=
"
(catalin… 0B <missing> 4 weeks ago /bin/sh -c set -eux; savedAptMark="
(catalin…0B<missing>4weeksago/bin/sh−cset−eux;savedAptMark="(apt-m… 20.3MB
5 weeks ago /bin/sh -c #(nop) ENV TOMCAT_SHA512=81bfbd1… 0B
5 weeks ago /bin/sh -c #(nop) ENV TOMCAT_VERSION=9.0.45 0B
5 weeks ago /bin/sh -c #(nop) ENV TOMCAT_MAJOR=9 0B
5 weeks ago /bin/sh -c #(nop) ENV GPG_KEYS=05AB33110949… 0B
5 weeks ago /bin/sh -c #(nop) ENV LD_LIBRARY_PATH=/usr/… 0B
5 weeks ago /bin/sh -c #(nop) ENV TOMCAT_NATIVE_LIBDIR=… 0B
5 weeks ago /bin/sh -c #(nop) WORKDIR /usr/local/tomcat 0B
5 weeks ago /bin/sh -c mkdir -p "KaTeX parse error: Expected 'EOF', got '#' at position 78: …o /bin/sh -c #̲(nop) ENV PATH…(dpkg --print-… 342MB
5 weeks ago /bin/sh -c #(nop) ENV JAVA_VERSION=11.0.11+9 0B
6 weeks ago /bin/sh -c #(nop) ENV LANG=C.UTF-8 0B
6 weeks ago /bin/sh -c #(nop) ENV PATH=/usr/local/openj… 0B
6 weeks ago /bin/sh -c { echo ‘#/bin/sh’; echo 'echo "$J… 27B
6 weeks ago /bin/sh -c #(nop) ENV JAVA_HOME=/usr/local/… 0B
6 weeks ago /bin/sh -c set -eux; apt-get update; apt-g… 11.1MB
6 weeks ago /bin/sh -c apt-get update && apt-get install… 146MB
6 weeks ago /bin/sh -c set -ex; if ! command -v gpg > /… 17.5MB
6 weeks ago /bin/sh -c set -eux; apt-get update; apt-g… 16.5MB
6 weeks ago /bin/sh -c #(nop) CMD [“bash”] 0B
6 weeks ago /bin/sh -c #(nop) ADD file:e18bc3e10e7c743f1… 114MB
Docker Daemon docker守护进程
看图说话-docker镜像拉取
看图说话
![img](https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3762540439,4180220111&fm=26&gp=0.jpg)
docker运行容器
[root@localhost ~]# docker run -it tomcat /bin/bash
进入了一个小型的linux容器,能通过ls 命令查看目录文件,及一些基本的linux命令
root@f8b4a8dc6f28:/usr/local/tomcat# ls
BUILDING.txt NOTICE RUNNING.txt lib temp work
CONTRIBUTING.md README.md bin logs webapps
LICENSE RELEASE-NOTES conf native-jni-lib webapps.dist
文件挂载
卷挂载
-v 文件映射 主机的文件路径 容器内的文件路径
特点
1.目录是实时同步的
2.并且容器关闭后不会丢失
dockerfile
镜像制作
别人怎么做的,docker history
看图说话
FROM centos
RUN yum install wget
RUN wget -O redis.tar.gz “http://download.redis.io/releases/redis-5.0.3.tar.gz”
RUN tar -xvf redis.tar.gz
ENV 加载环境变量
WORKDIR /usr/local/tomcat 设置主目录
CMD(常用) 和 ENTRYPOINT 要执行的命令
区别就是一个直接加参数就行,一个每次都要命令重新写
ls -a 只要+ ls -l 自动拼接为 ls -al
ls -al
分层原理->联合文件系统
docker 快的原因
多个应用之间可以使用相同的层
看图说话->
docker网络
启动docker服务之后,ip addr 查看网络
docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
link/ether 02:42:75:9d:0a:91 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever
docker创建后,默认会有一个docker0的网络
用的是Veth Pire
桥接技术
--link 就是等于在对面主机配置一个Host
https://www.yuque.com/byteark-cloud/k8s/azro6u
查看所有docker网络
docker network --help
docker network ls 查询docker网络情况
docker run 默认有参数是 --net bridge 加了一个桥接模式 192.168.0.2 192.168.0.1 (桥接) 192.168.0.3 本机作为数据包的转发
网络创建有2种
一、通过run的时候 --link参数配置
二、自定义配置网络
–link网络和自定义网络的区别
tomcat02 192.168.0.2
tomcat03 192.168.0.3
–link 创建连接后可以ping通ip,但无法用过容器名称ping
自定义网络 docker network create 创建的容器,既可以通过ip ping通,又可以通过名称ping通
自定义网络的好处是保证集群健康
redis 集群
mysql 集群
网络连通
docker network connect --help
Usage: docker network connect [OPTIONS] NETWORK CONTAINER
Connect a container to a network
Options:
–alias strings Add network-scoped alias for the container
–driver-opt strings driver options for the network
–ip string IPv4 address (e.g., 172.30.100.104)
–ip6 string IPv6 address (e.g., 2001:db8::33)
–link list Add link to another container
–link-local-ip strings Add a link-local address for the container
一般是先创建一段网络,然后通过connect将容器加入到这个网络中就OK了
基本流程
1、开发通过IDEA等工具开发完,打包成jar包
2、编排dockerfile, 放到服务器上,通过dockerfile 进行创建镜像,以后要交付的是镜像,不是Jar包
3、创建镜像后通过docker run 就可以启动了
docker企业级应用(进阶)
docker compose (容器编排)
前面我们使用 Docker 的时候,定义 Dockerfile 文件,然后使用 docker build、docker run 等命令操作容器。然而微服务架构的应用系统一般包含若干个微服务,每个微服务一般都会部署多个实例,如果每个微服务都要手动启停,那么效率之低,维护量之大可想而知
使用 Docker Compose 可以轻松、高效的管理容器,它是一个用于定义和运行多容器 Docker 的应用程序工具
https://www.jianshu.com/p/658911a8cff3
通过yaml文件来创建打包
创建dockerfile
创建yaml文件,包括服务web,redis,mysql等等
有手就行搭建wordpress
docker-compose up 一键启动
https://docs.docker.com/samples/wordpress/
docker Swarm (容器集群管理)
Swarm 是目前 Docker 官方唯一指定(绑定)的集群管理工具。Docker 1.12 内嵌了 swarm mode 集群管理模式。
https://www.jianshu.com/p/9eb9995884a5?from=singlemessage
进化后变成 K8S
看图说话