在Docker构建期间,Docker无法启动MariaDB / MySQL

时间:2017-04-28 20:07:53

标签: mysql docker mariadb dockerfile

我当前的Dockerfile看起来像这样:

FROM ubuntu:14.04

ENV DEBIAN_FRONTEND noninteractive
ENV INITRD No
ENV LANG en_US.UTF-8

# Maria DB Versions
ENV MARIADB_MAJOR 5.5
ENV MARIADB_VERSION 5.5.55+maria-1~trusty

# Create mysql user and group
RUN groupadd -r mysql && useradd -r -g mysql mysql

# Install needed dependencies
RUN apt-get update && apt-get install -y --no-install-recommends software-properties-common && rm -rf /var/lib/apt/lists/*

# Add the MariaDB PGP key to verify their Debian packages.
RUN apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db

# Add MariaDB's repository. We use the Ubuntu 14.04 version as there ain't no MariaDB 5.5 for Ubuntu 16.04.
RUN add-apt-repository 'deb http://mirror.jmu.edu/pub/mariadb/repo/5.5/ubuntu trusty main'

# Install Maria DB and open the access for outside of the container
RUN apt-get update \
    && apt-get install -y --no-install-recommends mariadb-server=$MARIADB_VERSION \
    && rm -rf /var/lib/apt/lists/* \
    && sed -i 's/^\(bind-address\s.*\)/# \1/' /etc/mysql/my.cnf \
    && update-rc.d -f mysql disable

# Run as user mysql
USER mysql

# Start the MariaDB to add a user and create the DB
RUN mysqld

RUN echo "@TODO: Create DB, User and grant access"

# Expose Port
EXPOSE 3306

使用docker build -t testmariadb .构建容器时,我得到以下输出:

    Step 13/17 : RUN mysqld
---> Running in 5aeb49c81f5e
170428 20:00:25 [Note] mysqld (mysqld 5.5.55-MariaDB-1~trusty) starting as process 7 ...
170428 20:00:25 InnoDB: The InnoDB memory heap is disabled
170428 20:00:25 InnoDB: Mutexes and rw_locks use GCC atomic builtins
170428 20:00:25 InnoDB: Compressed tables use zlib 1.2.8
170428 20:00:25 InnoDB: Using Linux native AIO
170428 20:00:25 InnoDB: Initializing buffer pool, size = 256.0M
170428 20:00:25 InnoDB: Completed initialization of buffer pool
InnoDB: The first specified data file ./ibdata1 did not exist:
InnoDB: a new database to be created!
170428 20:00:25  InnoDB: Setting file ./ibdata1 size to 10 MB
InnoDB: Database physically writes the file full: wait...
170428 20:00:25  InnoDB: Log file ./ib_logfile0 did not exist: new to be created
InnoDB: Setting log file ./ib_logfile0 size to 5 MB
InnoDB: Database physically writes the file full: wait...
170428 20:00:25  InnoDB: Log file ./ib_logfile1 did not exist: new to be created
InnoDB: Setting log file ./ib_logfile1 size to 5 MB
InnoDB: Database physically writes the file full: wait...
InnoDB: Doublewrite buffer not found: creating new
InnoDB: Doublewrite buffer created
InnoDB: 127 rollback segment(s) active.
InnoDB: Creating foreign key constraint system tables
InnoDB: Foreign key constraint system tables created
170428 20:00:25  InnoDB: Waiting for the background threads to start
170428 20:00:26 Percona XtraDB (http://www.percona.com) 5.5.52-MariaDB-38.3 started; log sequence number 0
170428 20:00:26 [Note] Plugin 'FEEDBACK' is disabled.
170428 20:00:26 [Note] Server socket created on IP: '0.0.0.0'.
170428 20:00:26 [Note] Event Scheduler: Loaded 0 events
170428 20:00:26 [Note] mysqld: ready for connections.
Version: '5.5.55-MariaDB-1~trusty'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  mariadb.org binary distribution

据我所知,它启动了MariaDB,但后来卡在那里并且没有执行下一个命令。还有另一种方法可以在docker中启动MariaDB吗?

3 个答案:

答案 0 :(得分:2)

1)没有卡住。 MariaDB开始运行。但是您需要使用CMD命令而不是RUN命令来实现您的目的。

与RUN类似,命令CMD可用于执行特定命令。但是,与RUN不同,它不会在构建期间执行,而是在使用正在构建的图像实例化容器时。

2)dockerfile的最后一部分应如下所示

#Run as user mysql
USER mysql

RUN echo "@TODO: Create DB, User and grant access"

#Expose Port
EXPOSE 3306

#Right way to run mysqld
CMD ["mysqld"]

用于构建图像

docker build -t testmariadb .

用于运行构建的图像

docker run testmariadb

OR(对于分离模式)

docker run -d testmariadb

3)请问你自己为什么使用ubuntu基本映像(FROM ubuntu:14.04)。据我说,如果你只想在容器中运行mariaDb,你应该使用https://hub.docker.com/_/mariadb/

答案 1 :(得分:0)

对于运行容器你正在使用什么docker命令?

请关注MariaDB的官方Docker文件。 Docker MariaDB

这是最新的DockerFile MariaDB

答案 2 :(得分:-1)

这是错误的

# Start the MariaDB to add a user and create the DB RUN mysqld

我猜你想要

CMD mysqld

或者这样的

https://github.com/docker-library/mysql/blob/7a850980c4b0d5fb5553986d280ebfb43230a6bb/8.0/Dockerfile

结束是

EXPOSE 3306 CMD ["mysqld"]

RUN,WORKDIR,ADD,COPY,ENV(以及其他一些)帮助您配置docker镜像,ENTRYPOINT和CMD以启动软件(nginx用于nginx图像,Wordpress ...)

文档

https://docs.docker.com/engine/reference/builder/