Docker教程: Ruby应用程序示例

2018-10-1914:24:24后端程序开发Comments2,734 views字数 1761阅读模式

Docker允许执行Ruby应用程序。 在下面的例子中,我们将演示如何创建一个使用Docker来运行Ruby文件。 它包括以下步骤:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6915.html

1. 创建一个目录用来组织应用程序文件文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6915.html

yiibai@ubuntu:~$ mkdir -p /home/yiibai/docker/ruby-docker-app
Shell

2. 更改进入工作目录文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6915.html

yiibai@ubuntu:~$ cd /home/yiibai/docker/ruby-docker-app
Shell

3. 创建一个 Dockerfile 文件文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6915.html

使用以下命令:vi /home/yiibai/docker/ruby-docker-app/Dockerfile 打文件写入以下内容 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6915.html

FROM ruby:2
COPY . /var/www/ruby  
WORKDIR /var/www/ruby  
CMD ["ruby","index.rb"]
Shell

4. 创建Ruby文件: index.rb文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6915.html

使用以下命令:vi /home/yiibai/docker/ruby-docker-app/index.rb 打文件写入以下内容 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6915.html

puts "Hello, This is Docker ruby application!\n";
Scala

5. 构建 Ruby 的 Docker映像文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6915.html

现在,创建这个Ruby应用程序的Docker映像。 以下命令用于创建Docker映像。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6915.html

yiibai@ubuntu:~/docker/ruby-docker-app$ sudo docker build -t ruby-app .
Shell

执行上面代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6915.html

yiibai@ubuntu:~/docker/ruby-docker-app$ sudo docker build -t ruby-app .
Sending build context to Docker daemon  3.072kB
Step 1/4 : FROM ruby:2
2: Pulling from library/ruby
10a267c67f42: Already exists
fb5937da9414: Already exists
9021b2326a1e: Already exists
dbed9b09434e: Already exists
a24b67985d10: Pull complete
6e2ffc998aca: Pull complete
9cc06f02044a: Pull complete
317603e4a1ed: Pull complete
Digest: sha256:fd5849fbbedd7308ea82feedb0ce190a0db8f9e2b76773957033a3beb3a5cab6
Status: Downloaded newer image for ruby:2
 ---> 4bcabaf0fe3c
Step 2/4 : COPY . /var/www/ruby
 ---> 9ed3248ed52e
Removing intermediate container 65d355a9c4a7
Step 3/4 : WORKDIR /var/www/ruby
 ---> 9f7f3c704acb
Removing intermediate container f64d2717f276
Step 4/4 : CMD ruby index.rb
 ---> Running in 2870476e9d07
 ---> 9df42d04cdb2
Removing intermediate container 2870476e9d07
Successfully built 9df42d04cdb2
Successfully tagged ruby-app:latest
yiibai@ubuntu:~/docker/ruby-docker-app$
Shell

6. 运行Ruby 的 Docker映像文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6915.html

yiibai@ubuntu:~/docker/ruby-docker-app$ sudo docker run ruby-app
Shell

运行docker映像后,执行ruby脚本,并在控制台屏幕中打印一条消息,如下所示 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6915.html

yiibai@ubuntu:~/docker/ruby-docker-app$ sudo docker run ruby-app
Hello, This is Docker ruby application!
yiibai@ubuntu:~/docker/ruby-docker-app$
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/6915.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/6915.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定