顯示具有 docker 標籤的文章。 顯示所有文章
顯示具有 docker 標籤的文章。 顯示所有文章

2020年12月15日 星期二

同時跑兩個PHP版本(5.6+7.3)的laradock

 參考資料

實作

拉兩個目錄的laradock,到時候跑同一個目錄的 info.php ( php_info(); ),相同的 nginx/sites/ 配置
/var/www/html/laradock/ - 跑PHP 7.3
/var/www/html/laradock5/ - 跑PHP 5.6

/var/www/html/laradock5/.env

使用不同的project name,最後會build出的容器名字會是:laradock5_docker-in-docker_1、laradock5_nginx_1、laradock5_php-fpm_1、laradock5_workspace_1
#   COMPOSE_PROJECT_NAME=laradock
COMPOSE_PROJECT_NAME=laradock5

使用PHP 5.6
#   PHP_VERSION=7.3
PHP_VERSION=5.6

如果你的網路不好,一直裝不成node,可以這樣跳過安裝node
#   WORKSPACE_INSTALL_NODE=true
WORKSPACE_INSTALL_NODE=false

如果你的網路不好,一直裝不成yarn,可以這樣跳過安裝yarn
#   WORKSPACE_INSTALL_YARN=true
WORKSPACE_INSTALL_YARN=false

避免端口和 /var/www/html/laradock/ (PHP 7.3 )的容器衝突
#   WORKSPACE_SSH_PORT=2222
WORKSPACE_SSH_PORT=2226
...
#   WORKSPACE_BROWSERSYNC_HOST_PORT=3000
WORKSPACE_BROWSERSYNC_HOST_PORT=3004
#   WORKSPACE_BROWSERSYNC_UI_HOST_PORT=3001
WORKSPACE_BROWSERSYNC_UI_HOST_PORT=3005
#   WORKSPACE_VUE_CLI_SERVE_HOST_PORT=8080
WORKSPACE_VUE_CLI_SERVE_HOST_PORT=8084
#   WORKSPACE_VUE_CLI_UI_HOST_PORT=8001
WORKSPACE_VUE_CLI_UI_HOST_PORT=8005

使用5567端口訪問PHP 5.6容器(PHP 7.3 端口5566)
#   NGINX_HOST_HTTP_PORT=80
NGINX_HOST_HTTP_PORT=5567
#   NGINX_HOST_HTTPS_PORT=443
NGINX_HOST_HTTPS_PORT=444

#   VARNISH_BACKEND_PORT=81
VARNISH_BACKEND_PORT=82

/var/www/html/laradock/ 和 /var/www/html/laradock5/ 都執行
[user@localhost laradock5 ]$ docker-compose up -d nginx workspace
Starting laradock5_docker-in-docker_1 ... done
Starting laradock5_workspace_1        ... done
Starting laradock5_php-fpm_1          ... done
Starting laradock5_nginx_1            ... done

[user@localhost laradock ]$ docker-compose up -d nginx workspace
Starting laradock_docker-in-docker_1 ... done
Recreating laradock_workspace_1      ... done
Recreating laradock_php-fpm_1        ... done
Recreating laradock_nginx_1          ... done

檢查

打開 http://app3.test:5566/info.php (php 7.3) 和 http://app3.test:5567/info.php (php 5.6) 






2020年12月4日 星期五

windows 10 docker心得

前言

到了2020年才知道docker也在windows 能執行了,之前一直都是在hyper-v的虛擬機上面跑
https://docs.docker.com/docker-for-windows/release-notes/  Docker Desktop for Windows Stable Release notes

安裝

https://docs.docker.com/docker-for-windows/install/  Install Docker Desktop on Windows
安裝成功後開機會自動執行Docker Desktop


開始使用

安裝成功Docker Desktop後無containers和images的畫面


安裝第一個container

直接使用git bash可以呼叫到docker 和docker-compose命令,直接在git bash上執行
(使用90端口,避免和XAMPP的80端口衝突)
$ docker run -d -p 90:80 docker/getting-started
Unable to find image 'docker/getting-started:latest' locally
latest: Pulling from docker/getting-started
188c0c94c7c5: Pull complete
61c2c0635c35: Pull complete
378d0a9d4d5f: Pull complete
2fe865f77305: Pull complete
b92535839843: Pull complete
ebe280ac36f4: Pull complete
b7beeb601852: Pull complete
Digest: sha256:7434aacfacb3c2d7d49f013e8a8092fde022c8b9f058b7fae7077e9cad1edc99
Status: Downloaded newer image for docker/getting-started:latest
661e2d85e8b11aad23994a53d869fd476a18e5524ebc442c2884ba0825cb9f59

然後就可以在 Docker Desktop 看到新的containers和images


在 Containers / Apps 中對執行中的container 點擊【open  in browser 】可以直接在瀏覽器打開docker/getting-started 容器的頁面


laradock

build容器時因為網路不好報錯:
Step 146/294 : RUN if [ ${INSTALL_NODE} = true ]; then     mkdir -p $NVM_DIR &&     curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash         && . $NVM_DIR/nvm.sh         && nvm install ${NODE_VERSION}         && nvm use ${NODE_VERSION}         && nvm alias ${NODE_VERSION}         && if [ ${NPM_REGISTRY} ]; then         npm config set registry ${NPM_REGISTRY}         ;fi         && if [ ${INSTALL_NPM_GULP} = true ]; then         npm install -g gulp         ;fi         && if [ ${INSTALL_NPM_BOWER} = true ]; then         npm install -g bower         ;fi         && if [ ${INSTALL_NPM_VUE_CLI} = true ]; then         npm install -g @vue/cli         ;fi         && if [ ${INSTALL_NPM_ANGULAR_CLI} = true ]; then         npm install -g @angular/cli         ;fi
     && ln -s `npm bin --global` /home/laradock/.node-bin ;fi
...
npm ERR! code ECONNRESET
npm ERR! network aborted
npm ERR! network This is a problem related to network connectivity.
npm ERR! network In most cases you are behind a proxy or have bad network settings.
npm ERR! network
npm ERR! network If you are behind a proxy, please make sure that the
npm ERR! network 'proxy' config is set properly.  See: 'npm help config'

解法

Windows 打開proxy和shadowsocks,換一個IP重新build就可以了

XDebug

放棄,搞不定。還是用hyper-v中的laradock斷點(HTTP+CLI都可以)











