Docker notes
Install Docker
Get docker shell scriptcurl -fsSL get.docker.com -o get-docker.sh
Run scriptsh get-docker.sh
Check docker version, make sure docker install successfullydocker version
Start docker serversudo 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 containerdocker container run nginx
Check container statusdocker container ls
To stop container, rundocker container stop <name or ID>
To check all container, include the container that already stopped, rundocker container ls -a
To remove a stopped containerdocker rm xxxxxx xxxxxx is you container id
To remove all stopped container, usedocker container prune
The docker you created are not removed, to remove docker, rundocker 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 containerdocker 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 filesrm -rf /home/nginx
正确的顺序是先创建挂载目录,然后运行容器复制进所有需要挂载的文件,删除容器,然后运行上面的命令(运行docker并且挂载文件)
创建Nginx docker container 并且挂载文件
创建挂载目录,接下来的步骤会在/home/nginx 目录挂载文件mkdir -p /home/nginx/confmkdir -p /home/nginx/logmkdir -p /home/nginx/html
运行容器docker run --name nginx -p 9001:80 -d nginx
复制进所有需要挂载的文件docker cp nginx:/etc/nginx/nginx.conf /home/nginx/conf/nginx.confdocker cp nginx:/etc/nginx/conf.d /home/nginx/conf/conf.ddocker cp nginx:/usr/share/nginx/html /home/nginx/
删除容器docker ps -adocker 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 execdocker exec -it {container_name/id} {command}
For example, to reload docker config nginx -s reloaddocker 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, rundocker container restart e08ef3452fac, you can find all container using docker ps -a
Useful command for nginx:nginx // start nginxnginx -s stop // force stopnginx -s quit // stopnginx -s reload // reload config (important, will use a lot)