ritarock’s blog

プログラミングとか映画とか趣味とか

Dockerのインストールからコンテナ作成まで

開発環境

ubuntu/trusty64

Dockerのインストール

$ sudo apt-get update
$ sudo apt-get -y install \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
$ sudo apt-key fingerprint 0EBFCD88
$ sudo add-apt-repository -y  \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"
$ sudo apt-get update
$ sudo apt-get install -y docker-ce

Dockerコマンド

ローカルに保存されたdockerイメージの一覧を取得

$docker images

docker hubにあるイメージを検索

$docker search <イメージ>

docker hubにあるイメージをローカルに取得する

$docker pull <イメージ>

コンテナでプロセスを起動する

$docker run -it <イメージ> /bin/bash

起動したコンテナはexitすれば自動的に終了

プロセスを確認する(停止されたものも含む)

$ docker ps -a

停止されたコンテナの削除

$ docker rm <コンテナid>

idは一意なので頭の4桁程度入力すればOK

何度もコンテナの起動と停止を行なっていると大量にプロセスが生まれるのでそれを避けたい

コンテナを全て削除

$ docker rm $(docker ps -aq)

exitと同時にコンテナの削除

$docker run --rm -it <イメージ> /bin/bash

ポートをホスト側に公開して起動

$ docker run -it -p 80:80 <イメージ> /bin/bash

外部ファイルを取り込む

$ docker run -it -v <ホスト側のパス>:<コンテナ側のパス> <イメージ> /bin/bash

Dockerfile作成してからの流れ

Dockerfileからイメージを作成

$ docker build -t <作成するイメージ名> .

作成したイメージからコンテナを起動

$ docker run --name <起動するコンテナ名> -d -p <イメージ名>

コンテナの停止

$ docker stop <コンテナ名>

hexoとやらを試してみた

hexo

hexo.io

githubのアカウントあれば誰でもブログを公開できる

まずはリポジトリ作成

[username].github.io というリポジトリを作成

hexoの準備

公式通りに hexoをグローバルにインストール

$ npm install hexo-cli -g

以下のコマンドでディレクトリを作成

$ hexo init blog

作成されたディレクトリに移動して、モジュールをインストール

$ cd blog
$ npm install

インストールできたら、以下のコマンドで起動

$ hexo server

http://localhost:4000/にアクセスすると雛形ができている

githubで公開

作成されたディレクトリ配下にあるconfig.ymlを弄る config.ymlではサイトのタイトル等を変更可能

deploy:
  type: git
  repo: https://github.com/[username]/[username].github.io.git
  branch: master

編集したら、以下のコマンドでデプロイ

hexo deploy -g

エラーがでた
どうやら、hexo-deployer-gitというモジュールをインストールしないといけないよう
ってことでインストール

$ npm install hexo-deployer-git --save

改めてデプロイ

$ hexo deploy -g

https:[username].github.io/にアクセスすると先程http://localhost:4000/でみたページにアクセス可能

※反映にそれなりに時間が掛かる

テーマの導入

hexo.io

こちらのサイトから気に入ったプラグインclone

例えば、これなら github.com READMEにあるようにコマンドを実行

$ git clone https://github.com/mkkhedawat/clexy themes/clexy
$ git clone https://github.com/mkkhedawat/clexy themes/clexy
$ yarn remove hexo-renderer-ejs
$ yarn add hexo-renderer-jade
$ yarn add hexo-prism-plugin

_config.ymlを編集

prism_plugin:
  mode: 'preprocess'    # realtime/preprocess
  theme: 'default'
  line_number: false    # default false
theme: clexy

デプロイ

hexo deploy -g

記事の投稿

以下のコマンドを実行

$ hexo new post 新規ページ

source/post/新規ページ.mdが作成されているので弄ってpushすれば記事が投稿できる

ところで、hexoってなんて読むんだろう・・・?

vagrant + Ubuntu 16.04 LTS でDjango開発環境を構築

はじめに

vagrantを使ってDjango開発環境を構築する

vagrantUbuntu 16.04 LTSの準備

ubuntu 16.04 LTSのboxを持っていることが前提

Vagrantfileの作成

$ vagrant init ubuntu/xenial64

作成されたVagrantfileのポートを設定

$ vim Vagrantfile

下記の部分を変更

config.vm.network "forwarded_port", guest: 8000, host: 8000

起動

$ vagrant init ubuntu/xenial64

接続

$ vagrant ssh

Djangoのインストール

pipのインストール

$ wget https://bootstrap.pypa.io/get-pip.py
$ sudo python3 get-pip.py
$ sudo pip install Django

Ubuntu 16.04 LTSにはデフォルトでpython3.5.2が入っている

バージョンの確認

$python3 -m django --version
1.11.5

Djangoを使ってみる

プロジェクトの作成

$ django-admin startproject mysite

作られたプロジェクトを見てみる

$ tree
.
├── get-pip.py
└── mysite
    ├── db.sqlite3
    ├── manage.py
    └── mysite
        ├── __init__.py
        ├── __pycache__
        │   ├── __init__.cpython-35.pyc
        │   ├── settings.cpython-35.pyc
        │   ├── urls.cpython-35.pyc
        │   └── wsgi.cpython-35.pyc
        ├── settings.py
        ├── urls.py
        └── wsgi.py

作成したプロジェクトのディレクトリに移動

$ cd mysite

Djangoを動かしてみる

python3 manage.py runserver 0.0.0.0:8000

ローカルマシンからアクセス http://127.0.0.1:8000/

It worked!

dein.vimを使ってみる

curl を使ってシェルをダウンロード

curl https://raw.githubusercontent.com/Shougo/dein.vim/master/bin/installer.sh -o /tmp/installer.sh

シェルの実行

sh /tmp/installer.sh ~/.vim/dein

"dein Scripts-----------------------------
if &compatible
set nocompatible " Be iMproved
endif

" Required:
set runtimepath+=/home/ubuntu/.vim/dein/repos/github.com/Shougo/dein.vim

" Required:
if dein#load_state('/home/ubuntu/.vim/dein')
call dein#begin('/home/ubuntu/.vim/dein')

" Let dein manage dein
" Required:
call dein#add('/home/ubuntu/.vim/dein/repos/github.com/Shougo/dein.vim')

" Add or remove your plugins here:
call dein#add('Shougo/neosnippet.vim')
call dein#add('Shougo/neosnippet-snippets')
" You can specify revision/branch/tag.
call dein#add('Shougo/vimshell', { 'rev': '3787e5' })

" Required:
call dein#end()
call dein#save_state()
endif

" Required:
filetype plugin indent on
syntax enable

" If you want to install not installed plugins onstartup.
"if dein#check_install()
" call dein#install()
"endif

"End dein Scripts-------------------------

コンソールに表示された↑を home直下の .vimrcに追記