dockerfile构建网站(示例)

dockerfile构建网站(示例)

  1. (后端网站代码)用python语言,基于flask web框架,开发一个网站;

  2. 开发dockerfile,部署该代码,生成镜像

  3. docker run 该镜像

构建网站

# 1. 在宿主机上准备好dockerfile和网站代码
[root@localhost LearnDocker]$ touch Eishin_flask.py
[root@localhost LearnDocker]$ cat <<EOF > Eishin_flask.py
> #coding:utf8
> from flask import Flask
> app=Flask(__name__)
> @app.route('/hello')
> def hello():
>     return "hello from docker, I am Eishin."
> if __name__ == "__main__":
>     app.run(host="0.0.0.0", port=8080)
> EOF

# 2. 编写dockerfile
[root@localhost LearnDocker]$ cat <<EOF > Dockerfile
> FROM centos:7.8.2003
> RUN curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.
repo;
> RUN curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo;
> RUN yum makecache fast;
> RUN yum install python3-devel python3-pip -y
> RUN pip3 install -i https://pypi.douban.com/simple flask
> COPY Eishin_flask.py /opt
> WORKDIR /opt
> EXPOSE 8080
> CMD ["python", "Eishin_flask.py"]
> EOF

# 3. 构建镜像
[root@localhost LearnDocker]$ docker build -t 'Eishin/my_flask_web' .
[+] Building 38.3s (13/13) FINISHED
 => [internal] load build definition from Dockerfile                                   0.0s
 => => transferring dockerfile: 506B                                                   0.0s
 => [internal] load .dockerignore                                                      0.0s
 => => transferring context: 2B                                                        0.0s
 => [internal] load metadata for docker.io/library/centos:7.8.2003                     0.0s
 => [1/8] FROM docker.io/library/centos:7.8.2003                                       0.0s
 => [internal] load build context                                                      0.1s
 => => transferring context: 96B                                                       0.0s
 => CACHED [2/8] RUN curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun  0.0s
 => CACHED [3/8] RUN curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/rep  0.0s
 => CACHED [4/8] RUN yum makecache fast;                                               0.0s
 => [5/8] RUN yum install python3-devel python3-pip -y                                27.2s
 => [6/8] RUN pip3 install -i https://pypi.douban.com/simple flask                     7.4s
 => [7/8] COPY Eishin_flask.py /opt                                                    0.1s
 => [8/8] WORKDIR /opt                                                                 0.0s
 => exporting to image                                                                 3.4s
 => => exporting layers                                                                3.4s
 => => writing image sha256:6fe63dfb6a91a0115f660ca106123a55382fd47beffefd95cf7f6f12c  0.0s
 => => naming to docker.io/eishin/my_flask_web                                         0.0s

[root@localhost LearnDocker]$ docker images
REPOSITORY            TAG        IMAGE ID       CREATED          SIZE
eishin/my_flask_web   latest     6fe63dfb6a91   10 seconds ago   767MB
ubuntu                latest     08d22c0ceb15   12 days ago      77.8MB
nginx                 latest     904b8cb13b93   2 weeks ago      142MB
hello-world           latest     feb5d9fea6a5   18 months ago    13.3kB
centos                7.8.2003   afb6fca791e0   2 years ago      203MB

# 4. 运行镜像,生成容器
[root@localhost LearnDocker]$  docker run -d --name my_flask_web_1 -p 90:8080 6fe63dfb6a91

# 5. 宿主机的浏览器访问90端口
http://192.168.74.128:90/hello

修改网站的内容

  1. 修改宿主机的代码以及dockerfile,重新构建镜像

  2. 进入到已经运行的容器内,修改代码,重启容器即可

[root@localhost LearnDocker]$ docker ps
CONTAINER ID   IMAGE                 COMMAND                   CREATED         STATUS         PORTS                                   NAMES
c252613f6615   eishin/my_flask_web   "python3 Eishin_flas…"   4 seconds ago   Up 3 seconds   0.0.0.0:90->8080/tcp, :::90->8080/tcp   my_flask_web_2

[root@localhost LearnDocker]$ docker exec -it c252613f6615 bash

# Dockerfile中 WORKDIR 在 /opt
[root@c252613f6615 opt]$ ls
Eishin_flask.py

[root@c252613f6615 opt]$ cat Eishin_flask.py
#coding:utf8
from flask import Flask
app=Flask(__name__)
@app.route('/hello')
def hello():
    return "hello from docker, I am Eishin."
if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8080)
[root@c252613f6615 opt]# ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 12:23 ?        00:00:00 python3 Eishin_flask.py
root         10      0  0 12:24 pts/0    00:00:00 bash
root         26     10  0 12:25 pts/0    00:00:00 ps -ef

# 修改python文件
[root@c252613f6615 opt]$ vi Eishin_flask.py

# 重启容器
[root@localhost LearnDocker]$ docker restart c252613f6615
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值