Docker安装Apache与运行简单的web服务——httpd helloworld




一、Docker简介

Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的 Linux或Windows 机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口。

在这里插入图片描述
观察Docker图标,其实很形象的解释了什么是Docker。在没有使用集装箱的情况下,我们需要考虑不同形状、尺寸的货物怎么安放,货物与货物之间是否能堆叠,这无疑是很繁琐的事情。现在有了集装箱(容器),我们就能将不同的货物放入集装箱之内这样就能以同样的方式来存放货物,实现了标准化。

Docker采用了集装箱原理,我们将程序放入到容器中,实现“一次封装,到处运行”,只要开发环境能跑,在其他任何流行的机器上都能运行。并且将程序丢入到容器中,很好的实现了程序与程序之间的隔离,避免了类似Java开发的程序和.net开发的程序安装在一个服务器上需要很多调试,有时还会产生冲突的情况。

Docker的应用场景:

Web 应用的自动化打包和发布。
自动化测试和持续集成、发布。
在服务型环境中部署和调整数据库或其他的后台应用。
从头编译或者扩展现有的 OpenShift 或 Cloud Foundry 平台来搭建自己的 PaaS 环境。



二、Docker的安装与配置【CentOS环境】

基本安装过程,请参考我之前的一篇博客:博客

由于docker pull会出现timeout error的情况,一般需要更换docker镜像源为国内的镜像源,请参考我之前的一篇博客:博客


三、Docker运行简单的web服务——httpd helloworld

在之前我们曾经使用Docker运行过一个最简单的helloworld映像:


[root@localhost henryhzy]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
Digest: sha256:1a523af650137b8accdaed439c17d684df61ee4d74feac151b5b337bd29e7eec
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

而最近在学习web和docker的相关操作,因此想顺便尝试一下简单的web服务,这里我使用docker Apache与httpd。

首先安装Apache与httpd:

[root@localhost henryhzy]# docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
6ec7b7d162b2: Already exists 
17e233bac21e: Pulling fs layer 
130aad5bf43a: Pulling fs layer 
81d0a34533d4: Pulling fs layer 
da240d12a8a4: Waiting 
latest: Pulling from library/httpd
6ec7b7d162b2: Already exists 
17e233bac21e: Pull complete 
130aad5bf43a: Pull complete 
81d0a34533d4: Pull complete 
da240d12a8a4: Pull complete 
Digest: sha256:a3a2886ec250194804974932eaf4a4ba2b77c4e7d551ddb63b01068bf70f4120
Status: Downloaded newer image for httpd:latest
docker.io/library/httpd:latest

查看镜像版本:

[root@localhost henryhzy]# docker images httpd
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
httpd        latest    dd85cdbb9987   10 days ago   138MB
[root@localhost henryhzy]# 

运行镜像如下:

docker run -itd \
--name test_web_server \
-p 8085:80 \
-v /home/user/website/:/usr/local/apache2/htdocs/ \
httpd:latest

-p 8085:80,意思是端口号的映射,<host_port>:<容器内apache的port>

-v /home/user/website/:/usr/local/apache2/htdocs/, 这是docker重要的volume的概念。<host_路径>:<容器内的路径>。

在这里插入图片描述

查看docker容器确认httpd已启动:

docker ps

在这里插入图片描述

此时访问 http://<YOUR_IP>:8085即可到达一个web page
在这里插入图片描述

在apache的folder里面放一个编写好的网页,之后就可以通过这个端口8085来访问,由此便完成了安装Apache与运行简单的web服务的流程。

下面给出一个具体的helloworld网页的例子,在你的host,到/home/user/website/文件夹下,建立一个helloworld.html网页,这个文件会自动映射到容器内的/usr/local/apache2/htdocs/下,被apache发布出来。

cd /home/user/website
vi helloworld.html

helloworld.html内容如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>hello world web page</title>
</head>
<body>
    <h1>Hello from HenryHZY 18342026!</h1>
</body>
</html>

然后保存,访问 http://<YOUR_IP>:8085/helloworld.html
在这里插入图片描述

大体步骤如上,just enjoy it~



四、References

  1. Docker 安装 Apache
  2. Docker Apache建一個hello world的Web
  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值