zoukankan      html  css  js  c++  java
  • docker操作的一些例子

    一、用docker run运行nginx

    执行docker run命令即可实现:

    $ docker run -p 8080:80 -d daocloud.io/nginx

    这里-p执行的是端口映射操作,将nginx运行的80端口映射到本机的8080端口。

    执行后,在localhost:8080即可看到nginx的界面。

    如果需要nginx运行特定的界面,则需要将自己编辑的html文件放入容器中。

    例如,编辑一个index.html文件:

    <html>
    <h1>Hello World.</h1>
    </html>

    用cp命令将这一文件放入容器:

    $ docker cp index.html [容器id或name]://usr/share/nginx/html

    再执行run命令,就可以在localhost:8080看到hello world了。

    但是,放入容器的文件在容器重启后会删除,因此需要将容器保存为新的镜像,以永远保存对容器所做的修改。用commit命令执行:

    docker commit -m 'mynginx' [容器id或name] mynginx

    将容器保存为一个新的镜像。这样,以后再启用时就可以直接看到hello world了。

    二、用dockerfile运行nginx

    对于较复杂的一系列操作,可以将这些操作编写为一个文件(即dockerfile),用docker build命令执行。

    文件编写如下:

    FROM ubuntu
    MAINTAINER Author
    RUN apt-get update
    RUN apt-get install -y nginx
    COPY index.html /var/www/html
    ENTRYPOINT ["/usr/sbin/nginx", "-g", "daemon off;"]
    EXPOSE 80

    from指定了基础镜像,maintainer指定了维护人,run是执行的指令,这里执行的是在容器中下载nginx。copy命令将index.html文件放入容器中。entrypoint命令指定了当容器运行时执行的指令,这里是指当容器运行时执行nginx。expose指定了运行的端口。

    编写完dockerfile后,用build命令创建镜像:

    $ docker build -t "test"

    这样,docker会按照dockerfile的顺序执行操作,并创建test镜像。

    docker run这个镜像,再通过curl查看对应的网址,就能看到index.html的内容。

    $ docker run -d -p 80:80 test
    $ curl http://localhost
  • 相关阅读:
    shell中使用echo命令改变输出显示样式
    Shell脚本报错unary operator expected
    shell运行报 too many arguments错误
    写shell,运行出错:syntax error near unexpected token `$’do ”
    shell 脚本执行,出现错误bad interpreter: No such file or directory
    Linux 基本命令学习笔记
    如何运行 O’Reilly 书 Python for Finance 的源代码
    IntelliJ 中配置 Anaconda
    Windows 10 中安装 Anaconda 3
    Windows 中安装的 Python 如何卸载
  • 原文地址:https://www.cnblogs.com/00986014w/p/9193027.html
Copyright © 2011-2022 走看看