4
4

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 5 years have passed since last update.

Docker Memo

Last updated at Posted at 2014-09-13

色々忘れるしオプション多いので。思い出し程度に継ぎ足して言う形。

ホスト側のディレクトリとDockerのディレクトリを共有

  1. DockerファイルにVOLUME を書く?

  2. docker run -v <LocalPath>:<docker path>:<rw?> オプションを追加

  3. ファイルの共有もできる -v ~/.bashrc:/root/.bashrc

Shell を使う

sshd をいちいち立てたくない時に使う方法で、nsenterを使う。

インストール

> docker run -v /usr/local/bin:/target jpetazzo/nsenter

使い方

インストールしたらコンテナに入る

> nsenter --target $PID --mount --uts --ipc --net --pid /bin/sh

PID は DockerコンテナのPID。

コンテナのPID確認方法

例えば、プロセスツリーを見て確認する。

> ps -awwux --forest
root     30712  0.5  4.2 1021992 43864 ?       Sl   Sep11  12:04 /usr/bin/docker -d
root     31201  0.0  0.0   4392   320 ?        Ss   16:07   0:00  \_ /bin/sh -c php5-fpm /bin/bash
root     31246  0.0  1.6 409752 16852 ?        Ss   16:07   0:00  |   \_ php-fpm: master process (/etc/php5/fpm/php-fpm.conf)
33       31267  0.0  1.3 412016 13924 ?        S    16:07   0:00  |       \_ php-fpm: pool www
33       31268  0.0  1.3 412460 13944 ?        S    16:07   0:00  |       \_ php-fpm: pool www
999       1182  0.1 10.8 482708 110356 ?       Ssl  16:29   0:01  \_ mysqld --datadir=/var/lib/mysql --user=mysql --init-file=/tmp/mysql-first-time.sql

この場合、php-fpm のコンテナに入る場合、PIDは31201になる。

Network

Docker 内は 172.14.. 的なネットワークでつながっている。Port Foward したくないアプリケーションを使う場合は、IPアドレスを調べて設定する方法がある。

Docker コンテナのIPアドレスの調べ方

> $ sudo docker inspect <Container Name or ID> | grep IPAddres | awk -F'"' '{print $4}'

2014/09/14 追記: 公式ドキュメントにちゃんと書いてた

> $ sudo docker inspect -f '{{ .NetworkSettings.IPAddress }}' <Container Name or ID>

MySQLを使う

こんな感じでさくっと使える。MySQLの細かいオプションなどを指定したい場合は、Dockerfile を https://registry.hub.docker.com/_/mysql/ あたりから落としてきて頑張って。

> sudo docker run --name mysql -e MYSQL_ROOT_PASSWORD=<password> -d mysql

接続には、IPを調べてこんな感じ

> mysql -uroot -p --host 172.14... --port 3306

参考

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?