mysql - 如何使MYSQL在Docker运行时启动并在Dockerfile中创建mysql用户

标签 mysql docker dockerfile

我有一个Dockerfile(如下所示)。图像生成没有问题。安装了所有我期望的项目。但是,我正在“还原”的MYSQL用户和数据库似乎不存在。我必须执行到实例并再次手动启动mysql服务,添加用户,创建数据库,然后再次运行gunzip行。我希望在图像生成时发生这种情况,然后希望mysql在run命令上启动。任何帮助表示赞赏。

更新了

因此,我了解到我试图创建mysql用户,数据库和还原数据库的原始版本不起作用。因此,在研究中,似乎应该将.sh或.sql文件等放入/docker-entrypoint-initdb.d/中,并且它们应该在我第一次运行容器时运行。但是,这似乎对我不起作用,因此很明显我缺少了一些东西。我也将下面的.sh文件也包括在内。

#Download base image ubuntu 16.04
FROM ubuntu:16.04

# Let the conatiner know that there is no tty
ENV DEBIAN_FRONTEND noninteractive

# Update Software repository
RUN apt-get update

# Add Language Packs
RUN apt-get install -y language-pack-en-base

# Set the locale
RUN sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen && \
    locale-gen
ENV LANG en_US.UTF-8  
ENV LANGUAGE en_US:en  
ENV LC_ALL en_US.UTF-8 

# Add Current PHP Repository
RUN apt-get install software-properties-common -y
RUN add-apt-repository -y ppa:ondrej/php
RUN add-apt-repository -y ppa:ondrej/mysql-5.6
RUN apt-get update