2020年4月14日 星期二

DesignPatternsPHP

下載

https://github.com/domnikl/DesignPatternsPHP
$ git clone git@github.com:domnikl/DesignPatternsPHP.git

瀏覽文檔(可選)

修改端口

因為8080被laradock佔用了,所以要改端口
docker-compose.yml
services:
  nginx:
    port:
      - "8090:80"

# docker-compose up --build
然後打開 http://192.168.1.x:8090/ 即可瀏覽像  https://designpatternsphp.readthedocs.io/en/latest/README.html 一樣的文檔
這個步驟新增了幾個images:
designpatternsphp_nginx   latest              3867bfce0be3        2 minutes ago       139MB
composer                  latest              b7fba84aad57        20 hours ago        179MB
python                    latest              d47898c6f4b0        9 days ago          933MB
nginx                     latest              ed21b7a8aee9        10 days ago         127MB

進入容器後發現沒php

# docker exec -it designpatternsphp_nginx_1 bash
root@adcb4fe4d5bb:/usr/share/nginx/html# php -v
bash: php: command not found

因為這個commit 把 php:7.3.8-cli 拿掉了..
commit be66dcc037f8b2293134efe3cc3f2e87a25b8f80
Author: Dominik Liebler <liebler.dominik@gmail.com>
Date:   Fri Apr 3 13:56:33 2020 +0200

    #376 use multi-stage Docker build

diff --git a/Dockerfile b/Dockerfile
index d62cca0..e319867 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,9 +1,17 @@
-FROM php:7.3.8-cli
+FROM composer AS composer
...

所以容器內跑不動 phpunit ...而且還沒composer install,所以 ./vendor/bin/phpunit 還不存在

跑composer install

我的hyper-v PHP 版本是7.3
# composer install
...
  Problem 1
    - This package requires php >=7.4 but your HHVM version does not satisfy that requirement.
因為
commit 579a5ac946330c8b966a23ba3a53434487c26941
Author: Dominik Liebler <liebler.dominik@gmail.com>
Date:   Sat Dec 14 12:50:05 2019 +0100

    update deps & install rector
...
diff --git a/composer.json b/composer.json
...
-        "php": ">=7.2",
+        "php": ">=7.4",

使用composer容器做composer install

[root@localhost DesignPatternsPHP]# docker run --rm --interactive --tty \
>   --volume $PWD:/app \
>   composer install

--rm : 自動移除容器,當離開時
--interactive :即-i ,保持STDIN開啟,即使不attached
--tty : 即-t
--volume :即-v

即可順利composer install 建立vendor/
但是還是不能用 php 7.3 跑 DesignPatternsPHP,因為用到了7.4的語法
# ./vendor/bin/phpunit
PHP Parse error:  syntax error, unexpected 'Handler' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) in /root/DesignPatternsPHP/Behavioral/ChainOfResponsibilities/Tests/ChainTest.php on line 14



使用php7.4-cli

新建 Dockerfile2 
FROM php:7.4-cli
COPY . /usr/src/myapp
WORKDIR /usr/src/myapp
RUN pecl install xdebug-2.8.1 \
    && docker-php-ext-enable xdebug \
    && echo "xdebug.remote_enable=1" >> /usr/local/etc/php/php.ini \
    && echo "xdebug.remote_connect_back=1" >> /usr/local/etc/php/php.ini \
    && echo "xdebug.idekey=PHPSTORM" >> /usr/local/etc/php/php.ini \
    && echo "xdebug.remote_log=/tmp/xdebug.log" >> /usr/local/etc/php/php.ini

CMD [ "php", "./vendor/bin/phpunit" ]

[root@localhost DesignPatternsPHP]# docker build -f Dockerfile2 -t my-php-7.4-cli .
這個步驟新增了幾個images:
my-php-7.4-cli            latest              11a27014b3a0        35 seconds ago      489MB
php                       7.4-cli             76a5dae0c884        9 days ago          405MB

要用run -it ,如果 run -d容器會起來後跑完 ./vendor/bin/phpunit 後就 Exited
# docker run -it --name my-running-php-7.4-cli -v /root/DesignPatternsPHP:/usr/src/myapp -e XDEBUG_CONFIG="remote_host=192.168.1.7 idekey=XDEBUG" -e PHP_IDE_CONFIG="serverName=localhost" my-php-7.4-cli:latest bash

-e :進入容器前設定環境變數,等同在容器內執行 export XDEBUG_CONFIG="remote_host=192.168.1.7 idekey=XDEBUG" 和 export PHP_IDE_CONFIG="serverName=localhost"

使用PHPStorm 時idekey要設PHPSTORM =》
export XDEBUG_CONFIG="remote_host=192.168.1.7 idekey=PHPSTORM"
export PHP_IDE_CONFIG="serverName=localhost"


PHPStorm中可能的報錯

To fix it set server name by environment variable PHP_IDE_CONFIG and restart debug session.
解法:
https://www.jetbrains.com/help/phpstorm/debugging-a-php-cli-script.html
容器中執行 export PHP_IDE_CONFIG="serverName=localhost"
Can't find a source position. Server with name 'localhost' doesn't exist.
點擊"Configure servers" 設定Servers:localhost
# ./vendor/bin/phpunit
Cannot find a local copy of the file on server /usr/src/myapp/Standard input code
解法:
指定Tests不要一次全跑
# ./vendor/bin/phpunit Creational/AbstractFactory/Tests/AbstractFactoryTest.php























2019年10月7日 星期一

docker 心得2019

系統:CentOS 7.6

安裝docker CE

由於 Docker 後來分為 Docker EE(Enterprise Edition)與 Docker CE(Community Edition)兩種版本,新的套件名稱與舊的不同,所以在安裝 Docker 之前,要先移除 CentOS Linux 系統上舊版的 Docker 套件:(新系統可略)
# yum remove docker docker-common container-selinux docker-selinux docker-engine docker-engine-selinux

安裝一些必要套件:
# yum install -y yum-utils device-mapper-persistent-data lvm2

新增 Docker 官方的 stable 套件庫(repository):
# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

安裝 Docker CE 版:
# yum install docker-ce

將你的用戶加入docker group (實測該用戶還是不能執行docker  重啟機器打開新的screen才生效)
$ sudo usermod -aG docker $(whoami)
執行後可檢查 /etc/gshadow 和 /etc/group

