Docker 基于Dockerfile创建镜像实践

Posted 授客的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker 基于Dockerfile创建镜像实践相关的知识,希望对你有一定的参考价值。

需求描述

简单说,就是创建一个服务型的镜像,即运行基于该镜像创建的容器时,基于该容器自动开启一个服务。具体来说,是创建一个部署了nginxuwsgipythondjango项目代码的镜像,运行基于该镜像创建的容器时,自动开启nginx,uwsgi等服务。简单理解就是在容器内部,通过nginx+uwsgi部署Django项目

实践环境

CentOS Linux release 7.9.2009

实践过程

实践过程1

Django项目代码组织结构

env_setup.sh
#!/bin/bash

yum install -y wget gcc make zlib* openssl-devel sqlite-devel

wget http://nginx.org/download/nginx-1.10.3.tar.gz
tar -xvzf nginx-1.10.3.tar.gz
cd nginx-1.10.3
./configure --prefix=/usr/local/nginx
make && make install
cp -f /opt/DjServer/docker/nginx.conf /usr/local/nginx/conf/nginx.conf
chmod 755 /usr/local/nginx/conf/nginx.conf
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
cd .. && rm -rf nginx-1.10.3*


wget http://www.sqlite.org/2019/sqlite-autoconf-3280000.tar.gz
tar -xvzf sqlite-autoconf-3280000.tar.gz
cd sqlite-autoconf-3280000
./configure --prefix=/usr/local/sqlite
make && make install
mv /usr/bin/sqlite3 /usr/bin/sqlite3_old
ln -s /usr/local/sqlite/bin/sqlite3 /usr/bin/sqlite3
echo "/usr/local/sqlite/lib" > /etc/ld.so.conf.d/sqlite3.conf
ldconfig
cd .. && rm -rf sqlite-autoconf-*

wget https://www.python.org/ftp/python/3.6.13/Python-3.6.13.tgz
tar -xvzf Python-3.6.13.tgz
cd Python-3.6.13
./configure --prefix=/usr/local/python36
make && make install
cd .. && rm -rf Python-3.6.13*
ln -s /usr/local/python36/bin/python3 /usr/bin/python3
ln -s /usr/local/python36/bin/pip3 /usr/bin/pip3
ls
pip3 install -r  /opt/DjServer/requirements.txt
ln -s /usr/local/python36/bin/uwsgi /usr/bin/uwsgi
mkdir -p /var/log/uwsgi
start.sh
#!/bin/bash
`uwsgi --ini uwsgi.ini &` && nginx -c /usr/local/nginx/conf/nginx.conf -g "daemon off;"
nginx.conf
worker_processes 16;

events {
    worker_connections  65535;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    keepalive_timeout  65;
    reset_timedout_connection on; 

    server {
        listen       80;
        server_name  localhost;

        charset utf-8;

        #access_log logs/access.log;
        access_log off;
        error_log logs/error.log;

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location / {
            uwsgi_send_timeout 60;        
            uwsgi_connect_timeout 60;    
            uwsgi_read_timeout 600; 
            include uwsgi_params;
            uwsgi_pass 127.0.0.1:8000;
        }
    }
}
requirements.txt
Django==3.0.6
uwsgi==2.0.8

前置准备工作

如下,创建一个干净的目录(例中为myworkspace),用于创建镜像,并将解压后的Django项目代码存放到该目录,然后在该目录下新建并编写dockerfile.dockerignore文件。

# pwd
/root/myworkspace
# ls -a
.  ..  DjServer  dockerfile  .dockerignore
dockerfile
FROM centos:7.9.2009

WORKDIR /opt/DjServer
COPY ./DjServer .
RUN chmod 755 ./docker/env_setup.sh \\
    && sh ./docker/env_setup.sh \\
    && chmod 755 ./docker/start.sh
EXPOSE 80
ENTRYPOINT ["./docker/start.sh"]
.dockerignore
*/__pycache__

创建服务镜像

如下,创建djserver:v1.0镜像

# docker build  -t djserver:v1.0 .
...略
Successfully tagged djserver:v1.0

创建容器

基于上述镜像创建名为dj80的容器,映射宿主主机80端口到容器的80端口,并为容器设置环境变量

# docker run --name dj9003 -p 9003:80 -e hostPort=10.1.66.163:9003 -d -it djserver:v1.0

测试验证

# curl http://10.7.0.236:9003/loadapi/api/v1/ip_forward # 10.7.0.236为容器所在宿主机

实践过程2

Django项目代码组织结构

注意:代码结构中的requirements.txt暂时没用到,对该文件的定位是,存放创建基础镜像时新增requirements.txt文件所包含依赖包除外的其它依赖包。

创建基础镜像前置准备工作

如下,创建一个干净的目录(例中为env_build),用于创建提供软件运行环境的基础镜像,并在该目录下新建并编写dockerfile,新建env_conf目录,然后在env_conf目录下新建env_setup.sh, requirements.txt

