1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

dockerinstall→dockerfile作成→ECRへpush

Last updated at Posted at 2019-06-26

##まだ、Dockerfileを理解できていません。
Linux等初心者ですが、EC2でデプロイしていたAPIをコンテナ化したと思っています。
そこで/bin/bashでの実行コマンドとDockerfileの違いと留意点をメモ程度に残していきます。

##Python3.6の実行環境を作成してAPIを実行する
###手順1
Dockerfileを作成する前に、SSHで接続して実行コマンドを確認します。
EC2にDockerをインストールして、centosを稼働させて、/binb/bashを開く

xxx.sh
[root@ip-10-0-0-0 /]# yum install -y docker 
[root@ip-10-0-0-0 /]# service docker start

[root@ip-10-0-0-0 /]# docker pull centos
[root@ip-10-0-0-0 /]# docker run -it centos /bin/bash
[root@<CONTAINER ID> /]# mkdir app

###手順2
別ターミナルからホストへ接続する
ファイルをcontainerにファイルをコピーする

xxx.sh
[root@ip-10-0-0-0 /]# cd /home/ec2-user/docker/app
[root@ip-10-0-0-0 /]# docker cp sample.txt <CONTAINER ID>:/app/
[root@ip-10-0-0-0 /]# docker cp sample.py <CONTAINER ID>:/app/

###手順3
centos7にpython3.6をインストールする

xxx.sh
[root@2be128fcf8e0 /]# yum install -y https://centos7.iuscommunity.org/ius-release.rpm
[root@2be128fcf8e0 /]# yum search python36
[root@2be128fcf8e0 /]# yum install -y python36u python36u-libs python36u-devel python36u-pip
[root@2be128fcf8e0 /]# python3.6 -V
[root@2be128fcf8e0 /]# yum install -y python-pip unzip
[root@2be128fcf8e0 /]# yum clean all

###手順4
API実行環境を構築する
依存関係を解決して、APIを実行する

xxx.sh
[root@2be128fcf8e0 /]# pip3.6 install -r /app/sample.txt
[root@2be128fcf8e0 /]# curl https://s3.dualstack.us-east-1.amazonaws.com/aws-xray-assets.us-east-1/xray-daemon/aws-xray-daemon-linux-2.x.zip -o /app/xray.zip
[root@2be128fcf8e0 /]# unzip /app/xray.zip -d /app/
[root@2be128fcf8e0 /]# chmod +x /app/xray
[root@2be128fcf8e0 /]# nohup ./app/xray &
[root@2be128fcf8e0 /]# export ID="xxxx"
[root@2be128fcf8e0 /]# export HASH="xxxx"

[root@2be128fcf8e0 /]# python3.6 /sample.py &

##上記の/bin/bashコマンドをDockerfileにする
###手順5
ホストでDockerfileを作成する

