参考:
https://www.bookstack.cn/read/docker-practice/appendix-best_practices.md
https://yeasy.gitbook.io/docker_practice/appendix/repo/redis#:~:text=%E9%BB%98%E8%AE%A4%E6%95%B0%E6%8D%AE%E5%AD%98%E5%82%A8%E4%BD%8D%E7%BD%AE%E5%9C%A8%20VOLUME%2Fdata%20%E3%80%82%20%E5%8F%AF%E4%BB%A5%E4%BD%BF%E7%94%A8%20–volumes-from,some-volume-container%20%E6%88%96%20-v%20%2Fdocker%2Fhost%2Fdir%3A%2Fdata%20%E5%B0%86%E6%95%B0%E6%8D%AE%E5%AD%98%E6%94%BE%E5%88%B0%E6%9C%AC%E5%9C%B0%E3%80%82
安装:
sudo curl -L "https://github.com/docker/compose/releases/download/2.2.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
$ sudo curl -L https://github.com/docker/compose/releases/download/2.2.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
$ sudo chmod +x /usr/local/bin/docker-compose
赋权限:
sudo chmod +x /usr/local/bin/docker-compose
docker ps -a
docker images
docker inspect container_name | grep Mounts -A 20
$ cd /etc/docker
$ vim daemon.json
{
"registry-mirrors": [
"https://hub-mirror.c.163.com",
"https://ustc-edu-cn.mirror.aliyuncs.com",
"https://ghcr.io",
"https://mirror.baidubce.com"
]
}
# 重启docker
$ service docker restart
# 重启所有容器
$ docker restart $(docker ps -a -q)
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' >/etc/timezone
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
或者
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
重启容器即可
# 创建并运行容器,通过 -e TZ="Asia/Shanghai" 设置时区 docker run -e TZ="Asia/Shanghai" -d -p 80:80 --name nginx nginx
docker run -itd --name=test -v /opt/test:/usr/databases docker-test
docker run -itd --name=go_test -p 9000:9000 -v /home/Go:/home/go golang:latest
# docker run -itd
--name py-ceshi
-p 9000:9000
-v /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime
-v /home/python/ceshi.py:/code/ceshi.py
python38/ceshi:v1
-d :在分离模式下(后台)运行
--name :给运行的容器取一个名字,即 py-ceshi
-p :宿主机端口映射py-ceshi容器端口,即2222映射2222,需确保安全组已开放端口
-v :宿主机与容器数据挂载路径;第一个v同步宿主机与容器时间;第二个用来同步代码,这样只需要修改宿主机上的 /home/python/ceshi.py 代码即可同步到容器
python38/ceshi:v1 :使用的镜像
docker inspect (container_id/container_name) | grep Mounts -A 20
docker cp Name:/container_path to_path
docker cp ID:/container_path to_path
# 示例
docker cp foo.txt mycontainer:/foo.txt
docker cp mycontainer:/foo.txt foo.txt
docker logs -f CONTAINER_ID
docker logs --tail 200 CONTAINER_ID
docker logs --since 30m CONTAINER_ID
docker logs -t --since "2020-10-25T14:06:30" CONTAINER_ID
docker logs -t --since "2020-10-25T14:06:30" --until "2020-10-26T14:06:30" CONTAINER_ID
# 例如: docker save -o 要保存的文件名 要保存的镜像
docker save -o python-test.tar python:v1
docker import python-test.tar test:v2
docker load -i python-test.tar
docker build -t test:v1 .
docker run --name (name) -itd test:v1
– name:个人设置的name
docker exec -it (name) /bin/bash
1、删除全部容器
如果容器已经全部停止的话可以这样使用
docker rm $(docker ps -aq)
2、停止并删除全部容器
如果不想挨个去停用容器,可以使用这条命令
docker stop $(docker ps -q) & docker rm $(docker ps -aq)
3、删除所有镜像
ps: 删除镜像之前你要保证镜像没有被使用,否则删除不了,所以需要删除全部容器的时候,你需要先执行上边的命令,然后再执行下边这条命令
docker rmi -f $(docker images -qa)
批量删除镜像
docker rmi -f $(docker images | grep "none" | awk '{print $3}')
批量删除已停止的镜像
docker rm `docker ps -a|grep Exited|awk '{print $1}'`
tips:
docker inspect 容器名,可以查看到容器的元信息,在返回的j’son信息里面有个Mounts字段可以看到挂载目录
# 指定基础镜像
FROM python:3.6.8
# 指定镜像的维护者
MAINTAINER shiyue "[email protected]"
# 将指定文件添加到容器中指定的位置
ADD /Alarm_Combine/ /home/alarm/
# 设置工作目录
WORKDIR /home/alarm/
# 执行命令(安装Python项目的依赖项)
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
RUN pip3 install --upgrade pip \
&& pip3 install -r /home/alarm/requirements.txt -i https://pypi.mirrors.ustc.edu.cn/simple/
# RUN pip install -r requirements.txt -i https://pypi.doubanio.com/simple/
# 容器启动时要执行的命令
# CMD ["/bin/echo", "this is a test image"]
# ENTRYPOINT ["sh start.sh"]
# 暴露端口
#EXPOSE 8000
# {concierge,librarian}/Dockerfile
FROM golang:1.9.1
# In case of librarian, '/concierge' will be replaced with '/librarian'
ADD . /go/src/github.com/last-ent/distributed-go/chapter5/goophr/concierge
WORKDIR /go/src/github.com/last-ent/distributed-go/chapter5/goophr/concierge
RUN go install github.com/last-ent/distributed-go/chapter5/goophr/concierge
ENTRYPOINT /go/bin/concierge
EXPOSE 9000
FROM python:2.7.5
ADD /home/python2_package/ /home/alarm/
WORKDIR /home/alarm/FIS_Logger
RUN sed -i 's#http://archive.ubuntu.com/#http://mirrors.tuna.tsinghua.edu.cn/#' /etc/apt/sources.list;
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone
# COPY ./master .
RUN pip install -r requirements.txt
CMD ["sh", "start.sh"]