docker 開機自動啟動
# systemctl enable docker.service

開啟docker 服務
# systemctl start docker.service

測試
# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:b8ba256769a0ac28dd126d584e0a2011cd2877f3f76e093a7ae560f2a5301c00
Status: Downloaded newer image for hello-world:latest
之後docker images 會多一個 hello-world:latest 的鏡像

安裝Docker Compose

安裝 epel
# yum install epel-release

查裝什麼package才能用pip
# yum provides */pip

安裝pip
# yum install python34-pip

安裝docker compose
# pip3.4 install docker-compose
...
You are using pip version 8.1.2, however version 19.2.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

升級pip
# pip3.4 install --upgrade pip
...
Successfully installed pip-19.2.3


檢測pip版本(升級後pip可以直接用了)
# pip --version
pip 19.2.3 from /usr/lib/python3.4/site-packages/pip (python 3.4)


升級Python packages,讓docker-compose 正確執行(我沒做)
# yum upgrade python*

查 docker-compose 版本
# docker-compose version
docker-compose version 1.24.1, build 4667896
docker-py version: 3.7.3
CPython version: 3.4.10

安裝Docker Compose - 方法2

如果pip安裝遇到 RuntimeError: Python 3.5 or later is required 錯誤,可使用這個方法。更無腦
https://phoenixnap.com/kb/install-docker-compose-centos-7  How To Install Docker Compose On CentOS 7
# curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# chmod +x /usr/local/bin/docker-compose

如果報錯:
curl: (35) Peer reports incompatible or unsupported protocol version.
https://blog.csdn.net/feinifi/article/details/79629904  git clone 报错:Peer reports incompatible or unsupported protocol version解决办法
原因是curl,nss的版本低

解法:

# yum update nss curl



docker-compose 自動完成

# curl -L https://raw.githubusercontent.com/docker/compose/1.25.4/contrib/completion/bash/docker-compose -o /etc/bash_completion.d/docker-compose
讓安裝前的tty  docker-compose可以自動完成
# . /etc/bash_completion.d/docker-compose

開啟ipv4 ip_forward

啟動時如果報錯
# docker run -d --name my-running-app -p 5656:80 -v /root/php5.6-apache/src:/var/www/html my-php-app:latest
WARNING: IPv4 forwarding is disabled. Networking will not work.
b44f4990a94025f7d03329dfb32c64b2677dc595d937a309582150b409c0404a

https://stackoverflow.com/questions/41453263/docker-networking-disabled-warning-ipv4-forwarding-is-disabled-networking-wil Docker Networking Disabled: WARNING: IPv4 forwarding is disabled. Networking will not work
https://blog.csdn.net/weiguang1017/article/details/76212203 centos 7 Docker容器启动报WARNING: IPv4 forwarding is disabled. Networking will not work
https://carlislebear.blogspot.com/2014/11/arch-linux-internet-sharing.html  Arch Internet sharing(透過另一台電腦上網)
Windows是無法訪問容器的web服務的(hyper-v上可以),要開啟以下設定
/etc/sysctl.conf
net.ipv4.ip_forward=1
重啟網路
# systemctl restart network
這樣windows才能訪問容器的web服務
如果打不開,要注意有沒有因為被XDebug斷住了所以打不開

laradock

重啟Exited 的 laradock container

# docker ps -a
CONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS                      PORTS                                        NAMES
cc866de06b61        laradock_nginx       "/bin/bash /opt/star…"   2 months ago        Exited (255) 11 days ago    0.0.0.0:443->443/tcp, 0.0.0.0:5566->80/tcp   laradock_ngin
x_1
ce974cde40bf        laradock_php-fpm     "docker-php-entrypoi…"   3 months ago        Exited (255) 11 days ago    9000/tcp                                     laradock_php-
fpm_1
cb36fbf53996        laradock_workspace   "/sbin/my_init"          3 months ago        Exited (255) 11 days ago    0.0.0.0:2222->22/tcp                         laradock_work
space_1
d475c0f6d18f        docker:dind          "dockerd-entrypoint.…"   3 months ago        Exited (255) 11 days ago    2375-2376/tcp                                laradock_dock
er-in-docker_1
64c1a5ed1659        laradock_mysql       "docker-entrypoint.s…"   3 months ago        Exited (255) 2 months ago   0.0.0.0:3306->3306/tcp, 33060/tcp            laradock_mysq
l_1

啟動單一容器

# docker start laradock_workspace_1
laradock_workspace_1

但因為laradock 是很多容器組合而成的

正確的重啟laradock

# docker-compose up -d workspace nginx
laradock_docker-in-docker_1 is up-to-date
laradock_workspace_1 is up-to-date
Starting laradock_php-fpm_1 ... done
Starting laradock_nginx_1   ... done

如果報錯:
Creating network "laradock_frontend" with driver "bridge"
ERROR: Failed to Setup IP tables: Unable to enable SKIP DNAT rule:  (iptables failed: iptables --wait -t nat -I DOCKER -i br-7d0b53dc35e0 -j RETURN: iptables: No chain/target/match by that name.
 (exit status 1))
原因:
應該是改過防火墻iptables造成的
https://stackoverflow.com/a/53766860  iptables: No chain/target/match error (with docker network create)
https://github.com/wodby/docker4drupal/issues/211#issuecomment-351016274  Unable to enable SKIP DNAT rule (未使用)
解法:
重啟docker
# systemctl restart docker
再重啟laradock
# docker-compose up -d workspace nginx

進入workspace容器

# docker exec -it laradock_workspace_1 bash
上面是以root身份進入,跑composer 會有警告。如何進入容器跑composer不報警告?
以laradock身份進入容器
# docker-compose exec --user=laradock workspace bash
laradock@daa4f318e534:/var/www$ composer --version
Composer version 1.9.0 2019-08-02 20:55:32


關閉laradock

# docker-compose down

多人開發

配置.env

#   APP_CODE_PATH_HOST=../  
# APP_CODE_PATH_HOST=../project-z/coolapp/   # 使用 laradock/nginx/sites/default.conf 不需特別設定
APP_CODE_PATH_HOST=../project-z/  # 多個專案,複製 laradock/nginx/sites/app.conf.example 成 coolapp.conf 和 coolapp2.conf
#   WORKSPACE_INSTALL_XDEBUG=false
WORKSPACE_INSTALL_XDEBUG=true
#   PHP_FPM_INSTALL_XDEBUG=false
PHP_FPM_INSTALL_XDEBUG=true  # workspace 和 php-fpm容器都裝XDebug
#   NGINX_HOST_HTTP_PORT=80
NGINX_HOST_HTTP_PORT=5566


