Docker配置阿里云镜像加速
#第一步
sudo mkdir -p /etc/docker
#第二步
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://7mp81chq.mirror.aliyuncs.com"]
}
EOF
#第三步
sudo systemctl daemon-reload
#第四步
sudo systemctl restart docker
Docker执行过程
[root@jun bin]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:9f6ad537c5132bcce57f7a0a20e317228d382c3cd61edae14650eec68b2b345c
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
[root@jun bin]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 3 months ago 13.3kB
执行时先查找有无镜像,没有镜像则下载,镜像不存在则无法运行
底层原理
docker为什么比VM快?
- 因为Docker有比虚拟机更少的抽象层.
- docker利用的是宿主机的内核,不需要像虚拟机一样重新加载一个操作系统内核,VM则需要Guest OS
Docker的常用命令
官方文档:https://docs.docker.com/reference/
docker version #显示Docker的版本信息
docker info #显示docker的系统信息,包括镜像和容器的数量
docker 命令 --help #帮助命令
镜像命令
docker images #查看已有镜像
docker images -a #列出所有镜像
docker images -q #只显示镜像的id
[root@jun /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 3 months ago 13.3kB
[root@jun /]# docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 3 months ago 13.3kB
[root@jun /]# docker images -q
d1165f221234
docker search #搜索镜像
[root@jun /]# docker search --help
Usage: docker search [OPTIONS] TERM
Search the Docker Hub for images
Options:
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print search using a Go template
--limit int Max number of search results (default 25)
--no-trunc Don't truncate output
[root@jun /]# docker search mysql --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11042 [OK]
mariadb MariaDB Server is a high performing open sou… 4186 [OK]
docker pull 镜像下载
[root@jun /]# docker pull --help
Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Pull an image or a repository from a registry
Options:
-a, --all-tags Download all tagged images in the repository
--disable-content-trust Skip image verification (default true)
--platform string Set platform if server is multi-platform capable
-q, --quiet Suppress verbose output
[root@jun /]# docker pull mysql
Using default tag: latest # 如果不指定版本,则下载最新的版本
latest: Pulling from library/mysql
b4d181a07f80: Pull complete # 分层下载,docker image的核心 联合文件系统
a462b60610f5: Pull complete
578fafb77ab8: Pull complete
524046006037: Pull complete
d0cbe54c8855: Pull complete
aa18e05cc46d: Pull complete
32ca814c833f: Pull complete
9ecc8abdb7f5: Pull complete
ad042b682e0f: Pull complete
71d327c6bb78: Pull complete
165d1d10a3fa: Pull complete
2f40c47d0626: Pull complete
Digest: sha256:52b8406e4c32b8cf0557f1b74517e14c5393aff5cf0384eff62d9e81f4985d4b
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest
docker rmi 删除镜像
[root@jun /]# docker rmi --help
Usage: docker rmi [OPTIONS] IMAGE [IMAGE...]
Remove one or more images
Options:
-f, --force Force removal of the image
--no-prune Do not delete untagged parents
容器命令
只有镜像存在才能创建容器,下载一个centos来测试学习
# 启动并进入容器
[root@jun /]# docker run -it centos /bin/bash 使用交互方式运行并进入容器查看内容
[root@f635888533e0 /]# ls 查看容器内部的centos
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
# 从容器中退回主机
[root@f635888533e0 /]# exit
exit
[root@jun /]# ls
bin dev home lib64 media opt root sbin sys usr
boot etc lib lost+found mnt proc run srv tmp var
docker ps 列出所有运行的容器
- -a 列出正在运行以及创建过的容器
- -a -n=1 显示最近的一个容器
- -aq 显示所有容器的编号
# 列出所有运行的容器
[root@jun /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@jun /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f635888533e0 centos "/bin/bash" 4 minutes ago Exited (0) 56 seconds ago competent_galileo
0809555ab0f6 hello-world "/hello" 5 hours ago Exited (0) 5 hours ago fervent_pasteur
退出容器
- exit # 容器停止并直接退出
- Crtl + P + Q # 容器不停止退出
删除容器
- docker rm [容器id] 删除指定的容器,不能删除正在运行的容器
- docker rm -f $(docker ps -aq) 删除所有的内容
- docker ps -a -q|xargs docker rm 删除所有的容器
启动和停止容器
- docker start 容器id 启动容器
- docker restart 容器id 重启容器
- docker stop 容器id 停止当前正在运行的容器
- docker kill 容器id 强制停止当前正在运行的容器
常用其他命令
后台启动容器
- docker run -d 容器名
- 常见的坑:docker容器使用后台运行,就必须要有一个其它进程,docker发现没有应用,就会自动停止
查看日志
- docker logs -tf --tail 10 容器id
[root@jun /]# docker logs --help
Usage: docker logs [OPTIONS] CONTAINER
Fetch the logs of a container
Options:
--details Show extra details provided to logs
-f, --follow Follow log output
--since string Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42
minutes)
-n, --tail string Number of lines to show from the end of the logs (default "all")
-t, --timestamps Show timestamps
--until string Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for
42 minutes)
查看容器的元数据
- docker inspects 容器Id
[root@jun /]# docker inspect --help
Usage: docker inspect [OPTIONS] NAME|ID [NAME|ID...]
Return low-level information on Docker objects
Options:
-f, --format string Format the output using the given Go template
-s, --size Display total file sizes if the type is container
--type string Return JSON for specified type
进入正在运行的容器
- docker exec -it 容器id bashshell
- docker attach 容器id
docker exec # 进入容器后开启一个新的终端,可以在里面操作
docker attach # 进入容器正在执行的终端,不会启动新的进程
从容器内拷贝文件到主机上面
- docker cp 容器id:容器内路径 目的主机路径
# 查看主机目录下的文件
[root@jun home]# touch hello.go
[root@jun home]# ls
hello.go jun Tim
[root@jun home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
39937021aebd centos "/bin/bash" 11 hours ago Up 11 hours confident_hoover
25e914d28107 centos "/bin/bash" 11 hours ago Up 11 hours happy_carver
# 进入docker 容器内部
[root@jun home]# docker attach 25e914d28107
[root@25e914d28107 /]# cd /home
[root@25e914d28107 home]# ls
# 在容器内新建文件
[root@25e914d28107 home]# touch test.go
[root@25e914d28107 home]# exit
exit
[root@jun home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
39937021aebd centos "/bin/bash" 11 hours ago Up 11 hours confident_hoover
[root@jun home]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
39937021aebd centos "/bin/bash" 11 hours ago Up 11 hours confident_hoover
25e914d28107 centos "/bin/bash" 11 hours ago Exited (0) 18 seconds ago happy_carver
f635888533e0 centos "/bin/bash" 17 hours ago Exited (0) 17 hours ago competent_galileo
0809555ab0f6 hello-world "/hello" 22 hours ago Exited (0) 22 hours ago fervent_pasteur
# 将这个文件拷贝出来到主机上
[root@jun home]# docker cp 25e914d28107:/home/test.go /home
[root@jun home]# ls
hello.go jun test.go Tim
网友评论