docker构建镜像

docker容器部署web应用



一、构建基础镜像

1.1基于CentOS7.9的ssh基础镜像

Dockerfile

FROM centos:7.9.2009
MAINTAINER this is ssh image <sshd>
RUN yum -y install openssh* net-tools lsof telnet passwd vim 
RUN echo 'oa654321' | passwd --stdin root
RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config
RUN sed -ri '/^session\s+required\s+pam_loginuid.so/ s/^/#/' /etc/pam.d/sshd
RUN ssh-keygen -t rsa -A	
RUN mkdir -p /root/.ssh && chown root.root /root && chmod 700 /root/.ssh
EXPOSE 22
CMD ["/usr/sbin/sshd","-D"]

启动2个容器名称分别为vm1,vm2

docker run -d --name=vm1 --privileged sshd:centos7.9
docker run -d --name=vm1 --privileged sshd:centos7.9

进入到容器当中然后对传文件

docker exec -it 8293c9381e3d /bin/bash
scp -r root@ip(vm2):/home

1.2基于龙晰Anolis OS 8.8的ssh基础镜像

[root@k8s-master01 ~]# docker search anolisos

在这里插入图片描述

[root@k8s-master01 ~]# docker pull openanolis/anolisos

在这里插入图片描述
创建容器

docker run -d --name anolisos --privileged -p 10022:22 openanolis/anolisos:latest /usr/sbin/init

进入容器并查看龙晰版本

docker exec -it anolisos bash

在这里插入图片描述
系统升级

yum update -y

在这里插入图片描述
安装必要的软件

yum -y install vim zip net-tools gcc gcc-c++ telnet lsof glibc-locale-source glibc-langpack-en

安装 ssh 并允许远程登陆

##安装ssh
yum install openssh-server -y
yum install openssh-clients -y
systemctl start sshd
##设为开机启动
systemctl enable sshd
##Anolis OS默认允许root远程登录,修改root密码
yum install passwd -y
passwd
密码
systemctl enable status

在这里插入图片描述
在这里插入图片描述

数据清理

