Dockerfile镜像实例

一、构建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. 测试

Dockerfile镜像实例的更多相关文章

  1. Dockerfile镜像的制作

    Dockerfile镜像的制作 如果学习Docker,那么制作镜像这一步肯定不能少的,别人给你的是环境,而你自己做的才是你最终需要的东西,接下来就记录一下如何制作一个满足自己的镜像,我们使用docke ...

  2. dockerfile镜像设置中文

    一.dockerfile镜像设置中文 centos镜像默认不支持中文,把下面的内容加到dockerfile即可 # 修改时区 RUN rm -rf /etc/localtime && ...

  3. 7.云原生之Docker容器Dockerfile镜像构建浅析与实践

    转载自:https://www.bilibili.com/read/cv15220707/?from=readlist Dockerfile 镜像构建浅析与实践 描述:Dockerfile是一个文本格 ...

  4. dockerfile 镜像构建

    1.镜像的构建有手动与自动方式,这里我们介绍自动化的构建方式 ,dockerfile常用指令如下 2.构建指令build Usage: docker image build [OPTIONS] PAT ...

  5. 基于Dockerfile镜像制作的基本操作

    一.使用Dockerfile制作镜像 前面的博客中已经介绍了如何基于容器制作镜像,此方法的原理是使用一个正在运行的容器,根据生产所需进行配置更改等操作后,使其满足生产环境,再将这个容器打包制作为镜像, ...

  6. Docker:dockerfile镜像的分层 [九]

    一.docker镜像的分层 1.图像呈现 2.命令呈现 [root@oldboy kod]# docker image history kod:v1 IMAGE CREATED CREATED BY ...

  7. Docker dockerfile镜像编码

    一. 大多数docker基础镜像使用locale查看编码,发现默认编码都是POSIX,这会导致中文乱码.解决方法如下: 二.首先使用locale -a查看容器所有语言环境 三.dockerfile中加 ...

  8. Dockerfile镜像优化,减小镜像

    前言镜像的优化注意几条: 选择最精简的基础镜像减少镜像的层数清理镜像构建的中间产物注意优化网络请求尽量去用构建缓存使用多阶段构建镜像接下来我们以rhel7镜像构建容器,并在容器中安装nginx的源码包 ...

  9. dockerfile 镜像 指定虚拟机的内存

    dockerfile eg: # sea, FROM frolvlad/alpine-oraclejdk8:slim #add volume VOLUME /tmp #add project ADD ...

随机推荐

  1. SpringBoot中JPA使用动态SQL查询

    https://www.jianshu.com/p/f72d82e90948 可以重点关注方法二,把原生sql传入数据库去查询 好处是:更加灵活坏处是:拼接sql比较痛苦,也容易出问题,比如拼接的sq ...

  2. C# .net 使用rabbitmq消息队列——EasyNetQ插件介绍

    EasyNetQ 是一个简洁而适用的RabbitMQ .NET类库,本质上是一个在RabbitMQ.Client之上提供服务的组件集合.

  3. 表达式树扩展 动态生成表达式树插件 Sy.ExpressionBuilder。

    CURD中,基础查询我感觉还是很烦人的一个浪费时间的工作,我经历过远古时代的GetAll(string name,int age),这种方式写服务的时候真的是心中一万个草泥马飞过,后面逐渐的变成了传一 ...

  4. CSS 表格基本使用 案例

    知识点普及: 表格是html中经常使用到的,简单的使用可能很多人都没问题,但是更深入的了解的人恐怕不多,下面我们先来看一下如何使用. <table>是<tr>的上层标签 < ...

  5. 设置Linux的一些文本输出方式

    更新一下yum咯 yum install -y epel-release 火车 sudo yum install sl $ sl 放火 sudo yum install libaa-bin 小老鼠 s ...

  6. HTTP 408 问题 - Koa body parser

    环境描述: 1.nodejs 作为 api 服务器,转发请求给 nginx 2.用 curl 测试,返回 408 找了很久没有找到原因,发现了一篇文章: https://lujunda.cn/2016 ...

  7. YBT 1633:【例 3】Sumdiv

    http://ybt.ssoier.cn:8088/problem_show.php?pid=1633 A^B 快速幂求结果,所有约数和,可以通过组合来进行得到. 技巧,通过递归得到1~n次的和.su ...

  8. SpringMVC 解析(一)概览

    Spring MVC是Spring提供的构建Web应用程序的框架,该框架遵循了Servlet规范,负责接收并处理Servelt容器传递的请求,并将响应写回Response.Spring MVC以Dis ...

  9. PHP靶场-bWAPP环境搭建

    0x00 靶场介绍 bwapp是一款非常好用的漏洞演示平台,包含有100多个漏洞.开源的php应用后台Mysql数据库. 0x01 安装 BWAPP有两种安装方式,一种是单独安装,需部署在Apache ...

  10. 一次神奇的Azure speech to text rest api之旅

    错误Max retries exceeded with url: requests.exceptions.ConnectionError: HTTPSConnectionPool(host='%20e ...