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
# Create and run a new container from an image
# It has a lot of flags and options can be set up when docker run.
# docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
# Options
# -i, --interactive Keep STDIN open even if not attached
# --name Assign a name to the container
# --privileged Give extended privileges to this container
# -p, --publish Publish a container's port(s) to the host
# -t, --tty Allocate a pseudo-TTY
# -u, --user Username or UID (format: <name|uid>[:<group|gid>])
# -v, --volume Bind mount a volume
# Run a container with root privileged and enable tty
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
# Run a container with root privileged and execute command when starts
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
# Add Docker's official GPG key:
$ 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
# Add the repository to Apt sources:
# If you posted this command failed, tried get it from docker tutorial.
$ 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
# Add Docker's official GPG key:
$ 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
# Add the repository to Apt sources:
# If you posted this command failed, tried get it from docker tutorial.
$ 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]
# re-login user to activate
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
# This command runs a new command in a running container, it only runs while the container's primary process (PID 1) is running.
$ docker exec [options] [container name] [command]
e.g.
$ docker exec -it [CONTAINER ID] bash
FETCH The Logs Of A Container
# The docker logs command batch-retrieves logs present at the time of execution.
$ docker logs [container name]
LIST Existing And Exited/Stopped Docker Containers
# List Existing Docker Containers
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
...
# List All Docker Containers
$ 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
# Set volume option when creating container
# -v, --volume Bind mount a volume
$ docker run -v [/local/dir]:[/target/container/dir]
SET Port
# Set port option when creating container
# -p, --publish Publish a container's port(s) to the host
# -P, --publish-all Publish all exposed ports to random ports in host
# docker run -p [/local/port]:[/container/port]
$ docker run -p 4723:4723 -p 10022:22
# With the -P flag, you can automatically publish all exposed ports to ephemeral ports.
# This is quite useful when you’re trying to avoid port conflicts in development or testing environments.
# docker run -P [/local/port]:[/container/port]
$ docker run -P
START A Exited/Stopped Docker Container
# When the docker container has been run but exited, and you want to re-launch it
$ docker start [container name]
STOP A Running Docker Container
# When we want to stop a running docker container
$ docker stop [container name]
REMOVE Docker Images
# Remove one or more images
$ docker rmi [Image Name]
REMOVE Exited/Stopped Docker Container
# Remove Exited / Stopped Docker Container
# The existing conatainer is Unremovable, you need to stop container first.
# rm [Container Name / Container ID], the "Container Name" can be found in "docker ps -a" command, if the container doesn't has name, the conatiner id is acceptable
$ docker rm [Container Name / Container ID]
# Remove all Stopped Containers at once
$ docker rm $(docker ps -aq --filter status="exited")
RENAME Docker Container
# Renames the given container to new_name
$ docker rename [container] [new_name]
RUN Docker Container With Interactive And Bash
# -i, --interactive Keep STDIN open even if not attached
# --name, Assign a name to the container
# -t, --tty Allocate a pseudo-TTY
# [IMAGE REPOSITORY], [TAG], and [IMAGE ID] can be found in "docker images" command
# the "bash" tells which shell run for TTY
$ 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.
# Before build docker image, you need create a file and put command into it. (refer Custome Dockerfile Example)
$ sudo vim [dockerfile name]
# When the file is ready, type the following command to transfer it to docker image
# -f, --file [/path/to/Dockerfile] Name of the Dockerfile (default: PATH/Dockerfile)
# --no-cache Do not use cache when building the image
# -t, --tag [REPOSITORY]:[TAG] Name and optionally a tag (format: name:tag)
$ 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
# Image info
LABEL description='A Robot Framework+Appium for CI in RPI ububtu' author='Victor.Chang'
# SET environment variables
ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=Asia/Taipei
ARG USER=jenkins_agent
ARG HOME_DIR=/home/$USER
EXPOSE 4723 5037 22
# CHANGE root password
RUN echo 'root:********' | chpasswd
# CREATE new account: jenkins_agent with password and groups
RUN useradd -m -s /bin/bash $USER
RUN echo $USER':********' | chpasswd
RUN usermod -aG root,dialout,sudo,plugdev $USER
# UPDATE to the latest
RUN apt update && apt upgrade -y
# INSTALL essential packages
# NOT recommended using sudo in docker
RUN apt install -y ssh
RUN apt install -y tzdata usbutils vim file
## INSTALL Appium relevant packages
## Including Node.js, NPM, android-sdk, adb and fastboot
RUN apt install -y curl
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash
RUN apt install -y nodejs
# RUN apt install build-essential -y
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
# INSTALL Python3.12
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 'export PATH=$PATH:/usr/bin/python3.12' >> ~/.bashrc
# SET python3.12 settings
RUN echo 'alias python=python3.12' >> ~/.bashrc
# RUN source ~/.bashrc
# Installing pip3
RUN curl -sS https://bootstrap.pypa.io/get-pip.py | python3.12
RUN echo 'export PATH=/home/$USER/.local/bin:$PATH' >> ~/.bashrc
# DOWNLOAD automation codes from git
RUN apt install -y git
# Add jenkins_agent into groups
RUN usermod -aG plugdev,dialout,root,$USER $USER
#
RUN cp /root/.bashrc /home/$USER/.bashrc
RUN cp /root/.profile /home/$USER/.profile
##
# Install Jenkins packages
##
# CHANGE user to jenkins_agent
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