Docker notes

Install Docker

Get docker shell script
curl -fsSL get.docker.com -o get-docker.sh
Run script
sh get-docker.sh
Check docker version, make sure docker install successfully
docker version
Start docker server
sudo systemctl start docker
Then check docker version again, if you can see engin information, the docker start success
Then create and run a container, this will install nginx and run the container
docker container run nginx
Check container status
docker container ls
To stop container, run
docker container stop <name or ID>
To check all container, include the container that already stopped, run
docker container ls -a
To remove a stopped container
docker rm xxxxxx xxxxxx is you container id
To remove all stopped container, use
docker container prune
The docker you created are not removed, to remove docker, run
docker container rm <name or ID>
Start nginx using detached mode, map port, -p <Port(Server), Port(Docker)>
docker run -d -p 80:80 nginx
You can also change name when create a container
docker run --name my-nginx -d -p 9001:80 nginx


docker run \
-p 9001:80 \
-v /home/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /home/nginx/conf/conf.d:/etc/nginx/conf.d \
-v /home/nginx/log:/var/log/nginx \
-v /home/nginx/html:/usr/share/nginx/html \
-d nginx:latest
Got error
docker: Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: error mounting "/home/nginx/conf/nginx.conf" to rootfs at "/etc/nginx/nginx.conf": mount /home/nginx/conf/nginx.conf:/etc/nginx/nginx.conf (via /proc/self/fd/6), flags: 0x5000: not a directory: unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type.

原因:

不支持直接挂载文件,只能挂载文件夹
想要挂载文件,必须宿主机也要有对应的同名文件

需要先创建或者复制文件
Need to create or copy file nginx.conf

删除之前创建的nginx挂载目录以及下面所有文件
Delete nginx folder and all files
rm -rf /home/nginx

正确的顺序是先创建挂载目录,然后运行容器复制进所有需要挂载的文件,删除容器,然后运行上面的命令(运行docker并且挂载文件)

创建Nginx docker container 并且挂载文件

创建挂载目录,接下来的步骤会在/home/nginx 目录挂载文件
mkdir -p /home/nginx/conf
mkdir -p /home/nginx/log
mkdir -p /home/nginx/html
运行容器
docker run --name nginx -p 9001:80 -d nginx
复制进所有需要挂载的文件
docker cp nginx:/etc/nginx/nginx.conf /home/nginx/conf/nginx.conf
docker cp nginx:/etc/nginx/conf.d /home/nginx/conf/conf.d
docker cp nginx:/usr/share/nginx/html /home/nginx/
删除容器
docker ps -a
docker container stop <name or ID>
docker rm <name or ID>
运行docker并且挂载文件

docker run \
-p 9001:80 \
-v /home/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /home/nginx/conf/conf.d:/etc/nginx/conf.d \
-v /home/nginx/log:/var/log/nginx \
-v /home/nginx/html:/usr/share/nginx/html \
-d nginx:latest

To run nginx command inside a docker container, use exec
docker exec -it {container_name/id} {command}
For example, to reload docker config nginx -s reload
docker exec -it e08ef3452fac nginx -s reload
If you run docker exec -it e08ef3452fac nginx -s stop, to container will also be stop, to restart container, run
docker container restart e08ef3452fac, you can find all container using docker ps -a
Useful command for nginx:
nginx // start nginx
nginx -s stop // force stop
nginx -s quit // stop
nginx -s reload // reload config (important, will use a lot)