1. docker container, virtual machine, hypervisor and docker engine
- docker container runs on docker engine, vm runs on hypervisor (vm engine)
- docker container is a running instance of docker image
2. docker command
- docker --version
- docker ps //list running containers
- docker pull
- docker image ls -a
- docker rmi $(docker images -q) //remove all images
- docker run -d -t //detached, run in background
- docker run -it //run in foreground with shell
- docker run -p 127.0.0.1:80:8080/tcp //public host:container port
- docker run -it --link server:redis --name client busybox //link client to server, add server in client's host file, can ping server from client
- docker stop $(docker ps -aq) //stop all containers
- docker rm $(docker ps -aq) //remove all containers
- docker container ls -a //same as docker ps -a
- docker container start
- docker container stop
- docker container rm
- docker build -t <mytag> . //build image from docker file
- docker run -d mytag //run the image
- docker run -it mytag /bin/ash //run image with shell
- docker run -it <mytag> sh //get the content of image
- docker export $(docker ps -lq) | tar tf -
- docker cp //copy file from local machine to container
3. dockerfile / docker-compose.yml / docker image / docker container
- docker container is running instance of docker image
- dockerfile is a file that produces a docker image when you build it
- dockerfile create a single image that can be run as a container
- dockerfile normally define the 'static' environment
- docker-compose.yml can build multiple container in one compose file that works together as an application
- docker-compose.yml file normally define the 'dynamic' services
- docker-compose.yml can overwrite settings in dockerfile and docker hub
4. docker-compose
- build: go through services listed in docker-compose.yml, look for 'build' key, and build image based on dockerfile
- up vs start: 'up' will build, create and start entire application for the 1st time, 'start' will start the containers that are stopped by 'stop'
- down vs stop: 'down' will stop and remove entire application, 'stop' will stop the containers
- looks like all we need is up/down and start/stop commands
5. sample dockerfile
# base image
FROM python:3.6
# RUN command
RUN mkdir -p /usr/src/app
# make the folder as a working directory
WORKDIR /usr/src/app //set working dir for any following RUN
//COMMAND, ENTRYPOINT, COPY, AND
# copy host files to the image folder
COPY package.json . //package.json is host, . is container
# CMD command
CMD [ “npm”, “start” ]
6. sample docker compose file
version: '2'
services:
selenium_hub:
image: selenium/hub
ports:
- "4444"
app:
build: app/. //where dockerfile is
ports:
- "80"
robottests:
command: testui/wait-for-it.sh -t 15 chromenode:5555
depends_on:
- selenium_hub
- app
build: testui/. //where dockerfile is
volumes:
- {WORKSPACE}/reports:/frontend-integration-tests/reports . //map host to container
//{WORKSPACE} is a Jenkins environment variable that defines a root of a repository with the project files
7. sample jenkins file that use docker
node {
stage('build an image with Tests') {
sh """
docker-compose build robottests:{BUILD_NUMBER}
"""
}
stage('run Docker Compose') {
sh """#!/bin/bash -e
docker-compose run --rm robottests:{BUILD_NUMBER}
"""
}
stage('stop all containers') {
sh """
docker-compose down
"""
}
}
8. link docker container
- link to another container by name so that you can ping it (add to hostname)
9. swarm mode
- like selenium grid, docker host can be manager and worker
10. docker volume
- to persist data
- volume will persist directory in container to host directory
- host directory can be specified or anonymous
- use 'docker inspect container_name' to see volume info under 'Mounts' section
- docker volume ls //list all volumes in the host
11. data sharing in docker
- dockerfile copy & add: add can do more than copy
- bind mount: /path/on/host:/path/in/container
- volume: volumes are managed by docker and are independent of the host filesystem, my_volume:/path/in/container
- share data between containers: use bind mount or volume
12. docker tag
- docker build -t repository_name/image_name:tag_name . //add tag to the image
13. running test automation in docker (selenium example)
- create docker image that contain everything necessary to run test (java, mvn, content of test)
- run image as docker container
- run test within container against external selenium grid
- after the test was run, temporary container would be destroyed (but persistent test result should be archived to S3 bucket)
14. docker registry and docker repositories
- default registry is dockerhub
- a registry is organised into repositories, each repository holds all versions of an image
15. ubank docker commands
- docker login ubank-docker.artifactory.dss.ext.national.com.au -u **** -p ****
- docker build -t mytag -f MyDockerfile .
- docker run -it mytag /bin/bash
reference