# pwd
/root/env_build
# ls -aR
.:
.  ..  env_conf  env_dockerfile

./env_conf:
.  ..  env_setup.sh  requirements.txt
env_dockerfile
FROM centos:7.9.2009
COPY ./env_conf .
RUN chmod 755 env_setup.sh \\
    && sh env_setup.sh \\
    && rm -f env_setup.sh requirements.txt
env_setup.sh
#!/bin/bash

yum install -y wget gcc make zlib* openssl-devel sqlite-devel

wget http://nginx.org/download/nginx-1.10.3.tar.gz
tar -xvzf nginx-1.10.3.tar.gz
cd nginx-1.10.3
./configure --prefix=/usr/local/nginx
make && make install
cd .. && rm -rf nginx-1.10.3*
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx


wget http://www.sqlite.org/2019/sqlite-autoconf-3280000.tar.gz
tar -xvzf sqlite-autoconf-3280000.tar.gz
cd sqlite-autoconf-3280000
./configure --prefix=/usr/local/sqlite
make && make install
mv /usr/bin/sqlite3 /usr/bin/sqlite3_old
ln -s /usr/local/sqlite/bin/sqlite3 /usr/bin/sqlite3
echo "/usr/local/sqlite/lib" > /etc/ld.so.conf.d/sqlite3.conf
ldconfig
cd .. && rm -rf sqlite-autoconf-*

wget https://www.python.org/ftp/python/3.6.13/Python-3.6.13.tgz
tar -xvzf Python-3.6.13.tgz
cd Python-3.6.13
./configure --prefix=/usr/local/python36
make && make install
cd .. && rm -rf Python-3.6.13*
ln -s /usr/local/python36/bin/python3 /usr/bin/python3
ln -s /usr/local/python36/bin/pip3 /usr/bin/pip3

pip3 install -r requirements.txt
ln -s /usr/local/python36/bin/uwsgi /usr/bin/uwsgi
mkdir -p /var/log/uwsgi
requirements.txt
Django==3.0.6
uwsgi==2.0.8

注意:这里对requirements.txt的定位是,用于存放一定需要安装,并且极少改动(比如更换版本)的软件依赖包

创建基础镜像

如下,创建djbase:v1.0基础镜像,为运行Django项目提供所需的软件运行环境

# docker build --no-cache -t djbase:v1.0 -f env_dockerfile .
...略docker build --no-cache -t djbase:v1.0 -f env_dockerfile .
Successfully tagged djbase:v1.0

创建服务镜像前置准备工作

创建一个干净的目录(例中为server_build),用于创建最终服务镜像,并将解压后的Django项目代码存放到该目录,然后在该目录下新建并编写dockerfile.dockerignore 文件。

# pwd
/root/server_build
# ls -a
.  ..  DjServer  dockerfile  .dockerignore
dockerfile
FROM djbase:v1.0

WORKDIR /opt/DjServer
RUN mkdir -p /var/log/uwsgi
COPY ./DjServer/docker/nginx.conf .
RUN mv -f ./nginx.conf /usr/local/nginx/conf/nginx.conf && chmod 755 /usr/local/nginx/conf/nginx.conf
COPY ./DjServer .
RUN chmod 755 ./docker/start.sh
EXPOSE 80
ENTRYPOINT ["./docker/start.sh"]
.dockerignore
*/__pycache__
docker/start.sh
#!/bin/bash
`uwsgi --ini uwsgi.ini &` && nginx -c /usr/local/nginx/conf/nginx.conf -g "daemon off;"
docker/nginx.conf

“实践过程1”

创建服务镜像

如下,创建djserver:v1.0服务镜像

# docker build -t djserver:v1.0 .
...略
Successfully tagged djserver:v1.0

创建容器

基于上述镜像创建名为djserver的容器,映射宿主主机80端口到容器的80端口

# docker run --name dj9003 -p 9003:80 -e hostPort=10.1.66.163:9003 -d -it djserver:v1.0

作者:授客
公众号:授客的知识库
QQ:1033553122
全国软件测试QQ交流群:7156436

Git地址:https://gitee.com/ishouke
友情提示:限于时间仓促,文中可能存在错误,欢迎指正、评论!
作者五行缺钱,如果觉得文章对您有帮助,请扫描下边的二维码打赏作者,金额随意,您的支持将是我继续创作的源动力,打赏后如有任何疑问,请联系我!!!
            微信打赏                       支付宝打赏                        授课的知识库               全国软件测试交流QQ群  
                      

以上是关于Docker 基于Dockerfile创建镜像实践的主要内容,如果未能解决你的问题,请参考以下文章

Docker------Docker数据管理与Dockerfile镜像创建

Docker------Docker数据管理与Dockerfile镜像创建

docker的数据管理镜像dockerfile创建

Dockerfile概念简述

docker 实践六:dockerfile 详解

docker数据管理和镜像创建dockerfile部署