文章目录

展开

Dockerfile介绍

Dockerfile是一个用来构建镜像的文本文件,文本内容包含了一条条构建镜像所需的指令和说明。

镜像:中药
dockerfile:配方
dockerfile常用指令

为什么使用Dockerfile?

手动打镜像的缺陷:
1)不支持环境变量
2)容器启动时.自动启动服务
3)不暴露端口

Dockerfile语法

FROM:指定基础镜像
RUN:在基础镜像中.执行命令
CMD:容器启动时.执行的pid为1的进程命令
ENTRYPOINT:容器启动时,执行的pid为1的进程命令(当ENTRYPOINT存在时,如果执行的是一个脚本,那么将CMD中的命令看成是参数)
ADD:将宿主机上的包或文件,传递到容器中(如果是压缩包,则自动解压)
COPY:将宿主机上的包或文件,传递到容器中(如果是压缩包,则不解压)
WORKDIR:指定工作目录
EXPOSE:声明端口
VOLUME:持久化卷,指定目录挂载点ENV:环境变量
LABEL:镜像标签
MAINTAINER:管理者标识(镜像作者)

Dockerfile编写

## 创建一个dockerfile目录并进入
[root@docker01 ~]# mkdir wordpress
[root@docker01 ~]# cd wordpress/
[root@docker01 wordpress]# ll
total 31052
-rw-rw-rw- 1 root root      431 Sep 11 10:09 blog.conf
-rw-r--r-- 1 root root      491 Sep 11 10:37 Dockerfile
-rw-rw-rw- 1 root root 20682659 Sep 11 10:09 nginx_php.tgz
-rw-rw-rw- 1 root root       83 Sep 11 10:09 start.sh
-rw-rw-rw- 1 root root 11098483 Sep 11 10:09 wordpress-5.0.3-zh_CN.tar.gz
## 编写dockerfile
[root@docker01 wordpress]# vim dockerfile 
FROM centos:7
RUN  curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
RUN curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
RUN yum install -y mariadb-server
ADD nginx_php.tgz /opt
RUN yum localinstall -y /opt/*.rpm
RUN /usr/libexec/mariadb-prepare-db-dir %n
RUN rm -fr /etc/nginx/conf.d/*
COPY blog.conf /etc/nginx/conf.d/
RUN mkdir /code
ADD wordpress-5.0.3-zh_CN.tar.gz /code
ADD start.sh ./
CMD ["/bin/sh","./start.sh"] 
## 构建镜像(.为当前目录也可以用绝对路径)
[root@docker01 wordpress]# docker build -t wp:v2 .
[root@docker01 wordpress]# docker images
REPOSITORY                      TAG                 IMAGE ID       CREATED         SIZE
wp                              v2                  4af812fe9cc3   9 hours ago     1.02GB

Dockerfile优化

优化原则:

1)用完包,删除

2)叠层,减少层数,将所有的RUN合并

3)yum缓存一定要清

[root@docker01 wordpress]# vim dockerfile 
FROM centos:centos7
ADD nginx_php.tgz /opt/
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo &&\
curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo &&\
yum -y localinstall /opt/*.rpm &&\
mkdir /code &&\
yum -y install mariadb-server.x86_64 &&\
/usr/libexec/mariadb-prepare-db-dir %n &&\
rm -f /opt/* &&\
yum clean all 
COPY start.sh /
ADD wordpress-5.0.3-zh_CN.tar.gz /code
COPY boke.conf /etc/nginx/conf.d/
CMD ["/bin/sh","/start.sh"]
EXPOSE 80/tcp 443/tcp
VOLUME /code/wordpress
[root@docker01 wordpress]# docker build -t wp:v3 .
[root@docker01 wordpress]# docker images
REPOSITORY                      TAG                 IMAGE ID       CREATED         SIZE
wp                              v3                  f742ff71f1e3   9 hours ago     1.02GB

使用docker运行MySQL

镜像地址:https://hub.docker.com/_/mysql/tags

#1.拉取镜像
[root@docker01 ~]# docker pull mysql:5.7

#2.查看本地镜像
[root@docker01 ~]# docker images
mysql                           5.7             92034fe9a41f   5 weeks ago      581MB

#3.启动镜像
[root@docker01 ~]# docker run --name mysql57 -v /data/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123 -e MYSQL_DATABASE='wordpress' -e MYSQL_USER='wp_user' -e MYSQL_PASSWORD='111' -p 3306:3306 -d mysql:5.7
参数说明:
--name mysql57                      #定于镜像名称为MySQL57
-v /data/mysql:/var/lib/mysql       #将镜像的/var/lib/mysql数据目录映射为宿主机的/data/mysql
-e MYSQL_ROOT_PASSWORD=123          #设置 MySQL 服务 root 用户的密码为123
-e MYSQL_DATABASE='wordpress'       #创建wordpress库
-e MYSQL_USER='wp_user'             #指定库的授权用户为wp_user
-e MYSQL_PASSWORD='111'             #指定库密码为111
-p 3306:3306                        #将镜像的端口3306映射到宿主机的3306端口
-d mysql:5.7                        #在后台运行容器名称为mysql:5.7

#4.查看镜像
[root@docker01 ~]# docker ps -a
CONTAINER ID   IMAGE                                         COMMAND                  CREATED       STATUS       PORTS       
ac4438581327   mysql:5.7                                     "docker-entrypoint.s…"   4 hours ago   Up 4 hours   33060/tcp, 0.0.0.0:33060->3306/tcp, :::33060->3306/tcp   zabbix-db

#5.使用宿主机进行登录并查看
[root@docker02 ~]# mysql -uwp_user -p111 -h 127.0.0.1
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.43 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| wordpress          |
+--------------------+
2 rows in set (0.01 sec)

容器通信

容器的单向通信

[root@docker01 ~]# docker run \
 --name mysql57 \
 -v /data/mysql:/var/lib/mysql \
 -e MYSQL_ROOT_PASSWORD=123 \
 -e MYSQL_DATABASE='wordpress' \
 -e MYSQL_USER='wp_user' \
 -e MYSQL_PASSWORD='111' \
 -p 3306:3306 \
 -d mysql:5.7
7c66f800ebbd3d106fe0ff319a82ec06c67e12e58a29adba4b6609256b011b3e

[root@docker01 ~]# docker run --name wp -p 80:80 --link mysql57 -d wp:v2
846291be5c5d9ee57693597723a8d09a83b1d04a6410af575b7d3508e8f642b3

[root@docker01 ~]# docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
846291be5c5d   wp:v2       "/bin/sh ./start.sh"     19 seconds ago   Up 18 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp                      wp
7c66f800ebbd   mysql:5.7   "docker-entrypoint.s…"   54 seconds ago   Up 54 seconds   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql57

再次访问可以进行连接数据库
image-20230911192920934

image-20230911193357436

## 授权
[root@docker01 ~]# docker exec -it wp /bin/bash
[root@846291be5c5d /]# chown -R apache.apache /code/wordpress/

## 重新访问

image-20230911193430888

启动nginx 启动php

nginx -g 'daemon off;'
/usr/sbin/php-fpm --nodaemonize

使用docker搭建zabbix(5.0)

# 1.启动zabbix数据库
docker run \
--name zabbix-db \
-v /data/zabbix:/var/lib/mysql \       ## 如果已经映射过了要先删除
-e MYSQL_ROOT_PASSWORD=123 \
-e MYSQL_DATABASE='zabbix' \
-e MYSQL_USER='zabbix' \
-e MYSQL_PASSWORD='123' \
-p 33060:3306 \
-d mysql:5.7 \
--character-set-server=utf8 \
--collation-server=utf8_bin

# 2.启动zabbix-server
docker run \
--name zabbix-server \
--link zabbix-db \
-e DB_SERVER_HOST="zabbix-db" \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="123" \
-p 10051:10051 \
-d zabbix/zabbix-server-mysql:5.0.37-alpine

# 3.启动zabbix前端
docker run \
--link zabbix-db \
--link zabbix-server \
--name zabbix-web \
-e DB_SERVER_HOST="zabbix-db" \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="123" \
-e ZBX_SERVER_HOST="zabbix-server" \
-e PHP_TZ="Asia/Shanghai" \
-p 8080:8080 \
-d zabbix/zabbix-web-nginx-mysql:5.0.37-alpine

image-20230911195518094

image-20230911195532168