Docker的基本使用
文章目录
Docker命令示意图:
1.1 准备工作
1.1.1 安装、启动以及卸载
(1)centos安装docker
-
删除旧版本
sudo yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-engine
-
The contents of
/var/lib/docker/
, including:- images
- containers
- volumes
- networks
-
The Docker Engine package is now called
docker-ce
.
(2)安装
-
安装时使用稳定版仓库
sudo yum install -y yum-utils sudo yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo
-
使用其他仓库
nightly:
sudo yum-config-manager --enable docker-ce-nightly
test channel:
sudo yum-config-manager --enable docker-ce-test
-
取消使用特定仓库:
sudo yum-config-manager --disable docker-ce-nightly sudo yum-config-manager --disable docker-ce-test
-
-
安装Docker引擎(最新版本)
sudo yum install docker-ce docker-ce-cli containerd.io
-
安装其他版本
检查可用版本:
yum list docker-ce --showduplicates | sort -r docker-ce.x86_64 3:18.09.1-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.0-3.el7 docker-ce-stable docker-ce.x86_64 18.06.1.ce-3.el7 docker-ce-stable docker-ce.x86_64 18.06.0.ce-3.el7
安装特定版本:
sudo yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io
-
脚本安装:
Warning:Always examine scripts downloaded from the internet before running them locally.
curl -fsSL https://get.docker.com -o get-docker.sh sudo sh get-docker.sh
-
(3)启动docker
-
启动
sudo systemctl start docker
-
开机启动docker守护进程服务
sudo systemctl enable docker
-
重启服务
sudo systemctl daemon-reload sudo systemctl restart docker
(4)卸载
-
卸载docker
sudo yum remove docker-ce docker-ce-cli containerd.io
-
删除容器,卷,自定义配置文件
sudo rm -rf /var/lib/docker
1.1.2 Docker在windows环境下的安装
(1)使用WSL2安装Docker
- 因为docker是采用直接利用宿主机内核的方式去执行虚拟机功能,所以在Windows环境下的docker只能运行Windows相关的系统或应用软件容器,但是我们可以利用Windows提供的WSl2来在Windows中运行一个Linux内核的Linux系统,进而运行docker;而且在Docker Desktop中,也提供了使用WSL2运行docker的方式,所以可以在Windows系统下运行基于Linux的docker!
(2)WSL2以及Windows docker desktop的安装
WSL2的介绍与使用可以去官网看,也可以找博客看。(WSL2相当于在Windows上并行运行了一个Linux内核的Linxu子系统,个人感觉使用Windows的WSl2后,最起码在新手阶段(非底层开发),可以代替常规的Linux来使用,加上Windows上的丰富的软件又可以弥补在Linux的软件匮乏,简直不要爽歪歪!!!)
(3)在WSL2中安装docker并测试:
最终效果:(使用Fluent Terminal做了一些美化)
1.2 常用命令
1.2.1 docker镜像基本使用
(1)镜像加速
-
创建或修改
/etc/docker/daemon.json
文件,修改为如下形式{ "registry-mirrors": [ "https://e9cvqj3d.mirror.aliyuncs.com", "http://hub-mirror.c.163.com", "https://registry.docker-cn.com", "https://docker.mirrors.ustc.edu.cn" ] }
-
重启生效
sudo systemctl daemon-reload sudo systemctl restart docker
(2)使用镜像
-
搜索镜像
docker search image_name
-
使用latest版本镜像
docker pull image_name
-
指定版本
docker pull image_name:tag
-
(3)查看镜像列表
-
查看下载的镜像
docker images -h Usage: docker images [OPTIONS] [REPOSITORY[:TAG]] List images Options: -a, --all Show all images (default hides intermediate images) --digests Show digests -f, --filter filter Filter output based on conditions provided --format string Pretty-print images using a Go template --no-trunc Don't truncate output -q, --quiet Only show numeric IDs
(4)删除镜像
-
删除镜像
docker rmi [OPTIONS] IMAGE [IMAGE...]
选项:
Options: -f, --force Force removal of the image --no-prune Do not delete untagged parents
1.2.2 容器的使用
(1)创建容器并启动
-
docker run
命令以镜像为模板作为容器执行 -
注意:如果运行的容器没有前台进程运行则该容器会直接自杀
- 可以使用top,tail命令查看日志文件来制造前台进程防止自杀
常用选项:
-
-d
:后台方式运行 -
-e Command
:指定容器应用参数 -
-it
:以终端交互方式运行 -
--name=
:指定创建的容器的名称(容器都只能使用唯一的名) -
-rm
:使用完后边删除容器 -
-p [主机IP:][docker内应用IP]
所有命令选项:
sudo docker run --help Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...] Run a command in a new container Options: --add-host list Add a custom host-to-IP mapping (host:ip) -a, --attach list Attach to STDIN, STDOUT or STDERR --blkio-weight uint16 Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0) --blkio-weight-device list Block IO weight (relative device weight) (default []) --cap-add list Add Linux capabilities --cap-drop list Drop Linux capabilities --cgroup-parent string Optional parent cgroup for the container --cidfile string Write the container ID to the file --cpu-period int Limit CPU CFS (Completely Fair Scheduler) period --cpu-quota int Limit CPU CFS (Completely Fair Scheduler) quota --cpu-rt-period int Limit CPU real-time period in microseconds --cpu-rt-runtime int Limit CPU real-time runtime in microseconds -c, --cpu-shares int CPU shares (relative weight) --cpus decimal Number of CPUs --cpuset-cpus string CPUs in which to allow execution (0-3, 0,1) --cpuset-mems string MEMs in which to allow execution (0-3, 0,1) -d, --detach Run container in background and print container ID --detach-keys string Override the key sequence for detaching a container --device list Add a host device to the container --device-cgroup-rule list Add a rule to the cgroup allowed devices list --device-read-bps list Limit read rate (bytes per second) from a device (default []) --device-read-iops list Limit read rate (IO per second) from a device (default []) --device-write-bps list Limit write rate (bytes per second) to a device (default []) --device-write-iops list Limit write rate (IO per second) to a device (default []) --disable-content-trust Skip image verification (default true) --dns list Set custom DNS servers --dns-option list Set DNS options --dns-search list Set custom DNS search domains --domainname string Container NIS domain name --entrypoint string Overwrite the default ENTRYPOINT of the image -e, --env list Set environment variables --env-file list Read in a file of environment variables --expose list Expose a port or a range of ports --gpus gpu-request GPU devices to add to the container ('all' to pass all GPUs) --group-add list Add additional groups to join --health-cmd string Command to run to check health --health-interval duration Time between running the check (ms|s|m|h) (default 0s) --health-retries int Consecutive failures needed to report unhealthy --health-start-period duration Start period for the container to initialize before starting health-retries countdown (ms|s|m|h) (default 0s) --health-timeout duration Maximum time to allow one check to run (ms|s|m|h) (default 0s) --help Print usage -h, --hostname string Container host name --init Run an init inside the container that forwards signals and reaps processes -i, --interactive Keep STDIN open even if not attached --ip string IPv4 address (e.g., 172.30.100.104) --ip6 string IPv6 address (e.g., 2001:db8::33) --ipc string IPC mode to use --isolation string Container isolation technology --kernel-memory bytes Kernel memory limit -l, --label list Set meta data on a container --label-file list Read in a line delimited file of labels --link list Add link to another container --link-local-ip list Container IPv4/IPv6 link-local addresses --log-driver string Logging driver for the