docker 实践

下载docker镜像

docker pull daocloud.io/centos:6

以交互方式运行

docker run -it daocloud.io/centos:6

以下是centos 安装各种软件~

安装sudo

yum install -y sudo

新建用户

useradd land && passwd land

修改sudoers文件允许land用户使用无密码sudo

visudo -f /etc/sudoers

## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL
land    ALL=(ALL)       NOPASSWD:ALL

使用land用户做接下来的操作

su land

安装一些必备库

sudo yum install -y gcc
sudo yum install -y libxml2-devel
sudo yum install -y libxslt-devel
sudo yum install -y bzip2-devel
sudo yum install -y m4
sudo yum install -y autoconf
sudo yum install -y libcurl-devel
sudo yum install -y libmemcached-devel
sudo yum install -y  cyrus-sasl-devel
 

扩展包更新

sudo yum  install epel-release
sudo yum install libmcrypt libmcrypt-devel mcrypt mhash
sudo yum install -y readline-devel
sudo yum install -y openssl*
sudo yum install -y wget
sudo yum install -y gcc+ gcc-c++
sudo yum install -y libtool

因为是64位系统,OpenSSL库安装在/usr/lib64/,但在安装php时,它还是去/usr/lib/ 下查找该库,故报上述错误

sudo ln -s /usr/lib64/libssl.so /usr/lib/

安装我们web必备nginx

sudo rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
sudo yum info nginx
sudo yum install -y nginx
sudo service nginx start

安装我们世界上最好的语言php(基础版本)

sudo yum install -y php

安装php多版本管理工具phpbrew

curl -L -O https://github.com/phpbrew/phpbrew/raw/master/phpbrew
chmod +x phpbrew
sudo mv phpbrew /usr/bin/phpbrew
phpbrew init
echo "[[ -e ~/.phpbrew/bashrc ]] && source ~/.phpbrew/bashrc" >> ~/.bashrc
source ~/.phpbrew/bashrc

安装php7.1

phpbrew install php-7.1.0 as php-7.1 +default +mysql +pdo +fpm +curl

默认使用php7.1

phpbrew switch php-7.1
如果报错:You should not see this, if you see this, it means you didn't load the ~/.phpbrew/bashrc script, please check if bashrc is sourced in your shell.,再次执行:phpbrew init && [[ -e ~/.phpbrew/bashrc ]] && source ~/.phpbrew/bashrc

phpbrew ext install curl
phpbrew ext enable curl
```shell
```shell
wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz
tar -zxvf libmemcached-1.0.18.tar.gz
cd libmemcached-1.0.18
./configure
make
sudo make install

phpbrew ext install https://github.com/php-memcached-dev/php-memcached php7 -- --disable-memcached-sasl

phpbrew ext enable memcached

nginx配置upstream、vhosts

nginx.conf:

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

 client_body_timeout 10;
        client_header_timeout 10;
        send_timeout 20;

        large_client_header_buffers 4 16k;
        client_max_body_size    60m;
        client_body_buffer_size 128k;
        client_header_buffer_size 4k;

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

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include       /etc/nginx/conf.d/*.conf.enabled;
    include       /etc/nginx/vhosts/*.conf;
}

/etc/nginx/conf.d/upstream.php.conf.enabled:

upstream phpfpm71 {
    server 127.0.0.1:9000;
}

/etc/nginx/vhosts/test.conf:

server {
    listen       80;
    server_name *.test.com;

    set $web_root /opt; 
    set $web_fpm phpfpm71;  

    root $web_root;
 
    index index.php index.html index.htm;

    # limit_req zone=one burst=40;

    if ($http_user_agent ~* (ApacheBench|EmbeddedWB) ) {
        return 403;
    }

    # Disable logging for favicon
    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    # Disable logging for robots.txt
    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }
   
    # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }

    # Block access to protected, framework, and nbproject (artifact from Netbeans)
    location ~ /(protected|framework|nbproject) {
        deny all;
        access_log off;
        log_not_found off;
    }

    # Block access to theme-folder views directories
    location ~ /themes/\w+/views {
        deny all;
        access_log off;
        log_not_found off;
    }

    # Attempt the uri, uri+/, then fall back to yii's index.php with args included
    # Note: old examples use IF statements, which nginx considers evil, this approach is more widely supported
    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    # END yiiframework.conf

    # Tell browser to cache image files for 24 hours, do not log missing images
    # I typically keep this after the yii rules, so that there is no conflict with content served by Yii
    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires 24h;
        log_not_found off;
    }

    # Block for processing PHP files
    # Specifically matches URIs ending in .php
    location ~ \.php$ {
        try_files $uri $uri/ /index.php?$args;
        # try_files $uri =404;

        # Fix for server variables that behave differently under nginx/php-fpm than typically expected
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # Include the standard fastcgi_params file included with nginx
        include fastcgi_params;
        fastcgi_param  PATH_INFO        $fastcgi_path_info;
        fastcgi_index index.php;
        # Override the SCRIPT_FILENAME variable set by fastcgi_params
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        # Pass to upstream PHP-FPM; This must match whatever you name your upstream connection
        fastcgi_pass $web_fpm;

    } 

}

/home/land/.phpbrew/php/php-7.1/etc/php-fpm.d/www.conf:

;                            a specific port;
;   'port'                 - to listen on a TCP socket to all addresses
;                            (IPv6 and IPv4-mapped) on a specific port;
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
; listen = /home/land/.phpbrew/php/php-7.1/var/run/php-fpm.sock
listen = 127.0.0.1:9000


; Set listen(2) backlog.
; Default Value: 511 (-1 on FreeBSD and OpenBSD)
;listen.backlog = 511
sudo service nginx restart
phpbrew fpm restart
curl http://127.0.0.1/a.html

我们提交docker改动,生成新的镜像

[root@194a44a03bf7 /]# exit
docker commit -m "nginx+phpbrew+php" -a "docker env" 194a44a03bf7 daocloud.io/centos:v2

我们挂载宿主机的代码目录,并且映射80端口给容器

docker run -ti -v /dockerdata:/dockerdata -p 80:80 daocloud.io/centos:v3
su land
sudo service nginx start
phpbrew fpm start

宿主机

curl http://127.0.0.1
大功告成

下一篇使用Dockerfile 来构建优化镜像通过dockerfile来构建优化容器

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 229,908评论 6 541
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 99,324评论 3 429
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 178,018评论 0 383
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 63,675评论 1 317
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 72,417评论 6 412
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 55,783评论 1 329
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 43,779评论 3 446
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 42,960评论 0 290
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 49,522评论 1 335
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 41,267评论 3 358
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 43,471评论 1 374
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 39,009评论 5 363
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 44,698评论 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 35,099评论 0 28
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 36,386评论 1 294
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 52,204评论 3 398
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 48,436评论 2 378

推荐阅读更多精彩内容