使用Docker创建Web服务详解

栏目: 编程工具 · 发布时间: 5年前

内容简介:在已经掌握docker安装、docker仓库的基本使用、docker镜像和容器的基本操作和相互转化的基础上,可尝试通过docker搭建一个web服务器,便于分布式部署或快速移植web服务器。通过本文的学习,可以了解学习docker容器与宿主机的文件和端口映射,进一步熟练使用docker容器。安装nginx

1、目的

在已经掌握 docker 安装、docker仓库的基本使用、docker镜像和容器的基本操作和相互转化的基础上,可尝试通过docker搭建一个web服务器,便于分布式部署或快速移植web服务器。

通过本文的学习,可以了解学习docker容器与宿主机的文件和端口映射,进一步熟练使用docker容器。

2、修改容器,搭建简单的web服务

安装nginx

# apt-get install nginx

修改nginx配置文件

# vi /etc/nginx/conf.d/web.conf

# server的配置

server {

# 监听端口

listen 81;

# 项目的初始化页面

location / {

root  /home/visual/nginx_web/;

index index.html;

}

}

修改开机启动项

# vi /etc/rc.local

####!/bin/sh -e

#

# rc.local

#

# This script is executed at the end of each multiuser runlevel.

# Make sure that the script will "exit 0" on success or any other

# value on error.

#

# In order to enable or disable this script just change the execution

# bits.

#

# By default this script does nothing.

service ssh start

service nginx start

/bin/bash

exit 0

3、创建镜像,便于再次启动容器

通过commit操作创建docker镜像文件,上篇文章已经讲过,命令如下

linuxidc@linuxidc:~/docker$ docker ps -a

CONTAINER ID        IMAGE              COMMAND                  CREATED            STATUS                      PORTS              NAMES

568e5204fff3       Ubuntu"/bin/sh -c 'while..."  40 hours ago        Exited (137) 38 hours ago                      kind_khorana

00f561d97811        ubuntu              "/bin/echo hello w..."  40 hours ago        Exited (0) 40 hours ago                        nifty_mcnulty

93a1b9d39683        ubuntu              "bash"                  40 hours ago        Exited (0) 5 seconds ago                        zealous_noether

abdc084f9821        hello-world        "/hello"                41 hours ago        Exited (0) 18 hours ago                        sleepy_clarke

linuxidc@linuxidc:~/docker$ docker commit 93a1b9d39683 learn/nginx:v2

sha256:ab92edd21696770f1eb37e9229b6834094a8d3381e5b4e9edc620b7927004bb4

linuxidc@linuxidc:~/docker$ docker images

REPOSITORY                                      TAG                IMAGE ID            CREATED            SIZE

learn/nginx                                      v2                  ab92edd21696        5 seconds ago      370MB

learn/visual_init                                v1                  56a4eab7dc5b        37 hours ago        321MB

registry.cn-beijing.aliyuncs.com/zhangsp/ai      visual_init        56a4eab7dc5b        37 hours ago        321MB

ubuntu                                          latest              14f60031763d        5 days ago          120MB

hello-world                                      latest              1815c82652c0        5 weeks ago        1.84kB

4、启动新容器

使用新创建的镜像learn/nginx:v2,启动新容器

# docker run -it --name nginx_test -h docker-nginx -p 8001:81 -p 8000:80 -v /home/linuxidc/docker/nginx_web/:/home/visual/nginx_web/ learn/nginx:v2 /bin/sh /etc/rc.local

启动容器的参数介绍

-it,交互方式启动

–name nginx_test,指定新容器的名称是nginx_test

-h docker-nginx,指定新容器的主机名是docker-nginx

-p 8001:81 -p 8000:80,指定宿主机与docker容器的端口映射,宿主机的8001对应docker容器的81,宿主机的8000对应docker容器的80

-v /home/linuxidc/docker/nginx_web/:/home/visual/nginx_web/,指定宿主机与docker容器的文件映射,宿主机的/home/linuxidc/docker/nginx_web/ 对应docker容器的 /home/visual/nginx_web/

