Data Engineering/Docker

[Docker] Dockerfile, docker build, Ruby

snoony 2024. 6. 5. 12:34
[rocky@localhost ~]$ cd docker_img
[rocky@localhost docker_img]$ vi Dockerfile 
[rocky@localhost docker_img]$ docker build .
[+] Building 2.3s (5/5) FINISHED                                                                 docker:default
 => [internal] load build definition from Dockerfile                                                       0.0s
 => => transferring dockerfile: 76B                                                                        0.0s
 => [internal] load metadata for docker.io/library/alpine:latest                                           2.1s
 => [internal] load .dockerignore                                                                          0.0s
 => => transferring context: 2B                                                                            0.0s
 => CACHED [1/1] FROM docker.io/library/alpine:latest@sha256:77726ef6b57ddf65bb551896826ec38bc3e53f75cdde  0.0s
 => exporting to image                                                                                     0.0s
 => => exporting layers                                                                                    0.0s
 => => writing image sha256:26e90fec456ffbef63e3fbfa9881975c70c7f6b05a1a86ec0124c95c535994c7               0.0s
[rocky@localhost docker_img]$

Dockerfile

FROM alpine
CMD ["echo","hello world"]

Docker rmi

존재하는 도커 이미지들 삭제

[rocky@localhost docker_img]$ docker images
REPOSITORY                  TAG            IMAGE ID       CREATED         SIZE
redis                       latest         1a83fd5edeed   13 days ago     117MB
<none>                      <none>         26e90fec456f   13 days ago     7.79MB
wordpress                   latest         c4d738408447   4 weeks ago     685MB
mysql                       5.7            5107333e08a8   5 months ago    501MB
hello-world                 latest         d2c94e258dcb   13 months ago   13.3kB
mysql                       8.0.27         3218b38490ce   2 years ago     516MB
mariadb                     10.6.4-focal   12e05d5da3c5   2 years ago     409MB
ubuntu                      16.04          b6f507652425   2 years ago     135MB
ubuntu                      14.04          13b66b487594   3 years ago     197MB
teamlab/pydata-tensorflow   0.1            7bdf5d7e0191   7 years ago     3.08GB
[rocky@localhost docker_img]$ docker rmi 7b 13b b6 12 32 d2 51 c4 1a

Docker image 이름 부여하기

docker build -t 이미지이름

[rocky@localhost docker_img]$ docker build -t helloworld:lastest ./

docker run -it 이미지이름

이미지이름으로 빌드하기

Sinatra web app

[rocky@localhost ~]$ mkdir docker_img2
[rocky@localhost ~]$ cd docker_img2
[rocky@localhost docker_img2]$ vi Gemfile
[rocky@localhost docker_img2]$ vi app.rb

Gemfile

source "https://rubygems.org"
gem "sinatra"

app.rb

require "sinatra"
require "socket"

get '/' do
  Socket.gethostname
end

Ruby app 실행하기

[rocky@localhost docker_img2]$ gem install bundler
Fetching bundler-2.5.11.gem
Successfully installed bundler-2.5.11
Parsing documentation for bundler-2.5.11
Installing ri documentation for bundler-2.5.11
Done installing documentation for bundler after 0 seconds
1 gem installed
[rocky@localhost docker_img2]$
[rocky@localhost docker_img2]$ bundle exec ruby app.rb
Sinatra could not start, the "rackup" gem was not found!

Add it to your bundle with:

    bundle add rackup

or install it with:

    gem install rackup

[rocky@localhost docker_img2]$ gem install rackup
Fetching rackup-2.1.0.gem
Fetching webrick-1.8.1.gem
Successfully installed webrick-1.8.1
Successfully installed rackup-2.1.0
Parsing documentation for webrick-1.8.1
Installing ri documentation for webrick-1.8.1
Parsing documentation for rackup-2.1.0
Installing ri documentation for rackup-2.1.0
Done installing documentation for webrick, rackup after 0 seconds
2 gems installed
[rocky@localhost docker_img2]$
[rocky@localhost docker_img2]$ docker run --rm -p 4567:4567 -v $PWD:/usr/src/app -w /usr/src/app ruby bash -c "bundle install && bundle exec ruby app.rb -o 0.0.0.0"

최초 실행했을 때 이런 오류가 나서

bundle app rackup, gem install rackup 명령어들을 차례로 실행해주었다.

 

localhost:4567 실행하면

Ruby : ubuntu 배포하기

  1. ubuntu 설치
  2. ruby 설치
  3. 소스 복사
  4. Gem 패키지 설치
  5. Sinatra 서버 실행

Dockerfile

# ubuntu install
FROM ubuntu:latest
RUN apt-get -y update

# ruby install
RUN apt-get -y install ruby
RUN gem install bundler

# source copy
COPY . /usr/src/app

# Gem package install(running directory set)
WORKDIR /usr/src/app
RUN bundle install

# Server run
EXPOSE 4567
CMD bundle exec ruby app.rb -o 0.0.0.0

실행 성공

도커 이미지가 생성된 것을 확인할 수 있다.

[rocky@localhost docker_img2]$ docker run -d -p 8080:4567 app
923ade02ce511d958b096e78775148a2ee2cf4d8a4d32e0db14825722afbaa1c
[rocky@localhost docker_img2]$ docker ps
CONTAINER ID   IMAGE              COMMAND                   CREATED         STATUS         PORTS                                       NAMES
923ade02ce51   app                "/bin/sh -c 'bundle …"   6 seconds ago   Up 6 seconds   0.0.0.0:8080->4567/tcp, :::8080->4567/tcp   sharp_payne
c775ecdc257d   wordpress:latest   "docker-entrypoint.s…"   20 hours ago    Up 2 hours     0.0.0.0:80->80/tcp, :::80->80/tcp           wp3-wordpress-1
ceaa8c944f68   mysql:8.0.27       "docker-entrypoint.s…"   21 hours ago    Up 2 hours     3306/tcp, 33060/tcp                         wp3-db-1