在已经掌握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
启动容器的参数介绍