coolapp.conf

root /var/www/app;  => root /var/www/coolapp/public;

coolapp2.conf

server_name app.test; => server_name app2.test;
root /var/www/app;  => root /var/www/coolapp2/public;

windows設hosts,重啟laradock(down 和 up),然後 http://app.test:5566/ 訪問coolapp,http://app2.test:5566/ 訪問 coolapp2

更新laradock

git pull 更新laradock後重新build會報錯,因為env-example 已經被改過了,加了一些配置
先備份自己的.env(要記得你改了什麼),然後cp env-example .env 然後再把你原本在.env改的配置改到新的.env。這樣build才會成功

php安裝memcache

修改.env
#   PHP_FPM_INSTALL_MEMCACHED=false
PHP_FPM_INSTALL_MEMCACHED=true

需重新build php-fpm容器出錯

# docker-compose build php-fpm
...
E: Failed to fetch http://deb.debian.org/debian/pool/main/c/cups/libcupsimage2_2.2.10-6+deb10u1_amd64.deb  404  Not Found [IP: 151.101.10.133 80]
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
ERROR: Service 'php-fpm' failed to build: The command '/bin/sh -c if [ ${INSTALL_IMAGEMAGICK} = true ]; then     apt-get install -y libmagickwand-dev imagemagick &&     pecl install imagick &&     docker-php-ext-enable imagick ;fi' returned a non-zero code: 100
build 失敗
https://github.com/laradock/laradock/issues/2434
https://blog.51cto.com/beijing0414/1633956 
原來是hyper-v 系統的時間錯誤(當天應為4/8)
[root@localhost laradock]# date
Mon Apr  6 00:06:11 -02 2020
解法:
先安裝ntpdate
# yum install ntpdate
[root@localhost laradock]# date
Mon Apr  6 00:36:28 -02 2020
[root@localhost laradock]# ntpdate cn.pool.ntp.org
 8 Apr 13:07:13 ntpdate[14154]: step time server 119.28.206.193 offset 217834.861195 sec
[root@localhost laradock]# date
Wed Apr  8 13:07:15 -02 2020

重新build php-fpm(不吃緩存,比較慢)
# docker-compose build --no-cache php-fpm
# docker-compose up --build  // 不要不指定service,否則會全部build

這樣phpinfo()和命令行php -m 才有memcached

安裝docker-machine (無使用)

# base=https://github.com/docker/machine/releases/download/v0.16.0 &&
  curl -L $base/docker-machine-$(uname -s)-$(uname -m) >/tmp/docker-machine &&
  sudo mv /tmp/docker-machine /usr/local/bin/docker-machine &&
  chmod +x /usr/local/bin/docker-machine

檢視配置檔案位於哪裡

https://codertw.com/%E4%BC%BA%E6%9C%8D%E5%99%A8/160314/
$ systemctl show --property=FragmentPath docker
FragmentPath=/usr/lib/systemd/system/docker.service

開啟docker API

https://gist.github.com/styblope/dc55e0ad2a9848f2cc3307d4819d819f
新增文件:/etc/docker/daemon.json
{"hosts": ["tcp://0.0.0.0:2375", "unix:///var/run/docker.sock"]}
新增文件:/etc/systemd/system/docker.service.d/override.conf
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd
重新加載 systemd daemon
# systemctl daemon-reload
重啟docker
# systemctl restart docker.service
在機器上測試
$ curl http://localhost:2375
{"message":"page not found"}
但是windows上打不開 => server要打開 2375 端口
# firewall-cmd --zone=public --add-port=2375/tcp --permanent
# firewall-cmd --reload
然後在Windows 瀏覽器上直接打開 http://192.168.1.9:2375/info 顯示json即成功

忽然進不了XDebug斷點

commit ab4e06f270ae83d6e41702b2b652a980b12816ce
Author: Jordy Schreuders <3071062+99linesofcode@users.noreply.github.com>
Date:   Fri Feb 14 15:15:04 2020 +0100

    Default to host.docker.internal

因為更新重新build php-fpm後這個commit把 xdebug.remote_connect_back 改成0了
修改 php-fpm/xdebug.ini 、 workspace/xdebug.ini 將 xdebug.remote_connect_back 改回1,然後重新build
# docker-compose build php-fpm
ps. 重新build 時間比較久,改 xdebug.ini 後沒法像  nginx/sites/default.conf 只需要重啟laradock就能生效,必須build

進不了XDebug斷點-2

新hyper-v虛擬機上裝的laradock,在windows本機不要用同一個PHPStorm 專案改deployment去連,會一直進不了斷點,開一個新project後重新設deployment,請求接口和cli時會自動匹配,PHP CLI無需設docker。不要在同一個PHPStorm projcet上折騰。
實體開發機裝了兩個PHP(php 5.5 + 7.3),上面的laradock http能進xdebug,但是cli不行 => php7.3改用hyper-v的laradock去斷點

進不了XDebug斷點-3

如果laradock中 http請求可以斷點,但是CLI不能斷點。別折騰了,直接在hyper-v上面裝PHP去做CLI斷點,不一定要在laradock中斷點。
檢查:
使用-d 自定義INI條目
$ php -dxdebug.remote_enable=1 -dxdebug.remote_mode=req -dxdebug.remote_port=9000 -dxdebug.remote_host=192.168.1.7 -dxdebug.remote_connect_back=0 -dxdebug.idekey=PHPSTORM public/index.php

要注意 xdebug.remote_enable=1 是否開啟

其實是 laradock 的 workspace 容器的xdebug 版本是3.0.0這個原因造成的,workspace和php-fpm容器裝的是不同的xdebug,workspace負責cli,php-fpm負責網頁,所以網頁上的phpinfo()查到的是是php-fpm的xdebug版本,而workspace的xdebug版本要在容器裡cli執行,如:
root@e7aa5f3bd006:/var/www# php -r 'phpinfo();' | grep Xdebug
    with Xdebug v2.5.5, Copyright (c) 2002-2017, by Derick Rethans
需要改 workspace/Dockerfile 的 xDebug 部分,讓workspcace 容器安裝低版本的xdebug

laradock中websocket 端口對外

