3
2

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 3 years have passed since last update.

Dockerでamazonlinuxを利用してnodejsの環境を作成する

Last updated at Posted at 2021-08-23

##Dockerfileの書き方
参考記事

##amazonlinuxをイメージにコンテナを作成
なるべくEC2と同じ環境で作業したいためamazonlinuxでの環境構築を行う

ローカルの作業ディレクトリ> docker run -it amazonlinux
Unable to find image 'amazonlinux:latest' locally
latest: Pulling from library/amazonlinux
85ee02fe1a09: Pull complete
Digest: sha256:2e42b5d170dbb6d54d35b3b64627480de9687b52cfbab2086267a0fd2acd7bef
Status: Downloaded newer image for amazonlinux:latest
bash-4.2# ls -lta
total 60
drwxr-xr-x   5 root root  360 Jul  3 11:32 dev
dr-xr-xr-x  13 root root    0 Jul  3 11:32 sys
dr-xr-xr-x 168 root root    0 Jul  3 11:32 proc
drwxr-xr-x   1 root root 4096 Jul  3 11:32 .
drwxr-xr-x   1 root root 4096 Jul  3 11:32 ..
-rwxr-xr-x   1 root root    0 Jul  3 11:32 .dockerenv
drwxr-xr-x   1 root root 4096 Jul  3 11:32 etc
drwxr-xr-x   2 root root 4096 Jun 17 01:48 run
drwxr-xr-x  18 root root 4096 Jun 17 01:48 var
lrwxrwxrwx   1 root root    7 Jun 17 01:48 bin -> usr/bin
lrwxrwxrwx   1 root root    7 Jun 17 01:48 lib -> usr/lib
lrwxrwxrwx   1 root root    9 Jun 17 01:48 lib64 -> usr/lib64
lrwxrwxrwx   1 root root    8 Jun 17 01:48 sbin -> usr/sbin
drwxr-xr-x  13 root root 4096 Jun 17 01:48 usr
drwxr-xr-x   2 root root 4096 Jun 17 01:48 local
drwxrwxrwt   2 root root 4096 Jun 17 01:48 tmp
dr-xr-xr-x   2 root root 4096 Apr  9  2019 boot
drwxr-xr-x   2 root root 4096 Apr  9  2019 home
drwxr-xr-x   2 root root 4096 Apr  9  2019 media
drwxr-xr-x   2 root root 4096 Apr  9  2019 mnt
drwxr-xr-x   2 root root 4096 Apr  9  2019 opt
dr-xr-x---   2 root root 4096 Apr  9  2019 root
drwxr-xr-x   2 root root 4096 Apr  9  2019 srv

無事にamazonlinuxをイメージとしてコンテナの起動ができてるはず

nodejsをインストールする

とりあえず下記コマンドを実行

yum update -y
~省略~
Complete!

nodejsのインストールを実施

bash-4.2# yum install nodejs npm
Loaded plugins: ovl, priorities
No package nodejs available.
No package npm available.
Error: Nothing to do

失敗、通常yumではnodejsはインストール出来ない模様
公式に記載されているamazonlinux用の作業を実践

bash-4.2# curl -fsSL https://rpm.nodesource.com/setup_16.x | bash -

## Installing the NodeSource Node.js 16.x repo...


## Inspecting system...

+ rpm -q --whatprovides redhat-release || rpm -q --whatprovides centos-release || rpm -q --whatprovides cloudlinux-release || rpm -q --whatprovides sl-release || rpm -q --whatprovides fedora-release
+ uname -m

## Confirming "el7-x86_64" is supported...

+ curl -sLf -o /dev/null 'https://rpm.nodesource.com/pub_16.x/el/7/x86_64/nodesource-release-el7-1.noarch.rpm'

## Downloading release setup RPM...

+ mktemp
+ curl -sL -o '/tmp/tmp.z8z9Q1v7OL' 'https://rpm.nodesource.com/pub_16.x/el/7/x86_64/nodesource-release-el7-1.noarch.rpm'

## Installing release setup RPM...

+ rpm -i --nosignature --force '/tmp/tmp.z8z9Q1v7OL'

## Cleaning up...

+ rm -f '/tmp/tmp.z8z9Q1v7OL'

## Checking for existing installations...

+ rpm -qa 'node|npm' | grep -v nodesource

## Run `sudo yum install -y nodejs` to install Node.js 16.x and npm.
## You may also need development tools to build native addons:
     sudo yum install gcc-c++ make
## To install the Yarn package manager, run:
     curl -sL https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
     sudo yum install yarn

NodeSourceのリポジトリを追加する事で実行可能になる

参考記事

nodejsのインストール作業

bash-4.2# yum install -y nodejs
~省略~
Installed:
  nodejs.x86_64 2:16.4.1-1nodesource

Complete!
bash-4.2# node -v
v16.4.1

##Dockerfileへとまとめる
Expressを利用する環境を作成し確認する。

FROM amazonlinux

WORKDIR /usr/src/app

RUN yum update -y && \
    curl -fsSL https://rpm.nodesource.com/setup_16.x | bash && \
    yum install -y nodejs && \
    # Expressの環境構築
    cd /usr/src/app && \
    npm init -y  && \
    npm install express --save

COPY . .

EXPOSE 8080
CMD [ "node", "server.js" ]

3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?