# Basic Requirements
RUN apt-get -y install pwgen python-setuptools curl git nano sudo unzip openssh-server openssl vim htop
RUN apt-get -y install php7.4-fpm php7.4-common php7.4-mysql php7.4-xml php7.4-xmlrpc php7.4-curl php7.4-gd php7.4-imagick php7.4-cli php7.4-dev php7.4-imap php7.4-mbstring php7.4-soap php7.4-zip php7.4-bcmath php7.4-memcache php7.4-mysql
RUN apt-get -y install mysql-server-5.6 mysql-client-5.6 nginx
RUN apt-get install -y supervisor && \
    rm -rf /var/lib/apt/lists/*

# mysql config
RUN sed -i -e"s/^bind-address\s*=\s*127.0.0.1/explicit_defaults_for_timestamp = true\nbind-address = 0.0.0.0/" /etc/mysql/mysql.conf.d/mysqld.cnf    

#Define the ENV variable
ENV nginx_vhost /etc/nginx/sites-available/default
ENV php_conf /etc/php/7.4/fpm/php.ini
ENV nginx_conf /etc/nginx/nginx.conf
ENV supervisor_conf /etc/supervisor/supervisord.conf

# Enable php-fpm on nginx virtualhost configuration
COPY default ${nginx_vhost}
RUN sed -i -e 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g' ${php_conf} && \
    echo "\ndaemon off;" >> ${nginx_conf}

#Copy supervisor configuration
COPY supervisord.conf ${supervisor_conf}    

RUN mkdir -p /run/php && \
    chown -R www-data:www-data /var/www/html && \
    chown -R www-data:www-data /run/php

# Volume configuration
VOLUME ["/etc/nginx/sites-enabled", "/etc/nginx/certs", "/etc/nginx/conf.d", "/var/log/nginx", "/var/www/html", "/var/lib/mysql"]

# ROOT PASSWORD
ENV MYSQL_ROOT_PASSWORD=root

ENV MYSQL_DATABASE=tsc_sandbox
ENV MYSQL_USER=tsc_user
ENV MYSQL_PASSWORD=tsc_user

# Copy Conf File Over
COPY web-application.conf /etc/nginx/conf.d/web-application.conf

# Handle SSL Certs
RUN mkdir /etc/nginx/ssl
COPY web-application.crt /etc/nginx/ssl/web-application.crt
COPY web-application.key /etc/nginx/ssl/web-application.key

# Setup Mysql DB
COPY init-file.sh /docker-entrypoint-initdb.d/init-file.sh
RUN chmod +x /docker-entrypoint-initdb.d/init-file.sh

# Configure Services and Port
COPY start.sh /start.sh
CMD ["./start.sh"]

COPY tsc-20200310.sql.gz /tsc-20200310.sql.gz
RUN chmod +x /tsc-20200310.sql.gz

# Network Ports
EXPOSE 80 443 3306 22

我用来运行容器的命令是:
docker run -detach -v ~/hosts/web-application:/var/www/html -p 80:80 -p 443:443 -p 3306:3306 -p 22:22 --name container_1 container_image

我用来生成图像的命令是:
docker build -t container_image .

初始化文件
#!/bin/bash

/etc/init.d/mysql start

mysql -u root -e "CREATE USER 'tsc_user'@'%' IDENTIFIED BY 'tsc_user';"
mysql -u root -e "CREATE USER 'the_master'@'%' IDENTIFIED BY 'the_master';"
mysql -u root -e "GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, SHOW VIEW ON *.* TO 'tsc_user'@'%';"
mysql -u root -e "GRANT SELECT ON *.* TO the_master'@'%';"
mysql -u root -e "CREATE DATABASE tsc_sandbox;"

gunzip < /tsc-20200310.sql.gz | mysql -u root tsc_sandbox;

值得注意的是,我显然可以在之后执行到容器中并运行该Shell文件,一切都会按预期进行。只是希望当我使用docker run命令时发生这种情况。

最佳答案

我想至少用部分答案进行更新。我还没有弄清楚如何做我想做的所有事情,但至少我已经走得更远了。由于我已经在使用 super 用户,因此我在其中添加了mysql的启动。在下面,您可以看到我的Dockerfile,.sh文件,default和supervisord.conf文件。

Dockerfile

#Download base image ubuntu 16.04
FROM ubuntu:16.04

# Let the conatiner know that there is no tty
ENV DEBIAN_FRONTEND noninteractive

# Update Software repository
RUN apt-get update

# Add Language Packs
RUN apt-get install -y language-pack-en-base

# Set the locale
RUN sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen && \
    locale-gen
ENV LANG en_US.UTF-8  
ENV LANGUAGE en_US:en  
ENV LC_ALL en_US.UTF-8 

# Add Current PHP Repository
RUN apt-get install software-properties-common -y
RUN add-apt-repository -y ppa:ondrej/php
RUN add-apt-repository -y ppa:ondrej/mysql-5.6
RUN apt-get update

# Basic Requirements
RUN apt-get -y install pwgen python-setuptools curl git nano sudo unzip openssh-server openssl vim htop
RUN apt-get -y install php7.4-fpm php7.4-common php7.4-mysql php7.4-xml php7.4-xmlrpc php7.4-curl php7.4-gd php7.4-imagick php7.4-cli php7.4-dev php7.4-imap php7.4-mbstring php7.4-soap php7.4-zip php7.4-bcmath php7.4-memcache php7.4-mysql
RUN apt-get -y install mysql-server-5.6 mysql-client-5.6 nginx
RUN apt-get install -y supervisor && \
    rm -rf /var/lib/apt/lists/*

# mysql config
RUN sed -i -e"s/^bind-address\s*=\s*127.0.0.1/explicit_defaults_for_timestamp = true\nbind-address = 0.0.0.0/" /etc/mysql/mysql.conf.d/mysqld.cnf    

#Define the ENV variable
ENV nginx_vhost /etc/nginx/sites-available/default
ENV php_conf /etc/php/7.4/fpm/php.ini
ENV nginx_conf /etc/nginx/nginx.conf
ENV supervisor_conf /etc/supervisor/supervisord.conf

# Enable php-fpm on nginx virtualhost configuration
COPY default ${nginx_vhost}
RUN sed -i -e 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g' ${php_conf} && \
    echo "\ndaemon off;" >> ${nginx_conf}

#Copy supervisor configuration
COPY supervisord.conf ${supervisor_conf}    

RUN mkdir -p /run/php && \
    chown -R www-data:www-data /var/www/html && \
    chown -R www-data:www-data /run/php

# Volume configuration
VOLUME ["/etc/nginx/sites-enabled", "/etc/nginx/certs", "/etc/nginx/conf.d", "/var/log/nginx", "/var/www/html", "/var/lib/mysql"]

# ROOT PASSWORD
ENV MYSQL_ROOT_PASSWORD=root

ENV MYSQL_DATABASE=tsc_sandbox
ENV MYSQL_USER=tsc_user
ENV MYSQL_PASSWORD=tsc_user

# Setup Mysql DB
COPY db-init.sh /docker-entrypoint-initdb.d/db-init.sh
RUN chmod +x /docker-entrypoint-initdb.d/db-init.sh
RUN /etc/init.d/mysql start

# Configure Services and Port
COPY start.sh /start.sh
CMD ["./start.sh"]

COPY tsc_sandbox.sql.gz /tsc_sandbox.sql.gz
RUN chmod +x /tsc_sandbox.sql.gz

# Network Ports
EXPOSE 80 443 3306 22 11211 3000 9000 10137 20080

start.sh
#!/bin/sh

/usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf

db-init.sh
#!/bin/bash

mysql -u root -e "CREATE USER 'tsc_user'@'%' IDENTIFIED BY 'tsc_user';"
mysql -u root -e "CREATE USER 'memaster'@'%' IDENTIFIED BY 'memaster';"
mysql -u root -e "GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, SHOW VIEW ON *.* TO 'tsc_user'@'%';"
mysql -u root -e "GRANT SELECT ON *.* TO 'memaster'@'%';"
mysql -u root -e "CREATE DATABASE tsc_sandbox;"

gunzip < /tsc_sandbox.sql.gz | mysql -u root tsc_sandbox;

默认
server {
    listen 80;
    listen [::]:80 ipv6only=on default_server;

    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny all;
    #}
}

administratord.conf
[unix_http_server]
file=/dev/shm/supervisor.sock   ; (the path to the socket file)

[supervisord]
logfile=/var/log/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB        ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10           ; (num of main logfile rotation backups;default 10)
loglevel=info                ; (log level;default info; others: debug,warn,trace)
pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false               ; (start in foreground if true;default false)
minfds=1024                  ; (min. avail startup file descriptors;default 1024)
minprocs=200                 ; (min. avail process descriptors;default 200)
user=root             ;

; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///dev/shm/supervisor.sock ; use a unix:// URL  for a unix socket

; The [include] section can just contain the "files" setting.  This
; setting can list multiple files (separated by whitespace or
; newlines).  It can also contain wildcards.  The filenames are
; interpreted as relative to this file.  Included files *cannot*
; include files themselves.

[include]
files = /etc/supervisor/conf.d/*.conf


[program:php-fpm7.4]
command=/usr/sbin/php-fpm7.4 -F
numprocs=1
autostart=true
autorestart=true

[program:nginx]
command=/usr/sbin/nginx
numprocs=1
autostart=true
autorestart=true

[program:mysql]
command=/usr/bin/pidproxy /var/run/mysqld/mysqld.pid /usr/sbin/mysqld
autorestart=true

理想情况下,我将能够在第一次运行docker时运行db-init.sh脚本,而无需执行并运行它。除此之外,到目前为止,这还算不错。

关于mysql - 如何使MYSQL在Docker运行时启动并在Dockerfile中创建mysql用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60836882/

相关文章:

docker - 将 args 从 docker-compose 传递到 Dockerfile

docker - 为什么 `docker images`也显示基本图像?

macos - 容器内的 Docker 套接字权限

php - 如何在 MySQL 数据库中搜索特定字符串

php - 将mysql数字更改为文本

php - 如果该行中的某个单元格为空,如何隐藏整行?

wordpress - Docker 卷和 Kubernetes 卷

php - CakePHP 迁移创建新表导致列名重复

docker - 如何更新 Google Container Engine 上的 Kubernetes 部署?

docker - Docker CP:将文件从Docker容器复制到主机失败