docker 操作基础
先查询系统是否已经有docker安装包
安装docker
两种方式,如果从阿里云下载的镜像,默认是不带docker安装包的,需要安装epel-release镜像源
第一种 ,阿里云镜像
[root@control yum.repos.d]# yum install epel-release -y
[root@control yum.repos.d]# yum install docker-io -y
第二种,官方下载的镜像
[root@control yum.repos.d]# yum install docker -y
两种方法,哪个都可以!
接下来操作部分
启动doker并加入到开机启动
[root@control yum.repos.d]# systemctl start docker
[root@control yum.repos.d]# systemctl enable docker
通过docker info查看简信息
[root@control yum.repos.d]# docker info
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 1.13.1
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: journald
Cgroup Driver: systemd
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Swarm: inactive
Runtimes: docker-runc runc
Default Runtime: docker-runc
Init Binary: /usr/libexec/docker/docker-init-current
containerd version: (expected: aa8187dbd3b7ad67d8e5e3a15115d3eef43a7ed1)
runc version: 8891bca22c049cd2dcf13ba2438c0bac8d7f3343 (expected: 9df8b306d01f59d3a8029be411de015b7304dd8f)
init version: fec3683b971d9c3ef73f284f176672c44b44866
配置docker加速器
容器镜像服务 (aliyun.com)
[root@control docker]# /etc/docker
[root@control docker]# ls
certs.d daemon.json key.json seccomp.json
[root@control docker]# vi daemon.json
先把原来的{}删除, 加入以下内容。
{
"registry-mirrors": ["https://zg11lmao.mirror.aliyuncs.com"]
}
下载时间明显缩短
[root@control docker]# time docker run centos
Unable to find image 'centos:latest' locally
Trying to pull repository docker.io/library/centos ...
latest: Pulling from docker.io/library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for docker.io/centos:latest
real 0m39.473s
user 0m0.028s
sys 0m0.108s
实验 2 运行镜像
[root@control docker]# docker -h
Flag shorthand -h has been deprecated, please use --help
Usage: docker COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/root/.docker")
-D, --debug Enable debug mode
--help Print usage
-H, --host list Daemon socket(s) to connect to (default [])
-l, --log-level string Set the logging level ("debug", "info", "warn", "error", "fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/root/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Management Commands:
container Manage containers
image Manage images
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
volume Manage volumes
Commands:
attach Attach to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
Run 'docker COMMAND --help' for more information on a command.
docker 拉取镜像
docker pull[选项] [仓库地址] 仓库名:标签
docker pull nginx
docker 运行镜像
docker run 等同于docker pull +docker create + docker start
docker 常用操作
docker run -itd -v /root:/root -p 80:80 --name aaa nginx:latest /bin/bash 运行一个最新版本的nginx 容器
docker ps #查看运行中的容器
docker ps -a #查看所有容器
docker run -itd
-i 在交互模式下运行
-t tty伪终端
-d 在后台运行
-v 把指定的文件夹,挂载到容器内部的文件夹
-p 把指定的端口绑给某个容器去使用
--name 指定容器名字
nginx:latest nginx是指容器名称 latest是指标容器上的标签,目前是最新的,如果不加签,也会以最新的版本运行某个容器
/bin/bash 在运行时容器内部使用/bin/bash解释器
docker start “容器名或者id” 启动容器
docker stop “容器名或者id” 停止容器
docker images 等同于docker image ls 查看容器本地所有镜像
删除容器
[root@control docker]# docker stop 1027d488b7e8
[root@control docker]# docker rm 102
删除容器的镜像
[root@control docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/nginx latest 605c77e624dd 11 months ago 141 MB
docker.io/centos latest 5d0da3dc9764 14 months ago 231 MB
[root@control docker]#
[root@control docker]# docker rmi 605
Untagged: docker.io/nginx:latest
Untagged: docker.io/nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Deleted: sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85
Deleted: sha256:b625d8e29573fa369e799ca7c5df8b7a902126d2b7cbeb390af59e4b9e1210c5
Deleted: sha256:7850d382fb05e393e211067c5ca0aada2111fcbe550a90fed04d1c634bd31a14
Deleted: sha256:02b80ac2055edd757a996c3d554e6a8906fd3521e14d1227440afd5163a5f1c4
Deleted: sha256:b92aa5824592ecb46e6d169f8e694a99150ccef01a2aabea7b9c02356cdabe7c
Deleted: sha256:780238f18c540007376dd5e904f583896a69fe620876cabc06977a3af4ba4fb5
Deleted: sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f
[root@control docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest 5d0da3dc9764 14 months ago 231 MB
[root@control docker]#
一键清理dokcer 停止的容器与无用的镜像
[root@control /]# docker system prune -a
WARNING! This will remove:
- all stopped containers
- all volumes not used by at least one container
- all networks not used by at least one container
- all images without at least one container associated to them
表评论8539