IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 系统运维 -> Dockerfile镜像实例 -> 正文阅读

[系统运维]Dockerfile镜像实例

目录

一、构建SSH镜像

1. 建立工作目录

2. 生成镜像

3. 启动容器并修改root密码

二、systemctl镜像

1. 建立工作目录

2. 生成镜像

3. 运行镜像容器

4. 测试容器systemctl

三、Nginx镜像

1. 建立工作目录

2. 编写Dockerfile脚本

3. 编写run.sh启动脚本

4. 生成镜像

5. 运行镜像容器

6. 验证

四、Tomcat镜像

1. 建立工作目录

2. 编写Dockerfile脚本

3. 生成镜像

4. 运行镜像容器

5. 测试


一、构建SSH镜像

1. 建立工作目录

[root@docker ~]# mkdir /opt/sshd
[root@docker ~]# cd /opt/sshd
[root@docker sshd]# vim Dockerfile

FROM centos:7
#第一行必须指明基于的基础镜像
MAINTAINER this is ssh image <test>
RUN yum -y update;\
yum install -y openssh* net-tools lsof telnet passwd;\
echo '123456' | passwd --stdin root;\
sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config;\
#不使用PAM认证
sed -ri '/^session\s+required\s+pam_loginuid.so/ s/^/#/' /etc/pam.d/sshd;\
#取消pam限制
ssh-keygen -t rsa -A;\
#生成密钥认证文件
mkdir -p /root/.ssh;\
chown root.root /root;\
chmod 700 /root/.ssh
EXPOSE 22
CMD ["/usr/sbin/sshd","-D"]
#/usr/sbin/sshd -D用于前台启动sshd服务

2. 生成镜像

[root@docker sshd]# docker build -t sshd:test .
[root@docker sshd]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED              SIZE
sshd         test      beca19c54c1c   About a minute ago   438MB
httpd        test2     6440a6fa57e0   44 minutes ago       623MB
httpd        test1     5ebb94751288   47 minutes ago       623MB
debian       test      f7fd702b88cc   4 hours ago          215MB
centos       test1     3e6e91b628ef   5 hours ago          204MB
nginx        latest    87a94228f133   33 hours ago         133MB
centos       7         eeb6ee3f44bd   3 weeks ago          204MB

3. 启动容器并修改root密码

启动容器

[root@docker sshd]# docker run -itd --name sshd -P sshd:test
e563038d8ba40b85f8888046a0c0f2316056aaecdf747734c269613f73861897
[root@docker sshd]# docker ps -a
CONTAINER ID   IMAGE       COMMAND               CREATED         STATUS         PORTS                                     NAMES
e563038d8ba4   sshd:test   "/usr/sbin/sshd -D"   8 seconds ago   Up 7 seconds   0.0.0.0:49155->22/tcp, :::49155->22/tcp   sshd

ssh登录后修改root密码

[root@docker sshd]# ssh localhost -p 49154
The authenticity of host '[localhost]:49154 ([::1]:49154)' can't be established.
ECDSA key fingerprint is SHA256:rNKUxooLUZXzxxNUTnlROAlwc17hfKwLDTfXn6r8qb8.
ECDSA key fingerprint is MD5:81:e4:46:dd:c1:04:7c:aa:00:a9:16:a4:a2:53:bc:c3.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[localhost]:49154' (ECDSA) to the list of known hosts.
root@localhost's password: 
[root@ec51447ac2a3 ~]# echo '654321' | passwd --stdin root
Changing password for user root.
passwd: all authentication tokens updated successfully.

二、systemctl镜像

1. 建立工作目录

[root@docker ~]# mkdir /opt/systemctl
[root@docker ~]# cd /opt/systemctl
[root@docker systemctl]# vim Dockerfile

