Docker & Testing in Docker

Why Docker?

  • Speed. Speed. Speed.
  • Making dev->test->prod easier and faster
  • "make my code run the same way on two different machines"
  • Reduce complexity of developing code for distributed systems
  • Reduce complexity of deploying code to the cloud
  • Reduce complexity of updating code in the cloud

Docker Editions

  • Docker is no longer just a "container runtime"
  • Docker moves fast, it matters how you install it
  • Docker CE (Community Edition)
  • Three major types of installs: Direct, Mac/Win, Cloud
  • Linux (different per distro) (don’t use default package)
  • Docker for Windows (or legacy Docker Toolbox)
  • Docker for Mac (or legacy Docker Toolbox) (don’t use brew)
  • Docker for AWS/Azure/Google

Docker for Mac

  • Use Docker for Mac, from store.docker.com
  • More features then just a Linux VM
  • Uses "xhyve" with tiny Linux VM for Linux Containers
  • Terminal/iTerm native

Containers vs VMs

  • Container VS Virtual Machine
  • Containers aren’t Mini-VM’s
  • They are just processes
  • Limited to what resources they can access (file paths, network devices, running processes)
  • Exit when process stops

Docker CLI

  • docker version
  • docker info
  • docker command line structure
docker <command> <sub-command> (options)

Let's create our first docker container

docker run -d mongo
docker container ls
docker run --name mongo -d mongo
docker run --name mongo -d mongo // Will throw an error
docker container ls
docker container logs mongo
docker container stop mongo
docker container stop [id of first mongo]

docker run --name mongo -d mongo // Will throw an error
docker start mongo
docker container stop mongo

docker container ls -a
docker container rm mongo
docker container ls -a

Image vs Containers

  • An Image is the application we want to run
  • A Container is an instance of that image running as a process
  • You can have many containers running off the same image
  • Docker's default image "registry" is called Docker Hub (hub.docker.com) 
docker container run --publish 80:80 nginx
  • Downloaded image 'nginx' from Docker Hub
  • Started a new container from that image
  • Opened port 80 on the host IP

Task

  • Run a nginx, a mysql, and a httpd (apache) server
  • Run all of them --detached (or -d), name them with --name
  • nginx should listen on 80:80, httpd on 8080:80, mysql on 3306:3306
  • When running mysql, use the --environment option (or -e) to pass in MYSQL_RANDOM_ROOT_PASSWORD=yes
  • Use docker container logs on mysql to find the random password it created on startup
  • Clean it all up with docker container stop and docker container rm (both can accept multiple names or ID's)
  • Use docker container ls to ensure everything is correct before and after cleanup

Integration with Testcafe

  • TestCafe provides a preconfigured Docker image with Chromium and Firefox installed. Therefore, you can avoid manual installation of browsers and the testing framework on the server.
docker pull testcafe/testcafe

Prepare Tests

  • Create a folder called dockerTests
  • Create a file called test.js that opens zemoga website, navigate to jobs page.
  • Try to submit the form
  • Test for error message to appear 
  • Something happened. Please make sure all the fields above are properly completed

 

Run TestCafe from the docker image.

docker run
    -v ${TEST_FOLDER}:/tests
    -it testcafe/testcafe ${TESTCAFE_ARGS}
docker run
    -v /path/to/dir/dockerTests:/tests
    -it testcafe/testcafe 'chromium --no-sandbox,firefox' /tests/test.js
    --rm

deck

By Cesar Guerrero Rincon