https://github.com/laradock/laradock/issues/2002#issuecomment-468884705
編輯 docker-compose.yml 加入
services:
  workspace:
    port:
      - 2346:2346
然後重啟laradock



和gitlab端口衝突

目前已知 8080 和3000端口會和gitlab衝突(gitlab已開啟)

解法:

https://github.com/laradock/laradock/issues/2512#issuecomment-589719158  Bind for 0.0.0.0:8080 failed: port is already allocated

換一個端口,修改.env
WORKSPACE_VUE_CLI_SERVE_HOST_PORT=8080 => 8084
...
WORKSPACE_BROWSERSYNC_HOST_PORT=3000 => 3002

安裝特定版本的xdebug

如果你的專案和xdebug最新版本(3.0.0+)有衝突
https://bugs.xdebug.org/view.php?id=1883  0001883: Function xdebug_is_enabled has been removed
XDebug 3.0.0beta1版本把 xdebug_is_enabled() 移除了
可以這樣做
diff --git a/php-fpm/Dockerfile b/php-fpm/Dockerfile
index 1837b45..a64bf63 100644
--- a/php-fpm/Dockerfile
+++ b/php-fpm/Dockerfile
@@ -193,7 +193,7 @@ RUN if [ ${INSTALL_XDEBUG} = true ]; then \
     if [ $(php -r "echo PHP_MINOR_VERSION;") = "0" ]; then \
       pecl install xdebug-2.9.0; \
     else \
-      pecl install xdebug; \
+      pecl install xdebug-2.9.8; \
     fi \
   fi && \
   docker-php-ext-enable xdebug \
ps. PHP_MINOR_VERSION就是PHP的中版本號,ex. php 7.3,PHP_MINOR_VERSION就是3

因為改了Dockerfile,所以要重新build,雖然有緩存,但還是有點久
$ docker-compose build php-fpm
...
Step 29/159 : RUN if [ ${INSTALL_XDEBUG} = true ]; then   if [ $(php -r "echo PHP_MAJOR_VERSION;") = "5" ]; then     pecl install xdebug-2.5.5;   else     if [ $(php -r "echo PHP_MINOR_VERSION;") = "0" ]; then       pecl install xdebug-2.9.0;     else       pecl install xdebug-2.9.8;     fi   fi &&   docker-php-ext-enable xdebug ;fi
 ---> Running in 999878a0db51
downloading xdebug-2.9.8.tgz ...
Starting to download xdebug-2.9.8.tgz (245,293 bytes)
...................................................done: 245,293 bytes
91 source files, building
running: phpize
Configuring for:
PHP Api Version:         20180731
Zend Module Api No:      20180731
Zend Extension Api No:   320180731
building in /tmp/pear/temp/pear-build-defaultusergNFJEA/xdebug-2.9.8
running: /tmp/pear/temp/xdebug/configure --with-php-config=/usr/local/bin/php-config
checking for grep that handles long lines and -e... /bin/grep

自定義workspace中的.bashrc

由 workspace/Dockerfile 
COPY ./aliases.sh /home/laradock/aliases.sh
...
USER laradock
RUN echo "" >> ~/.bashrc && \
    echo "# Load Custom Aliases" >> ~/.bashrc && \
    echo "source ~/aliases.sh" >> ~/.bashrc && \
      echo "" >> ~/.bashrc

修改 workspace/aliases.sh 可以客製化容器中laradock用戶的.bashrc
然後需重新build workspace容器
# docker-compose build workspace

Dockerfile

以安裝 php:5.6-apache 為例
https://hub.docker.com/_/php

新建 Dockerfile 

內容為
FROM php:5.6-apache
COPY src/ /var/www/html/
新增目錄src 和 文件src/index.php

build Docker image

# docker build -t my-php-app .
Step 1/2 : FROM php:5.6-apache
5.6-apache: Pulling from library/php
...
他會從線上把 php:5.6-apache 拉下來,用下面方法查看
# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
my-php-app              latest              10cc9a5aa0c2        25 seconds ago      355MB
...
php                     5.6-apache          24c791995c1e        14 months ago       355MB

刪除dangling的images(部份<none>TAG的image)

https://stackoverflow.com/a/33913711
因為每次都build同一個image名字時,前一個同名的image的REPOSITORY和TAG會變成<none>
# docker rmi $(docker images --filter "dangling=true" -q --no-trunc)


啟動容器

# docker run -d --name my-running-app -p 5656:80 -v /root/php5.6-apache/src:/var/www/html my-php-app:latest

--name my-running-app => 指定容器的名字
-p 5656:80 => 容器中的80端口對應到host的5656端口,http://192.168.1.9:5656/ 可訪問容器中的web伺服器
-v /root/php5.6-apache/src:/var/www/html => host的/root/php5.6-apache/src對應到容器中的/var/www/html,直接改host目錄的文件容器中文件就會改動

進入容器

# docker exec -it my-running-app bash
用 -d 啟動後再用此目錄進容器,否則容器內的apache不會啟動

停止容器

# docker stop my-running-app

刪除 Exited 的容器

# docker rm my-running-app

進階使用

Dockerfile
FROM php:5.6-apache
COPY src/ /var/www/html/
RUN docker-php-ext-install mysqli \
    && apt-get update \
    && apt-get install -y libfreetype6-dev libjpeg62-turbo-dev \
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install gd \
    && pecl install xdebug-2.5.5 && docker-php-ext-enable xdebug \
    && echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" >> /usr/local/etc/php/php.ini  \
    && echo "xdebug.remote_enable=1" >> /usr/local/etc/php/php.ini \
    && echo "xdebug.remote_connect_back=1" >> /usr/local/etc/php/php.ini \
    && echo "xdebug.idekey=PHPSTORM" >> /usr/local/etc/php/php.ini \
    && echo "xdebug.remote_log=/tmp/xdebug.log" >> /usr/local/etc/php/php.ini

註:
安裝mysqli
安裝gd函式庫
安裝xdebug並配置


使用docker安裝beanstalk console

github上推薦用composer安裝,但是如果你的機器沒有php和composer,你可以這樣做。直接拉取github上專案
$ git clone https://github.com/ptrofimov/beanstalk_console.git
$ cd beanstalk_console/
$ docker build --rm -t beanstalk_console .
$ docker run -d -p "8899:80" --name beanstalk_console beanstalk_console

