Overview
- Concept For Docker
- Docker Image - Images are read-only templates containing instructions for creating a container. A Docker image creates containers to run on the Docker platform.
- Docker Container - A container is an isolated environment where an application runs without affecting the rest of the system and without the system impacting the application.
- Existing Container - When the user starts up a Docker image as a container, the container will be called “Existing Container“ and you can find it on “docker ps“.
- Stop Docker Container - Docker container cannot be removed until it stops, it can be stopped via “docker stop [container]“ or “exit for pseudo tty mode“.
- Exited / Stopped Container - The exited/stopped container will NOT show in the “docker ps“ command, but stores in “docker ps -a“.
- Remove Docker Container - The container ONLY allows removal after stops.
START Docker Container
EXPLAIN How To START Docker Container
e.g.
$ docker run --privileged -it -u jenkins_agent -p 4723:4723 -p 10022:22 -v /dev/bus/usb:/dev/bus/usb --name test test-ubuntu:0.33
e.g.
$ docker run --privileged -d -p 4723:4723 -v /dev/bus/usb:/dev/bus/usb --name test test-ubuntu:0.33 appium -a 172.17.0.2 -p 4723 --allow-cors --session-override --relaxed-security --log-timestamp --local-timezone
START Docker Container
SET Docker's apt Repository
$ sudo apt update
$ sudo apt install ca-certificates curl
$ sudo install -m 0755 -d /etc/apt/keyrings
$ sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
$ sudo chmod a+r /etc/apt/keyrings/docker.asc
$ echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
$ sudo apt update
Install The Docker Engine
SET Docker's apt Repository
$ sudo apt update
$ sudo apt install ca-certificates curl
$ sudo install -m 0755 -d /etc/apt/keyrings
$ sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
$ sudo chmod a+r /etc/apt/keyrings/docker.asc
$ echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
$ sudo apt update
INSTALL The Docker Packages
$ sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
ADD User To Docker Group
$ sudo usermod -aG docker [$USER]
VERIFY The Docker Engine Has Been Installed
$ sudo docker run hello-world
...
Hello from Docker!
This message shows that your installation appears to be working correctly.
...
Frequently Used Docker Commands
EXECUTE A Command In A Running Container
$ docker exec [options] [container name] [command]
e.g.
$ docker exec -it [CONTAINER ID] bash
FETCH The Logs Of A Container
$ docker logs [container name]
LIST Existing And Exited/Stopped Docker Containers
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
...
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4189dc7e795c test-ubuntu:0.33 "/bin/bash" 2 weeks ago Exited (129) 2 weeks ago test
282d0759f1bb jenkins/agent:latest-jdk17 "java -jar /usr/shar¡K" 2 weeks ago Exited (1) 2 weeks ago agent
97a305060cf1 jxpower/appium-server:latest-arm64 "bash" 3 weeks ago Exited (129) 3 weeks ago appium
6ae3c160405b ubuntu:jammy-20240627.1 "/bin/bash" 4 weeks ago Exited (255) 3 weeks ago ubuntu
...
SET Volume
$ docker run -v [/local/dir]:[/target/container/dir]
SET Port
$ docker run -p 4723:4723 -p 10022:22
$ docker run -P
START A Exited/Stopped Docker Container
$ docker start [container name]
STOP A Running Docker Container
$ docker stop [container name]
REMOVE Docker Images
$ docker rmi [Image Name]
REMOVE Exited/Stopped Docker Container
$ docker rm [Container Name / Container ID]
$ docker rm $(docker ps -aq --filter status="exited")
RENAME Docker Container
$ docker rename [container] [new_name]
RUN Docker Container With Interactive And Bash
$ docker run --name [name you like] -it [IMAGE REPOSITORY:TAG] bash
or
$ docker run --name [name you like] -it [IMAGE ID] bash
Build Dockerfile
BUILD A Custom Docker Image
Type this command to build the file into a docker image.
$ sudo vim [dockerfile name]
$ docker build -f [/path/to/Dockerfile] [--no-cache] -t [REPOSITORY]:[TAG] .
e.g.
$ docker build -f Dockerfile_ubuntu --no-cache -t test-ubuntu:0.33 .
Custom Dockerfile Example
The following example is NOT recommended to take place in an actual scenario, I made it for running automation on docker but it has too many limitations.
It installed Python, Robot Framework, Appium, and other packages and ran on Ubuntu.
• It’s “NOT“ a good idea to install sudo on Docker
• Keep the Dockerfile simplify
FROM ubuntu:jammy-20240627.1
LABEL description='A Robot Framework+Appium for CI in RPI ububtu' author='Victor.Chang'
ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=Asia/Taipei
ARG USER=jenkins_agent
ARG HOME_DIR=/home/$USER
EXPOSE 4723 5037 22
RUN echo 'root:********' | chpasswd
RUN useradd -m -s /bin/bash $USER
RUN echo $USER':********' | chpasswd
RUN usermod -aG root,dialout,sudo,plugdev $USER
RUN apt update && apt upgrade -y
RUN apt install -y ssh
RUN apt install -y tzdata usbutils vim file
RUN apt install -y curl
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash
RUN apt install -y nodejs
RUN apt install -y android-sdk android-tools-adb android-tools-fastboot
RUN npm install -g appium
RUN echo 'export ANDROID_HOME=/usr/lib/android-sdk' >> ~/.bashrc
RUN echo 'export PATH=$PATH:$ANDROID_HOME/tools' >> ~/.bashrc
RUN echo 'export PATH=$PATH:$ANDROID_HOME/platform-tools' >> ~/.bashrc
RUN apt install -y software-properties-common
RUN add-apt-repository ppa:deadsnakes/ppa -y
RUN apt update
RUN apt install -y python3.12
RUN echo 'alias python=python3.12' >> ~/.bashrc
RUN curl -sS https://bootstrap.pypa.io/get-pip.py | python3.12
RUN echo 'export PATH=/home/$USER/.local/bin:$PATH' >> ~/.bashrc
RUN apt install -y git
RUN usermod -aG plugdev,dialout,root,$USER $USER
RUN cp /root/.bashrc /home/$USER/.bashrc
RUN cp /root/.profile /home/$USER/.profile
USER $USER
WORKDIR $HOME_DIR
RUN appium driver install uiautomator2
Build Docker Compose
Build Docker Compose
- Install Docker Engine on Ubuntu
- Docker Images Commands List
- Docker buildx build
- Docker container run