Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Docker in testing

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article shows you how to use Docker in the test, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.

Next, let's take a look at how to use Docker in actual development and testing.

Use Docker to test static websites

Initial Dockerfile

Create new directories sample and nginx, and place the nginx configuration under the nginx directory.

├── sample │ ├── Dockerfile │ └── nginx │ ├── global.conf │ └── nginx.conf

Dockerfile

# Version: 0.01FROM ubuntuMAINTAINER HUANG "ituzhi@163.com" ENV REFRESHED_AT 2015-10-12RUN apt-get updateRUN apt-get-y-q install nginxRUN mkdir-p / var/www/htmlADD nginx/global.conf / etc/nginx/conf.d/ADD nginx/nginx.conf / etc/nginx/nginx.confEXPOSE 80 build Sample website and Nginx image

Sudo docker build-t HUANG/nginx.

Create a new folder website under the directory to store the web page file index.html

. ├── Dockerfile ├── nginx │ ├── global.conf │ └── nginx.conf └── website └── index.html

Web page file

Test websiteThis is a test website

Create the container and execute the command in the sample directory

Sudo docker run-d-p 80-- name web-v $PWD/website:/var/www/html/website HUANG/nginx nginx

-v mounts the host's $PWD/website directory as a volume under the container's / var/www/html/website. This allows you to modify the site directly in the host.

# build and test Web programs using Docker

FROM ubuntuMAINTAINER HUANG "ituzhi@163.com" RUN adduser web-- home/ home/web-- shell / bin/bash-- disabled-password-- gecos "RUN apt-get update-- fix-missingRUN apt-get-y install ruby ruby-dev build-essential redis-toolsRUN gem install-- no-rdoc-- no-ri sinatra json redisRUN mkdir-p / var/www/webappRUN chmod-R 777 / var/www/webappRUN chown-R web:web / var/www/webappUSER webEXPOSE 4567CMD [" / var/www/webapp/bin/webapp "]

Launch a new container sudo docker run-p 4567-d-name titusapp9-v $PWD/webapp:/var/www/webapp HUANG/sinatra

View the port on which the container is mapped to the host

Sudo docker port titusapp9 4567

0.0.0.0:49154

Access the webapp in the container

Huangyi@HP ~ / Practice/Docker/sinatra $curl-I-H 'Accept: application/json'\-d 'name=HUANG&status=bar' http://localhost:49154/jsonHTTP/1.1 200 OK Content-Type: text/html;charset=utf-8Content-Length: 36X-Xss-Protection: 1 Mode=blockX-Content-Type-Options: nosniffX-Frame-Options: SAMEORIGINServer: WEBrick/1.3.1 (Ruby/1.9.3/2013-11-22) Date: Wed, 21 Oct 2015 13:48:41 GMTConnection: Keep-Alive {"name": "HUANG", "status": "bar"}

The meaning of curl parameter

Usage: curl [options...]-I,-- include Include protocol headers in the output (Hmax F)-H,-- header LINE Custom header to pass to server (H) these two parameters pass the specified HTTP header to webapp-d,-- data DATA HTTP POST data (H)

It is equivalent to typing in a browser, except that curl calls the post method and the browser calls the get method.

Http://localhost:49154/json?name=HUANG&status=bar

The returned data is constructed by CGI

Require "rubygems" require "sinatra" require "json" class App

< Sinatra::Application set :bind, '0.0.0.0' get '/' do "DockerBook Test Sinatra app" end post '/json/?' do params.to_json endend 即echo请求数据。 #构建 Redis 镜像和容器 Dockerfile FROM ubuntuMAINTAINER HUANG "ituzhi@163.com"RUN apt-get updateRUN apt-get -y install redis-server redis-toolsEXPOSE 6379ENTRYPOINT ["/usr/bin/redis-server"]CMD [] ENTRYPOINT和CMD作用类似,相当于容器的自启动程序。 构建镜像 sudo docker build -t HUANG/redis . 从镜像构建容器 sudo docker run -d -p 6379 --name redis HUANG/redis 查看Redis的端口映射到宿主机哪个端口 huangyi@HP ~/Practice/Docker/redis $ sudo docker port redis 63790.0.0.0:49155 在宿主机上运行 Redis 客户端连接到容器中的 Redis 服务器端。 redis-cli -h 127.0.0.1 -p 49155 #让 Docker 容器互联 首先启动 Redis 容器,不指定端口 sudo docker run -d --name redis HUANG/redis 启动 WebApp 容器,连接到 Redis 容器上。 sudo docker run -p 4567 --name WebappDB --link redis:db -t -i -v $PWD/webapp:/var/www/webapp HUANG/sinatra /bin/bash redis:db 中的 redis 是要连接的容器, db 是连接后的别名。 WebappDB 容器可以访问 redis 容器的所有端口。 在 WebappDB 容器中查看连接父子容器后在 /etc/hosts 中做的改变 web@2c3f5f9a136f:/$ cat /etc/hosts 172.17.0.3 2c3f5f9a136f...172.17.0.2 db 第一项是 WebappDB 容器的 IP 地址和主机名。第二项是 redis 容器的 IP 地址和别名。 在 app.rb 中添加以下代码存储数据。 require "rubygems"require "sinatra"require "json"require "redis"class App < Sinatra::Application redis = Redis.new(:host =>

'db',: port = >' 6379') set: bind, '0.0.0.0' get' /'do "DockerBook Test Redis-enabled Sinatra app" end get'/ json' do params = redis.get "params" params.to_json end post'/ json/?' Do redis.set "params", [params] .to _ json params.to_json endend the above is how to use Docker in a test. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report