ritarock’s blog

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

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に追記

Ubuntu 16.04 LTS にdockerをインストールする

sudo apt-get update
apt-get install -y software-properties-common
sudo apt-get 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 "deb [arch=amd64]
https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
sudo apt-get update
sudo apt-get install docker-ce
docker --version