Dockerfile: design a container to meet SQLServer mirror the development of test requirements

This article is based on k8s as a platform to develop a test run container business design mirror Microsoft SQL Server 2017 database services. Mirroring container design focus is to meet the needs of development and testing work, followed try to control the image file size, image file final delivery volume is 1.82GB.

This example is used as the base image CentOS7, installed and configured SQL Server 2017 services, database services configured to employ developerlicense.

Note: Due to the use of start-stop systemctl CentOS7 tool management software and services, and in the case of the container is not the privilege of container authorized execution will complain systemctl command Failed to get D-Bus connection: Operation not permitted. For security reasons, we do not want to open the container vessel authorization privileges, so in this example will additionally provides methods for managing mssql-server service start and stop instructions.

Dockerfile file

###########################################################
FROM centos:7
MAINTAINER watermelonbig <[email protected]>

# 重置系统root密码
RUN echo "root:YYtest2019" | chpasswd
# 配置yum源并安装基础工具包
RUN curl -o /etc/yum.repos.d/mssql-server.repo https://packages.microsoft.com/config/rhel/7/mssql-server-2017.repo;\
        curl -o /etc/yum.repos.d/msprod.repo https://packages.microsoft.com/config/rhel/7/prod.repo
RUN yum clean all; \
        yum makecache; \
        yum install -y sudo which openssh-server openssh-clients rsync wget iproute net-tools sysstat lsof tcpdump telnet iputils lrzsz zip unzip kde-l10n-Chinese
# 中文支持和时区设置
RUN  sed -i 's/LANG="en_US.UTF-8"/LANG="zh_CN.UTF-8"/g' /etc/locale.conf;\
        cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# 必要的系统参数调优
RUN echo 'net.ipv6.conf.all.disable_ipv6 = 1' >> /etc/sysctl.conf;\
        echo 'net.ipv6.conf.default.disable_ipv6 = 1' >> /etc/sysctl.conf;\
        echo 'net.ipv4.ip_local_port_range = 1024 65000' >> /etc/sysctl.conf;\
        echo 'vm.swappiness = 10' >> /etc/sysctl.conf;\
        echo '* soft nofile 65535' >> /etc/security/limits.conf;\
        echo '* hard nofile 65535' >> /etc/security/limits.conf;\
        echo "session   required        /lib64/security/pam_limits.so" >> /etc/pam.d/login;\
        sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config;\
        sed -i '/UseDNS yes/s/.*/UseDNS no/g' /etc/ssh/sshd_config
RUN ssh-keygen -t rsa -P "" -f /etc/ssh/ssh_host_rsa_key;\
        ssh-keygen -t dsa -P ""  -f /etc/ssh/ssh_host_dsa_key
RUN    chmod u+s /usr/sbin/lsof;\
       chmod u+s /usr/sbin/tcpdump
# 采用离线方式安装
COPY ./mssql-server-14.0.3015.40-1.x86_64.rpm .
COPY ./mssql-server-agent-14.0.3015.40-1.x86_64.rpm .
# 拷贝一份静默安装mssql-server服务的shell脚本
COPY ./install_sqlserver.sh .
# 安装并配置mssql-server
RUN bash install_sqlserver.sh
RUN /opt/mssql/bin/mssql-conf set memory.memorylimitmb 7000

RUN rm -rf mssql-server-14.0.3015.40-1.x86_64.rpm;\
    rm -rf mssql-server-agent-14.0.3015.40-1.x86_64.rpm;\
    rm -rf install_sqlserver.sh

ENV LANG=zh_CN.UTF-8
# 提供ssh登录管理的支持
CMD ["/usr/sbin/sshd","-D"]
EXPOSE 1433 22

A silent installation script Linux SQL Server 2017 database

#!/bin/bash -e

# Use the following variables to control your install:
# Password for the SA user (required)
MSSQL_SA_PASSWORD='YY12345678!'

# Product ID of the version of SQL server you're installing
# Must be evaluation, developer, express, web, standard, enterprise, or your 25 digit product key
# Defaults to developer
MSSQL_PID='developer'

# Install SQL Server Agent (recommended)
SQL_INSTALL_AGENT='y'

# Install SQL Server Full Text Search (optional)
# SQL_INSTALL_FULLTEXT='y'

# Create an additional user with sysadmin privileges (optional)
#SQL_INSTALL_USER='yytester'
#SQL_INSTALL_USER_PASSWORD='yy123456!'

if [ -z $MSSQL_SA_PASSWORD ]
then
  echo Environment variable MSSQL_SA_PASSWORD must be set for unattended install
  exit 1
fi

#echo Adding Microsoft repositories...
#curl -o /etc/yum.repos.d/mssql-server.repo https://packages.microsoft.com/config/rhel/7/mssql-server-2017.repo
#curl -o /etc/yum.repos.d/msprod.repo https://packages.microsoft.com/config/rhel/7/prod.repo

echo Installing SQL Server...
yum localinstall -y mssql-server-14.0.3015.40-1.x86_64.rpm

echo Running mssql-conf setup...
MSSQL_SA_PASSWORD=$MSSQL_SA_PASSWORD \
     MSSQL_PID=$MSSQL_PID \
     /opt/mssql/bin/mssql-conf -n setup accept-eula

echo Installing mssql-tools and unixODBC developer...
ACCEPT_EULA=Y yum install -y mssql-tools unixODBC-devel

