实现部署jenkins的流程
配置java环境,导入jenkins包,运行命令 java -jar jenkins包,这里为了减少进入jenkins的web端安装插件,将插件提前部署到容器内。
制作dockerfile
创建镜像所在的文件夹和Dockerfile文件
mkdir /test
cd /testvim Dockerfile
###在Dockerfile文件中写入指令FROM centos:7.9.2009 #镜像的基层WORKDIR /opt #进入镜像到/opt下 功能等同与cdRUN yum -y install fontconfig #构建镜像时执行的命令,安装fontconfigADD jdk-11.0.16_linux-x64_bin.tar.gz /opt/ #将本地文件添加到容器中,tar类型文件会自动解压ADD jenkins-2.414.3.war /opt/ENV JAVA_HOME=/opt/jdk-11.0.16 #设置环境变量
ENV PATH=${PATH}:${JAVA_HOME}/bin
ENV JENKINS_HOME=${HOME}/.jenkins #定义jenkins的默认数据存放目录CMD [ "java","-jar","/opt/jenkins-2.414.3.war" ] #容器启动时 进行调用
创建镜像
docker build -t jenkins:1.0 .docker build 是docker创建镜像的命令
查看镜像
[root@docker test]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
jenkins 1.0 533af05bcb29 3 hours ago 1.01GB
nginx latest 605c77e624dd 23 months ago 141MB
mysql latest 3218b38490ce 23 months ago 516MB
centos latest 5d0da3dc9764 2 years ago 231MB
从镜像创建容器
docker run -itd -p 80:8080 -e JENKINS_HOME=/opt/.jenkins -v plugins:/opt/.jenkins/plugins/ jenkins:1.0-v 将宿主机的文件映射到容器内对应的文件
访问宿主机192.168.231.192
由于已经将插件的包从宿主机映射到容器内,因此不在需要我们手动安装,点进去会很快加载好
成功
实现部署wordpress
流程大概就是需要nginx 与php,需要MySQL数据库存储,需要拉取wordpress代码,我们不能在容器内修改nginx的配置文件,因此需要在创建镜像的时候就将宿主机的nginx.conf拷贝到镜像内。通过nginx来访问php,因此需要将nginx的默认网页发布页面修改为wordpress,将权限改为777,方便我们后续操作。我们手动启动不了nginx与php,因此可以写个脚本 将脚本拷贝到镜像内,让其在容器启动时代替我们启动nginx与php。
创建镜像所在的文件夹和Dockerfile文件
mkdir /root/wordpress
cd /root/wordpressvim Dockerfile####Dockerfile的内容
FROM centos:7.9.2009##下载nginx与php
RUN yum install -y epel-release \
; yum install -y nginx \
; yum install -y http://rpms.remirepo.net/enterprise/remi-release-7.rpm \
; yum install -y php80-php-xsl php80-php php80-php-cli php80-php-devel php80-php-gd php80-php-pdo php80-php-mysql php80-php-fpm##拷贝wordpress代码到镜像
ADD wordpress-6.4.2-zh_CN.tar.gz /usr/share/nginxCOPY nginx.conf /etc/nginx/COPY wordpress.sh /CMD [ "sh","/wordpress.sh" ]
宿主机的nginx配置文件
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;events {worker_connections 1024;
}http {log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';access_log /var/log/nginx/access.log main;sendfile on;tcp_nopush on;tcp_nodelay on;keepalive_timeout 65;types_hash_max_size 4096;include /etc/nginx/mime.types;default_type application/octet-stream;# Load modular configuration files from the /etc/nginx/conf.d directory.# See http://nginx.org/en/docs/ngx_core_module.html#include# for more information.server {listen 80;server_name www.itnzs.top;location / {root /usr/share/nginx/wordpress;index index.php;}location ~ \.php$ {root /usr/share/nginx/wordpress; #指定网站目录fastcgi_pass 127.0.0.1:9000; #指定访问地址fastcgi_index index.php; #指定默认文件fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #站点根目录,取决于root配置项include fastcgi_params; #包含nginx常量定义}}# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2;
# listen [::]:443 ssl http2;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }}
脚本
chmod 777 /usr/share/nginx/wordpress -R ##设置nginx网页发布目录的权限
nginx #启动nginx#启动php
/opt/remi/php80/root/usr/sbin/php-fpm
echo "nginx与php已启动成功"#因为你现在还没有做任何操作,docker容器是进程型的虚拟化,和vmware不同,vmware是真正的虚拟出来内存和核心的,docker容器当你的进程停止以后,容器也就停止了,这时候你需要把它夯住,我这里是动态查看nginx的日志 这个进程就自动不会停止,你也可以使用top 随机应变
tailf /var/log/nginx/*
创建镜像
docker build -t wordpress:5.0 .
创建容器,并启动
[root@docker wordpress]# docker run -it -p 80:80 wordpress:5.0
创建mysql的容器,这里是宿主机已经有mysql的镜像了
[root@docker ~]# docker run -itd -e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_DATABASE=wordpress --name mysql mysql##-e指定环境变量 指定登录数据库的root用户密码,指定创建mysql的数据库