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-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article is about how to use Docker in testing. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

5.1 use Docker to test static websites (Nginx)

Name the project Sample

First of all, establish a build environment.

Mkdir samplecd sampletouch Dockerfile

Download two nginx configuration files configured by the author in the build environment:

Mkdir nginx & & cd nginxwget https://raw.githubusercontent.com/jamtur01/dockerbook-code/master/code/5/sample/nginx/global.conf wget https://raw.githubusercontent.com/jamtur01/dockerbook-code/master/code/5/sample/nginx/nginx.conf

Write Dockerfile

FROM ubuntu:14.04MAINTAINER Ivan Jiangzhi "ivanjz93@163.com" ENV REFRESHED_AT 2016-07-09RUN 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

In the nginx.conf configuration file, the daemon off; option prevents Nginx from entering the background, forcing it to run in the foreground. This is because if you want to keep the Docker container active, the processes you need to run in it cannot be interrupted. By default, Nginx starts as a daemon, which causes the container to run only briefly. After the daemon is started by fork, the original process that initiated the daemon exits and the container stops.

Build Sample

Docker build-t ivan/nginx.

After the construction is complete, you can use docker history to view the build steps:

Docker history ivan/nginx

Download the html written by the author (or write one yourself)

Mkdir website & & cd websitewget https://raw.githubusercontent.com/jamtur01/dockerbook-code/master/code/5/sample/website/index.html

Create a container and run

Docker run-d-p 80-- name website-v $PWD/website:/var/www/html/website ivan/nginx nginx

The-v option mounts the host's directory into the container as a volume.

A volume is a directory selected in one or more containers that can bypass a hierarchical federated file system to provide persistent or shared data for Docker. This means that changes to the volume take effect directly and bypass the mirror. When you commit or create a mirror, the volume is not included in the mirror.

Consider using volumes when:

Want to develop and test the code at the same time

The code changes frequently and I don't want to reconstruct the image during the development process.

You want to share code among multiple containers.

-v specifies the source directory of the volume (host directory) and the directory in the container, which are used: split. If the destination directory does not exist, Docker automatically creates one.

You can also specify the read and write status of the destination directory by adding rw or ro to the destination directory:

Docker run-d-p 80-- name website-v $PWD/website:/var/www/html/website:ro ivan/nginx nginx

The above example makes the destination directory read-only.

After the docker run is successful, visit the host IP in the browser: map the port to see the page. Refresh the browser immediately after modifying $PWD/website/index.html to save.

5.2 build and test Web applications (Sinatra + Redis)

1. Build Sinatra applications

Create a sinatra build environment:

Mkdir sinatracd sinatratouch Dockerfile

Write Dockerfile, examples from the book:

FROM ubuntu:14.04MAINTAINER James Turnbull james@example.comENV REFRESHED_AT 2014-6-1RUN apt-get updateRUN apt-get-y install ruby ruby-dev build-essential redis-toolsRUN gem install-- no-rdoc-- no-ri sinatra json redisRUN mkdir-p / opt/webappEXPOSE 4567CMD ["/ opt/webapp/bin/webapp"]

Build an image

Docker build-t ivan/sinatra.

2. Create a Sinatra container

Download the source code for the Sinatra Web application written by the author:

Wget-- cut-dirs=3-nH-- no-parent-- no-check-certificate http://dockerbook.com/code/5/sinatra/webapp/

An error will be reported when downloading using the command in the book. Press the error prompt to add the-no-check-certificate parameter and download it successfully.

Add execute permissions for webapp/bin/webapp:

Chmod + x $PWD/webapp/bin/webapp

Start the container and hang $PWD/webapp in the container by volume:

Docker run-d-p 4567-- name webapp-v $PWD/webapp:/opt/webapp ivan/sinatra

The problems and solutions I encountered in the operation

An error will be reported when building, saying that the installed ruby is version 1.9, while gem install json requires version 2.0 or later.

