1 Docker 课程简介
Docker Go Sware/Compose/mesos/k8s
2 Docker 是什么、解决什么问题?
开发 JAR/WAR- 运维-部署
环境、配置不同
代码/配置/系统/数据。。。 一起打包
部署-集群/弹性扩容
3 Docker 概念
基于Go语言,解决运行环境、配置问题的软件容器,是一种虚拟化技术。
一次封装,到处运行
下载 http://www.docker.com
中文 http://docker-cn.com
仓库 Docker Hub
https://hub.docker.com
镜像
容器
仓库 // 阿里云 网易云
4 安装、运行Docker
4.1更新
apt-get update
apt-get install apt-transport-https ca-certificates curl software-properties-common
4.2 添加仓库地址
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
4.3 添加 Docker 的官方 GPG 密钥
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
apt-key fingerprint
/etc/apt/trusted.gpg
--------------------
pub rsa4096 2017-02-22 [SCEA]
9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88
uid [ unknown] Docker Release (CE deb) <docker@docker.com>
sub rsa4096 2017-02-22 [S]
4.4 安装Docker
apt-get install docker-ce
4.5 启动
sudo systemctl enable docker
sudo systemctl start docker
4.6 测试运行 hello word
root@gmt-PowerEdge-R730:~# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:8e3114318a995a1ee497790535e7b88365222a21771ae7e53687ad76563e8e76
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
5 阿里云加速
阿里云账户,密码-你懂的
6 Docker 常用命令
6.1 帮助命令
docker verson
docker info
docker --help
6.2 镜像命令
// 列出本地镜像
docker images
docker images -a
docker images -q
docker images -qa
docker images --digests
root@gmt-PowerEdge-R730:/etc/docker# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 3 months ago 13.3kB
REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE
hello-world latest sha256:8e3114318a995a1ee497790535e7b88365222a21771ae7e53687ad76563e8e76 bf756fb1ae65 3 months ago 13.3kB
root@gmt-PowerEdge-R730:/etc/docker#
6.3 查找镜像
docker search
docker search -s 30
docker search tomcat
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
tomcat Apache Tomcat is an open source implementati… 2718 [OK]
tomee Apache TomEE is an all-Apache Java EE certif… 77 [OK]
dordoka/tomcat Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 base… 53 [OK]
bitnami/tomcat Bitnami Tomcat Docker Image 32 [OK]
kubeguide/tomcat-app Tomcat image for Chapter 1 28
consol/tomcat-7.0 Tomcat 7.0.57, 8080, "admin/admin" 17 [OK]
cloudesire/tomcat Tomcat server, 6/7/8 15 [OK]
aallam/tomcat-mysql Debian, Oracle JDK, Tomcat & MySQL 12 [OK]
arm32v7/tomcat Apache Tomcat is an open source implementati… 10
rightctrl/tomcat CentOS , Oracle Java, tomcat application ssl… 6 [OK]
maluuba/tomcat7-java8 Tomcat7 with java8. 4
unidata/tomcat-docker Security-hardened Tomcat Docker container. 4 [OK]
amd64/tomcat Apache Tomcat is an open source implementati… 2
jelastic/tomcat An image of the Tomcat Java application serv… 2
arm64v8/tomcat Apache Tomcat is an open source implementati… 2
ppc64le/tomcat Apache Tomcat is an open source implementati… 1
camptocamp/tomcat-logback Docker image for tomcat with logback integra… 1 [OK]
99taxis/tomcat7 Tomcat7 1 [OK]
oobsri/tomcat8 Testing CI Jobs with different names. 1
i386/tomcat Apache Tomcat is an open source implementati… 1
secoresearch/tomcat-varnish Tomcat and Varnish 5.0 0 [OK]
picoded/tomcat7 tomcat7 with jre8 and MANAGER_USER / MANAGER… 0 [OK]
appsvc/tomcat 0
cfje/tomcat-resource Tomcat Concourse Resource 0
s390x/tomcat Apache Tomcat is an open source implementati… 0
6.4 下载镜像
docker pull tomcat
6.5 删除镜像
docker rmi -f hello-word // 强制删除
docker rmi -f $(docker images -a -q) // 全部删除
6.6 容器命令-01
//运行
docker run [options] image [command] [arg…]
options:
–name : 容器的名字
-d:后台运行,返回容器ID
-i:交互式运行
-t:为容器分配一个为输入终端
-P:随机端口映射
-p:指定端口映射
// 列出所有运行的容器
docker ps -q
docker ps -l // 列出上次运行的
// 退出运行的容器
exit
ctrl+p+q
// 启动容器
docker start id // 容器id
//重启容器
docker restart id //
// 停止容器
// docker stop id //
// 强制停止
// docker kill id
// 删除容器
docker rm id
docker rm -f $(docker ps -q) // 删除全部正在运行的容器
6.7 容器命令-02
1、后台启动容器
docker run -d 镜像名称
2、查看容器日志
docker logs -f -t --tail id
3、docker run -d ubuntu /bin/sh -c “while ture; 、do echo hello zzzy;sleep 2;done”
4、docker top id
5、docker inspect id
6、进入正在运行的容器
docker attach id
docker exec -t id ls -l /tmp
docker exec -it d7b49adf9f7a /bin/bash
7、docker cp id:src_path dst_path
7 Docker 镜像原理
1、镜像:可独立运行的软件包, UnionFs(联合文件系统)
bootfs
rootfs
// 提交新的镜像
2、docker commit -m =“xxx” -a= “xxx” id 命名空间/镜像名:版本标签
docker run -it -p 8888:8080 tomcat
8 容器数据卷
容器的外部存储设备
数据持久化
容器之间共享数据
容器 到 主机
docker cp id:src_path:local_path
-v
默认 读写权限
docker run -it -v 宿主机绝对路径:容器内绝对路径 镜像名
容器只读
docker run -it -v 宿主机绝对路径:容器内绝对路径:ro 镜像名
DockerFile 添加
镜像的描述文件
https://hub.docker.com
// 创建dockerfile 在容器中创建
touch dockerfile
//编辑脚本
vim docker file
#volume test
FROM centos
VOLUME ["/dataVolumeContainer1","/dataVolumeContainer2"]
CMD echo "finished,-------success!"
cmd /bin/bash
docker build -f dockerfile -t 命名空间/镜像名 .
容器间传递共享数据
–volume-from
8 DockerFile
Docerfille 是构建镜像的构建文件,有一系列的命令和参数构成的脚本文件;
1、编译dockerfile
2、docker build 产生镜像
3、docker run
DockerFile 关键字
FROM
MAINTAINER
RUN
EXPOSE
WORKDIR
ENV
ADD
COPY
VOLUME
CMD
ENTRYPOINT
ONBUILD
8.1 DockerFile 案例-1
DockerFile dockerfile
FROM centos
MAINTINER zzzwan.l@avsgm.com
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 80
CMD echo $MYPATH
CMD echo “success------------------------ok”
cmd /bin/bash
docker build -f /mydocker/dockerfile -t mycentos:1.3 .
8.2 DockerFile :案例-02 自定义Tomcat
8.2.1 编辑Dockerfile
FROM ubuntu
MAINTAINER wanli<wan.l@avsgm.com>
ADD jdk-8u211-linux-x64.tar.gz /usr/local
ADD apache-tomcat-9.0.14.tar.gz /usr/local
RUN apt-get update
RUN apt-get install -y vim
#RUN apt-get install mysql-server
ENV MYPATH /usr/local
WORKDIR $MYPATH
ENV JAVA_HOME /usr/local/jdk1.8.0_211
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.14
ENV CATALINA_BASE /usr/local/apache-tomcat-9.0.14
ENV PATH=$PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
EXPOSE 9081
#ENTRYPOINT ["/usr/local/apache-tomcat-9.0.14/bin/startup.sh"]
#CMD ["/usr/local/apache-tomcat-9.0.14/bin/catalina.sh","run"]
CMD /usr/local/apache-tomcat-9.0.14/bin/startup.sh&&tail -F /usr/local/apache-tomcat-9.0.14/logs/catalina.out
8.2.2 BUILD 镜像
docker build -t mytomcat9 .
8.2.3 运行镜像
docker run -it -p 9081:8080 --name myt9 mytomcat9 /bin/bash
docker run -d -p 9080:8080 --name myt9 -v /zzyyuse/mydockerfile/tomcat9/test:/usr/local/apache-tomcat-9.08/webapps/test -v /zzzuse/mydockerfile/tomcat9/tomcat9logs/:usr/local/apache-tomcat-9.0.8/logs --privileged=true zzyytomcat9
8.2.4 总结
9、Docker安装MySQL
9.1 拉取mysql 镜像
root@gmt-PowerEdge-R730:~# docker pull mysql:5.7.26
5.7.26: Pulling from library/mysql
0a4690c5d889: Pull complete
98aa2fc6cbeb: Pull complete
0777e6eb0e6f: Pull complete
2464189c041c: Pull complete
b45df9dc827d: Pull complete
b42b00086160: Pull complete
bb93567627c7: Pull complete
48acc32b4863: Pull complete
6257d2da4815: Pull complete
1cd5ed3b2653: Pull complete
f4ba7ff24ae9: Pull complete
Digest: sha256:bdee7a98276ccf377d2c00b8ceaa9f65455a9376481467bbcc3d1e6b662dac5d
Status: Downloaded newer image for mysql:5.7.26
docker.io/library/mysql:5.7.26