docker基础用法
什么是docker
docker中的容器:
- lxc --> libcontainer --> runC
OCI
Open Container-initiative
- 由Linux基金会主导于2015年6月创立
- 旨在围绕容器格式和运行时制定一个开放的工业化标准
- contains two specifications
- the Runtime Specification(runtime-spec)
- the Image Specification(image-spec)
OCF
Open Container Format
runC is a CLI tool for spawning and running containers according to the OCI specification
- Containers are started as a child process of runC and can be embedded into various other systems without having to run a daemon
- runC is built on libcontainer, the same container technology powering millions of Docker Engine installations
docker提供了一个专门容纳容器镜像的站点:https://hub.docker.com
docker架构
docker镜像与镜像仓库
为什么镜像仓库名字是Registry而不是repository?在docker中仓库的名字是以应用的名称取名的。
镜像是静态的,而容器是动态的,容器有其生命周期,镜像与容器的关系类似于程序与进程的关系。镜像类似于文件系统中的程序文件,而容器则类似于将一个程序运行起来的状态,也即进程。所以容器是可以删除的,容器被删除后其镜像是不会被删除的。
docker对象
When you use docker, you are creating and using images, containers, networks, volumes, pluginns, and other objects.
- IMAGES
- An image is a read-only template with instructions for creating a docker container.
- Often, an image is based on another image, with some additional customization.
- You might create your own images or you might only use those created by others and published in a registry.
- CONTAINERS
- A conntainer is a runnable instance of an image.
- You can create, run, stop, move, or delete a container using the docker API or CLI.
- You can connect a container to one or more networks, attach storage to it, or even create a new image based on its current state.
docker加速
docker-ce的配置文件是/etc/docker/daemon.json,此文件默认不存在,需要我们手动创建并进行配置,而docker的加速就是通过配置此文件来实现的。
docker的加速有多种方式:
- docker cn
- 中国科技大学加速器
- 阿里云加速器(需要通过阿里云开发者平台注册帐号,免费使用个人私有的加速器
https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors
[root@lvs ~]# systemctl start docker
[root@lvs docker]# cat daemon.json
{
"registry-mirrors": ["https://8iitoqxc.mirror.aliyuncs.com"]
}
[root@lvs docker]#
[root@lvs docker]# systemctl restart docker
[root@lvs docker]# docker version
[root@lvs docker]# docker info
Client: Docker Engine - Community
Version: 24.0.7
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://8iitoqxc.mirror.aliyuncs.com/ //看到这表示成功
Live Restore Enabled: false
docker常用操作
命令 | 功能 |
---|---|
docker search | Search the Docker Hub for images |
docker pull | Pull an image or a repository from a registry |
docker images | List images |
docker create | Create a new conntainer |
docker start | Start one or more stopped containers |
docker run | Run a command in a new container |
docker attach | Attach to a runninng container |
docker ps | List containers |
docker logs | Fetch the logs of a container |
docker restart | Restart a container |
docker stop | Stop one or more running containers |
docker kill | Kill one or more running containers |
docker rm | Remove onne or more containers |
docker exec | Run a command in a running container |
docker info | Display system-wide information |
docker inspect | Return low-level information on Docker objects |
[root@lvs ~]# docker search http
[root@lvs docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@lvs docker]# docker pull httpd
[root@lvs ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@lvs ~]# docker create --name httpd httpd
[root@lvs ~]# docker inspect httpd
[root@lvs ~]# docker ps -a
[root@lvs ~]# docker create --name httpd httpd
[root@lvs ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f22488055a15 httpd "httpd-foreground" 58 seconds ago Created httpd
03cbb799ad7b centos "/bin/bash" 8 hours ago Up 2 hours https
[root@lvs ~]# docker start httpd
httpd
# 容器退出后进入容器
[root@lvs ~]# docker exec -it https /bin/bash
[root@lvs ~]# docker kill https
# 把容器的端口映射到真机上面
[root@lvs ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
03cbb799ad7b centos "/bin/bash" 8 hours ago Up 7 minutes https
[root@lvs ~]# docker run -d --name centos -p 8080:80 https