FROM sshd:test
MAINTAINER this is systemctl iamge <test>
ENV container docker
#除了systemd-tmpfiles-setup.service,删除其他所有文件
RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == systemd-tmpfiles-setup.service ] || rm -f $i; done);\
rm -f /lib/systemd/system/multi-user.target.wants/*;\
rm -f /lib/systemd/system/*.wants/*;\
rm -f /lib/systemd/system/local-fs.target.wants/*;\
rm -f /lib/systemd/system/sockets.target.wants/*udev*;\
rm -f /lib/systemd/system/sockets.target.wants/*initctl*;\
rm -f /lib/systemd/system/basic.target.wants/*;\
rm -f /lib/systemd/system/anaconda.target.wants/*;
VOLUME [ "/sys/fs/cgroup" ]
CMD [ "/usr/sbin/init" ]

2. 生成镜像

[root@docker systemctl]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
systemctl    test      ef46e114991b   10 seconds ago   438MB
sshd         test      beca19c54c1c   19 hours ago     438MB
httpd        test2     6440a6fa57e0   19 hours ago     623MB
httpd        test1     5ebb94751288   19 hours ago     623MB
debian       test      f7fd702b88cc   23 hours ago     215MB
centos       test1     3e6e91b628ef   24 hours ago     204MB
nginx        latest    87a94228f133   2 days ago       133MB
centos       7         eeb6ee3f44bd   4 weeks ago      204MB

3. 运行镜像容器

[root@docker systemctl]# docker run -d --privileged --name systemctl -P -v /sys/fs/cgroup:/sys/fs/cgroup:ro  systemctl:test
#启动容器,并挂载宿主机目录挂载到容器中并进行初始化
#--privileged:使container内的root拥有真正的root权限。否则,container内的root只是外部的一个普通用户权限。
2fab83d597621709eeee0c5e544334b5f85767bdacb1421842e0f4842b1176ee
[root@docker systemctl]# docker ps -a
CONTAINER ID   IMAGE            COMMAND               CREATED          STATUS          PORTS                                     NAMES
2fab83d59762   systemctl:test   "/usr/sbin/init"      16 seconds ago   Up 14 seconds   0.0.0.0:49157->22/tcp, :::49157->22/tcp   systemctl
e563038d8ba4   sshd:test        "/usr/sbin/sshd -D"   18 minutes ago   Up 18 minutes   0.0.0.0:49155->22/tcp, :::49155->22/tcp   sshd

4. 测试容器systemctl

[root@docker systemctl]# docker exec -it systemctl bash
[root@2fab83d59762 /]# systemctl status sshd
● sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2021-10-14 05:56:07 UTC; 2min 20s ago
     Docs: man:sshd(8)
           man:sshd_config(5)
 Main PID: 17 (sshd)
   CGroup: /docker/2fab83d597621709eeee0c5e544334b5f85767bdacb1421842e0f4842b1176ee/system.slice/sshd.service
           └─17 /usr/sbin/sshd -D
[root@2fab83d59762 /]# systemctl stop sshd
[root@2fab83d59762 /]# systemctl start sshd
[root@2fab83d59762 /]# systemctl status sshd
● sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2021-10-14 05:58:43 UTC; 2s ago
     Docs: man:sshd(8)
           man:sshd_config(5)
 Main PID: 39 (sshd)
   CGroup: /docker/2fab83d597621709eeee0c5e544334b5f85767bdacb1421842e0f4842b1176ee/system.slice/sshd.service
           └─39 /usr/sbin/sshd -D

三、Nginx镜像

1. 建立工作目录

[root@docker ~]# mkdir /opt/nginx
[root@docker ~]# cd /opt/nginx/
[root@docker nginx]# rz -E
#传入nginx安装包nginx-1.12.0.tar.gz
rz waiting to receive.

2. 编写Dockerfile脚本

[root@docker nginx]# vim Dockerfile

FROM centos:7
#基于基础镜像
MAINTAINER this is nginx image <test>
#用户信息
RUN yum -y update;yum install -y pcre-devel zlib-devel gcc gcc-c++ make;useradd -M -s /sbin/nologin nginx
ADD nginx-1.12.0.tar.gz /opt/
#添加环境包,上传nginx软件压缩包并解压
WORKDIR /opt/nginx-1.12.0
#指定工作目录
RUN ./configure\
--prefix=/usr/local/nginx\
--user=nginx\
--group=nginx\
--with-http_stub_status_module;make;make install
ENV PATH /usr/local/nginx/sbin:$PATH
EXPOSE 80
EXPOSE 443
#指定http和https端口
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf
ADD run.sh /run.sh
#添加宿主机中run.sh
RUN chmod 755 /run.sh
CMD [ "/run.sh" ]

3. 编写run.sh启动脚本

[root@docker nginx]# vim run.sh

#!/bin/bash
/usr/local/nginx/sbin/nginx

4. 生成镜像

[root@docker nginx]# docker build -t nginx:test .
[root@docker nginx]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
nginx        test      a8b4c04322fc   10 seconds ago   545MB
systemctl    test      ef46e114991b   2 hours ago      438MB
sshd         test      beca19c54c1c   20 hours ago     438MB
httpd        test2     6440a6fa57e0   21 hours ago     623MB
httpd        test1     5ebb94751288   21 hours ago     623MB
debian       test      f7fd702b88cc   25 hours ago     215MB
centos       test1     3e6e91b628ef   26 hours ago     204MB
nginx        latest    87a94228f133   2 days ago       133MB
centos       7         eeb6ee3f44bd   4 weeks ago      204MB

5. 运行镜像容器

[root@docker nginx]# docker run -d --name nginx -P nginx:test
66ac671b3766ce71422c0998807c609a07eead5d4c221ee97e4a75f46afe13bf
[root@docker nginx]# docker ps -a
CONTAINER ID   IMAGE            COMMAND               CREATED         STATUS         PORTS                                                                                NAMES
66ac671b3766   nginx:test       "/run.sh"             7 seconds ago   Up 5 seconds   0.0.0.0:49163->80/tcp, :::49163->80/tcp, 0.0.0.0:49162->443/tcp, :::49162->443/tcp   nginx
2fab83d59762   systemctl:test   "/usr/sbin/init"      2 hours ago     Up 2 hours     0.0.0.0:49157->22/tcp, :::49157->22/tcp                                              systemctl
e563038d8ba4   sshd:test        "/usr/sbin/sshd -D"   2 hours ago     Up 2 hours     0.0.0.0:49155->22/tcp, :::49155->22/tcp                                              sshd

6. 验证

[root@docker nginx]# curl http://192.168.122.10:49163
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    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>

四、Tomcat镜像

1. 建立工作目录

[root@docker ~]# mkdir /opt/tomcat
[root@docker ~]# cd /opt/tomcat
[root@docker tomcat]# rz -E
rz waiting to receive.
#传入jdk安装包jdk-8u91-linux-x64.tar.gz
[root@docker tomcat]# rz -E
rz waiting to receive.
#传入tomcat安装包apache-tomcat-8.5.16.tar.gz

2. 编写Dockerfile脚本

[root@docker tomcat]# vim Dockerfile

FROM centos:7
MAINTAINER this is tomcat image <test>
ADD jdk-8u91-linux-x64.tar.gz /usr/local/
WORKDIR /usr/local/
RUN mv jdk1.8.0_91 /usr/local/java
ENV JAVA_HOME /usr/local/java
ENV JRE_HOME ${JAVA_HOME}/jre
ENV CLASSPATH .:${JAVA_HOME}/lib:${JRE_HOME}/lib
ENV PATH $JAVA_HOME/bin:$PATH
ADD apache-tomcat-8.5.16.tar.gz /usr/local/
WORKDIR /usr/local/
RUN mv apache-tomcat-8.5.16 /usr/local/tomcat
EXPOSE 8080
ENTRYPOINT [ "/usr/local/tomcat/bin/catalina.sh","run" ]

3. 生成镜像

[root@docker tomcat]# docker build -t tomcat:test .
[root@docker tomcat]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
tomcat       test      8a7be313dd18   33 seconds ago   960MB
nginx        test      a8b4c04322fc   25 minutes ago   545MB
systemctl    test      ef46e114991b   2 hours ago      438MB
sshd         test      beca19c54c1c   21 hours ago     438MB
httpd        test2     6440a6fa57e0   21 hours ago     623MB
httpd        test1     5ebb94751288   21 hours ago     623MB
debian       test      f7fd702b88cc   25 hours ago     215MB
centos       test1     3e6e91b628ef   26 hours ago     204MB
nginx        latest    87a94228f133   2 days ago       133MB
centos       7         eeb6ee3f44bd   4 weeks ago      204MB

4. 运行镜像容器

[root@docker tomcat]# docker run -d --name tomcat -P tomcat:test
35fef1ece2a3f1872ce0692da28f8e662e329d0e5a76a003a433fd5f72a5f1bb
[root@docker tomcat]# docker ps -a
CONTAINER ID   IMAGE            COMMAND                  CREATED          STATUS          PORTS                                                                                NAMES
35fef1ece2a3   tomcat:test      "/usr/local/tomcat/b…"   6 seconds ago    Up 5 seconds    0.0.0.0:49165->8080/tcp, :::49165->8080/tcp                                          tomcat
66ac671b3766   nginx:test       "/run.sh"                25 minutes ago   Up 25 minutes   0.0.0.0:49163->80/tcp, :::49163->80/tcp, 0.0.0.0:49162->443/tcp, :::49162->443/tcp   nginx
2fab83d59762   systemctl:test   "/usr/sbin/init"         2 hours ago      Up 2 hours      0.0.0.0:49157->22/tcp, :::49157->22/tcp                                              systemctl
e563038d8ba4   sshd:test        "/usr/sbin/sshd -D"      2 hours ago      Up 2 hours      0.0.0.0:49155->22/tcp, :::49155->22/tcp                                              sshd

5. 测试

?

  系统运维 最新文章
配置小型公司网络WLAN基本业务(AC通过三层
如何在交付运维过程中建立风险底线意识,提
快速传输大文件,怎么通过网络传大文件给对
从游戏服务端角度分析移动同步(状态同步)
MySQL使用MyCat实现分库分表
如何用DWDM射频光纤技术实现200公里外的站点
国内顺畅下载k8s.gcr.io的镜像
自动化测试appium
ctfshow ssrf
Linux操作系统学习之实用指令(Centos7/8均
上一篇文章      下一篇文章      查看所有文章
加:2021-12-06 15:37:23  更:2021-12-06 15:39:25 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/16 4:15:04-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码