centos7虛擬機使用docker搭建swoole環境

由於用的是window電腦,安裝swoole比較麻煩,因此裝了虛擬機centos7,使用docker把swoole環
境跑起來。php

1.安裝docker

這裏就不贅述了,已經有不少教程了linux

2.找一個docker鏡像

我這利用的是cmptech/auto_cmp_php_docker_servergit

3.安裝鏡像

  • 我是root登陸的,家目錄是/root,在這裏新建文件swoole_server.php,代碼就是swoole官網的例子github

    <?php
        $http = new swoole_http_server("0.0.0.0", 9501);
        
        $http->on("start", function ($server) {
            echo "Swoole http server is started at http://127.0.0.1:9501\n";
        });
        
        $http->on("request", function ($request, $response) {
            $response->header("Content-Type", "text/plain");
            $response->end("Hello World\n".json_encode(['hello'=>'nihao'.time()]));
        });
        
        $http->start();
  • 運行命令docker

    docker run  -ti -p 9501:9501 -v `pwd`:/root/ -d cmptech/auto_cmp_php_docker_server

    而後運行 docker ps ,查看是否運行成功,我這裏是一直失敗,提示json

    [root@localhost ~]# docker run  -ti -p 9501:9501 -v `pwd`:/root/ -d cmptech/auto_cmp_php_docker_server
    /usr/bin/docker-current: Error response from daemon: No command specified.
    See '/usr/bin/docker-current run --help'.

    在網上搜了下,發現要在命令後面加上 /bin/bashcentos

    [root@localhost ~]# docker run  -ti -p 9501:9501 -v `pwd`:/root/ -d cmptech/auto_cmp_php_docker_server /bin/bash
    133406c3e1370d33cccb365d0a2b8978d872f8271b24f14dc1d7fab626fe7867

    這樣就成功了,容器已經跑起來了bash

  • 進入容器,運行命令docker attach ID,這個ID就是容器的ID,能夠用命令docker ps查看容器IDswoole

    [root@localhost ~]# docker ps
    CONTAINER ID        IMAGE                                COMMAND             CREATED             STATUS              PORTS                    NAMES
    133406c3e137        cmptech/auto_cmp_php_docker_server   "/bin/bash"         2 minutes ago       Up 2 minutes        0.0.0.0:9501->9501/tcp   agitated_meitner
    
    [root@localhost ~]# docker attach 133
    bash-4.3#

    這樣就已經進入容器了php7

  • 運行 php7 /root/swoole_server.php
    若是提示沒有權限,執行setenforce 0 臨時關閉selinux

    bash-4.3# php7 /root/test_swoole.php
    Swoole http server is started at http://127.0.0.1:9501

    在另外一個窗口執行命令curl http://127.0.0.1:9501,會看到返回hello world,swoole安裝成功了

相關文章
相關標籤/搜索