4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Mac】Docker+rails6+MySQLで環境構築中にaddress already in useと出たら?

Last updated at Posted at 2021-09-06

環境

・macOS Big Sur バーション11.5.2
・シェル zsh
・Ruby 3.0.2
・rails 6.1.4
・My SQL 8.0.23
・Docker 20.10.8

Docker file

Dockerfile
FROM ruby:3.0.2
RUN apt-get update -qq && apt-get install -y nodejs

# yarnパッケージ管理ツールをインストール
# https://classic.yarnpkg.com/en/docs/install/#debian-stable
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update && apt-get install yarn

WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp

# Add a script to be executed every time the container starts
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]

docker-compose.yml

docker-compose.yml
version: "3"

services:
  db:
    image: mysql:8.0.26
    command: mysqld --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_USER: user
      MYSQL_PASSWORD: password
      MYSQL_ROOT_PASSWORD: password
    ports:
        - 3306:3306
    volumes:
      - ./tmp/db:/var/lib/mysql
  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    environment:
      MYSQL_HOST: db
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db

エラー内容

$ docker compose up

Starting mysql_db_1 ... error

ERROR: for mysql_db_1  Cannot start service db: Ports are not available: listen tcp 0.0.0.0:3306: bind: address already in use

ERROR: for db  Cannot start service db: Ports are not available: listen tcp 0.0.0.0:3306: bind: address already in use
ERROR: Encountered errors while bringing up the project.

【Docker】Rails+MySQLの環境をDockerで構築中「ポートの衝突」に遭遇してから解決するまで
こちらの記事を参考に2つの解決策を試してみるも解決できず、、、

エラーにはaddress already in useとあるので他にaddressが使われていないか調べていきます。

$ lsof -i:3306

COMMAND   PID  USER  ...
mysqld    117 SAKAE  ...

出てきました!これを消せばいいわけです!

消すコマンドはこちら

$ kill 117

それでもうまくいかない場合

探すとき

$ sudo lsof -i:3306

or

$ sudo lsof -i -P | grep "LISTEN"

消したい時

$ sudo kill <killしたいportのPID>

それでもダメなら

docker-compose.yaml
ports:
      - "3306:3306"

ports:
      - "3307:3306"

このようにポート番号を変えることで解決できる場合もあります。

4
1
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
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?