前言

本来一直使用 Ubuntu 镜像搭建环境的,后来发现好像还是使用官方的镜像更加简单快捷一点。

来整几个官方镜像试试,好像官方镜像以 Debian 系统居多。


MySQL

官方镜像说明:https://hub.docker.com/_/mysql

可以看到一个简单的例子如下:

# Use root/example as user/password credentials
version: '3.1'
services:
  db:
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: example

这里配置了 root 账号的密码,也修改了认证插件让我们的 MySQL Client 可以进行登录。

除了这些,我们还需要什么?

新建数据库、导入 SQL 文件、配置 IP。

查看手册,我们可以看到 MYSQL_DATABASE 环境变量:

This variable is optional and allows you to specify the name of a database to be created on image startup. If a user/password was supplied (see below) then that user will be granted superuser access (corresponding to GRANT ALL) to this database.

设置这个环境变量可以创建数据库,如果我们还指定了用户/密码,还会自动给该用户赋予该数据库的全部权限。

然后是导入 SQL 文件:

When a container is started for the first time, a new database with the specified name will be created and initialized with the provided configuration variables. Furthermore, it will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order. You can easily populate your mysql services by mounting a SQL dump into that directory and provide custom images with contributed data. SQL files will be imported by default to the database specified by the MYSQL_DATABASE variable.

看起来我们只要把 SQL 文件放在 /docker-entrypoint-initdb.d 目录下面就可以了,最后的 docker-compose.yml:

version: '2.4'
services:
  mysql:
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_ROOT_PASSWORD: twingsdocker
      MYSQL_DATABASE: flag
    volumes:
      - ./import.sql:/docker-entrypoint-initdb.d/init.sql
    restart: always
    networks:
      out_network:
        ipv4_address: 10.0.10.2
networks:
  out_network:
    ipam:
      driver: default
      config:
        - subnet: 10.0.10.0/24

import.sql 里面就简单的两句:

create table flag(username varchar(255))engine=innodb default charset=utf8 COLLATE=utf8_general_ci;
insert into flag values("flag{1234567890}");

最后尝试一下:

root@1f39692ad792:/# mysql -uroot -ptwingsdocker
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 8
Server version: 8.0.16 MySQL Community Server - GPL

Copyright (c) 2000, 2019, 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> use flag;

Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from flag;
+------------------+
| username         |
+------------------+
| flag{1234567890} |
+------------------+
1 row in set (0.00 sec)

mysql>

不得不说 MySQL 占用的内存是真的挺多的,本来限制了内存为 200M,结果镜像开不起来。


PHP

官方镜像描述:https://hub.docker.com/_/php/

目前来说官方的 PHP 镜像最高版本是 7.3,如果需要更新版本的 PHP,可以自行仿照官方的 Dockerfile:https://github.com/docker-library/php/tree/85b2af63546309c3c7b895524db10ef02aa4edba/7.3/stretch/apache

这样写的 Dockerfile 有几百行,比较麻烦,如果不是特别需要新版本的话使用官方镜像会比较方便一点。

官方提供了三个脚本:docker-php-ext-configure,docker-php-ext-install 以及 docker-php-ext-enable 来让我们更轻松地安装扩展,栗子如下,核心扩展:

RUN apt-get update && apt-get install -y \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libpng-dev \
    && docker-php-ext-install -j$(nproc) iconv \
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install -j$(nproc) gd

PECL 扩展:

RUN pecl install redis-4.0.1 \
    && pecl install xdebug-2.6.0 \
    && docker-php-ext-enable redis xdebug
RUN apt-get update && apt-get install -y libmemcached-dev zlib1g-dev \
    && pecl install memcached-2.2.0 \
    && docker-php-ext-enable memcached

其他扩展:

RUN curl -fsSL 'https://xcache.lighttpd.net/pub/Releases/3.2.0/xcache-3.2.0.tar.gz' -o xcache.tar.gz \
    && mkdir -p xcache \
    && tar -xf xcache.tar.gz -C xcache --strip-components=1 \
    && rm xcache.tar.gz \
    && ( \
        cd xcache \
        && phpize \
        && ./configure --enable-xcache \
        && make -j "$(nproc)" \
        && make install \
    ) \
    && rm -r xcache \
    && docker-php-ext-enable xcache

如果不需要安装扩展那甚至不需要用到 Dockerfile。


Python

Python 我不太熟悉,留个坑,以后学习 django 框架的时候顺便一起学。


Nodejs

Node 的官方镜像使用比较简单,类似这样子就好了:

FROM node:11-alpine

LABEL maintainer="Twings"

ENV BASE_DIR=/var/www

# 换源、安装软件
RUN yarn config set registry https://registry.npm.taobao.org/
...

# 用户密码
...

# 放置其他文件
...

# 安装pm2,建立web目录
RUN set -ex \
    && yarn global add pm2 \
    && mkdir -p ${BASE_DIR}/web

# 放置web文件
COPY web/ ${BASE_DIR}/web
COPY process.yaml ${BASE_DIR}

# 修改权限
...

# 下载依赖
RUN set -ex \
    && cd ${BASE_DIR}/web \
    && yarn install --production --network-timeout 30000

# 启动web服务
WORKDIR ${BASE_DIR}
USER node
CMD [ "pm2-docker", "process.yaml" ]

pm2 是个挺好用的 node 进程管理器:http://pm2.keymetrics.io/docs/usage/quick-start/


Go

同样留个坑,等我会用 Go 实现个题目了再说。


Java

Java 还是留坑,Spring 框架不太会用还。


Orz


Docker

本博客所有文章除特别声明外,均采用 CC BY-SA 3.0协议 。转载请注明出处!

Go(2)
内网端口转发