然後瀏覽器打開 http://x.x.x.x:8899  即可顯示 Beanstalk console

在容器內測端口

https://stackoverflow.com/a/2226759  Test if port open and forwarded using PHP
php-fpm容器內可能沒有telnet,可以用PHP去測端口通不通
$host = 'stackoverflow.com';
$ports = array(21, 25, 80, 81, 110, 443, 3306);

foreach ($ports as $port)
{
    $connection = @fsockopen($host, $port);

    if (is_resource($connection))
    {
        echo '<h2>' . $host . ':' . $port . ' ' . '(' . getservbyport($port, 'tcp') . ') is open.</h2>' . "\n";

        fclose($connection);
    }

    else
    {
        echo '<h2>' . $host . ':' . $port . ' is not responding.</h2>' . "\n";
    }
}
輸出:
stackoverflow.com:21 is not responding.
stackoverflow.com:25 is not responding.
stackoverflow.com:80 (http) is open.
stackoverflow.com:81 is not responding.
stackoverflow.com:110 is not responding.
stackoverflow.com:443 is not responding.
stackoverflow.com:3306 is not responding.

如果端口忽然不通可以嘗試重啟docker

參考資料:

https://blog.gtwang.org/linux/centos-linux-7-install-docker-tutorial/  CentOS Linux 7 安裝 Docker 步驟與使用教學
https://github.com/NaturalHistoryMuseum/scratchpads2/wiki/Install-Docker-and-Docker-Compose-(Centos-7)   Install Docker and Docker Compose (Centos 7)





2015年10月20日 星期二

在Windows 7 使用docker

下載安裝 docker-install.exe , 如遇上這個錯誤:
https://github.com/boot2docker/windows-installer/issues/67
則移除VirtualBox 後重裝 VirtualBox 4.3.12 版本,重啟boot2docker-vm

中間為了讓virtualbox 與Win7共用資料夾 ( 失敗 )
http://askubuntu.com/questions/456400/why-cant-i-access-a-shared-folder-from-within-my-virtualbox-machine
必須從 C:\Program Files\Oracle\VirtualBox\VBoxGuestAdditions.iso 安裝 Guest Additions
在開機前先設定掛載的光碟
然後設定共享文件夾
開機進入VM後
1. I had to manually mount the CD:
$ sudo mount /dev/cdrom /media/cdrom
掛載之後 /media/cdrom 下面就有光碟內的東西了
/media/cdrom# ls
32Bit  AUTORUN.INF  cert  runasroot.sh            VBoxSolarisAdditions.pkg        VBoxWindowsAdditions.exe
64Bit  autorun.sh   OS2   VBoxLinuxAdditions.run  VBoxWindowsAdditions-amd64.exe  VBoxWindowsAdditions-x86.exe

2. Install the necessary packages:
$ sudo apt-get install make gcc linux-headers-$(uname -r)
3. Install the Guest Additions:
$ sudo /media/cdrom/VBoxLinuxAdditions.run
中間報錯
...
The headers for the current running kernel were not found. If the following
module compilation fails then this could be the reason.

Building the main Guest Additions module ...done.
Building the shared folder support module ...fail!
(Look at /var/log/vboxadd-install.log to find out what went wrong)
...
Setting up the Window System to use the Guest Additions ...done.
You may need to restart the hal service and the Window System (or just restart
the guest system) to enable the Guest Additions.

Installing graphics libraries and desktop services components ...done.

嘗試mount共享資料夾
$ mkdir new
$ sudo mount -t vboxsf New ~/new
/sbin/mount.vboxsf: mounting failed with the error: No such device

嘗試用其他方法解
http://stackoverflow.com/questions/28328775/virtualbox-mount-vboxsf-mounting-failed-with-the-error-no-such-device
# cd /opt/VBoxGuestAdditions-4.3.12/init/
# ./vboxadd setup
與跑 VBoxLinuxAdditions.run 一樣的錯誤

嘗試解決 Error: kernel headers not found.的問題
http://askubuntu.com/questions/98416/error-kernel-headers-not-found-but-they-are-in-place
# apt-get install xserver-xorg xserver-xorg-core
安裝了以下東西
The following NEW packages will be installed:
  libmtdev1 libxi6 libxinerama1 xserver-xorg xserver-xorg-input-all
  xserver-xorg-input-evdev xserver-xorg-input-mouse
  xserver-xorg-input-synaptics xserver-xorg-input-vmmouse
  xserver-xorg-input-wacom
0 upgraded, 10 newly installed, 0 to remove and 1 not upgraded.

嘗試解決The headers for the current running kernel were not found.
http://linuxconfig.org/ubuntu-the-headers-for-the-current-running-kernel-were-not-found-solution
# apt-get install linux-headers-`uname -r` dkms build-essential
報錯:
...
The following packages have unmet dependencies:
 build-essential : Depends: libc6-dev but it is not going to be installed or
                            libc-dev
                   Depends: g++ (>= 4:4.4.3) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

原因:
# apt-get install libc6-dev
...
libc6-dev : Depends: libc6 (= 2.19-0ubuntu6) but 2.19-0ubuntu6.5 is to be installed
E: Unable to correct problems, you have held broken packages.
# apt-show-versions libc6
libc6:amd64 2.19-0ubuntu6.5 newer than version in archive
看來是libc6 太新了
切勿移除libc6重裝,剛剛VM就這樣被我弄掛的。還好昨天剛好有備份

嘗試更新Virtual Box到最新版 4.3.30
更新到最新版要執行原本的虛擬電腦時報錯:
unable to load r3 module vboxdd.dll (vboxdd) getlasterror=1790 (verr_unresolved_error)
嘗試過這篇 http://blog.sina.com.cn/s/blog_4dc988240102vj8a.html 但無效
只好把Virtual Box再還原回4.3.12版


2015年7月9日 星期四

docker centos:7 安裝LNMP

啟動docker
docker run -t -i -p 5566:80 xxx/centos7:latest bash

安裝Nginx 

新增CentOS 7 的Ngnix yum repository ,因為Ngnix 不直接由CentOS提供
$ sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
ps. 如果是CentOS 6 則安裝 epel repository ( CentOS )
$ sudo yum install epel-release
安裝ngnix
$ sudo yum install nginx

啟動php-fpm
# php-fpm