I build directly from the ruby image. Without the installation of apt-get ruby, the container can be built normally. However, when starting, the error that / opt/webapp/bin/webapp can not be found, and the permission for execution has been added. The / bin/bash command startup container found that webapp was started with / usr/bin/ruby, but the ruby in the container was / usr/local/bin/ruby, so add the cp command during build. My Dockerfile is as follows:

FROM rubyMAINTAINER James Turnbull james@example.comENV REFRESHED_AT 2014-6-1RUN apt-get updateRUN apt-get-y install ruby-dev build-essential redis-toolsRUN gem install-- no-rdoc-- no-ri sinatra json redisRUN mkdir-p / opt/webappRUN cp / usr/local/bin/ruby / usr/bin/rubyEXPOSE 4567CMD ["/ opt/webapp/bin/webapp"]

View the output of the execution command after a successful startup:

Docker logs webapp

View the processes that the container is running:

Docker top webapp

Check the port 4567 of the container that is mapped to the host:

Docker port webapp 4567

Test the operation of the Sinatra application, assuming that the port mapped to the host is 49160 (it receives input parameters and converts them into Json returns):

Curl-l-H 'Accept:application/json'-d' name=Foo&status=Bar' http://localhost:49160/json

3. Build Redis images and containers

Create a redis build environment:

Mkdir rediscd redistouch Dockerfile

The content of redis Dockerfile:

FROM ubuntu:14.04MAINTAINER James Turnbull james@example.comENV REFRESHED_AT 2016-07-11RUN apt-get updateRUN apt-get-y install redis-server redis-toolsEXPOSE 6379ENTRYPOINT ["/ user/bin/redis-server"] CMD []

Build a redis image:

Docker build-t ivan/redis.

Create and start the redis container:

Docker run-d-p 6379-- name redis ivan/redis

Check which port of container 6379 is mapped to the host:

Docker port redis 6379

Then use redis-cli on the host to test the performance of redis-server in the container:

Sudo apt-get-y install redis-toolsredis-cli-h 127.0.0.1-p 49161

(assume that port 6379 of the container is mapped to port 49161 of the host.)

I use apt-get-y install redis-tools on the ubuntu of the host to report that I can't locate the error of redis-tools packages. I can use redis-cli after apt-get-y install redis-server directly.

4. Connect to the Redis container

There are generally two ways to achieve network connection between containers:

First, the Docker container exposes the port and binds to the local network interface, so that the services in the container can be exposed on the same external network as the local Docker host (for example, bind port 80 in the container to the high port of the local host).

Second, intranet. When you install Docker, a new network interface is created, named docker0. The docker0 interface has a private IP address that conforms to RFC1918, with a range of 172.16 to 172.30. The address of the docker0 interface itself is the gateway address of the Docker network and the gateway address of all Docker containers. By default, Docker uses 172.17.x.x as the subnet address, and if this subnet is occupied, it will attempt to create a subnet within the range of 172.16 / 172.30. The interface docker0 is a virtual Ethernet bridge that connects the container to the local host network. The Docker host will have a series of interfaces whose names begin with veth. Every time Docker creates a container, it creates a set of interconnected network interfaces. This set of interfaces is like two ends of a pipe, one end of which serves as an eth0 interface in the container, while the other end is uniformly named with the name beginning with veth as a port of the host. By binding each veth* interface to the docker0 bridge, Docker creates a virtual subnet that is shared by the host and all Docker containers.

Check the routing information of peer-to-peer traffic inside the running container and find that the next hop after the container address is the address of docker0 on the host network:

Apt-get-yqq update & & apt-get install-yqq traceroutetraceroute google.com

(3) there is another part of the Docker network configuration that allows connections to be established: firewall rules and NAT configuration. These configurations allow Docker to route between the host network and the container. View the IPTables NAT configuration on the host machine:

Sudo iptables-t nat-L-n

The container is inaccessible by default. When communicating with the container from the host network, you must explicitly specify the open port.

