在docker中跑nginx,gogs,mysql服务

操作方式(以nginx为例)

下载加速

echo "OPTIONS='--registry-mirror=https://mirror.ccs.tencentyun.com'" >> /etc/sysconfig/docker systemctl daemon-reload service docker restart

搜索

docker search nginx

下载

docker pull docker.io/nginx

运行

docker run -it -d -p 80:80 --name nginx docker.io/nginx /bin/bash

重启

docker restart nginx

进入

docker exec -it nginx /bin/sh

退出

ctl+P ctl+Q

停止

docker stop nginx

启动

docker start nginx

删除

docker rm nginx

列表

docker ps -a

日志

docker logs nginx

centos 7关闭防火墙

停止firewall

systemctl stop firewalld.service

禁止firewall开机启动

systemctl disable firewalld.service

安装mysql

https://www.jianshu.com/p/5f5e419b5de8

创建网络

为了使多个docker之间通信,需要创建docker网络
docker network create backend 查看所有
docker network ls

下载镜像

参考上面的命令示例

存放数据文件

mkdir /data/mysql/data

存放配置文件

mkdir /data/mysql/conf

文件内容

/data/mysql/conf目录下建立3个mysql配置文件 docker.cnf, mysql.cnf, mysqldump.cnf

docker.cnf 内容

1
2
3
[mysqld]
skip-host-cache
skip-name-resolve

mysql.cnf内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[mysql]
default-character-set = utf8

[mysql_safe]
default-character-set = utf8

[client]
default-character-set = utf8

[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
#log-error = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

# 修改mysql默认字符集
init_connect = 'SET NAMES utf8'
character-set-server = utf8
collation-server = utf8_unicode_ci

mysqldump.cnf内容

1
2
3
4
[mysqldump]
quick
quote-names
max_allowed_packet = 16M

启动

1
2
3
4
5
docker run -d --name mysql --net=backend -p 3306:3306 \
-e MYSQL_ROOT_PASSWORD=root \
-v /data/mysql/data:/var/lib/mysql \
-v /data/mysql/conf:/etc/mysql/conf.d \
docker.io/mysql
  • 进入到mysql docker容器
    SHOW DATABASES;
    mysql -u root -p
    密码为: root
  • 创建一个库 CREATE DATABASE 库名;
  • 然后删除docker docker stop mysql docker rm mysql
  • 再次创建后查看是否存在库,存在则创建完成

安装gogs

https://www.jianshu.com/p/2a7acb07b352

创建目录

mkdir -p /data/gogs

启动

1
2
3
docker run -d -p 10022:10022 -p 10080:3000 --name=gogs --net=backend \
-v /data/gogs/:/data \
docker.io/gogs/gogs

NGINX

启动

1
2
3
4
5
6
7
docker run -d --name nginx -p 80:80 -p 443:443 --net=backend \
-v /data/nginx/nginx.conf:/etc/nginx/nginx.conf:ro \
-v /data/www:/www \
-v /data/nginx/conf.d:/etc/nginx/conf.d \
-v /data/nginx/keys:/etc/nginx/keys \
-v /data/nginx/err:/etc/nginx/err \
docker.io/nginx

配置方式

遇到一个坑,当转发端口时的配置方式为
proxy_pass http://gogs:3000;
网关名称:内部端口号

docker-compose

  • centos有pyhone2.7,但是没有pip,先安装一下,顺手再升级一下。
    yum install python-pip
    pip install --upgrade pip
  • 安装compose
    pip install docker-compose
  • 检查一下版本
    docker-compose version
  • 配置
    新建文件docker-compose.yml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    version: "3"
    services:

    mysql:
    image: docker.io/mysql
    ports:
    - 3306:3306
    environment:
    MYSQL_ROOT_PASSWORD: root
    volumes:
    - "/data/mysql/data:/var/lib/mysql"
    - "/data/mysql/conf:/etc/mysql/conf.d"
    networks:
    - backend

    gogs:
    image: docker.io/gogs/gogs
    ports:
    - 10022:10022
    - 10080:3000
    volumes:
    - "/data/gogs/:/data"
    depends_on:
    - mysql
    networks:
    - backend

    nginx:
    image: docker.io/nginx
    ports:
    - 80:80
    - 443:443
    depends_on:
    - gogs
    volumes:
    - "/data/nginx/nginx.conf:/etc/nginx/nginx.conf:ro"
    - "/data/www:/www"
    - "/data/nginx/conf.d:/etc/nginx/conf.d"
    - "/data/nginx/keys:/etc/nginx/keys"
    - "/data/nginx/err:/etc/nginx/err"
    networks:
    - backend

    networks:
    backend:
  • 设置下环境
    COMPOSE_FILE=docker-compose.yml

  • 启动即可
    docker-compose up -d