docker中的容器:
Open Container-initiative
Open Container Format
runC is a CLI tool for spawning and running containers according to the OCI specification
docker提供了一个专门容纳容器镜像的站点:https://hub.docker.com
为什么镜像仓库名字是Registry而不是repository?在docker中仓库的名字是以应用的名称取名的。
镜像是静态的,而容器是动态的,容器有其生命周期,镜像与容器的关系类似于程序与进程的关系。镜像类似于文件系统中的程序文件,而容器则类似于将一个程序运行起来的状态,也即进程。所以容器是可以删除的,容器被删除后其镜像是不会被删除的。
When you use docker, you are creating and using images, containers, networks, volumes, pluginns, and other objects.
[root@localhost ~]# yum install -y yum-utils
[root@localhost ~]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
[root@localhost ~]# yum clean all && yum makecache fast
[root@localhost ~]# yum -y install docker-ce docker-ce-cli containerd.io
[root@localhost ~]# systemctl enable --now docker
docker-ce的配置文件是/etc/docker/daemon.json,此文件默认不存在,需要我们手动创建并进行配置,而docker的加速就是通过配置此文件来实现的。
docker的加速有多种方式:
[root@localhost ~]# vim /etc/docker/daemon.json
//添加以下内容
{
"registry-mirrors": ["https://hub-mirror.c.163.com/"]
}
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart docker
//可以使用docker info查看信息
[root@localhost ~]# docker info
Client:
Debug Mode: false
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 19.03.12
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 7ad184331fa3e55e52b890ea95e65ba581ae3429
runc version: dc9208a3303feef5b3839f4323d9beb36df0a9dd
init version: fec3683
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-1127.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 972.3MiB
Name: localhost.localdomain
ID: S4BD:L74S:I27T:NTTE:SPCV:SZGS:TNBP:7N7V:2VGC:7MM7:FJE6:XY6N
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://registry.docker-cn.com/ //这里就是加速地址
Live Restore Enabled: false
可参考菜鸟教程
命令 | 功能 |
---|---|
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@localhost ~]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 13662 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1866 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 782 [OK]
linuxserver/nginx An Nginx container, brought to you by LinuxS… 127
bitnami/nginx Bitnami nginx Docker Image 89 [OK]
[root@localhost ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
bf5952930446: Pull complete
cb9a6de05e5a: Pull complete
9513ea0afb93: Pull complete
b49ea07d2e93: Pull complete
a5e4a503d449: Pull complete
Digest: sha256:b0ad43f7ee5edbc0effbc14645ae7055e21bc1973aee5150745632a24a752661
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 4bb46517cac3 12 days ago 133MB
[root@localhost ~]# docker create --name t1 nginx:latest
e2caa3f5f3382fc3bb0dbf67fc2556d74a8fe9cb498c6e2d104b7c2fd87dca41
[root@localhost ~]# docker start t1
t1
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e2caa3f5f338 nginx:latest "/docker-entrypoint.…" About a minute ago Up 2 seconds 80/tcp t1
[root@localhost ~]# docker run -it nginx:latest /bin/bash
root@e7ae58242760:/# ls
bin docker-entrypoint.d home media proc sbin tmp
boot docker-entrypoint.sh lib mnt root srv usr
dev etc lib64 opt run sys var
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e7ae58242760 nginx:latest "/docker-entrypoint.…" 6 minutes ago Up 6 minutes 80/tcp musing_saha
[root@localhost ~]# docker attach e7ae58242760
root@e7ae58242760:/# ls
bin docker-entrypoint.d home media proc sbin tmp
boot docker-entrypoint.sh lib mnt root srv usr
dev etc lib64 opt run sys var
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
25622c5bff24 nginx:latest "/docker-entrypoint.…" 6 seconds ago Up 5 seconds 80/tcp infallible_thompson
[root@localhost ~]# docker logs t1
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
25622c5bff24 nginx:latest "/docker-entrypoint.…" 4 minutes ago Up 4 minutes 80/tcp infallible_thompson
[root@localhost ~]# docker kill 25622c5bff24
25622c5bff24
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
25622c5bff24 nginx:latest "/docker-entrypoint.…" 6 minutes ago Exited (137) About a minute ago infallible_thompson
e7ae58242760 nginx:latest "/docker-entrypoint.…" 14 minutes ago Exited (0) 7 minutes ago musing_saha
e2caa3f5f338 nginx:latest "/docker-entrypoint.…" 17 minutes ago Exited (0) 8 minutes ago t1
[root@localhost ~]# docker rm e2caa3f5f338
e2caa3f5f338
[root@localhost ~]# docker rm e7ae58242760
e7ae58242760
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
25622c5bff24 nginx:latest "/docker-entrypoint.…" 6 minutes ago Exited (137) 2 minutes ago infallible_thompson
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
25622c5bff24 nginx:latest "/docker-entrypoint.…" 9 minutes ago Up 24 seconds 80/tcp infallible_thompson
[root@localhost ~]# docker exec -it 25622c5bff24 /bin/bash
root@25622c5bff24:/# ls
bin docker-entrypoint.d home media proc sbin tmp
boot docker-entrypoint.sh lib mnt root srv usr
dev etc lib64 opt run sys var
[root@localhost ~]# docker inspect nginx:latest
[
{
"Id": "sha256:4bb46517cac397bdb0bab6eba09b0e1f8e90ddd17cf99662997c3253531136f8",
"RepoTags": [
"nginx:latest"
],
...此处内容略
...
"Cmd": [
"/bin/sh",
"-c",
"#(nop) ",
"CMD [\"nginx\" \"-g\" \"daemon off;\"]"
],
"ArgsEscaped": true,
"Image": "sha256:e046ee8113f48aaab8b7326ab6aed674eead374224b513de0684c2802ed2f3b9",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": [
"/docker-entrypoint.sh"
],
"OnBuild": null,
"Labels": {
"maintainer": "NGINX Docker Maintainers "
},
"StopSignal": "SIGTERM"
},
...此处内容略
...
]
},
"Metadata": {
"LastTagTime": "0001-01-01T00:00:00Z"
}
}
]