5. Connect Redis

Use docker inspect to view the network configuration of the Redis container:

Docker inspect redis

This command shows the details of the Docker container, including configuration information and network conditions. You can use the-f flag to get only the IP address:

Docker inspect-f'{{.NetworkSettings.IPAddress}} 'redis

If the Docker host is running locally, you can communicate with the Redis server directly using the IP address of the Redis server.

There are two problems with this approach: first, you have to hard-code the IP address of the Redis container in the application; second, if you restart the container, Docker may change the IP address of the container.

6. Interconnect Docker containers

Delete the previously run redis container and webapp container:

Docker rm-f webappdocker rm-f redis

Create a new redis container. Unlike before, it does not expose any ports:

Docker run-d-name redis ivan/redis

Start the Web application container and connect it to the new Redis container:

Docker run-p 4567-- name webapp-- link redis:db-t-I-v $PWD/webapp:/opt/webapp ivan/sinatra / bin/bash

The link orchestration creates a parent-child connection between two containers. This flag requires two parameters: one is the name of the container to be connected, and the other is the alias of the container after the connection. In this example, connect the new container to the redis container and use db as the alias. Connections give the parent container the ability to access the child container and share some of the connection details of the child container with the parent container, which helps to configure the application and use the connection. (the newly created container is the parent container and the child container is connected to the container)

Connections can also get some security benefits. The port of the container does not need to be exposed to the local host.

For security reasons, you can force Docker to allow only connected containers to communicate with each other, and you need to start the Docker daemon with the-- icc=false flag to turn off communication between all unconnected containers.

You can also connect multiple containers together. For example, if you want this Redis instance to serve multiple Web applications, you can connect the container of each Web application to the same redis container.

The connected container must be running on the same Docker host. Containers running on different Docker hosts cannot connect.

Docker writes connection information in the following two places in the parent container:

/ etc/hosts file

In the environment variable that contains the connection information.

The item created by the connection instruction in the / etc/hosts file is the IP address of the redis container and the corresponding child container alias for the connection.

If the container is restarted, the IP address of the container may change. Starting with Docker1.3, if the connected container is restarted, the IP address in the / etc/host file will be updated with the new IP address.

Run env in the bash environment to see the environment variables, and you can see some environment variables that start with DB. When Docker connects the webapp and redis containers, it automatically creates environment variables that start with DB. DB is the alias of the child container when the connection is created. These environment variables mainly contain the following information:

The name of the child container

The protocol, IP, and port number used to run the service in the sub-container

The value of the environment variable set by Docker in the child container.

These environment variables vary from container to container, depending on how the child containers are configured.

7. Test the connection of containers

You need to download the contents of the author code / code/5/sinatra/webapp_redis to replace the contents of the original sinatra/webapp, and then launch the / opt/webapp/bin/webapp of the webapp container in the background. Submit data to the web service (assuming that the port mapped to the host is 49160):

Curl-l-H 'Accept:application/json'-d' name=Foo&status=Bar' http://localhost:49160/json

Test the submission with the get method:

Cuil-I http://localhost:49160/json

5.3 Docker for continuous integration (Jenkins)

1. Build Jenkins and Docker containers

The Dockerfile of the code in the book could not be built successfully, probably due to a version upgrade. Download the updated code from Github and put 5/jenkins/Dockerfile and dockerjenkins.sh in the folder of the build environment.

The VOLUME instruction is used in Dockerfile. Mount a volume from the host where the container is running.

Start the container after the construction is successful:

Docker run-p 8080 ivan/dockerjenkins 8080-- name jenkins-- privileged-d

A new flag, privileged, is used here, which starts Docker's privileged mode, allowing us to run the container with all the capabilities of its host, including some kernel features and device access. This is a necessary ability to run Docker in Docker.

Letting Docker run in privileged mode has some security implications. The running container in this mode has root access to the Docker host.

Thank you for reading! This is the end of the article on "how to use Docker in testing". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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