Docker的常规使用方法是什么

51次阅读
没有评论

共计 4657 个字符,预计需要花费 12 分钟才能阅读完成。

本篇内容介绍了“Docker 的常规使用方法是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让丸趣 TV 小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

7. 进阶

本节我们以运行 Gitlab 实例为例,进一步介绍 Docker 的常规使用。

root@ubuntu:~# docker pull redis   7.1. 获取 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
root@7c8110b8151e:/# exit
exit

exec  表示进入一个运行中的容器
MySQL 中执行的语句见: https://hub.docker.com/r/sameersbn/gitlab/#external-mysql-server7.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

如果使用 MySQL 数据库一定要加上 -e DB_TYPE=mysql 否则这个容器会以默认的 Postgre 类型去连接导致无法启动
–dns  用于指定容器的 DNS,我们可以指定一个公共 DNS 以实现不同容器互 PING
Docker 默认情况下容器间是不能互访的,要实现交互主要有两种途径,一是用 –link 这也是 Docker 推荐的做法,它会在容器中建立被 Link 容器的 Host 记录实现单向访问,另一种是开放服务实现调用,对于上述案例,Redis 使用的是 Link 方式,MySQL 使用是开放服务方式。

访问一下:http://192.168.4.99:10080 (192.168.4.99 是我宿主机的 IP),等等,为什么访问不了?

7.6. 查看运行日志

在出问题时我们首先想到的是   去看看日志  ,Docker 容器运行日志的查看如下:

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

提示是需要 GITLAB_SECRETS_DB_KEY_BASE 变量,查看官方说明:

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.

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

好的,我们用 pwgen -Bsv1 64 生成一个密钥

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

“Docker 的常规使用方法是什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注丸趣 TV 网站,丸趣 TV 小编将为大家输出更多高质量的实用文章!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-08-16发表,共计4657字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)