【问题标题】:creating docker container to host website创建 docker 容器来托管网站
【发布时间】:2019-04-05 05:09:50
【问题描述】:

我想在 docker 容器中运行静态网站。 为此,我创建了 ubuntu EC2 机器,安装了 docker 并提取了 centos 映像。

docker pull centos
docker run -td 9f38484d220f bash
docker exec -it aa779e39eb0f bash
===>now inside the container i am using below command
    yum update
    yum install apache
    service httpd start

但我收到命令无法识别的错误。

请帮我弄清楚我做错了什么。

另外,我想运行静态网站,一旦成功安装 apache,我将在下面放置代码

     $touch /var/www/html/index.html
     $chkconfig httpd on                 
     $echo "<b>Hii this is my first conatiner running/b>" 
      >> /var/www/html/index.html

这是正确的做法吗?

【问题讨论】:

  • 这个id是什么aa779e39eb0f,是容器id还是镜像id?你为什么不试试yum install httpd mod_ssl 而不是yum install apache
  • 当您 docker rm 容器时,您将失去一切。如果你只有一堆静态文件,你不需要“运行”任何东西,容器不是很好的匹配;考虑将它们托管在 S3 之类的地方。

标签: apache amazon-web-services docker amazon-ec2


【解决方案1】:

您安装了 apache 并尝试运行 httpd。请参考this 阅读 apache2 和 httpd 之间的区别。您可以运行以下命令来安装 apache 并在本地主机上运行静态的 hello world 页面。

$ sudo yum update -y
$ sudo yum install -y httpd
$ sudo service httpd start
$ echo "<html><h1>Hello World!</h1></html>" > test
$ cat test > /var/www/html/index.html

【讨论】:

  • 嗨......我这样做是为了学习。我知道 S3 选项。 @bot 谢谢...您的解决方案有效。但是对于服务 httpd start "bash: service: command not found"。另外,如果我想从浏览器访问该网站,该怎么做?
  • 您是否收到 httpdservice 错误?我会尝试两件事(A)以 sudo 用户身份运行此命令或(B)运行 sudo systemctl start httpd。运行我提到的命令后,您应该能够在浏览器中看到它。如果您在安全组中添加入站规则以接受来自 0.0.0.0 的所有流量,您可以点击实例的公共 DNS 并返回“hello world”页面。
  • 参考此link 以检查以修复bash: service: command not found 错误。
【解决方案2】:

您不需要容器来托管静态网站。 S3 是一个更好的选择。

如果你想把它作为一个练习,考虑这个简单的 nginx 解决方案,请参阅:https://hub.docker.com/_/nginx

您在该部分中有一个示例:托管一些简单的静态内容

FROM nginx:alpine
COPY . /usr/share/nginx/html

请记住,您通常不会先启动容器然后再启动其中的服务(用于测试和调试)。入口点和命令是启动您的服务,也就是您手动执行的操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-24
    • 2021-07-22
    • 1970-01-01
    • 2020-10-29
    • 2013-05-13
    • 1970-01-01
    • 2018-06-12
    • 1970-01-01
    相关资源
    最近更新 更多