qdyxmas's Room

Done is better than perfect...

docker下搭建selenium环境

Docker简介:

安装

1.添加yum源
    tee /etc/yum.repos.d/docker.repo <<-'EOF'
    [dockerrepo]
    name=Docker Repository
    baseurl=https://yum.dockerproject.org/repo/main/centos/$releasever/
    enabled=1
    gpgcheck=1
    gpgkey=https://yum.dockerproject.org/gpg
    EOF
2.安装docker-engine
    yum install -y docker-engine
    完成以后使用docker version查看是否安装成功
3.启动docker
    systemctl start docker.service
4.添加新的docker镜像
    docker pull markadams/chromium-xvfb-py3
    https://hub.docker.com/r/markadams/chromium-xvfb/
5.进入容器中,修改默认的安装源,否则可能会很慢
    docker run --rm -it markadams/chromium-xvfb-py3 bash
    sudo sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
    安装需要使用的包
    apt-get update && apt-get install -y python3 python3-pip curl unzip libgconf-2-4 vim
    更改pip源vim ~/.pip/pip.conf
        [global]
        index-url = https://pypi.tuna.tsinghua.edu.cn/simple
    pip install selenium
6.安装chromedriver,由于chromium-xvfb-py3里面安装的chrome浏览器版本为64,所以chromedriver也要安装对应的版本
    curl -SLO "http://npm.taobao.org/mirrors/chromedriver/2.36/chromedriver_linux64.zip"
    unzip "chromedriver_linux64.zip" -d /usr/local/bin
7.运行测试脚本test.py
    root@ea1b89f47281:~# vim test.py 
        from selenium import webdriver
        driver = webdriver.Chrome()
        url = 'https://www.baidu.com/'
        driver.get(url)
        print(driver.title)
    运行脚本
    root@ea1b89f47281:~# python3 test.py 
    百度一下,你就知道
    能够打印出来“百度一下,你就知道”说明已经成功运行

8.改写Dockerfile文件
    FROM markadams/chromium-xvfb-py3
    RUN sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
    RUN apt-get update && apt-get install -y \
        python3 python3-pip curl unzip libgconf-2-4 vim

    ARG pip_mirror=http://mirrors.aliyun.com/pypi/simple
    ARG pip_host=mirrors.aliyun.com

    # Upgrade pip3
    # Install app requirements
    RUN pip3 install --upgrade pip -i ${pip_mirror} --trusted-host ${pip_host} --no-cache-dir
    RUN pip3 install pytest selenium  -i ${pip_mirror} --trusted-host ${pip_host} --no-cache-dir


10.从文件中build创建docker镜像
    docker build -t docker_selenium .
    最后的.表示当前路径的Dockerfile文件
11. 查看镜像id后,启动镜像
    1.[root@localhost docker_selenium]# docker images
    REPOSITORY                              TAG                 IMAGE ID            CREATED             SIZE
    selenium_image                          latest              45606da701b2        15 minutes ago      1.17 GB
    docker.io/markadams/chromium-xvfb-py3   latest              177ab60b914b        20 months ago       923 MB 
    2.启动镜像 docker run  -d -i -t 45606da701b2 /bin/bash
    3.查看容器id后进入容器中
        [root@localhost docker_selenium]# docker ps
            CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
            262e01cb976f        45606da701b2        "/bin/bash"         7 minutes ago       Up 7 minutes                            hopeful_stallman

标签 

评论