啟動ngnix
因為CentOS 7已不使用service啟動服務,而使用systemctl start <service> , 且使用systemctl 啟動服務時會報錯
# systemctl restart nginx
Failed to get D-Bus connection: No connection to service manager.
原因:
https://github.com/docker/docker/issues/7459#issuecomment-52327775
大意是說:
在container內必須使用程式本身的管理工具去啟動。systemctl不會作用。
ex.
啟動nginx
# nginx
停止nginx
# nginx -s stop
啟動mysql(MariaDB)
切換成mysql使用者
# su mysql
This account is currently not available.
解決mysql使用者不能使用
# vim /etc/passwd
mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin  =>
mysql:x:27:27:MariaDB Server:/var/lib/mysql:/bin/bash
重新登陸
# su mysql
啟動mysql
$ cd /usr/libexec/
$ ./mysqld
報錯:
...
151023 10:27:11 [ERROR] mysqld: File '/var/lib/mysql/aria_log_control' not found (Errcode: 13)
原來是aria_log_control這隻檔案有問題
# ls /var/lib/mysql/aria_log_control -l
-rw-rw---- 1 root root 52 Jul  8 10:33 /var/lib/mysql/aria_log_control
把它刪掉
# rm /var/lib/mysql/aria_log_control

再度重啟mysql
$ ./mysqld
報錯:
...
151023 10:36:27 [ERROR] mysqld: Can't create/write to file '/var/lib/mysql/aria_log.00000001' (Errcode: 13)
一樣是權限問題
# ls /var/lib/mysql/aria_log.00000001 -l
-rw-rw----  1 root  root     16384 Jul  8 10:33 aria_log.00000001
更變權限
# cd /var/lib/
# chown mysql:mysql -R mysql/
啟動mysql(成功)
$ ./mysqld

停止mysql
# mysqladmin shutdown

MySQL停止的方法
http://stackoverflow.com/questions/11091414/how-to-stop-mysqld
$ /usr/local/mysql/bin/mysqladmin -u root -p shutdown
或
$ sudo mysqld stop
或
$ sudo /usr/local/mysql/bin/mysqld stop
或
$ sudo mysql.server stop

php頁面打不開(連結變成下載php檔案) [ 與 nginx vhost配置 類似問題 ]
在 /etc/nginx/conf.d/default.conf 中server{}區塊解除註解這一段,並修改
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
    #root           html;
    root /usr/share/nginx/html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}
如果上面那段root和fastcgi_param 維持原值任一一個沒改,頁面會出現File not found,nginx error log會報下面錯誤
2015/11/16 09:08:00 [error] 941#0: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 192.168.x.x, server: localhost, request: "GET /info.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "192.168.x.x:5566"


請勿解除註解下面這一段
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
#    proxy_pass   http://127.0.0.1;
#}
不然會報錯
2015/11/16 08:22:55 [alert] 837#0: 1024 worker_connections are not enough
2015/11/16 08:22:55 [error] 837#0: *1022 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /info.php HTTP/1.0", upstream: "http://127.0.0.1:80/info.php", host: "127.0.0.1"


最後瀏覽器打開
http://192.168.x.x:5566/
就能訪問你docker上的php程式了




參考資料:
https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-centos-7  # How To Install Linux, Nginx, MySQL, PHP (LEMP) stack On CentOS 7
https://bbs.archlinux.org/viewtopic.php?id=160277 [SOLVED] Cannot start mysqld after switching to MariaDB
https://mariadb.com/kb/en/mariadb/problem-while-starting-mariadb-server/ problem while starting mariadb server?
http://www.ahlinux.com/nginx/4936.html  nginx fastcgi错误primary script unknown如何解决
http://serverfault.com/questions/406158/nginx-php5-fpm-file-not-found  Nginx + php5-fpm = “File not found”






2015年7月8日 星期三

ubuntu 安裝docker

系統 ubuntu 14.04

透過系統內建套件安裝
$ sudo apt-get update
$ sudo apt-get install -y docker.io
$ sudo ln -sf /usr/bin/docker.io /usr/local/bin/docker
$ sudo sed -i '$acomplete -F _docker docker' /etc/bash_completion.d/docker.io

透過Docker 套件庫安裝最新版本
$ sudo apt-get install apt-transport-https
$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 36A1D7869245C8950F966E92D8576A8BA88D21E9
$ sudo bash -c "echo deb https://get.docker.io/ubuntu docker main > /etc/apt/sources.list.d/docker.list"
$ sudo apt-get update
$ sudo apt-get install -y lxc-docker

快速安裝法(建議使用)
$ curl -sSL https://get.docker.com/ubuntu/ | sudo sh

ps. 我是使用上面比較麻煩的方法安裝的

參考資料:
http://philipzheng.gitbooks.io/docker_practice/content/install/ubuntu.html

2015年2月16日 星期一

MySQL權限管理

在連docker container ( IP: 172.17.0.2 )的mysql時發現抓不到newdatabase的資料
mysql.php:
mysql_connect("172.17.0.2", "root", "password");
mysql_select_db("newdatabase");

$result = mysql_query("SELECT database();");

while ($row = mysql_fetch_array($result,  MYSQL_ASSOC)) {
    echo $row['database()'];
}

mysql_free_result($result);
結果:
$ php mysql.php
(空)

連進去檢查( 注意,下面每次更改權限後,必須重新登錄才有效 )
$ mysql -u root -p -h 172.17.0.2
然後發現找不到table ( 但是在172.17.0.2上直接連正常 )
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
+--------------------+

檢查權限
mysql> SHOW GRANTS FOR 'root'@'localhost';
+--------------------------------------------------------------------------------------------------+
| Grants for root@localhost                                                                        |
+--------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD 'xxx' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION                                     |
+--------------------------------------------------------------------------------------------------+
mysql> SHOW GRANTS FOR 'root'@'%';
+----------------------------------------------------------------+
| Grants for root@%                                              |
+----------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'root'@'%' IDENTIFIED BY PASSWORD '*xxx' |
+----------------------------------------------------------------+

原來是newdatabase表沒開放權限給其他ip

開放(GRANT)權限
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' ;
這樣Grants for root@% 就會變成:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY PASSWORD '*xxx'

亦可以通過特定的權限
mysql> GRANT SELECT, INSERT, UPDATE, DELETE ON *.* TO 'root'@'%';

移除權限
mysql> REVOKE ALL PRIVILEGES ON *.* FROM 'root'@'%' ;
這樣Grants for root@% 就會變成:
GRANT USAGE ON *.* TO 'root'@'%' IDENTIFIED BY PASSWORD '*xxx'