rm -rf ~/.cache/*
yum clean all

在这里插入图片描述
打包自己的镜像

root@k8s-master01 ssh]# docker commit anolisos anolisos:v1

在这里插入图片描述

二、构建web镜像

2.1web部署文件准备

[root@shigj A8]# ll
总用量 12
drwxrwxr-x. 10 root root  252 1月   9 18:13 ApacheJetspeed
drwxrwxr-x. 15 root root  211 1月   9 18:13 base
drwxrwxr-x.  2 root root  274 1月   9 18:14 inst
drwxrwxr-x.  7 root root  208 1月   9 18:13 jdk
drwxr-xr-x.  2 root root  182 1月   9 18:13 Logs
drwxrwxr-x.  6 root root 4096 1月   9 18:14 OfficeTrans
drwxrwxrwx.  8 root root 4096 1月   9 18:14 S1
drwxr-xr-x.  2 root root 4096 1月   9 18:13 SiiFiles
drwxrwxr-x.  2 root root  144 1月   9 18:14 Uninstall_A8
drwxr-xr-x.  7 root root  208 1月   9 18:15 Uninstall_jdk
[root@shigj A8]# pwd
/home/V80sp2Seeyon/A8

2.2Centos基础镜像准备

[root@shigj V80sp2Seeyon]# docker pull centos:7.9.2009
7.9.2009: Pulling from library/centos
2d473b07cdd5: Pull complete 
Digest: sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987
Status: Downloaded newer image for centos:7.9.2009
docker.io/library/centos:7.9.2009
[root@shigj V80sp2Seeyon]# docker images|grep centos
centos        7.9.2009   eeb6ee3f44bd   3 months ago   204MB

2.3Dockerfile的编写

[root@shigj V80sp2Seeyon]# ls
A8  Dockerfile  start.sh
[root@shigj V80sp2Seeyon]# cat Dockerfile 
FROM centos:7.9.2009
LABEL maintainer="app@seeyon.com"
WORKDIR /home

#容器基础环境处理,编码、时区、文件目录等。
RUN localedef -c -i zh_CN -f UTF-8 zh_CN.UTF-8 \
    && echo '' >> /etc/bashrc \
    && echo 'export LANGUAGE="zh_CN.UTF-8"' >> /etc/bashrc \
    && echo 'export LC_ALL="zh_CN.UTF-8"' >> /etc/bashrc \
    && echo 'export LANG="zh_CN.UTF-8"' >> /etc/bashrc \
    && rm -f /etc/locale.conf \
    && echo 'LANG="zh_CN.UTF-8"' >> /etc/locale.conf \
    && rm -f /etc/localtime \
    && ln -sv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && echo "Asia/Shanghai" > /etc/timezone \
    && mkdir -p /home/A8/ApacheJetspeed \
    && mkdir -p /home/A8/base \
    && mkdir -p /home/A8/jdk
	
# 添加A8应用文件,不添加配置文件
ADD ./A8/ApacheJetspeed /home/A8/ApacheJetspeed
ADD ./A8/jdk /home/A8/jdk
ADD ./start.sh /home/A8/
ADD ./A8/ApacheJetspeed/bin/catalina_custom.sh /home/A8/ApacheJetspeed/bin/

#给脚本执行权限
RUN chmod +x /home/A8/start.sh \
&& chmod +x /home/A8/ApacheJetspeed/bin/catalina_custom.sh \
&& chmod +x /home/A8/ApacheJetspeed/bin/startup.sh

#暴露容器端口
EXPOSE 80

#启动时执行start.sh脚本
ENTRYPOINT ["/home/A8/start.sh"]

启动脚本编写

[root@shigj V80sp2Seeyon]# cat start.sh 
#!/bin/bash
#让 Tomcat 使用调用 startup.sh 的那个 shell 而不是生成一个新 shell
sed -i 's/exec "\$PRGDIR"\/"\$EXECUTABLE" start "\$@"/exec "$PRGDIR"\/"$EXECUTABLE" run "$@"/' /home/A8/Apach
eJetspeed/bin/startup.sh#启动应用
sh /home/A8/ApacheJetspeed/bin/startup.sh

2.4打包镜像并导出

[root@shigj V80sp2Seeyon]# docker build -t a8v5:v8.0sp2 .
Sending build context to Docker daemon  4.636GB
Step 1/11 : FROM centos:7.9.2009
 ---> eeb6ee3f44bd
Step 2/11 : LABEL maintainer="app@seeyon.com"
 ---> Running in b383d4117218
Removing intermediate container b383d4117218
 ---> 5d51f9a04e40
Step 3/11 : WORKDIR /home
 ---> Running in 72a25d65d84a
Removing intermediate container 72a25d65d84a
 ---> 4e7feee18cd8
Step 4/11 : RUN localedef -c -i zh_CN -f UTF-8 zh_CN.UTF-8     && echo '' >> /etc/bashrc     && echo 'export 
LANGUAGE="zh_CN.UTF-8"' >> /etc/bashrc     && echo 'export LC_ALL="zh_CN.UTF-8"' >> /etc/bashrc     && echo 'export LANG="zh_CN.UTF-8"' >> /etc/bashrc     && rm -f /etc/locale.conf     && echo 'LANG="zh_CN.UTF-8"' >> /etc/locale.conf     && rm -f /etc/localtime     && ln -sv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime     && echo "Asia/Shanghai" > /etc/timezone     && mkdir -p /home/A8/ApacheJetspeed     && mkdir -p /home/A8/base     && mkdir -p /home/A8/jdk ---> Running in e2e44e34ae76
'/etc/localtime' -> '/usr/share/zoneinfo/Asia/Shanghai'
Removing intermediate container e2e44e34ae76
 ---> cae16c52657f
Step 5/11 : ADD ./A8/ApacheJetspeed /home/A8/ApacheJetspeed
 ---> eff47f4d4d9f
Step 6/11 : ADD ./A8/jdk /home/A8/jdk
 ---> 12064c3ec9cf
Step 7/11 : ADD ./start.sh /home/A8/
 ---> df9cc75d04fc
Step 8/11 : ADD ./A8/ApacheJetspeed/bin/catalina_custom.sh /home/A8/ApacheJetspeed/bin/
 ---> 1ee6fd695904
Step 9/11 : RUN chmod +x /home/A8/start.sh && chmod +x /home/A8/ApacheJetspeed/bin/catalina_custom.sh && chmo
d +x /home/A8/ApacheJetspeed/bin/startup.sh ---> Running in ccc70dbe7a5d
Removing intermediate container ccc70dbe7a5d
 ---> d0fad5578951
Step 10/11 : EXPOSE 80
 ---> Running in d536591daf71
Removing intermediate container d536591daf71
 ---> 160ff6682ee7
Step 11/11 : ENTRYPOINT ["/home/A8/start.sh"]
 ---> Running in 24aa9f511cb2
Removing intermediate container 24aa9f511cb2
 ---> ad044bd131fa
Successfully built ad044bd131fa
Successfully tagged a8v5:v8.0sp2
查看镜像
[root@shigj V80sp2Seeyon]# docker images|grep a8v5
a8v5          v8.0sp2    ad044bd131fa   About a minute ago   2.72GB
导出镜像
[root@shigj V80sp2Seeyon]# docker save a8v5 -o a8v5.tar
[root@shigj V80sp2Seeyon]# ls
A8  a8v5.tar  Dockerfile  start.sh

2.5导入镜像

删除镜像
[root@shigj home]# docker rmi a21675dcd486
Untagged: a8v5:v8.0sp2
Deleted: sha256:a21675dcd486da05a14c9507d7b27581c52a54a33383e45605016757db74001d
Deleted: sha256:4371be2a90bf6792c37e2707957a0bb5c3fc210759efb42aad8b4d5bf29c90d8
Deleted: sha256:273af8f08f8197be97fd34e0b106d93465799c1e1e17ddaadd15003ada87b0aa
Deleted: sha256:ce263708adb2d8bce5766055e82ba56f2cd6a907699046df4e4993ed7cd5b2be
Deleted: sha256:b437f28447b5433b210ce46686173c798464fd23676b4b2c41d437b57725a712
Deleted: sha256:7ae7f779fabc9d89eb52bf9c19a211bed8f7c6506d696ef54abea07e1f70e4e3
Deleted: sha256:c31bb060253b4c35a7e4ddf4aa915973bf36171cf8c6a69ea457a9b15f472c99
Deleted: sha256:b7bbe5c96cb9658c583ed364caa1ba60a0ba1116ce6666f7d617a878f390661a
Deleted: sha256:def50275ecdfd39924ae327d25fa4d9cdb3422886e95728a2822f7101d57e25f
Deleted: sha256:29aced9bb164cdda0f4c52e49909039174c57d45ca8fc42b92403ca7d210d7a0
Deleted: sha256:4589c706c07895e682d678ffe69f2bc46658da4bb642e1a446314ddd6fc5ff78
Deleted: sha256:81a5410c4cc30c879b4b7ec98672e006b733690ebecb91b030905f1a9d413110
Deleted: sha256:bcbe40d74abed6aef16c1772171a353760aa5b1776a33234630d3971140db4cb
Deleted: sha256:85b75def5d1c65874a783a4f5416ed8a80e1dadfbbaa08ccd52ccf1e5f598290
Deleted: sha256:c98f4c0624e534f5a17e2cc1e3c81dffe6368249e7943c16bad675821ebe6eeb
Deleted: sha256:c5e358f96d0751fcf888ccb87e408eba8ee1f2113e4fdee91744d31e423fc3a8
镜像加载
[root@shigj home]# docker load -i a8v5.tar
300ac7ddffe5: Loading layer   5.22MB/5.22MB
586ef1308810: Loading layer  2.176GB/2.176GB
d3e9354e7ddb: Loading layer  435.5MB/435.5MB
11e7e90ef227: Loading layer  3.072kB/3.072kB
3616af8ff9ce: Loading layer  4.608kB/4.608kB
07b75d062ffe: Loading layer  11.78kB/11.78kB
Loaded image: a8v5:v8.0sp2
[root@shigj home]# docker images|grep a8v5
a8v5          v8.0sp2    a21675dcd486   43 hours ago   2.8GB

三、docker容器下web日常应用以及维护

3.1运行

docker run -d -e LANGUAGE=zh_CN.UTF-8 -e LC_ALL=zh_CN.UTF-8 -e LANG=zh_CN.UTF-8 -p 80:80 -v /home/V80sp2Seeyon/A8/base:/home/A8/base -v /home/V80sp2Seeyon/A8/ApacheJetspeed/logs:/home/A8/ApacheJetspeed/logs -v /home/V80sp2Seeyon/A8/ApacheJetspeed/logs_sy:/home/A8/ApacheJetspeed/logs_sy  a8v5:v8.0sp2 /home/A8/ApacheJetspeed/bin/startup.sh

3.2其他有关docker的命令行

查看所有的镜像
docker ps -a
查看正在运行的镜像
docker ps
查看web启动信息
docker attach containerID
停止
docker stop containerID
删除
docker rm containerID
查看镜像
docker images|grep a8v5
进入到容器当中
docker exec -it containerID /bin/bash
文件的拷贝
从计算节点复制到容器中
docker cp FILE_PATH 容器ID:/root
从容器复制到计算节点
docker cp 容器ID:/root/data.tar /home/user
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值