Dockerfile的编写:
# vim Dockerfile
FROM rhel7:yum 指定基础镜像
MAINTAINER yakexi007@westos.org 作者信息
ENV HOSTNAME server1 设置容器主机名
EXPOSE 80 暴露容器端口
RUN yum install -y httpd && yum clean all 镜像操作命令
CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"] 镜像启动命令,默认只能启动一条。
# docker build -t rhel7:web . 创建镜像 rhel7:web
Dockerfile 常用指令:
1. 指定容器运行的用户
该用户将作为后续的 RUN 命令执行的用户。这个命令本实验不需要,但在一些需要指定用
户来运行的应用部署时非常关键,比如提供 hadoop 服务的容器通常会使用 hadoop 用户来启
动服务。
命令使用方式,例如使用 shiyanlou 用户来执行后续命令:
USER shiyanlou
2. 指定后续命令的执行目录
由于我们需要运行的是一个静态网站,将启动后的工作目录切换到/var/www/html 目录:WORKDIR /var/www/html
3. 对外连接端口号
由于内部服务会启动 Web 服务,我们需要把对应的 80 端口暴露出来,可以提供给容器间互
联使用,可以使用 EXPOSE 命令。
在镜像操作部分增加下面一句:
EXPOSE 80
4. 设置容器主机名
ENV 命令能够对容器内的环境变量进行设置:
ENV HOSTNAME sevrer1.example.com
5. 向镜像中增加文件
6. CMD 与 ENTRYPOINT。
7. 挂载数据卷
8. 设置容器内的环境变量
9. 使用 Supervisord
编写DockFile实现安装httpd服务:
[root@foundation38 docker]# ls
Dockerfile web
[root@foundation38 docker]# touch yum.repo
[root@foundation38 docker]# vim yum.repo
[root@foundation38 docker]# cat yum.repo 编写yum源
[source7.3]
name=source7.3
baseurl=http://172.25.38.250/source7.3
gpgcheck=0
[root@foundation38 docker]# ls
Dockerfile web yum.repo
[root@foundation38 docker]# cat Dockerfile 编写Dockfile
FROM rhel7docker run -it --name vm1 rhel7 bash
ENV HOSTNAME server1
MAINTAINER 1757159864@qq.com
EXPOSE 80
COPY yum.repo /etc/yum.repos.d/yum.repo
RUN yum install -y httpd && yum clean all
VOLUME ["/var/www/html"]
CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]
[root@foundation38 docker]# docker build -t rhel7:v1 .
创建容器进行环境的实践来保证Dockerfile可以正确执行:
[root@foundation38 netns]# docker run -it --name vm1 rhel7 bash 创建容器交互式运行rhel7的shell
bash-4.2# ls
bin dev home lib64 mnt proc run srv tmp var
boot etc lib media opt root sbin sys usr
bash-4.2# cd /etc/yum.repos.d/
bash-4.2# ls
rhel7.repo
bash-4.2# vi yum.repo
bash-4.2# yum repolist
Skipping unreadable repository '///etc/yum.repos.d/rhel7.repo'
source7.3 | 4.1 kB 00:00
(1/2): source7.3/group_gz | 136 kB 00:00
(2/2): source7.3/primary_db | 3.9 MB 00:00
repo id repo name status
source7.3 source7.3 4751
repolist: 4751
bash-4.2# cat yum.repo 配置yum源
[source7.3]
name=source7.3
baseurl=http://172.25.38.250/source7.3
gpgcheck=0
[root@foundation38 docker]# ls
Dockerfile web yum.repo
[root@foundation38 docker]# vim Dockerfile
[root@foundation38 docker]# cat Dockerfile
FROM rhel7
ENV HOSTNAME server1
MAINTAINER 1757159864@qq.com
EXPOSE 80
COPY yum.repo /etc/yum.repos.d/yum.repo
RUN rpmdb --rebuilddb && yum install -y httpd && yum clean all
VOLUME ["/var/www/html"]
CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]
[root@foundation38 docker]# docker build -t rhel7:v1 . 运行Dockerfile
Sending build context to Docker daemon 7.168 kB
Step 1/8 : FROM rhel7
---> 0a3eb3fde7fd
Step 2/8 : ENV HOSTNAME server1
---> Using cache
---> bbda1be07d9e
Step 3/8 : MAINTAINER 1757159864@qq.com
---> Using cache
---> 9de9da7f6841
Step 4/8 : EXPOSE 80
---> Using cache
---> a4504786b603
Step 5/8 : COPY yum.repo /etc/yum.repos.d/yum.repo
---> Using cache
---> e8c13b6b6ffc
Step 6/8 : RUN rpmdb --rebuilddb && yum install -y httpd && yum clean all
---> Running in 830a02486077
Skipping unreadable repository '///etc/yum.repos.d/rhel7.repo'
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-45.el7 will be installed
--> Processing Dependency: httpd-tools = 2.4.6-45.el7 for package: httpd-2.4.6-45.el7.x86_64
--> Processing Dependency: system-logos >= 7.92.1-1 for package: httpd-2.4.6-45.el7.x86_64
--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-45.el7.x86_64
--> Processing Dependency: libsystemd-daemon.so.0(LIBSYSTEMD_DAEMON_31)(64bit) for package: httpd-2.4.6-45.el7.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-45.el7.x86_64
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-45.el7.x86_64
--> Processing Dependency: libsystemd-daemon.so.0()(64bit) for package: httpd-2.4.6-45.el7.x86_64
--> Running transaction check
---> Package apr.x86_64 0:1.4.8-3.el7 will be installed
---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed
---> Package httpd-tools.x86_64 0:2.4.6-45.el7 will be installed
---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed
---> Package redhat-logos.noarch 0:70.0.3-6.el7 will be installed
---> Package systemd-libs.x86_64 0:219-30.el7 will be installed
--> Processing Dependency: libdw.so.1()(64bit) for package: systemd-libs-219-30.el7.x86_64
--> Running transaction check
---> Package elfutils-libs.x86_64 0:0.166-2.el7 will be installed
--> Processing Dependency: elfutils-libelf(x86-64) = 0.166-2.el7 for package: elfutils-libs-0.166-2.el7.x86_64
--> Processing Dependency: libelf.so.1(ELFUTILS_1.7)(64bit) for package: elfutils-libs-0.166-2.el7.x86_64
--> Running transaction check
---> Package elfutils-libelf.x86_64 0:0.158-3.el7 will be updated
---> Package elfutils-libelf.x86_64 0:0.166-2.el7 will be an update
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
httpd x86_64 2.4.6-45.el7 source7.3 1.2 M
Installing for dependencies:
apr x86_64 1.4.8-3.el7 source7.3 103 k
apr-util x86_64 1.5.2-6.el7 source7.3 92 k
elfutils-libs x86_64 0.166-2.el7 source7.3 262 k
httpd-tools x86_64 2.4.6-45.el7 source7.3 84 k
mailcap noarch 2.1.41-2.el7 source7.3 31 k
redhat-logos noarch 70.0.3-6.el7 source7.3 13 M
systemd-libs x86_64 219-30.el7 source7.3 367 k
Updating for dependencies:
elfutils-libelf x86_64 0.166-2.el7 source7.3 208 k
Transaction Summary
================================================================================
Install 1 Package (+7 Dependent packages)
Upgrade ( 1 Dependent package)
Total download size: 15 M
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
--------------------------------------------------------------------------------
Total 63 MB/s | 15 MB 00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : apr-1.4.8-3.el7.x86_64 1/10
Installing : apr-util-1.5.2-6.el7.x86_64 2/10
Installing : httpd-tools-2.4.6-45.el7.x86_64 3/10
Updating : elfutils-libelf-0.166-2.el7.x86_64 4/10
Installing : elfutils-libs-0.166-2.el7.x86_64 5/10
Installing : systemd-libs-219-30.el7.x86_64 6/10
Installing : redhat-logos-70.0.3-6.el7.noarch 7/10
Installing : mailcap-2.1.41-2.el7.noarch 8/10
Installing : httpd-2.4.6-45.el7.x86_64 9/10
Cleanup : elfutils-libelf-0.158-3.el7.x86_64 10/10
Verifying : systemd-libs-219-30.el7.x86_64 1/10
Verifying : elfutils-libs-0.166-2.el7.x86_64 2/10
Verifying : apr-1.4.8-3.el7.x86_64 3/10
Verifying : mailcap-2.1.41-2.el7.noarch 4/10
Verifying : redhat-logos-70.0.3-6.el7.noarch 5/10
Verifying : apr-util-1.5.2-6.el7.x86_64 6/10
Verifying : httpd-tools-2.4.6-45.el7.x86_64 7/10
Verifying : httpd-2.4.6-45.el7.x86_64 8/10
Verifying : elfutils-libelf-0.166-2.el7.x86_64 9/10
Verifying : elfutils-libelf-0.158-3.el7.x86_64 10/10
Installed:
httpd.x86_64 0:2.4.6-45.el7
Dependency Installed:
apr.x8