秀出系統現在有哪些 user
mysql> SELECT User,Host FROM mysql.user;

參考資料:
http://blog.longwin.com.tw/2009/06/query-mysql-show-grant-permission-2009/


2015年2月9日 星期一

docker 指令自動完成

將
https://github.com/docker/docker/blob/master/contrib/completion/bash/docker (採用)
或
https://gist.github.com/felixbuenemann/5605854
的內容複製貼上到
/etc/bash_completion.d/docker 
然後重新登錄

或( 於上面第一個網址內容註解中的教學 )
#  - copy this file to e.g. ~/.docker-completion.sh and add the line
#    below to your .bashrc after bash completion features are loaded
#    . ~/.docker-completion.sh

Docker's process的attach 和 detach
detach - Ctrl-p  + Ctrl-q
attach - docker attach -sig-proxy=false container_id ( ex. docker attach -sig-proxy=false 304f5db405ec )

參考資料:
http://stackoverflow.com/questions/19688314/how-do-you-attach-and-detach-from-dockers-process

docker 使用心得


查docker資訊

# docker info

下載archlinux 的docker images
# docker pull base/archlinux

查有哪些images
# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
base/archlinux      latest              dce0559daa1b        6 months ago        282.9 MB

執行容器
# docker run -t -i base/archlinux:latest bash
[root@b42626ce6a25 /]#
進入容器後,因為root家目錄是/,直接設定HOME環境變數即可 。相關issue:https://github.com/docker/docker/issues/2968
[root@b42626ce6a25 /]# HOME=/root

查docker的環境變數
# docker run -i -t bear-test env|grep HOME
HOME=/

修改Arch的 Mirrors
編輯:/etc/pacman.d/mirrorlist
去 https://www.archlinux.org/mirrorlist/ 選擇你的國家,直接貼上產生的mirrorlist,然後更新
# pacman -Sy

安裝wget
# pacman -S wget

保存目前的容器更變

在主機系統上開一個新的終端機
查看目前正在運行的容器狀態
# docker ps   # -a tag: Show all containers.不加只會顯示在跑的containers
CONTAINER ID        IMAGE                   COMMAND             CREATED             STATUS              PORTS               NAMES
b42626ce6a25        base/archlinux:latest   "bash"              17 minutes ago      Up 17 minutes                           elated_fermi

commit更變
# docker commit -m "Add wget!" -a "Bear" b42626ce6a25 bear-test

查commit messages
# docker inspect image_id | grep Comment
ex. 
# docker inspect d98d54bb671a | grep Comment
   "Comment": "Add wget!",

docker logs
# docker logs b42626ce6a25
( 會顯示你的console 畫面的紀錄 )

查docker comment message
# docker images --no-trunc -a | awk '{print $3}' | xargs docker inspect | less

保存到https://hub.docker.com ( 需先去該docker hub註冊帳號 )
# docker login 
Username: username
Password: 
Email: your@email
Login Succeeded

push
# docker push bear-test
FATA[0000] You cannot push a "root" repository. Please rename your repository in <user>/<repo> (ex: kalecgos0616/bear-test)
所以當初在命名repository名字時,要以<user>/<repo> 的格式命名

複製且重新命名docker images
# docker tag image_id username/repository_name
ex.
# docker tag d98d54bb671a kalecgos0616/bear-test

再次push到docker hub
# docker push kalecgos0616/bear-test
The push refers to a repository [kalecgos0616/bear-test] (len: 1)
Sending image list
Pushing repository kalecgos0616/bear-test (1 tags)
511136ea3c5a: Image already pushed, skipping 
9b0516337e5a: Image already pushed, skipping 
dce0559daa1b: Image already pushed, skipping 
d98d54bb671a: Image successfully pushed 
Pushing tag for rev [d98d54bb671a] on {https://cdn-registry-1.docker.io/v1/repositories/kalecgos0616/bear-test/tags/latest}

這時候你上docker hub ( https://hub.docker.com/u/kalecgos0616/ ),就會看到你剛剛推上去的docker Repository

移除images
# docker rmi base/archlinux
如果報錯
# docker rmi bear-test    
Error response from daemon: Conflict, cannot delete debdf1ba8b30 because the container 113b1d60ffe1 is using it, use -f to force
FATA[0000] Error: failed to remove one or more images
則使用-f 強制刪除
# docker rmi -f bear-test

pull docker hub上的repository
# docker pull kalecgos0616/bear-test

容器使用host的ip和port
# docker run -t -i -p <hostPort>:<containerPort> REPOSITORY:TAG bash
例:
# docker run -t -i -p 5566:80 base/archlinux:latest bash

將容器的80 port 發布到 host的5566 port,假設host 的ip 是 192.168.1.2 ,即可以 http://192.168.1.2:5566 訪問容器的網頁伺服器


centos:6.6 安裝執行screen會報錯
Cannot access '/7': No such file or directory
解法:
改使用centos:latest ( CentOS 7)

下載官方的repos
去 https://registry.hub.docker.com/repos/library/ 找你要下載的repos,假設要下載ubuntu最新版
# docker pull ubuntu
如要下載centos 6.6(Tag)版,可以指定Tags
# docker pull centos:6.6

參考資料:
https://wiki.archlinux.org/index.php/Docker
http://www.codedata.com.tw/social-coding/docker-layman-abc/ # 門外漢的 Docker 小試身手
http://stackoverflow.com/questions/19035358/how-to-copy-and-rename-a-docker-container # 複製且重新命名docker container

2015年2月5日 星期四

Arch 安裝docker

安裝docker ( 必須是64位元電腦,用 file /bin/bash 檢查 )
# pacman -S docker

運行docker
# systemctl start docker

檢查運行狀態
# systemctl status docker

如果運行失敗,則將依賴的包,安裝更新成最新板:
bridge-utils
device-mapper
iproute2
lxc
sqlite

設置網路環境
IPv4数据包转发在缺省值的情况下是禁用的,所以容器可能是不起作用的,
需要开启转发,作为root用户运行在主机系统上
# sysctl net.ipv4.ip_forward=1
允许IPv4修改主机的/etc/sysctl.d/docker.conf,需要重新启动:
net.ipv4.ip_forward=1

參考資料:
https://github.com/widuu/docker_course/blob/master/install/docker-arch.md