Docker概述
类似集装箱隔离,
jar — 容器(mysql,redis)----发布到仓库-----从仓库下载
Docker历史
2010年dotcloud公司成立
2013 Docker开源
2014年4月9日,Docker1.0发布
在此之前都是用的虚拟机
基于go语言开发
Docker安装
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
yum install -y yum-utils
yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache fast
yum install docker-ce docker-ce-cli containerd.io
systemctl start docker
systemctl restart docker
docker version
docker run hello-world
[root@VM-0-15-centos ~]
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 5 weeks ago 13.3kB
yum remove docker-ce docker-ce-cli containerd.io
rm -rf /var/lib/docker
rm -rf /var/lib/containerd
回顾hello-world流程
底层原理
-
Docker是怎么工作的? Docker是一个Client-Server结构的系统,Docker的守护进程运行在主机上。通过Socket从客户端访 问! Docker-Server接收到Docker-Client的指令,就会执行这个命令! -
为什么Docker比Vm快? 1、docker有着比虚拟机更少的抽象层。由于docker不需要Hypervisor实现硬件资源虚拟化,运行在 docker容器上的程序直接使用的都是实际物理机的硬件资源。因此在CPU、内存利用率上docker将会在 效率上有明显优势。 2、docker利用的是宿主机的内核,而不需要Guest OS。 GuestOS: VM(虚拟机)里的的系统(OS); HostOS:物理机里的系统(OS);
因此,当新建一个 容器时,docker不需要和虚拟机一样重新加载一个操作系统内核。然而避免引导、加 载操作系统内核是个比较费时费资源的过程,当新建一个虚拟机时,虚拟机软件需要加载GuestOS,返 个新建过程是分钟级别的。而docker由于直接利用宿主机的操作系统,则省略了这个复杂的过程,因此 新建一个docker容器只需要几秒钟。
Docker的常用命令
帮助命令
docker version
docker info
docker <命令> --help
帮助文档
镜像命令
docker images
docker images -q
docker images -a
[root@VM-0-15-centos ~]
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 5 weeks ago 13.3kB
REPOSITORY 镜像仓库源
[root@VM-0-15-centos ~]
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11635 [OK]
mariadb MariaDB Server is a high performing open sou… 4429 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 863 [OK]
phpmyadmin phpMyAdmin - A web interface for MySQL and M… 362 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 91
--filter=STARS=3000 搜索出来的镜像就是STARS>3000
docker pull mysql[:tag]
tag版本号
[root@VM-0-15-centos ~]
Using default tag: latest
latest: Pulling from library/mysql
b380bbd43752: Pull complete
f23cbf2ecc5d: Pull complete
30cfc6c29c0a: Pull complete
b38609286cbe: Pull complete
8211d9e66cd6: Pull complete
2313f9eeca4a: Pull complete
7eb487d00da0: Pull complete
4d7421c8152e: Pull complete
77f3d8811a28: Pull complete
cce755338cba: Pull complete
69b753046b9f: Pull complete
b2e64b0ab53c: Pull complete
Digest: sha256:6d7d4524463fe6e2b893ffc2b89543c81dec7ef82fb2020a1b27606666464d87
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest
docker pull mysql
docker pull mysql[:latest]
docker rmi -f mysql:5.7
docker rmi -f 938b57d64674
docker rmi -f 容器ID 容器ID 容器ID 容器ID...
docker rmi -f $(docker images -aq)
容器命令
docker pull centos
docker run [可选参数] image
--name="Name" 容器名字
-d 后台方式运行
-it 交互方式运行,进入容器查看内容
-p (小写)指定容器的端口
-p ip:主机端口:容器端口
-p 主机端口:容器端口
-p 容器端口
-P 随机指定端口
docker run -it centos /bin/bash
exit
-a
-n=?
-q
docker ps
docker ps -a
exit
ctrl + P + Q
docker rm 容器id
docker rm -f $(docker ps -aq)
docker ps -a -q|xargs docker rm
docker start 容器id
docker restart 容器id
docker stop 容器id
docker kill 容器id
常用其他命令
[root@VM-0-15-centos ~]
cb597e7918560bb7ed2b9e7d068061e49a040e7a090f037d54747e6e56263144
[root@VM-0-15-centos ~]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
docker logs -f -t --tail 显示条数 容器id
docker logs -f -t 容器id
docker run -d centos /bin/sh -c "while true;do echo kuangshen;sleep 1;done"
docker logs -f -t --tail 10 06bea14ffa75
docker top 容器id
[root@VM-0-15-centos ~]
UID PID PPID C STIME TTY TIME CMD
root 19496 19477 0 21:13 ? 00:00:00 /bin/sh -c while true;do echo kuangshen;sleep 1;done
root 20997 19496 0 21:18 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
docker inspect 容器id
[root@VM-0-15-centos ~]
[
{
"Id": "06bea14ffa75b860cdfe105f5045b1be83a6639eee9552ffc159ae01b401a5c4",
"Created": "2021-11-07T13:13:56.785284074Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo kuangshen;sleep 1;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 19496,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-11-07T13:13:57.071684542Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/06bea14ffa75b860cdfe105f5045b1be83a6639eee9552ffc159ae01b401a5c4/resolv.conf",
docker exec -it 容器id /bin/bash
[root@VM-0-15-centos ~]
[root@06bea14ffa75 /]
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 13:13 ? 00:00:00 /bin/sh -c while true;do echo kuangshen;sleep 1;done
root 719 0 0 13:25 pts/0 00:00:00 /bin/bash
root 742 1 0 13:25 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
root 743 719 0 13:25 pts/0 00:00:00 ps -ef
docker attach 容器id
正在执行当前的代码...
docker exec
docker attach
docker cp 容器id:容器内路径 目的主机路径
[root@VM-0-15-centos home]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bad34ff90469 centos "/bin/bash" 31 seconds ago Up 30 seconds keen_einstein
[root@VM-0-15-centos home]
[root@bad34ff90469 /]
[root@bad34ff90469 home]
[root@bad34ff90469 home]
[root@bad34ff90469 home]
exit
[root@VM-0-15-centos home]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@VM-0-15-centos home]
[root@VM-0-15-centos home]
lighthouse redis test.java www
小结
练习
安装nginx
[root@VM-0-15-centos home]
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 15763 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 2091 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 818 [OK]
jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho… 273
linuxserver/nginx An Nginx container, brought to you by LinuxS… 159
tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 143 [OK]
jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager 143 [OK]
alfg/nginx-rtmp NGINX, nginx-rtmp-module and FFmpeg from sou… 110 [OK]
jasonrivers/nginx-rtmp Docker images to host RTMP streams using NGI… 95 [OK]
nginxdemos/hello NGINX webserver that serves a simple page co… 76 [OK]
privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm & Al… 60 [OK]
nginx/nginx-ingress NGINX and NGINX Plus Ingress Controllers fo… 55
nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles 54
staticfloat/nginx-certbot Opinionated setup for automatic TLS certs lo… 25 [OK]
nginxproxy/nginx-proxy Automated Nginx reverse proxy for docker con… 24
nginx/nginx-prometheus-exporter NGINX Prometheus Exporter for NGINX and NGIN… 22
schmunk42/nginx-redirect A very simple container to redirect HTTP tra… 19 [OK]
centos/nginx-112-centos7 Platform for running nginx 1.12 or building … 15
centos/nginx-18-centos7 Platform for running nginx 1.8 or building n… 13
flashspys/nginx-static Super Lightweight Nginx Image 11 [OK]
mailu/nginx Mailu nginx frontend 9 [OK]
webdevops/nginx Nginx container 9 [OK]
sophos/nginx-vts-exporter Simple server that scrapes Nginx vts stats a… 7 [OK]
ansibleplaybookbundle/nginx-apb An APB to deploy NGINX 3 [OK]
wodby/nginx Generic nginx 1 [OK]
[root@VM-0-15-centos home]
Using default tag: latest
latest: Pulling from library/nginx
b380bbd43752: Pull complete
fca7e12d1754: Pull complete
745ab57616cb: Pull complete
a4723e260b6f: Pull complete
1c84ebdff681: Pull complete
858292fd2e56: Pull complete
Digest: sha256:644a70516a26004c97d0d85c7fe1d0c3a67ea8ab7ddf4aff193d9f301670cf36
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@VM-0-15-centos home]
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 87a94228f133 3 weeks ago 133MB
centos latest 5d0da3dc9764 7 weeks ago 231MB
[root@VM-0-15-centos home]
bd1dd8f51eb8efe2359ed1e21ede663f82fc22a630f83ef87a85f0b24aaa96e3
[root@VM-0-15-centos home]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bd1dd8f51eb8 nginx "/docker-entrypoint.…" 3 seconds ago Up 2 seconds 0.0.0.0:3344->80/tcp, :::3344->80/tcp nginx01
bad34ff90469 centos "/bin/bash" 33 minutes ago Up 16 minutes keen_einstein
[root@VM-0-15-centos home]
<!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>
docker run -d --name nginx01 -p 3344:80 nginx
安装tomcat
docker run -it --rm tomcat:9.0
docker run -d -p 3355:8080 --name tomca01 tomcat
部署es+kibana
docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.6.2
docker stats
docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xmx512m" elasticsearch:7.6.2
使用kibana连接es
可视化
docker图形化界面管理工具,提供一个后台面板
docker run -d -p 8088:9000 --restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer
访问测试
42.193.51.130:8088
Commit镜像
docker commit
docker commit -m="提交的描述信息" -a="作者" 容器id 目标镜像名:TAG
测试
docker run -it -p 8081:8080 tomcat
cp -r webapps.dist/* webapps/
[root@VM-0-15-centos ~]
sha256:2a9f9f5e81867c0be15d11b88200ba76e8876f26506bf20c38e562283268b396
[root@VM-0-15-centos ~]
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat02 1.0 2a9f9f5e8186 4 seconds ago 684MB
tomcat latest b0e0b0a92cf9 2 weeks ago 680MB
nginx latest 87a94228f133 3 weeks ago 133MB
centos latest 5d0da3dc9764 7 weeks ago 231MB
portainer/portainer latest 580c0e4e98b0 7 months ago 79.1MB
elasticsearch 7.6.2 f29a1ee41030 19 months ago 791MB
容器数据卷
如果数据在容器里,容器删了后数据会丢失
需求:数据持久化
数据可以存储在本地
目录的挂载
容器的持久化和同步操作,容器间可以数据共享
docker run -v 本机目录:容器目录
docker run -it -v /home/ceshi:/home centos /bin/bash
测试
docker pull mysql:5.7
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 mysql02 mysql:5.7
具名和匿名挂载
-v容器内的路径
docker run -d -P --name nginx01 -v /etc/nginx nginx
[root@VM-0-15-centos data]
DRIVER VOLUME NAME
local a8af848d9eaef19d1f4250e67d4036f0dd6972d91d61d72370d363637f196cfb
[root@VM-0-15-centos data]
82c334c8dca81c12780d2b0698e27495dad4733b7fab15a44944d8ba6e892ff2
[root@VM-0-15-centos data]
DRIVER VOLUME NAME
local a8af848d9eaef19d1f4250e67d4036f0dd6972d91d61d72370d363637f196cfb
local juming-nginx
[root@VM-0-15-centos data]
[
{
"CreatedAt": "2021-11-08T16:59:02+08:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/juming-nginx/_data",
"Name": "juming-nginx",
"Options": null,
"Scope": "local"
}
]
大多使用具名挂载
-v 容器内路径
-v 卷名:容器内路径
-v /宿主机路径:容器内路径
拓展:
ro
rw
docker run -d -P --name nginx05 -v juming:/etc/nginx:ro nginx
docker run -d -P --name nginx05 -v juming:/etc/nginx:rw nginx
构建docker镜像的构建文件,命令脚本
FROM centos
VOLUME ["volume01","volume02"]
CMD echo "----end----"
CMD /bin/bash
--volumes-from list Mount volumes from the specified container(s)
docker run -d -p 3306: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
docker run -d -p 3307:3306 -e MYSQL_ROOT_PASSWORD=123456 --name
mysql02 --volumes-from mysql01 mysql:5.7
容器之间的配置信息的传递,数据卷容器的生命周期一直持续到没有容器使用为止。 但是一旦你持久化到了本地,这个时候,本地的数据是不会删除的!
DockerFile
1.DockerFile介绍
dockerfile 是用来构建docker镜像的文件!命令参数脚本! 构建步骤: 1、 编写一个dockerfile文件 2、 docker build 构建称为一个镜像 3、 docker run运行镜像 4、 docker push发布镜像(DockerHub 、阿里云仓库) 但是很多官方镜像都是基础包,很多功能没有,我们通常会自己搭建自己的镜像! 官方既然可以制作镜像,那我们也可以!
2.DockerFile构建过程
基础知识:
1、每个保留关键字(指令)都是必须是大写字母 2、执行从上到下顺序 3、#表示注释 4、每一个指令都会创建提交一个新的镜像层,并提交!
Dockerfile是面向开发的,我们以后要发布项目,做镜像,就需要编写dockerfile文件,这个文件十分简 单! Docker镜像逐渐成企业交付的标准,必须要掌握! DockerFile:构建文件,定义了一切的步骤,源代码 DockerImages:通过DockerFile构建生成的镜像,最终发布和运行产品。 Docker容器:容器就是镜像运行起来提供服务。
3.DockerFile的指令
FROM
MAINTAINER
RUN
ADD
WORKDIR
VOLUME
EXPOSE
CMD
ENTRYPOINT
ONBUILD
COPY
ENV
FROM scratch
创建centos
FROM centos
MAINTAINER ghhgy<你的邮箱>
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 80
CMD echo $MYPATH
CMD echo "----end----"
CMD /bin/bash
docker build -f mydockerfile-centos -t mycentos:0.1 .
[root@VM-0-15-centos dockerfile]
IMAGE CREATED CREATED BY SIZE COMMENT
9956616c2ab2 5 minutes ago /bin/sh -c
b300db55d9c9 5 minutes ago /bin/sh -c
e5c27ca58c9c 6 minutes ago /bin/sh -c
b100a7b87ba1 6 minutes ago /bin/sh -c
680c65bf9616 6 minutes ago /bin/sh -c yum -y install net-tools 33.7MB
3d2b83702280 6 minutes ago /bin/sh -c yum -y install vim 75MB
80225a43f177 8 minutes ago /bin/sh -c
7984a76b67ce 8 minutes ago /bin/sh -c
a7c6e1014333 8 minutes ago /bin/sh -c
5d0da3dc9764 7 weeks ago /bin/sh -c
<missing> 7 weeks ago /bin/sh -c
<missing> 7 weeks ago /bin/sh -c
CMD 和 ENTYPOINT 区别
CMD
ENTRYPOINT
测试CMD
[root@VM-0-15-centos dockerfile]
FROM centos
CMD ["ls","-a"]
[root@VM-0-15-centos dockerfile]
[root@VM-0-15-centos dockerfile]
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
[root@VM-0-15-centos dockerfile]
docker: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "-l": executable file not found in $PATH: unknown.
测试ENTRYPOINT
[root@VM-0-15-centos dockerfile]
FROM centos
ENTRYPOINT ["ls","-a"]
[root@VM-0-15-centos dockerfile]
[root@VM-0-15-centos dockerfile]
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
[root@VM-0-15-centos dockerfile]
total 56
drwxr-xr-x 1 root root 4096 Nov 8 10:19 .
drwxr-xr-x 1 root root 4096 Nov 8 10:19 ..
-rwxr-xr-x 1 root root 0 Nov 8 10:19 .dockerenv
lrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bin
drwxr-xr-x 5 root root 340 Nov 8 10:19 dev
drwxr-xr-x 1 root root 4096 Nov 8 10:19 etc
drwxr-xr-x 2 root root 4096 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 4096 Sep 15 14:17 lost+found
drwxr-xr-x 2 root root 4096 Nov 3 2020 media
drwxr-xr-x 2 root root 4096 Nov 3 2020 mnt
drwxr-xr-x 2 root root 4096 Nov 3 2020 opt
dr-xr-xr-x 142 root root 0 Nov 8 10:19 proc
dr-xr-x--- 2 root root 4096 Sep 15 14:17 root
drwxr-xr-x 11 root root 4096 Sep 15 14:17 run
lrwxrwxrwx 1 root root 8 Nov 3 2020 sbin -> usr/sbin
drwxr-xr-x 2 root root 4096 Nov 3 2020 srv
dr-xr-xr-x 13 root root 0 Nov 8 10:17 sys
drwxrwxrwt 7 root root 4096 Sep 15 14:17 tmp
drwxr-xr-x 12 root root 4096 Sep 15 14:17 usr
drwxr-xr-x 20 root root 4096 Sep 15 14:17 var
Dockerfile中很多命令都十分的相似,我们需要了解它们的区别,我们最好的学习就是对比他们然后测 试效果!
4.实战:Tomcat镜像
1、准备镜像文件 准备tomcat 和 jdk到当前目录,编写好README 。
[root@VM-0-15-centos webapps]
FROM centos
MAINTAINER ghhgy<你的邮箱>
COPY readme.txt /usr/local/readme.txt
ADD jdk-8u211-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-9.0.29.tar.gz /usr/local/
RUN yum -y install vim
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.29
ENV CATALINA_BASH /usr/local/apache-tomcat-9.0.29
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
EXPOSE 8080
CMD /usr/local/apache-tomcat-9.0.29/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.29/logs/catalina.out
2、构建镜像
docker build -t diytomcat .
3、运行镜像并挂载卷
docker run -d -p 8081:8080 -v /home/mytomcat/webapps/test:/usr/local/apache-tomcat-9.0.29/webapps/test -v /home/mytomcat/logs:/usr/local/apache-tomcat-9.0.29/logs diytomcat
4、在本机/home/mytomcat/webapps/test目录下新建WEB-INFO/web.xml和index.jsp
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"
metadata-complete="true">
</web-app>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>
Hello World!<br/>
<%
out.println("你的 IP 地址 " + request.getRemoteAddr());
%>
</body>
</html>
进入test的url进行访问
5.发布自己的镜像
1、地址 https://hub.docker.com/ 2、确定这个账号可以登录 3、登录
[root@VM-0-15-centos test]
Usage: docker login [OPTIONS] [SERVER]
Log in to a Docker registry.
If no server is specified, the default is defined by the daemon.
Options:
-p, --password string Password
--password-stdin Take the password from stdin
-u, --username string Username
docker login -u ghhgy
docker tag diytomcat:latest ghhgy/tomcat:1.0
docker push ghhgy/tomcat:1.0
小结
Docker网络
|