一、MySQL容器

拉取步骤

这里我们拉取官方的镜像,标签为5.7
docker pull mysql:5.7

使用mysql镜像运行容器
docker run -p 3306:3306 --name mymysql -v $PWD/MySQL/conf.d:/etc/mysql/conf.d 
-v $PWD/MySQL/logs:/logs 
-v $PWD/MySQL/data:/var/lib/mysql 
-e MYSQL_ROOT_PASSWORD=123456 
-d mysql:5.7

命令说明:

-p 3306:3306:将容器的3306端口映射到主机的3306端口

-v $PWD/conf/my.cnf:/etc/mysql/my.cnf:将主机当前目录下的conf/my.cnf挂载到容器的/etc/mysql/my.cnf

-v $PWD/logs:/logs:将主机当前目录下的logs目录挂载到容器的/logs

-v $PWD/data:/mysql_data:将主机当前目录下的data目录挂载到容器的/mysql_data

-e MYSQL_ROOT_PASSWORD=123456:初始化root用户的密码

查看容器启动情况
docker ps -a

View Code

安装成功后进入数据库操作 

④进入容器
docker exec -it 容器id或名称 /bin/bash 注:容器开启的状态下
cd /root/ 切换到容器的家目录下,ls可以查看是否有sql文件


⑤连接数据库
mysql -uroot –p密码 如:mysql -uroot –ppassword

⑥创建库
create database tesudrm charset=utf8;
show databases; 查看库是否创建成功

⑦注意:导入文件需要在容器的内部,而不是在数据库mysql中执行
退出mysql:Ctrl+D

cd /root/ 切换到容器的家目录下面执行导入sql文件
mysql -uroot -ppassword tesudrm< tesudrm.sql 

进入到数据库中:mysql –uroot –ppassword
use tesudrm;
show tables; 

⑧退出mysql以及mysql容器
Ctrl+P+Q 或 exit

View Code

 

二、Centos7安装python应用

1.1 建立Dockerfile

vim Dockerfile

FROM centos/python-36-centos7
MAINTAINER bobby
USER root
ADD hello.py /home/    #一个简单的flask程序,将当前目录下的py文件添加到容器的/home下
ADD hello.ini /home/   #uwsgi配置文件
ADD entry-point.sh  /home/  #shell脚本
WORKDIR /home

# 下载程序安装包
RUN pip install flask -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
RUN pip install uwsgi -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
RUN pip install flask_sqlalchemy -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
RUN pip install pymysql -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
# 或者  RUN pip install -r requirements.txt

ENTRYPOINT [ "/bin/bash", "/home/entry-point.sh"]
#ENTRYPOINT  uwsgi --ini /home/hello.ini
#CMD ["uwsgi","--ini", "/home/hello.ini"] #启动uwsgi

View Code

vim entry-point.sh #启动文件

if [ -e /debug1 ]; then
echo "Running app in debug mode!"
python /home/hello.ini
else
echo "Running app in production mode!"
uwsgi --ini /home/hello.ini
tail -f /home/hello.ini
fi

View Code

vim hello.ini #uwsgi配置文件 

[uwsgi]
# htmlWeb.py文件所在目录
#plugins = python3
chdir  = /home
callable = app
# flask文件名
wsgi-file= hello.py
# 进程数
processes   = 4
# 使用5000端口
http = 0.0.0.0:5000
# 日志输出目录
#daemonize = /home/hello.log
pidfile = /home/project-master.pid

View Code

vim hello.py #flask程序

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
# import pymysql

app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = "mysql+pymysql://root:123456@192.168.234.134/dockerpro"
# app.config['SQLALCHEMY_DATABASE_URI'] = "mysql+mysqlconnector://root:123456@ip/dockerpro"

#配置flask配置对象中键:SQLALCHEMY_COMMIT_TEARDOWN,设置为True,应用会自动在每次请求结束后提交数据库中变动

app.config['SQLALCHEMY_COMMIT_TEARDOWN'] = True
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True

#获取SQLAlchemy实例对象,接下来就可以使用对象调用数据

db = SQLAlchemy(app)

#创建模型对象
class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)

def __repr__(self):
    return '<User %r>' % self.username



@app.route("/")
def index():
    # db.session.query(User).filter(User.username=='aaa').first()
    email = User.query.filter_by(username='aaa').first()
    print(email.email)
    return """
        <h1>Python Flask in Docker!</h1>
        <p>A sample web-app for running Flask inside Docker.</p>
        <p>Email: %s.</p>
        """% email.email
if __name__ == "__main__":
    # 1.创建表
   # db.create_all()
    app.run(debug=True, host='0.0.0.0')

View Code

docker build -t docker-flask:0.1 .  #拉取镜像

docker run -d –name flaskapp –restart=always -p 5000:5000  docker-flask:0.1  #运行容器

这时候访问 ip:5000 就可以了

三、nginx容器

用来做反向代理:

# 拉取镜像
docker pull nginx
#运行容器
docker run -p 80:80 --name mynginx -v /home/hello_nginx.conf:/etc/nginx/nginx.conf -v $PWD/logs:/wwwlogs  -d nginx 

View Code

nginx配置文件

#创建配置文件
vim hello_nginx.conf

# 内容

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


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

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        server_name  ip;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
               # proxy_pass http://127.0.0.1:5000;
                proxy_pass http://ip:5000;
                proxy_redirect     off;
                proxy_set_header   Host                 $http_host;
                proxy_set_header   X-Real-IP            $remote_addr;
                proxy_set_header   X-Forwarded-For      $proxy_add_x_forwarded_for;
                proxy_set_header   X-Forwarded-Proto    $scheme;

        }


}
}

View Code
 

四、打包自定义镜像上传阿里云

6.1 先关闭容器:docker stop 容器ID 6.2 基于当前容器重新创建镜像

docker commit -m “描述” -a “root” -p 容器id 镜像名称:版本号

如:docker commit -m “centos6.8_python3.4” -a “root” -p 容器id en_centos:2.2

p 表示提交时停止容器 –a 提交镜像的作者 -m 提交时的说明文字

6.3 创建阿里云账号

https://cr.console.aliyun.com/cn-hangzhou/instances/repositories

6.4 登录阿里云账号

docker login –username=zgs1121 registry.cn-hangzhou.aliyuncs.com

password:**********

6.5 将镜像上传到阿里云的镜像仓库

docker tag 镜像ID registry.cn-hangzhou.aliyuncs.com/tesu/仓库名(en_centos)
docker push registry.cn-hangzhou.aliyuncs.com/tesu/仓库名(en_centos)

6.6 拉取镜像

docker pull registry.cn-hangzhou.aliyuncs.com/tesunet/en_centos

6.7 命名镜像

docker tag registry.cn-hangzhou.aliyuncs.com/tesunet/en_centos 新镜像名称