0
0

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.

dockerでコンテナ外部にファイル保存

Last updated at Posted at 2021-04-20

ボリュームを利用してdockerのコンテナ外にファイルを保存する方法。
例としてwordpressのdockerコンテナで記載する。

元のdocker-compose (wordpress)

docker hub参考

docker-compose.yml
version: '3.1'

services:

  wordpress:
    container_name: wp-trial
    image: wordpress
    restart: always
    ports:
      - 55555:80
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: exampleuser
      WORDPRESS_DB_PASSWORD: examplepass
      WORDPRESS_DB_NAME: exampledb
    volumes:
      - wordpress:/var/www/html

  db:
    container_name: wp-db
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_DATABASE: exampledb
      MYSQL_USER: exampleuser
      MYSQL_PASSWORD: examplepass
      MYSQL_RANDOM_ROOT_PASSWORD: '1'
    volumes:
      - db:/var/lib/mysql

volumes:
  wordpress
  db:

ディレクトリを直接指定

C:\wordpress\htmlをボリュームとして設定する場合はこんな感じ。
ただ、このままサーバでは使えないので環境変数の方がいい。

docker-compose.yml
version: '3.1'

services:

  wordpress:
# (省略)
    volumes:
      - /C/wordpress/html:/var/www/html

# (省略)
volumes:
# (wordpressは削除)
  db:

環境変数で指定

WORDPRESS_HTML_PATHという環境変数を設定する場合はこんな感じ。
※環境変数は開発環境やサーバ環境で設定すればOK

docker-compose.yml
version: '3.1'

services:

  wordpress:
# (省略)
    volumes:
      - ${WORDPRESS_HTML_PATH}:/var/www/html

# (省略)
volumes:
# (wordpressは削除)
  db:

参考記事

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?