Docker

整体容器服务层面

# 安装容器服务
apt install docker.io
# 启动
systemctl start docker
# 自动启动
systemctl enable docker
# --help 
docker stats --help
docker container --help
# 列出所有容器
docker ps -a

Run docker

# -d run as daemon, in background
# --name unique name, referencable later
# --link depends on other docker
# -p port mapping, host_port:container_port
docker run -d --name nextcloud --link mysqlnc -v /root/nextcloud/data:/data -p 3000:80 tag/image_name

# -i interactive
# -t in a terminal
# ubuntu: image name
# /bin/bash: run docker之后
docker run -it ubuntu /bin/bash

进入容器

# 如果从这个容器退出,会导致容器的停止
docker attach cc3f2ff51cab 
# 如果从这个容器退出,不会导致容器的停止,这就是为什么推荐大家使用 docker exec 的原因
docker exec -it cc3f2ff51cab /bin/bash

容器导出(snapshot)和导入

docker export cc3f2ff51cab > ubuntu.tar
docker import http://example.com/exampleimage.tgz example/imagerepo
# or
cat docker/ubuntu.tar | docker import - test/ubuntu:v1

容器命令

# get a list of all containers
# 这两条命令效果一样
docker ps -a
docker container ls -a
# 停止某容器id
docker stop cc3f2ff51cab
docker restart cc3f2ff51cab

# 受不了了
docker kill container_id_or_name

# 移除容器
docker rm cc3f2ff51cab
# To remove all stopped containers
docker container prune

# 展示某容器的log
docker logs log_id
# like tail -f
docker logs -f container_id

# 容器的5000端口映射到了宿主机的的49155端口
docker port cc3f2ff51cab
0.0.0.0:49155

重启Policy

docker container update --restart=always 容器名字/id
systemctl restart docker

卸载

apt purge docker docker-engine docker.io containerd runc
apt autoremove --purge docker-engine
rm -rf /var/lib/docker # This deletes all images, containers, and volumes

特别技巧

# 复原最初的 docker run 命令的所有原始参数
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock \
    assaflavie/runlike docker_name_or_id

Leave a Reply