LoginSignup
0
1

More than 5 years have passed since last update.

docker メモ2

Last updated at Posted at 2018-07-15

Docker で cenots7 上で nginx を起動

作業ディレクトリ

/Users/#{user名}/docker_centos_project

ファイル構成

  • Dockerfile
  • docker-compose.yml

docker-compose.yml

version: '3'
services:
  web_server:
    build: "./"
    ports:
      - "8080:80"
    # tty: true
    # stdin_open: true
    privileged: true
    command: /sbin/init
    networks:
      app_net:
        ipv4_address: 172.16.238.2
networks:
  app_net:
    driver: bridge
    ipam:
     driver: default
     config:
       - subnet: 172.16.238.0/24
  • web-server という名でカレントディレクトリの Dockerfile を起動。
  • ポートフォワードの設定は、ホストの8080番を起動するコンテナの80番ポートを参照させる
  • tty : 疑似ターミナル (pseudo-TTY) を割り当て。ttyを確保。/bin/bashなどでコンテナを操作する際に指定。
  • stdin_open : コンテナの STDIN にアタッチ。コンテナの標準入力を開く。/bin/bashなどでコンテナーを操作する際に指定
  • privileged : 特権モードを指定してコンテナ作成(問題がある模様・・・参照
  • command : コンテナを起動して最初に動かすコマンド
  • ipアドレスを固定するためにnetworksを指定しておく

※ デタッチドモードで起動したいが、docker-composeでのやり方がわからない
systemctl を使うために特権モードで起動するが問題があるようなので別の方法を探る

Dockerfile

FROM centos:7
RUN touch /etc/yum.repos.d/nginx.repo \
  && echo -e "[nginx]\nname=nginx repo\nbaseurl=http://nginx.org/packages/centos/7/\$basearch/\ngpgcheck=0\nenabled=1" > /etc/yum.repos.d/nginx.repo \
  && yum -y install nginx \
  && yum -y install openssh-server \
  && sed -i -e "s/PermitRootLogin yes/PermitRootLogin yes\nPermitRootLogin no/g" /etc/ssh/sshd_config
  • centos7のイメージからコンテナ作成
  • nginxをインストールするため/etc/yum.repos.d/nginx.repoを作成
  • ファイルの中身に記述する。echoコマンドを使って>でファイルに渡す。改行を反映させるために-eオプションを渡す。$はエスケープしなければならない。
  • yum -y install nginxnginxをインストール
  • yum -y install openssh-serveropenssh-serverをインストール
  • sedを用いてsshのログイン設定ファイルを変更

起動コマンド

$ docker-compose up -d

$ docker ps -a
CONTAINER ID        IMAGE                              COMMAND             CREATED             STATUS              PORTS                  NAMES
39dd5320ba92        docker_centos_project_web_server   "/sbin/init"        17 hours ago        Up 17 hours         0.0.0.0:8080->80/tcp   docker_centos_project_web_server_1

$ docker exec -it docker_centos_project_web_server_1 bash
  • docker-compose up : コンテナを作成し起動。オプションで-dをつけることでバックグラウンドで実行することがでる。
  • docker ps : 現在起動中のコンテナを確認
  • docker exec : コンテナに入る。オプションとして-i-tを渡し、bashでコンテナ操作するための準備をし、最後にコンテナ起動後に最初に動かすコマンドとしてbashを指定

コンテナに入った後

# systemctl start nginx
# exit

ホスト側から nginx 起動を確認

ブラウザで http://localhost:8080/ で確認

スクリーンショット 2018-07-15 16.09.34.png

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