Docker入門與實戰系列:進階

Docker入門與實戰——《Docker ABC》電子書 https://github.com/gudaoxuri/Docker_ABCmysql

7. 進階

本節咱們以運行Gitlab實例爲例,進一步介紹Docker的常規使用。git

上節MySQL是獨立的鏡像,沒有依賴,但Gitlab會依賴數據庫及緩存,咱們看看容器間如何交互。Gitlab有官方鏡像,但我更喜歡sameersbn的版本,這個版本介紹詳細,而且做者一直在持續更新。

7.1. 獲取Redis鏡像並運行

root@ubuntu:~# docker pull redis   
...  
root@ubuntu:~# docker run --name gitlab-cache -d redis   
a2c183f16cee0aa30eca8f27875d3770ac1291fc22b218d4b5e673ce604074c8

7.2. 獲取MySQL鏡像並運行

root@ubuntu:~# mkdir -p /opt/test/gitlab/db/   <1>
root@ubuntu:~# docker pull mysql...  
root@ubuntu:~# docker run --name gitlab-db -e MYSQL_ROOT_PASSWORD=123456 -d -p 3307:3306 -v /opt/test/gitlab/db:/var/lib/mysql mysql:5.7   
7c8110b8151e2ddbc67d2bb1d96a9cc24ef68546a885d065e78bd274775ec7f7
<1> 建立數據庫目錄映射

7.3. 建立相應的數據庫及權限

root@ubuntu:~# docker exec -it gitlab-db /bin/bash
root@7c8110b8151e:/# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.9 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> CREATE USER 'gitlab'@'%.%.%.%' IDENTIFIED BY 'password';
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE DATABASE IF NOT EXISTS `gitlabhq_production` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;
Query OK, 1 row affected (0.00 sec)

mysql> GRANT ALL PRIVILEGES ON `gitlabhq_production`.* TO 'gitlab'@'%.%.%.%';
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye
root@7c8110b8151e:/# exit
exit
exec 表示進入一個運行中的容器
MySQL中執行的語句見: https://hub.docker.com/r/sameersbn/gitlab/#external-mysql-server

7.4. 獲取Gitlab鏡像

root@ubuntu:~# mkdir -p /opt/test/gitlab/data  <1> 
root@ubuntu:~# docker pull sameersbn/gitlab
<1> 建立GIT數據目錄映射

7.5. 運行Gitlab鏡像

root@ubuntu:~# docker run --name=gitlab -d \
 -e GITLAB_PORT=10080 \ <1>
 -e GITLAB_SSH_PORT=10022 \ <2>
 -e GITLAB_HOST=inner.ecfront.com \ <3>
 -e DB_TYPE=mysql \ <4>
 -e DB_HOST=192.168.4.99 \
 -e DB_PORT=3307 \
 -e DB_NAME=gitlabhq_production \
 -e DB_USER=gitlab \
 -e DB_PASS=password \
 -e GITLAB_EMAIL=git@ecfront.com \ <5>
 -e SMTP_ENABLED=true \
 -e SMTP_DOMAIN=ecfront.com \
 -e SMTP_HOST=smtp.exmail.qq.com \
 -e SMTP_PORT=25 \
 -e SMTP_USER=git@ecfront.com \
 -e SMTP_PASS=xxx \
 -e SMTP_AUTHENTICATION=login \
 -e NGINX_MAX_UPLOAD_SIZE=512m \ <6>
 -e GITLAB_BACKUPS=monthly \ <7>
 -p 10022:22 \ <8>
 -p 10080:80 \ <9>
 -v /opt/test/gitlab/data:/home/git/data \ <10> 
 --link gitlab-cache:redisio \ <11>
 --dns=192.168.4.99 \ <12>
 sameersbn/gitlab

