31
17

More than 3 years have passed since last update.

docker-composeコマンドで起動したコンテナがExit 1となってSSH接続出来ない

Posted at

docker-composeコマンドで起動したコンテナがExit 1となってSSH接続出来ない

環境情報

  • MacOS X 10.15.1(19B88)
  • Docker 19.03.5
  • nginx 1.17.6
  • php-fpm 7.4.0
  • MySQL 8.0.18

ディレクトリ構成

PHP+nginx+MySQLの環境を用意しようと進めているときにdocker-compose up -dしたmysqlコンテナでエラーが発生し、正常にコンテナが起動しない事象が発生した。

.
├── docker-compose.yml
├── mysql
│   ├── init
│   │   ├── 10_ddl.sql
├── php
│   └── src
│       ├── html
│       └── index.php
├── src
│   └── html
└── web
    ├── conf
    │   └── default.conf
    └── src
        └── index.html

各設定ファイルなどの中身

docker-compose.yml

version: '3'
services:
  web:
    image: nginx:latest
    ports:
      - "80:80"
    depends_on:
      - app
    volumes:
      - ./web/conf/default.conf:/etc/nginx/conf.d/default.conf
      #- ./web/src:/var/www/ PHP側でソースを管理するためコメントアウト
  app:
    image: php:7.4.0-fpm
    volumes:
      - ./php/src:/var/www/
  mysql:
    #https://hub.docker.com/_/mysql
    #image: mysql:8.0.18 <-- latestだとこのバージョン(2019/12/4時点)
    image: mysql:latest
    environment:
      #イメージの起動時に作成するデータベースの名前
      MYSQL_DATABASE: yudb
      #このユーザはMYSQL_DATABASE変数で指定されたデータベースに対してスーパーユーザとしての権限(GRANT ALL)を保持する
      MYSQL_USER: mysqluser
      #MYSQL_USERのパスワード
      MYSQL_PASSWORD: MySQLPass00
      # MySQLにおけるスーパーユーザであるrootアカウントに設定するためのパスワード
      MYSQL_ROOT_PASSWORD: MySQLRootPass00
    ports:
      - "3306:3306"
    volumes:
      #- ./mysql/var_lib_mysql:/var/lib/mysql
      # /docker-entrypoint-initdb.d/配下は、Dockerコンテナが初回起動(初期化)される際に1度だけ実行されるスクリプトなどを配置
      # *.sh / *.sql / *.sql.gzの拡張子のファイルはファイル名の昇順に実行される。
      - ./mysql/init:/docker-entrypoint-initdb.d

docker-entrypoint-initdb.dで自動実行される初期化用のSQLファイル

10_ddl.sql

create table if not exists yudb.m_sample(
  `code` char(3) not null primary key,
  `name` varchar not null
) engine=innodb default charset=utf8;

エラーが発生した内容

まず、docker-composeでコンテナを起動。

docker-compose up -d
Creating network "php-docker_default" with the default driver
Creating php-docker_mysql_1 ... done
Creating php-docker_app_1   ... done
Creating php-docker_web_1   ... done

docker-compose exec mysql bashコマンドを実行してもSSHでmysqlコンテナにアクセスできないので、とりあえずプロセスを確認。

$ docker-compose ps
       Name                     Command              State          Ports       
--------------------------------------------------------------------------------
php-docker_app_1     docker-php-entrypoint php-fpm   Up       9000/tcp          
php-docker_mysql_1   docker-entrypoint.sh mysqld     Exit 1                     
php-docker_web_1     nginx -g daemon off;            Up       0.0.0.0:80->80/tcp

php-docker_mysql_1のプロセスがExit 1という状態になっている?
そこで、なぜ落ちているのか確認するためdocker-compose logsを実行

$ docker-compose logs
Attaching to php-docker_web_1, php-docker_app_1, php-docker_mysql_1
app_1    | [05-Dec-2019 22:46:48] NOTICE: fpm is running, pid 1
app_1    | [05-Dec-2019 22:46:48] NOTICE: ready to handle connections
mysql_1  | 2019-12-05 22:46:48+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.18-1debian9 started.
mysql_1  | 2019-12-05 22:46:48+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
mysql_1  | 2019-12-05 22:46:48+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.18-1debian9 started.
mysql_1  | 2019-12-05 22:46:48+00:00 [Note] [Entrypoint]: Initializing database files
mysql_1  | 2019-12-05T22:46:48.983209Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
mysql_1  | 2019-12-05T22:46:48.983344Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.18) initializing of server in progress as process 45
mysql_1  | 2019-12-05T22:46:51.172276Z 5 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
mysql_1  | 2019-12-05 22:46:54+00:00 [Note] [Entrypoint]: Database files initialized
mysql_1  | 2019-12-05 22:46:54+00:00 [Note] [Entrypoint]: Starting temporary server
mysql_1  | 2019-12-05T22:46:55.024214Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
mysql_1  | 2019-12-05T22:46:55.024425Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.18) starting as process 94
mysql_1  | 2019-12-05T22:46:55.604032Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
mysql_1  | 2019-12-05T22:46:55.607684Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
mysql_1  | 2019-12-05T22:46:55.663728Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.18'  socket: '/var/run/mysqld/mysqld.sock'  port: 0  MySQL Community Server - GPL.
mysql_1  | 2019-12-05 22:46:55+00:00 [Note] [Entrypoint]: Temporary server started.
mysql_1  | 2019-12-05T22:46:55.875687Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock'
mysql_1  | Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
mysql_1  | Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
mysql_1  | Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
mysql_1  | Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
mysql_1  | 2019-12-05 22:47:00+00:00 [Note] [Entrypoint]: Creating database yudb
mysql_1  | 2019-12-05 22:47:00+00:00 [Note] [Entrypoint]: Creating user mysqluser
mysql_1  | 2019-12-05 22:47:00+00:00 [Note] [Entrypoint]: Giving user mysqluser access to schema yudb
mysql_1  |
mysql_1  | 2019-12-05 22:47:00+00:00 [Note] [Entrypoint]: /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/10_ddl.sql
mysql_1  | ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'not null
mysql_1  | ) engine=innodb default charset=utf8' at line 3

上記ログの中で、ERRORとなっているのは以下の箇所。

mysql_1  | ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'not null
mysql_1  | ) engine=innodb default charset=utf8' at line 3

ログを見る限り、初期化スクリプトとして用意した10_ddl.sqlの3行目がおかしいよ。と言っている。

create table if not exists yudb.m_sample(
  `code` char(3) not null primary key,
  `name` varchar not null  ←ココ
) engine=innodb default charset=utf8;

凡ミスでした。。。
varchar型に対して、サイズを指定していなかったことが原因でした。

create table if not exists yudb.m_sample(
  `code` char(3) not null primary key,
  `name` varchar(80) not null
) engine=innodb default charset=utf8;

適当にサイズを指定して、

$ docker-compose down
$ docker-compose up -d
$ docker-compose ps

Name                     Command              State                 Ports              
----------------------------------------------------------------------------------------------
php-docker_app_1     docker-php-entrypoint php-fpm   Up      9000/tcp                         
php-docker_mysql_1   docker-entrypoint.sh mysqld     Up      0.0.0.0:3306->3306/tcp, 33060/tcp
php-docker_web_1     nginx -g daemon off;            Up      0.0.0.0:80->80/tcp

無事にプロセスが起動しているので、SSH接続を試してみる。

$ docker-compose exec mysql bash
root@534edcb8c512:/#

無事解決!
あんまり大した問題では無かった。。。

31
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
31
17