# [Docker] Tips # 1. Image operations: ### 1.1 Build an image if you've already written a Dockerfile ``` ## run the following command in the folder containing Dockerfile docker build -t imageName:imageTag ``` ### 1.2. List all images ``` docker images ``` ![image](https://hackmd.io/_uploads/rk_Zb-x8a.png) --- ### 1.3. Save and load image: Step1. Save a Docker Image as a compressed file. ``` $ docker save mytomcat -o mytomcat.tar ``` ![](https://i.imgur.com/gICuWiK.png) Step2. Put this compressed file to the other computational resource, and then load it to docker. ``` $ docker load -i mytomcat.tar ``` ![](https://i.imgur.com/YERVBNY.png) Resource: a. https://ithelp.ithome.com.tw/articles/10191387 b. https://www.twblogs.net/a/5cc16cf6bd9eee397113f7a7 --- # 2. Container operations ### 2.1. Basic operations ``` ## 2.1.1 stop a container docker stop containerID ## 2.1.2 restart a container if it exited docker restart containerID ## 2.1.3 execute a container if it exists docker exec -it containerID /bin/bash ## 2.1.4 remove a exited container docker rm containerID ``` ### 2.2. List all existed containers ``` docker ps -a ``` ![image](https://hackmd.io/_uploads/HklHWWxIT.png) Without **-a** option, only running containers will be shown. --- ### 2.3. Remove all existing containers ``` docker rm $(docker ps -a -q) ``` [Reference](https://www.freecodecamp.org/news/how-to-remove-images-in-docker/) --- ## Advenced ### Run Multiple Commands through one line docker command: We can use the -c option with the shell to execute multiple commands simultaneously. The sh -c option allows us to pass a string containing multiple commands as an argument. Let's run the whoami and date commands using sh -c: ``` docker run centos:latest sh -c "whoami && date" ``` [Reference]( https://www.baeldung.com/ops/docker-run-multiple-commands) --- ### Mount nfs to docker container: Make sure *nfs-common* has been installed. ``` docker run --rm -it --mount type=bind,source=<your nfs folder>,target=<which folder you want to mount in container> <your_image_name> ``` --- user group https://docs.docker.com/engine/install/linux-postinstall/ ###### tags: `docker`