<1> HTTP服務端口
<2> SSH請求端口
<3> 服務域名
<4> 數據庫鏈接信息
<5> Email信息
<6> 解決`RPC failed; result=22, HTTP code = 413`問題,見 http://stackoverflow.com/questions/7489813/github-push-error-rpc-failed-result-22-http-code-413
<7> 備份策略,天天
<8> SSH請求端口映射
<9> HTTP請求端口映射
<10> GIT數據目錄映射
<11> 鏈接Reids容器,實現緩存
<12> 配置DNS
github

若是使用MySQL數據庫必定要加上-e DB_TYPE=mysql不然這個容器會以默認的Postgre類型去鏈接致使沒法啓動
--dns 用於指定容器的DNS,咱們能夠指定一個公共DNS以實現不一樣容器互PING
Docker默認狀況下容器間是不能互訪的,要實現交互主要有兩種途徑,一是用--link這也是Docker推薦的作法,它會在容器中創建被Link容器的Host記錄實現單向訪問,另外一種是開放服務實現調用,對於上述案例,Redis使用的是Link方式,MySQL使用是開放服務方式。

7.6. 查看運行日誌

  • 在出問題時咱們首先想到的是 去看看日誌 ,Docker容器運行日誌的查看以下:sql

root@ubuntu:~# docker logs gitlab  
   ERROR:   Please configure the GITLAB_SECRETS_DB_KEY_BASE parameter. 
   Cannot continue. Aborting...

提示是須要GITLAB_SECRETS_DB_KEY_BASE變量,查看官方說明:docker

Note: Since GitLab 8.0.0 you need to provide the GITLAB_SECRETS_DB_KEY_BASE parameter while starting the image.數據庫

Tip: You can generate a random string using pwgen -Bsv1 64 and assign it as the value ofGITLAB_SECRETS_DB_KEY_BASE.ubuntu

— https://hub.docker.com/r/sameersbn/gitlab/緩存

  • 好的,咱們用pwgen -Bsv1 64生成一個密鑰bash

root@ubuntu:~# pwgen -Bsv1 64  
7hpTqCXgf4tVbnFmdC7PNn9n4hWmCnvF479fsJtcdTkhmVfWfzpwTJ4sNRzNkkXf
  • GITLAB_SECRETS_DB_KEY_BASE加上從新運行

root@ubuntu:~# docker rm -f gitlab
root@ubuntu:~# docker run --name=gitlab -d \
 -e GITLAB_PORT=10080 \
 -e GITLAB_SSH_PORT=10022 \
 -e GITLAB_HOST=inner.ecfront.com \
 -e DB_TYPE=mysql \
 -e DB_HOST=192.168.4.99 \
 -e DB_PORT=3307 \
 -e DB_NAME=gitlabhq_production \
 -e DB_USER=gitlab \
 -e DB_PASS=password \
 -e GITLAB_SECRETS_DB_KEY_BASE=7hpTqCXgf4tVbnFmdC7PNn9n4hWmCnvF479fsJtcdTkhmVfWfzpwTJ4sNRzNkkXf \
 -e GITLAB_EMAIL=git@ecfront.com \
 -e SMTP_ENABLED=true \
 -e SMTP_DOMAIN=ecfront.com \
 -e SMTP_HOST=smtp.exmail.qq.com \
 -e SMTP_PORT=25 \
 -e SMTP_USER=git@ecfront.com \
 -e SMTP_PASS=xxx \
 -e SMTP_AUTHENTICATION=login \
 -e NGINX_MAX_UPLOAD_SIZE=512m \
 -e GITLAB_BACKUPS=monthly \
 -p 10022:22 \
 -p 10080:80 \
 -v /opt/test/gitlab/data:/home/git/data \
 --link gitlab-cache:redisio \
 --dns=192.168.4.99 \
 sameersbn/gitlab

7.7. 完成

  • 好了,第一次要作的事情比較多,過一會就能夠打開頁面了

用戶名:root 密碼:5iveL!fe
相關文章
相關標籤/搜索