Docker container (Container) runs based on image (image), you can Docker Hub , or build the image by yourself through Dockerfile.

First pull the latest version (latest) of MySQL official mirror

docker pull mysql

Use docker images to display local images.

docker images

Use the docker run command to run the container, the -d setting runs in the background and returns the container ID.

docker run -itd --name db-server -e MYSQL_ROOT_PASSWORD=P2ssw0rd mysql

Use docker ps to view the running image, the -a parameter shows all instances.

docker ps -a

Use the docker exec command to enter the shell of the container's internal system.

docker exec -it db-server /bin/bash

After completing the corresponding operation/modification in the container, the commit command can submit the update to the specified image.

docker commit -m "update" -a "aaron" db-server registry.cn-shanghai.aliyuncs.com/aaron-share/db-server

Update the local mirror to the mirror management server, here first complete the login to the Alibaba Cloud mirror server.

docker login --username=18626200000 registry.cn-shanghai.aliyuncs.com
docker push registry.cn-shanghai.aliyuncs.com/aaron-share/db-server:latest

The following commands delete the container and image respectively, and -f can forcefully delete the running container.

docker rm -f db-server
docker rmi registry.cn-shanghai.aliyuncs.com/aaron-share/db-server

Below, we briefly introduce how to use Dockerfile to build a mirror. The following Dockerfile uses the official ubuntu system image as a starting point to complete a simple example.

FROM ubuntu:latest                         # 基准镜像
MAINTAINER aaron                           # 作者信息
ARG APT=apt-get                            # 临时变量,构建容器时有效
RUN $APT update \                          # 构建容器时执行的命令
  && $APT install -y curl \
  && rm -rf /var/lib/apt/lists/*
CMD [ "curl", "-s", "http://baidu.com" ]   # 运行容器时执行的命令

Then, you can use the docker build command to build the image.

docker build -t registry.cn-shanghai.aliyuncs.com/aaron-share/ubuntu-server:latest .

In software testing, we usually use pre-made Docker images of different environments to carry out corresponding tests.
You can use the above docker exec command to enter the container and execute the test command; you can also execute the docker run directly on the host to execute the test.

docker run -i --rm \
  --name testng-in-docker 
  -v "$(pwd)":/usr/src/mymaven 
  -v ~/.m2:/root/.m2 -w /usr/src/mymaven 
  maven:latest 
  mvn clean test

The above uses Java's common Maven build tool to execute the test, -v represents the path mapping from the host to the container, it

  • Get the current project path through the $(pwd) command, and map it to the /usr/src/mymaven directory inside the container;
  • Map the root directory of the Maven class library ~/.m2 to the /usr/src/mymaven directory inside the container;
  • Pull and run the latest version of the container image named maven;
  • When the container starts, immediately run the mvn clean test command to execute the test;
  • After running, the container exits directly, and the test result is located in the current path.

In addition, Docker Compose is a tool for defining and running multiple containers. If your service includes multiple interdependent containers, you can use it to orchestrate and start all services. For details, please refer to the article .

I have implemented an on-demand test environment management based on Docker containers in an open source project. For details, please refer to the URL https://github.com/easysoft/zagent.

thematic catalog


陈哥聊测试
158 声望3.3k 粉丝

资深敏捷测试顾问,国内知名项目管理软件禅道团队成员。