[root@ip-10-0-0-0 /]# cd /home/ec2-user/docker/
[root@ip-10-0-0-0 docker]# vim Dockerfile
FROM centos
SHELL ["/bin/bash", "-c"]
RUN mkdir app
ADD ./app/sample.txt /app/
ADD ./app/sample.py /app/
RUN yum install -y https://centos7.iuscommunity.org/ius-release.rpm
RUN yum install -y python36u python36u-libs python36u-devel python36u-pip
RUN yum install -y python-pip unzip
RUN yum clean all
RUN pip3.6 install -r /app/sample.txt
RUN curl https://s3.dualstack.us-east-1.amazonaws.com/aws-xray-assets.us-east-1/xray-daemon/aws-xray-daemon-linux-2.x.zip -o /app/xray.zip
RUN unzip /app/xray.zip -d /app/
RUN chmod +x /app/xray
ENV ID xxxx
ENV HASH xxxx
EXPOSE 80
ENTRYPOINT ["/bin/bash", "-c", "nohup /app/xray & python3.6 /app/sample.py"

##注意点
ADDを行うとき、ホストのディレクトリはDockerfileからの相対パスで、contaierは絶対パスで指定する

###手順6
コンテナイメージを作成する

[root@ip-10-0-0-0 docker]# sudo docker build -t hoge/sample .
[root@ip-10-0-0-11 docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
hoge/sample         latest              dbf27332668d        About a minute ago   766MB

###手順6
動作確認を行う

[root@ip-10-0-0-11 docker]# docker run -p 8080:80 hoge/sample
[root@ip-10-0-0-11 docker]# curl http://localhost:8080
OK

Clean upを停止する

[root@ip-10-0-0-11 docker]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
3a29b60ce0d0        hoge/sample         "/bin/bash -c 'nohup…"   2 minutes ago       Up 2 minutes        0.0.0.0:8080->80/tcp   upbeat_bhaskara
[root@ip-10-0-0-11 docker]# docker stop 3a29b60ce0d0
[root@ip-10-0-0-11 docker]# docker rm 3a29b60ce0d0
[root@ip-10-0-0-11 docker]# docker rmi hoge/sample

#imageをECRにpushする

[root@ip-10-0-0-11 docker]# aws ecr create-repository --repository-name sample
{
    "repository": {
        "registryId": "xxxxxxxxxxxx",
        "repositoryName": "sample",
        "repositoryArn": "arn:aws:ecr:us-west-2:986846499074:repository/sample",
        "createdAt": 1561552246.0,
        "repositoryUri": "xxxxxxxxxxxx.dkr.ecr.us-west-2.amazonaws.com/sample"
    }
}
[root@ip-10-0-0-11 docker]# docker tag hoge/sample xxxxxxxxxxxx.dkr.ecr.us-west-2.amazonaws.com/sample
[root@ip-10-0-0-11 docker]# docker images
REPOSITORY                                            TAG                 IMAGE ID            CREATED             SIZE
xxxxxxxxxxxx.dkr.ecr.us-west-2.amazonaws.com/sample   latest              dbf27332668d        30 minutes ago      766MB
hoge/sample                                           latest              dbf27332668d        30 minutes ago      766MB

[root@ip-10-0-0-11 docker]# aws ecr get-login --no-include-email
docker login -u AWS -p eyJw...
[root@ip-10-0-0-11 docker]# docker login -u AWS -p eyJw...
Login Succeeded
[root@ip-10-0-0-11 docker]# docker push xxxxxxxxxxxx.dkr.ecr.us-west-2.amazonaws.com/sample
latest: digest: sha256:0a3...

##おまけ
DockerimageからDokerfileの内容を除くことができます。

[root@ip-10-0-0-11 docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
hoge/sample         latest              dbf27332668d        About a minute ago   766MB
[root@ip-10-0-0-11 docker]# docker history dbf27332668d --no-trunc
IMAGE                                                                     CREATED             CREATED BY                                                                                                                                                                                                SIZE                COMMENT
sha256:dbf27332668d18bc48a2cb30468aee4b16c8bea6f596a98b312e172c4366aa84   13 minutes ago      /bin/bash -c #(nop)  ENTRYPOINT ["/bin/bash" "-c" "nohup /app/xray & python3.6 /app/sample.py"]                                                                                                 0B
sha256:220eca136f7db5e9c9f224551932e49a79959230b4f679dc2ddf2292a418e351   13 minutes ago      /bin/bash -c #(nop)  EXPOSE 80                                                                                                                                                                            0B
sha256:df33c7600f7f5d08715c97a5a905b17f58a7cd88ce759fd9d2aea63aea745dbb   13 minutes ago      /bin/bash -c #(nop)  ENV HASH=xxxx                                                                                                                                                                0B
sha256:f2ab49e976a045ed27096e7dea36b561ef1fb2e3b4dcb7b3253c8cd8b8c88507   13 minutes ago      /bin/bash -c #(nop)  ENV ID=xxxx                                                                                                                                         0B
sha256:5c0eb56c3535bca518ef49b5c20dd2826852cd3873208aa2e70622cfb53516d0   13 minutes ago      /bin/bash -c chmod +x /app/xray                                                                                                                                                                           7.33MB
sha256:92e923a246f3515851fb4b456ff931dea201be7de61a1262016522c3d7560865   13 minutes ago      /bin/bash -c unzip /app/xray.zip -d /app/                                                                                                                                                                 7.34MB
sha256:312af55cdcd4788a870189a30d6a8cce66f7e389c00e35bfec82c30c454c68e0   13 minutes ago      /bin/bash -c curl https://s3.dualstack.us-east-1.amazonaws.com/aws-xray-assets.us-east-1/xray-daemon/aws-xray-daemon-linux-2.x.zip -o /app/xray.zip                                                       2.56MB
sha256:6e376cd82ad2b5bf121a631f6f86a499cf0bcce5a0f48744d4ca33998167aa36   13 minutes ago      /bin/bash -c pip3.6 install -r /app/sample.txt                                                                                                                                                      64.4MB
sha256:0e74d5056f1eeb936a9a65a1029d35b8f66a80a9a62159ea2f961da18d93cd86   13 minutes ago      /bin/bash -c yum clean all                                                                                                                                                                                22.7MB
sha256:d7d36ad40d4befb6271606b43dd02e9d59cb04ef367339524d3d5bd502fef4d8   13 minutes ago      /bin/bash -c yum install -y python-pip unzip                                                                                                                                                              153MB
sha256:2d5b649ef91d757fca8ab08343f2fa4246d9fc0f0eb1357ace1226437699b3ef   13 minutes ago      /bin/bash -c yum install -y python36u python36u-libs python36u-devel python36u-pip                                                                                                                        203MB
sha256:3a0dd4e152f70eaf44ac5452aa37bfef047cc0e5f823e2d8e132dea29aec4788   14 minutes ago      /bin/bash -c yum install -y https://centos7.iuscommunity.org/ius-release.rpm                                                                                                                              104MB
sha256:a3dbf80799613b9cf11da134b395f14adc59b62fc888057a5fb808736fb8ce5f   14 minutes ago      /bin/bash -c #(nop) ADD file:60d101d667a50acd110f26c14c5fa29f0d3302b4cc23afcfac055d1fdb8c9635 in /app/                                                                                                    4.54kB
sha256:9daa21003b7ec0402969dac14ad87e0f09f875af46b8ff24df5586484140718d   14 minutes ago      /bin/bash -c #(nop) ADD file:509b139751359e0a747e3d5f2831a82b53fbe23f49494140cd3a85ac7c00b7b0 in /app/                                                                                                    41B
sha256:b826b60c9dadfee14b6b52835ee73c432f2c7f8604939478545de4cea2837d81   14 minutes ago      /bin/bash -c mkdir app                                                                                                                                                                                    0B
sha256:e9fac8bf88041e83cb12c6c031c1924f7fa1b1766ef3b804d551b1743d4ecc10   14 minutes ago      /bin/bash -c #(nop)  SHELL [/bin/bash -c]                                                                                                                                                                 0B
sha256:9f38484d220fa527b1fb19747638497179500a1bed8bf0498eb788229229e6e1   3 months ago        /bin/sh -c #(nop)  CMD ["/bin/bash"]                                                                                                                                                                      0B
<missing>                                                                 3 months ago        /bin/sh -c #(nop)  LABEL org.label-schema.schema-version=1.0 org.label-schema.name=CentOS Base Image org.label-schema.vendor=CentOS org.label-schema.license=GPLv2 org.label-schema.build-date=20190305   0B
<missing>                                                                 3 months ago        /bin/sh -c #(nop) ADD file:074f2c974463ab38cf3532134e8ba2c91c9e346457713f2e8b8e2ac0ee9fd83d in /                                                                                                          202MB

```

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?