# Add SQL Server tools to the path by default:
echo Adding SQL Server tools to your path...
echo PATH="$PATH:/opt/mssql-tools/bin" >> ~/.bash_profile
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc

# Optional SQL Server Agent installation:
if [ ! -z $SQL_INSTALL_AGENT ]
then
  echo Installing SQL Server Agent...
  yum localinstall -y mssql-server-agent-14.0.3015.40-1.x86_64.rpm
fi

# Optional SQL Server Full Text Search installation:
if [ ! -z $SQL_INSTALL_FULLTEXT ]
then
    echo Installing SQL Server Full-Text Search...
    yum install -y mssql-server-fts
fi

# Optional new user creation:
if [ ! -z $SQL_INSTALL_USER ] && [ ! -z $SQL_INSTALL_USER_PASSWORD ]
then
  echo Creating user $SQL_INSTALL_USER
  /opt/mssql-tools/bin/sqlcmd \
    -S localhost \
    -U SA \
    -P $MSSQL_SA_PASSWORD \
    -Q "CREATE LOGIN [$SQL_INSTALL_USER] WITH PASSWORD=N'$SQL_INSTALL_USER_PASSWORD', DEFAULT_DATABASE=[master], CHECK_EXPIRATION=ON, CHECK_POLICY=ON; ALTER SERVER ROLE [sysadmin] ADD MEMBER [$SQL_INSTALL_USER]"
fi

echo Done!
  • MSSQL_SA_PASSWORDParameter specifies the database management system sa account passwords;
  • MSSQL_PIDParameter specifies which one to use sql server licensing;
  • SQL_INSTALL_AGENTSimultaneous installation of a client management tool;
  • yum localinstall -y mssql-server-14.0.3015.40-1.x86_64.rpm, Offline database server installation packages, the installation process is dependent on other toolkits yum directly from the source line to download and install;
  • MSSQL_SA_PASSWORD=$MSSQL_SA_PASSWORD MSSQL_PID=$MSSQL_PID /opt/mssql/bin/mssql-conf -n setup accept-eula Of sqlserver initial configuration, this step an error message warning D-Bus can not be accessed when there is a call systemctl, can be ignored;

NOTE: The above silent installation script has some features we have not enabled, in part because temporary use less, partly because the implementation of these configurations and start these services will fail in this configuration stage, you need to use a mirror to create a delayed business container after the line configuration.

Create a mirror container

# ls
Dockerfile  install_sqlserver.sh  mssql-server-14.0.3015.40-1.x86_64.rpm  mssql-server-agent-14.0.3015.40-1.x86_64.rpm  
# docker build -t centos7-sqlserver2017 .

Note: Due to mirror the production process involves a lot of packages to download, install and configure, so the error message image created during printing needs special attention. In addition to previously mentioned D-Bus无法访问the error can be ignored, but there are several warning information relating to retrieve packages signature file can be ignored. If there is found in the download package DNS service failure occurs, the package installation fails or is not installed, or error information to other error class, need special attention, which will lead to the final service installation problems. If it is because of Microsoft Access or yum install centos source of problems caused by the error, the probability of a large network is affected by the delay, after the error, can be repeated several times to create a retry command.

After creating business database configuration container

Due to the lack of container business privileges required systemctl tools, so we can not use start-stop systemctl mssql-server management services.

Sqlserver method manually start and stop services

sqlserver 2017 service manual startup method:

sudo su - mssql -c "/opt/mssql/bin/sqlservr &"

stop:

ps -ef|grep mssql|grep -v grep|awk '{print $2}'|xargs kill

Configuration Manager user accounts database sqlserver

Administrator account is: sa / YY12345678!

The password is specified in the silent installation script

Using an administrator account to log database:

sqlcmd -S localhost  -U SA  -P 'YY12345678!'

If successful, it should show sqlcmd command prompt: 1>.

Proceed to create a normal user account number used in the business program: yytester / yy123456!

Create a regular user account yytester manual, and the sysadmin privileges.
Please confirm mssql-server service started, copy the following four lines of command and to execute in the system:

MSSQL_SA_PASSWORD='YY12345678!'
SQL_INSTALL_USER='yytester'
SQL_INSTALL_USER_PASSWORD='yy123456!'
/opt/mssql-tools/bin/sqlcmd \
    -S localhost \
    -U SA \
    -P $MSSQL_SA_PASSWORD \
    -Q "CREATE LOGIN [$SQL_INSTALL_USER] WITH PASSWORD=N'$SQL_INSTALL_USER_PASSWORD', DEFAULT_DATABASE=[master], CHECK_EXPIRATION=ON, CHECK_POLICY=ON; ALTER SERVER ROLE [sysadmin] ADD MEMBER [$SQL_INSTALL_USER]"

The method of building a database built sqlserver tables and queries

Building a database

CREATE DATABASE TestDB
SELECT Name from sys.Databases
GO

To build the table

USE TestDB
CREATE TABLE Inventory (id INT, name NVARCHAR(50), quantity INT)
INSERT INTO Inventory VALUES (1, 'banana', 150); INSERT INTO Inventory VALUES (2, 'orange', 154);
GO

Look-up table data

SELECT * FROM Inventory WHERE quantity > 152;
GO

Reference: https://docs.microsoft.com/zh-cn/sql/linux/quickstart-install-connect-red-hat?view=sql-server-2017

Guess you like

Origin blog.csdn.net/watermelonbig/article/details/90585157