Introduction to Docker
Published:
A step by step introduction to git. Also, see reference1
Outline
- What is Docker (DevOps=Devlops+Operations)
- Docker Homepage: https://www.docker.com/
- Docker Documents: https://docs.docker.com/
- Docker Hub: https://hub.docker.com/
- Understand:
- Image: Like a templete or class, can be used to create (multiple) container. E.g., tomcat image ==> run ==> tomcat01 container (provide server).
- Container: Created by image (object). Including basic commands as run, stop, delete, etc. Like a simplified linux system.
- Repository: The place to deposit images, including public and private repositories.
- Install Docker @ Ubuntu 22.04 LTS
- Method1: official mannual
- Prerequisites
- $ sudo apt-get remove docker docker-engine docker.io containerd runc
- Install Docker Engine
- Update the apt package index and install packages to allow apt to use a repository over HTTPS:
- $ sudo apt-get update
- $ sudo apt-get install ca-certificates curl gnupg lsb-release
- Add Docker’s official GPG key:
- $ sudo mkdir -p /etc/apt/keyrings
Official: $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg sudo gpg –dearmor -o /etc/apt/keyrings/docker.gpg Use aliyun (optional): $ curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg sudo gpg –dearmor -o /etc/apt/keyrings/docker.gpg
- Use the following command to set up the repository:
- Official: $ echo
“deb [arch=$(dpkg –print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu
$(lsb_release -cs) stable” | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null - Use aliyun (optional): $ echo
“deb [arch=$(dpkg –print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu
$(lsb_release -cs) stable” | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
- Official: $ echo
- Install Docker Engine
- $ sudo apt-get update
- $ sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
- Change the mirror of Docker Hub to aliyun (optional):
- $ sudo mkdir -p /etc/docker
- $ sudo tee /etc/docker/daemon.json «-‘EOF’
{
“registry-mirrors”: [“https://zjh3aomb.mirror.aliyuncs.com”]
}
EOF - $ sudo systemctl daemon-reload
- $ sudo systemctl restart docker
- Test and verify
- $ sudo docker version
- $ sudo docker run hello-world
- Manage Docker as a non-root user
- Update the apt package index and install packages to allow apt to use a repository over HTTPS:
- Change the default storage location of Docker images
- Check the present location
- $ sudo docker info | grep Dir
- Shutdown the Docker service
- $ sudo systemctl stop docker
- $ sudo systemctl status docker
- Move the data to the new location, e.g., /lustre/haowei/Docker/lib/docker
- $ sudo mv /var/lib/docker /lustre/haowei/Docker/lib/
- Modify the docker.service ($ sudo vim /lib/systemd/system/docker.service) startup configuration file using the –graph parameter to specify the storage location
- Change
- ExecStart=/usr/bin/dockerd -H fd:// –containerd=/run/containerd/containerd.sock
- into
- ExecStart=/usr/bin/dockerd -H fd:// –containerd=/run/containerd/containerd.sock –graph /lustre/haowei/Docker/lib/docker
- Reload the configuration file
- $ sudo systemctl daemon-reload
- Restart Docker service & check
- $ sudo systemctl start docker
- $ sudo systemctl enable docker
- $ sudo systemctl status docker
- $ sudo docker run hello-world
- Check the present location
- [Uninstall Docker Engine] (https://docs.docker.com/engine/install/ubuntu/) and also [this reference] (https://askubuntu.com/questions/935569/how-to-completely-uninstall-docker)
- $ sudo apt-get purge docker-ce docker-ce-cli containerd.io docker-compose-plugin
- $ sudo rm -rf /var/lib/docker
- $ sudo rm -rf /var/lib/containerd
- Docker commands
- $ sudo docker xxx –help
- $ sudo docker version
- $ sudo docker info: system information of docker, including the number of mirrors and containers
- Image commands
- $ sudo docker images: List images
- $ sudo docker search: Search images
- $ sudo docker pull mirrorName:versionTag (note the versionTag is optional used for select version, w/o versionTag, the latest will be downloaded.): Pull/download an image or a repository from a registry
- $ sudo docker rmi mirrorName/ID: Remove images (such as class)
- Container commands
- $ sudo docker run: Run a command in a new container
- –name=”aName”: the name of the running container
- -d: run on background, same as the nohup
- -it: run in interactive mode and enter the container to view the contents
- -c: follow a bash command, e.g. $ sudo docker run -d centos /bin/bash -c “while true;do echo kuangshen;sleep 1;done”
- -p: specify and expose the port 8080:8080
- -p ip : hostPort : containerPort (most used)
- -p hostPort : containerPort (most used)
- -p containerPort
- containerPort
- -P: specify the port randomly
- –rm: delete the container when exit
- $ sudo docker ps: see the running container
- $ sudo docker ps -a: see the run container history
- E.g., run and enter intel/oneapi-basekit: $ sudo docker run -it intel/oneapi-basekit /bin/bash
- Exit container
- $ exit: exit directly
- $ ctrl+p+q (or ctrl+p & ctrl+q): exit without shutting down the container
- $ sudo docker attach containerID: Come back to the background container with the old/running bash, or attach local standard input, output, and error streams to a running container
- $ sudo docker exec -it containerID /bin/bash: Come back to the background container with a new bash, or run a command in a running container
- $ sudo docker rm containerID: Remove container (such as object)
- Start/stop containers
- $ sudo docker start containerID
- $ sudo docker restart containerID
- $ sudo docker stop containerID
- $ sudo docker kill containerID
- $ sudo docker run: Run a command in a new container
- Other useful commands
- $ sudo docker logs: Fetch the logs of a container
- $ sudo docker logs -tf –tail 10 containerID: see 10 pieces of logs
- $ sudo docker top containerID: Display the running processes of a container
- $ sudo docker inspect containerID: Return low-level information on Docker objects
- $ sudo docker cp containerID:[path]/[filename] localPath: Copy files between host and container, similar as the scp.
- $ sudo docker stats: display a live stream of container(s) resource usage statistics
- $ sudo docker logs: Fetch the logs of a container
- Visualization
- portainer (Docker graphical interface management tool)
- $ sudo docker run -d -p 8088:9000 –restart=always -v /var/run/docker.sock:/var/run/docker.sock –privileged=true portainer/portainer
- visit http://10.72.197.103:8088/, set password and select the local choice
- Rancher (CI/CD)
- ……
- portainer (Docker graphical interface management tool)
- Docker commit
- To save the modified container as a new docker image, use commit as:
- $ sudo docker commit -m=”your message” -a=”author” containerID dockerName:versionTag
- To save the modified container as a new docker image, use commit as:
- Docker - Data Volume
- Synchronize the data between container and host, achieved by mount the directory of the container into the host. The directory can also be shared between different containers.
- Method 1: $ sudo docker run -it -v hostDirectory:containerDirectory
- E.g. $ sudo docker run -it -v /home/haowei/test:/home centos
- Direct mount: -v hostDirectory:containerDirectory
- Implicit mount: -v /containerDirectory
- Explicit mount: -v volumeName:/containerDirectory
- Share the mount between host and several containers with <–volumes-from>:
- $ sudo docker run -it –name centos01 -v /home/haowei/centosHome:/home centos
- $ sudo docker run -it –name centos02 –volumes-from centos01 centos
- Specific permissions:
- Read only: -v hostDirectory:containerDirectory:ro
- Read and write: -v hostDirectory:containerDirectory:rw
- Check the mount information:
- $ sudo docker volume ls
- $ sudo docker volume inspect volumeName
- Method 1: $ sudo docker run -it -v hostDirectory:containerDirectory
- Synchronize the data between container and host, achieved by mount the directory of the container into the host. The directory can also be shared between different containers.
- DockerFile
- Create your own mirrors, steps:
- Write a dockfile file
- docker build: create the mirror
- docker run: execute your mirror as a container
- docker push: publish your mirror (DockerHub or aliyun)
- DockerFile commands:
- FROM: the base mirror
- MAINTAINER: name+email
- RUN: the commands to be executed when build
- ADD: add content/file
- WORKDIR: the workdir of mirror/container
- VOLUME: the mounted directory
- EXPOSE: reserve the port settings
- CMD: the commands to be executed when container is started, only the last one will be executed (replace)
- ENTRYPOINT: the commands to be executed when container is started, we can add more commands after run the container (append)
- ONBUILD: when build a heritable DockFile, the ONBUILD command will be executed.
- COPY: copy files to the mirror
- ENV: set environment when build
Example01: create a new ubuntu
FROM ubuntu MAINTAINER haowei<changhw@zju.edu.cn> ENV MYPATH /usr/local WORKDIR $MYPATH RUN touch aFile #RUN sed -i 's/archive.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list #RUN apt update #RUN apt -y install vim EXPOSE 80 CMD echo $MYPATH CMD /bin/bash
- Build with: $ sudo docker build -f mydockerfile-ubuntu -t myubuntu:latest .
- write a Dockerfile, ‘-f’ flag will be ignored if the name of Dockerfile is ‘Dockerfile’
- See the Docker image history with: $ sudo docker history imageID
- Build with: $ sudo docker build -f mydockerfile-ubuntu -t myubuntu:latest .
- Example02: create a new ubuntu with oneAPI (something went wrong)
$ sudo docker build -t ubuntuonepai:latest .
From ubuntu MAINTAINER haowei<changhw@zju.edu.cn> COPY readme.txt /usr/local/readme.txt COPY l_BaseKit_p_2022.2.0.262_offline.sh /root/ ENV MYPATH /root/ WORKDIR $MYPATH RUN sh l_BaseKit_p_2022.2.0.262_offline.sh EXPOSE 8080
Example03: create a docker with oneapi samples
# run the development container and name it mybuild FROM intel/oneapi-basekit:devel-ubuntu20.04 as mybuild # get oneAPI sample code RUN git clone https://github.com/oneapi-src/oneAPI-samples # build the Nbody sample to root directory RUN cmake /oneAPI-samples/DirectProgramming/DPC++/N-BodyMethods/Nbody RUN make # use oneapi-runtime container as my production container FROM intel/oneapi-runtime:latest # copy file from mybuild to the production container COPY - from=mybuild src/nbody / CMD ["/nbody"]
- Create your own mirrors, steps:
- Publish your Docker images
- Docker Network Principle
- Docker Compose
- Docker Swarm
- CI/CD Jenkins
- Else
- Pull the Nvidia hpcsdk container: $ sudo docker run –gpus all -it –rm nvcr.io/nvidia/nvhpc:22.5-devel-cuda_multi-ubuntu20.04