LoginSignup
0
0

More than 3 years have passed since last update.

AWS上にDockerで環境構築する

Last updated at Posted at 2020-10-28

コンテナをAWSに送る3つの方法

・Dockerレジストリを使う(Docker hub)
・Dockerfileを送る
・Docker imageをtarにして送る

ホストのEC2にSSHでログインする

シェルを使う時にSSHでアクセスする。
SSHでログインすると、AWSのインスタンスにDockerをインストールすることなどができる。

SSH・・・secure shell
secureにシェルを起動する

ssh -i xx.pem username@hostname

xx.pem ・・・ サーバーを立てた際にダウンロードした鍵ファイル
-i ・・・ 鍵ファイルを指定するためのオプション

例)
ssh -i mydocker.pem ubuntu@ec2 ・・・ .amazonaws.com

AWSのインスタンスにDockerをインストールする

・SSHでサーバーにアクセスする。
 ssh -i xx.pem username@hostname
・パッケージをアップデート
 sudo apt-get update
・ dockerをインストール
 sudo apt-get install docker.io

※sudo gpasswd -a ubuntu docker
dockerというグループを作り、そこにubuntuを入れることで、グループに属している人はdockerを使えるようになる。
ubuntuのサーバーでdockerを使う時は、基本的に必ずやる
(sudoを付けなくても実行できるようになる)

Docker imageをtarにして送り、コンテナを作る

Docker imageをtarにして送る。

tarファイルに変換する

docker save イメージID > 任意のファイル名.tar
docker save fd867gy7fg > myimage.tar

sftpでAWSにアクセスする

sftpでアクセスすることでファイルを転送させることができる。

sftp ・・・ Secure File Transfer Protocol

sftp -i xx.pem username@hostname
例)
ssh -i mydocker.pem ubuntu@ec2 ・・・ .amazonaws.com

転送する

・put local/path [remote/path]
put temp_folder/myimage.tar /home/ubuntu
ローカルのファイルをリモートサーバー(EC2)にファイルを送る
※リモートのパスを指定しない場合、sftpでログインした場合のカレントディレクトリに転送される

・get local/path [remote/path]
リモートサーバー(EC2)からファイルをとってくる。

.tarからDocker imageに戻す

sshでAWSにアクセスし、docker loadでイメージに戻す。
・ssh -i xx.pem username@hostname
・docker load < ファイル名.tar
・docker run でコンテナにする。

例)
docker load < myimage.tar

Dockerfileを転送する方法でコンテナを作る

・sftpでアクセスし、dockerfileを転送する。
 sftp -i xxx.pem ubuntu@[hostname]
 put local/path [remote/path]

・sshでアクセスし、buildしコンテナを作る
 ssh -i xxx.pem ubuntu@[hostname]
 (新しくフォルダを作り、ビルドコンテクストを作りそこにDockerfileを置く)
 docker build .

最後に

この記事は、かめさん( https://twitter.com/usdatascientist?s=21 )のudemyのdocker講座の( https://www.udemy.com/share/103aTRAEMfeVhaTXoB/ )書き起こしです。

※あくまで、自分のための書き残しとして投稿しているので、講座と異なる部分を含んでいる可能性があります。

かめさんのブログ( https://datawokagaku.com/docker_lecture/ )
 

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