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

Dockerの基本的な操作

Last updated at Posted at 2017-04-04

docker commands

# イメージ取得
docker pull ruby:2.4.1-alpine

# イメージ確認
~/ $ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ruby                2.4.1-alpine        5eadd5d1419a        12 days ago         56.3 MB

# コンテナ確認
docker ps

# コンテナ削除
docker rm container_id

# イメージ削除
docker rmi image_id

docker-compose commands

Start

# Runs a one-time command
docker-compose run
# Remove container after run
--rm
# DB周りの初期設定
# rails:db:create, rails db:schema:load, rails db:seed
rails db:setup

# app, rails db:setup実行、
docker-compose run --rm app rails db:setup

# Builds, (re)creates, starts
docker-compose up

# Stops and removes containers
docker-compose down

# Starts existing containers 
docker-compose start

# Stops running containers without removing them
docker-compose stop

# コンテナの確認
docker-compose ls

# コンテナの削除
docker-compose rm

Gemsを更新した場合

docker run --rm -v ${PWD}:/home -w /home ruby:2.4 bundle lock 

# コンテナを開始前にイメージを構築(--build)
docker-compose up -d --build

その他

# Get a shell in container
docker-compose exec app bash

# Logging can be configured→default outputs everything on stdout/stderr
docker-compose logs
    
# Run the rails minitest.
docker-compose run --rm app rails test

Dockerfile

イメージを作り上げる命令を記述する
docker buildでイメージを構築

  1. FROM: ベース・イメージ を指定
    FROM <イメージ>
  2. RUN: イメージ上でコマンドを実行
    RUN /bin/bash -c 'source $HOME/.bashrc ; echo $HOME'
  3. CMD: 構築時には何もしませんが、イメージで実行するコマンドを指定
    コンテナ実行時のデフォルトを提供
    CMD echo "This is a test." | wc -
  4. EXPOSE: portをコンテナが実行時にリッスンすることをDockerに伝える
    EXPOSE <port> [<port>...]
  5. COPY: sourceのfileやdirectoryをコンテナ内のファイルシステム上にコピー
    COPY ソース 送信先(絶対パスor WORKDIRからの相対パス)
  6. WORKDIR: RUN,CMD,ENTRYPOINT,COPY,ADD実行時の作業ディレクトリ
    WORKDIR /a

docker-compose.yml

networksvolumesservicesを定義する

build: 構築時に適用するオプション

build: ./dir

enviroment

environment:
  RACK_ENV: development
  SHOW: 'true'
  SESSION_SECRET:

volumes: マウント・パス指定

volumes:
    - ./cache:/tmp/cache

depends_on: docker-compose up を実行したら、依存関係のある順番に従ってサービスを起動

depends_on:
      - db
      - redis

commnad: デフォルトのコマンドを上書き

    command: [bundle, exec, thin, -p, 3000]

ports: 公開用のポート

ports:
 - "3000"
 - "8000:8000"

image: コンテナを実行時に元となるイメージを指定

image: redis
image: ubuntu:14.04
2
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
2
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?