Visual Studio for MacからDockerコンテナにアプリケーションを配置する

概要

Visual Studio for Mac 7.2 からDockerサポートが追加されたので試してみる

環境

実践

プロジェクト作成

テスト用のプロジェクトを作成する。 f:id:kyokushi7ya:20171030021102p:plain

プロジェクトを開く

作成したプロジェクトをVisual Studio で開く f:id:kyokushi7ya:20171030020843p:plain

Dokcerサポートを追加する

プロジェクト右クリック→追加→Dockerサポートを追加 として、Dokcerサポートを追加する。 f:id:kyokushi7ya:20171030020905p:plain

そうすると以下のようになるはずです。 f:id:kyokushi7ya:20171030020922p:plain これで準備は整いました。

実行してみる

それでは実行してみます。 f:id:kyokushi7ya:20171030021014p:plain 無事に表示できました。

 ~  docker ps                                                         月 10/30 02:13:34 2017
CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS              PORTS                   NAMES
0cdb7f3a9055        dockertest:dev      "tail -f /dev/null"   7 minutes ago       Up 7 minutes        0.0.0.0:32779->80/tcp   dockercompose2839551963734038042_dockertest_1
 ~                                                                    月 10/30 02:13:38 2017

ちゃんとDocker上で動いています。

課題

プロジェクトを作成する際、最初はコマンドdotnet new mvcで作成してたんですが、そうするとデバッグ実行しようとすると失敗してしまいました。
原因はわかりませんが。。。

ミリ秒を切り捨てる拡張メソッド

概要

ミリ秒を切り捨てる拡張メソッドを作成、動作検証してみる。

ソース

using System;

namespace ExtensionMethods
{
    public static class DateTimeExtensions
    {
        public static DateTime TruncationMillsecond(this DateTime date)
        {
            return new DateTime(date.Year, date.Month, date.Day, date.Hour, date.Minute, date.Second, 0);
        }
    }
}

使ってみる

using System;

namespace ExtensionMethods
{
    class Program
    {
        static void Main(string[] args)
        {
            // 現在時刻を表示
            DateTime date = DateTime.Now;
            Console.WriteLine($"date:{date.ToString("yyyyMMdd HHmmss fff")}");

            // ミリ秒を切り捨て
            DateTime date2 = date.TruncationMillsecond();
            Console.WriteLine($"date2:{date2.ToString("yyyyMMdd HHmmss fff")}");
        }
    }
}
date:20171029 232733 143  
date2:20171029 232733 000

デフォルトシェルがBashじゃないとVisual Studio for Macでコンソールアプリをデバッグしようとするとエラーになる

環境情報

内容

タイトルの通り。 VS for Macで、C#で作成したコンソールアプリをデバッグしてみたところエラーになってしまった。 f:id:kyokushi7ya:20171029221145p:plain f:id:kyokushi7ya:20171029214718p:plain

ter=vscode --connection=/var/folders/sp/mwvxp3qs2x590qp6vj7fjlcr0000gn/T/CoreFxPipe_vsdbg-ui-be8ed2c9c49644b4b22eb0e0ad822650; echo $? > /var/folders/sp/mwvxp3qs2x590qp6vj7fjlcr0000gn/T/tmp607f41ec.tmp; echo; read -p 'Press any key to continue...' -n1; exit
fish: $? is not the exit status. In fish, please use $status.
clear; cd "/Applications/Visual Studio.app/Contents/Resources/lib/monodevelop/AddIns/DotNetCore.Debugger/Adapter"; "/Applications/Visual Studio.app/Contents/Resources/lib/monodevelop/AddIns/DotNetCore.Debugger/Adapter/vsdbg" --interpreter=vscode --connection=/var/folders/sp/mwvxp3qs2x590qp6vj7fjlcr0000gn/T/CoreFxPipe_vsdbg-ui-be8ed2c9c49644b4b22eb0e0ad822650; echo $? > /var/folders/sp/mwvxp3qs2x590qp6vj7fjlcr0000gn/T/tmp607f41ec.tmp; echo; read -p 'Press any key to continue...' -n1; exit

原因

これもタイトルの通りで、デフォルトシェルがBashでなく、Fishを使っているから。

解決方法

Bashを使う。 、、、だとあんまりなので以下のように対応

  • デフォルトシェルはBash
  • iTerm2の設定でFishを使うように設定 f:id:kyokushi7ya:20171029221040p:plain

これだけでiTerm2実行時はFishが、デバッグ実行時にはBashが起動するようになる。 普段使っているのがITerm2なので自分的にはこれで解決。

一応、公式も対応するつもりのようですが、保留中になってますね。。。

https://developercommunity.visualstudio.com/content/problem/58220/visual-studio-2017-console-projects-are-incompatib.html

いつになるのだろう。。。

Powerlineインストールでエラーになった際の対処方法

概要

MacにPowerlineを導入した際に困った点をメモする

環境情報

事象

Powerlineをインストールしようとしたことろ、エラーとなった

 

❯ sudo pip install powerline-status==2.4
Exception:
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/pip/basecommand.py", line 215, in main
status = self.run(options, args)
File "/Library/Python/2.7/site-packages/pip/commands/install.py", line 342, in run
prefix=options.prefix_path,
File "/Library/Python/2.7/site-packages/pip/req/req_set.py", line 784, in install
**kwargs
File "/Library/Python/2.7/site-packages/pip/req/req_install.py", line 851, in install
self.move_wheel_files(self.source_dir, root=root, prefix=prefix)
File "/Library/Python/2.7/site-packages/pip/req/req_install.py", line 1064, in move_wheel_files
isolated=self.isolated,
File "/Library/Python/2.7/site-packages/pip/wheel.py", line 377, in move_wheel_files
clobber(source, dest, False, fixer=fixer, filter=filter)
File "/Library/Python/2.7/site-packages/pip/wheel.py", line 323, in clobber
shutil.copyfile(srcfile, destfile)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 83, in copyfile
with open(dst, 'wb') as fdst:
IOError: [Errno 1] Operation not permitted: '/System/Library/Frameworks/Python.framework/Versions/2.7/bin/powerline'

原因

調べたところ、System Integrity Protection (SIP)が原因になっている模様 SIPOS X El Capitanで導入されたセキュリティで、 これにより/Systemが変更できなくなってしまっており、」「Operation not permitted」が発生する。

解決方法

一時的にSIPを無効にすればよい  ⌘+Rを押しながら再起動し、コンソール画面で以下を入力

 # csrutil disable 
 # reboot

再びインストールを実行するとエラーなく成功した。 インストール終了後、SIPはもとに戻す。

 # csrutil enable 
 # reboot