您的位置:首页 > 运维架构 > Docker

docker学习笔记3—使用Dockerfile与docker build命令创建一个nginx服务器镜像,并使用浏览器进行访问

2015-08-26 00:34 1586 查看
使用Dockerfile文件创建一个镜像,运行该镜像后并使用浏览器进行访问

1、Dockerfile文件
#Version: 0.0.1
FROM ubuntu:14.04
MAINTAINER Ren Shangtao "renshangtao@sina.com"
RUN apt-get update
RUN apt-get install -y nginx
RUN echo 'Hello Everyone This is My First Dockerfile!!! \r\n ' > /usr/share/nginx/html/index.html


2、使用docker build -t="lingyu/nginx:v0.1" .
docker build 使用Dockerfile文件来构建镜像,-t选项为新镜像设置仓库和名称,在本例中仓库为lingyu,镜像名为nginx。后面的‘.’表示Dockerfile文件在当前目录中。
root@ubuntu:~/myWeb# docker build -t="lingyu/nginx:v0.1" .
Sending build context to Docker daemon  2.56 kB
Sending build context to Docker daemon
Step 0 : FROM ubuntu:14.04
---> 91e54dfb1179
Step 1 : MAINTAINER Ren Shangtao "renshangtao@sina.com"
---> Using cache
---> eec87d0b3d9c
Step 2 : RUN apt-get update
---> Using cache
---> c3c048059c8c
Step 3 : RUN apt-get install -y nginx
---> Using cache
---> 2e035c26a9e7
Step 4 : RUN echo 'Hello Everyone This is My First Dockerfile!!! \r\n ' > /usr/share/nginx/html/index.html
---> Running in b8274a4d7f83
---> 7e3682375a00
Removing intermediate container b8274a4d7f83
Successfully built 7e3682375a00
root@ubuntu:~/myWeb#


3、使用docker images来查看我们创建的镜像
root@ubuntu:~/myWeb# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
lingyu/nginx        v0.1                73d019478ecf        30 minutes ago      228 MB
ubuntu              14.04               91e54dfb1179        4 days ago          188.4 MB


4、使用docker run运行我们创建的镜像
运行 -p 34567:80表示将Docker中的80号端口,映射到宿主机的34567端口
root@ubuntu:~/myWeb# docker run -d -p 34567:80 --name myNginx lingyu/nginx:v0.1 nginx -g "daemon off;"
7cb761b30ad540b553f6aca6713cd6745c4bd296d4132b0e827592b1f5810b11

查看
root@ubuntu:~/myWeb# docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                   NAMES
7cb761b30ad5        lingyu/nginx:v0.1   nginx -g 'daemon off   6 seconds ago       Up 5 seconds        0.0.0.0:34567->80/tcp   myNginx
root@ubuntu:~/myWeb#


5、使用浏览器访问


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息