Some Docker commands often used
After install docker, to try if it works:
docker run hello-world
docker run --rm docker/whalesay cowsay "hello hello hello"
To generate a docker image using a docker file
docker build --network host -t image_name:tag_whatever -f dockerfilename .
To build a container and use tensorboard and then destroy it
docker run -it --rm --user $(id -u):$(id -g) --name containername -v /some_directory:/directory_under_docker --shm-size=1g --gpus=all --ulimit memlock=-1 --network host kirinspeech:tf1.15.2 tensorboard --host 0.0.0.0 --logdir ckpt_dirs --port 3456
To build a container from the above image
docker run -it --user $(id -u):$(id -g) --name containername -v /some_directory:/directory_under_docker --shm-size=1g --ulimit memlock=-1 --network host image_name:tag_whatever bash
docker restart container_name ## the first time when it was built
docker run --name=lite_test -it --user $(id -u):$(id -g) -v /some_directory:/directory_under_docker --shm-size=1g --ulimit memlock=-1 docker.io/library/python:3 /bin/bash
docker run --name=containername -it --user $(id -u):$(id -g) --runtime=nvidia -v /some_directory:/directory_under_docker --shm-size=2g --ulimit memlock=-1 --gpus all nvcr.io/nvidia/pytorch:21.12-py3 /bin/bash
Go to a saved container
docker exec -it container_name bash
docker exec -it --user root container_name bash
Other common docker operations
docker ps -a
docker images -a
docker container stop a_container_id
docker container rm a_container_id
docker image rm an_image_id
# stop all containers from certain image
sudo docker ps -a -q --filter="name=face_tracker/mofapp"
# remove all stopped container
sudo docker container prune
# remove old containers on linux
sudo docker ps -a | grep "weeks ago" | awk "{print $1}" | xargs --no-run-if-empty sudo docker rm
sudo docker ps -a | grep 'weeks ago\|months ago' | awk '{print $1}' | xargs --no-run-if-empty sudo docker rm
To save the docker image from a docker container
docker commit [NAME] [TAG]
docker save -o [DIR]/abcde.tar [TAG]
docker load -i abcde.tar
## or
docker load -i < abcde.tar.gz
To use pycharm professional installed at local machine to remotely debug projects at server using remote docker container (from zixing):
At the server side, set up a docker container:
#### build a new docker with port map
docker run -p 1234:22 --name=somename -it --user $(id -u):$(id -g) --runtime=nvidia -v /aa/bb:/cc -v /dd:/ee --shm-size=1g --ulimit memlock=-1 --ulimit stack=67108864 --gpus all dockerimage:tag /bin/bash
§ (disable --network host)
§ 22: container port for sftp; 1234: host port, self-defined
§ Use `docker ps` to check whether the port map works
docker port somename
Within the docker container, apply the following changes
## exec --user root -it somename bash
§ apt-get update
§ apt-get install openssh-server openssh-client
§ nano /etc/ssh/sshd_config
change #PermitRootLogin without-password -----> PermitRootLogin yes
change #PasswordAuthentication yes -----> PasswordAuthentication yes
change #UsePAM yes -----> UsePAM no
§ passwd root
§ service ssh restart
## test if the ssh access to this container is built
ssh root@ipaddress -p 1234
#### Setup the pycharm at local machine
§ Deployment--> configuration --> sftp
§ Setting --> project interprater --> ssh interpreter --> existing --> (others)
https://www.cnblogs.com/ruiyang-/p/10158658.html
https://blog.csdn.net/u012620515/article/details/89856072
An example of dockerfile:
FROM nvcr.io/nvidia/tensorflow:20.06-tf1-py3
# Run and updates and cleans.
RUN apt-get clean -y
RUN apt-get install -f -y
RUN dpkg --configure -a
RUN apt-get update -y
RUN apt-get install cmake build-essential pkg-config
# Install ffmpeg
RUN apt install ffmpeg -y
# Install pip packages.
RUN pip install --upgrade pip
RUN pip install \
num2words \
pypinyin \
subword-nmt==0.3.6 \
tqdm==4.34.0
RUN pip list
# Finishing touches.
WORKDIR .