learn/nginx:v2,指定启动容器对应的镜像是learn/nginx:v2,可以是镜像ID

/bin/sh /etc/rc.local,指定容器启动后,执行 shell 脚本是/etc/rc.local

查看docker容器,容器nginx_test处于up状态,说明启动正常

linuxidc@linuxidc:~/docker/nginx_web$ docker ps -a

CONTAINER ID        IMAGE              COMMAND                  CREATED            STATUS                        PORTS                                        NAMES

cbbbe7a5d47a        learn/nginx:v2      "/bin/sh /etc/rc.l..."  25 minutes ago      Up 24 minutes                  0.0.0.0:8000->80/tcp, 0.0.0.0:8001->81/tcp  nginx_test

966bd52b72da        ubuntu              "/bin/sh -c 'while..."  42 hours ago        Exited (137) 40 hours ago                                                  stupefied_knuth

00f561d97811        ubuntu              "/bin/echo hello w..."  42 hours ago        Exited (0) 42 hours ago                                                    nifty_mcnulty

93a1b9d39683        ubuntu              "bash"                  43 hours ago        Exited (0) About an hour ago                                                zealous_noether

abdc084f9821        hello-world        "/hello"                43 hours ago        Exited (0) 20 hours ago                                                    sleepy_clarke

5、测试docker_nginx是否正常

通过浏览器测试8001端口

使用Docker创建Web服务详解

通过浏览器测试8000端口

使用Docker创建Web服务详解

修改宿主机的文件,并测试8001端口

修改宿主机的/home/linuxidc/docker/nginx_web/index.html文件

linuxidc@linuxidc:~/docker/nginx_web$ vi index.html

<!DOCTYPE html>

<html>

<head>

<title>Welcome to nginx! I am in docker!</title>

<style>

body {

width: 35em;

margin: 0 auto;

font-family: Tahoma, Verdana, Arial, sans-serif;

}

</style>

</head>

<body>

<h1>Welcome to nginx! I am in Docker!</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>

通过浏览器测试8001端口,发现“Welcome to nginx! I am in docker!”,说明内容已经修改,使用docker做为web服务器的功能已经OK

使用Docker创建Web服务详解

更多 Docker 相关教程见以下内容 : 

Linux 下的 Docker 安装与使用 https://www.linuxidc.com/Linux/2018-06/152996.htm

CentOS 7安装Docker应用容器引擎 https://www.linuxidc.com/Linux/2018-06/152856.htm

CentOS 7.3环境安装Docker 18.03 https://www.linuxidc.com/Linux/2018-05/152356.htm

使用Docker分分钟启动常用应用  https://www.linuxidc.com/Linux/2017-04/142649.htm

CentOS 7使用Docker搭建GitLab服务器  https://www.linuxidc.com/Linux/2018-04/151725.htm

30分钟带你了解Docker  https://www.linuxidc.com/Linux/2018-08/153346.htm

Docker容器常见操作详解 https://www.linuxidc.com/Linux/2018-08/153685.htm

Docker发布应用程序指南 https://www.linuxidc.com/Linux/2018-08/153405.htm

Docker 的详细介绍 请点这里

Docker 的下载地址 请点这里

Linux公社的RSS地址: https://www.linuxidc.com/rssFeed.aspx

本文永久更新链接地址: https://www.linuxidc.com/Linux/2018-08/153734.htm


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

黑客与画家

黑客与画家

[美] Paul Graham / 阮一峰 / 人民邮电出版社 / 2013-10 / 69.00元

本书是硅谷创业之父Paul Graham 的文集,主要介绍黑客即优秀程序员的爱好和动机,讨论黑客成长、黑客对世界的贡献以及编程语言和黑客工作方法等所有对计算机时代感兴趣的人的一些话题。书中的内容不但有助于了解计算机编程的本质、互联网行业的规则,还会帮助读者了解我们这个时代,迫使读者独立思考。 本书适合所有程序员和互联网创业者,也适合一切对计算机行业感兴趣的读者。一起来看看 《黑客与画家》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具