docker基础用法
什么是docker
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的 Linux或Windows 机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器或Windows 机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。
一个完整的Docker有以下几个部分组成:
- DockerClient客户端
- Docker Daemon守护进程
- Docker Image镜像
- DockerContainer容器 [2]
docker加速器
//开机自启docker
[root@localhost ~]# systemctl enable --now docker
//配置加速器
[root@localhost ~]# cat > /etc/docker/daemon.json <<EOF
{
"registry-mirrors": ["https://registry.docker-cn.com"]
}
EOF
//使用docker search 命令可以搜索docker hub官方仓库中的httpd镜像
[root@localhost ~]# docker search httpd
//下载官方仓库中的httpd镜像
[root@localhost ~]# docker pull httpd
6ec7b7d162b2: Downloading 3.7
6ec7b7d162b2: Pull complete
17e233bac21e: Pull complete
130aad5bf43a: Pull complete
81d0a34533d4: Pull complete
da240d12a8a4: Pull complete
Digest: sha256:a3a2886ec250194804974932eaf4a4ba2b77c4e7d551ddb63b01068bf70f4120
Status: Downloaded newer image for httpd:latest
docker.io/library/httpd:latest
//查看本地有不有镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ae2feff98a0c 2 weeks ago 133MB
httpd latest dd85cdbb9987 3 weeks ago 138MB
//查看某一个镜像
[root@localhost ~]# docker images httpd
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd latest dd85cdbb9987 3 weeks ago 138MB
//查看镜像历史
[root@localhost ~]# docker image history nginx
IMAGE CREATED CREATED BY SIZE COMMENT
ae2feff98a0c 2 weeks ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon… 0B
<missing> 2 weeks ago /bin/sh -c #(nop) STOPSIGNAL SIGQUIT 0B
<missing> 2 weeks ago /bin/sh -c #(nop) EXPOSE 80 0B
<missing> 2 weeks ago /bin/sh -c #(nop) ENTRYPOINT ["/docker-entr… 0B
<missing> 2 weeks ago /bin/sh -c #(nop) COPY file:0fd5fca330dcd6a7… 1.04kB
<missing> 2 weeks ago /bin/sh -c #(nop) COPY file:0b866ff3fc1ef5b0… 1.96kB
<missing> 2 weeks ago /bin/sh -c #(nop) COPY file:e7e183879c35719c… 1.2kB
<missing> 2 weeks ago /bin/sh -c set -x && addgroup --system -… 63.7MB
<missing> 2 weeks ago /bin/sh -c #(nop) ENV PKG_RELEASE=1~buster 0B
<missing> 2 weeks ago /bin/sh -c #(nop) ENV NJS_VERSION=0.5.0 0B
<missing> 2 weeks ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.19.6 0B
<missing> 3 weeks ago /bin/sh -c #(nop) LABEL maintainer=NGINX Do… 0B
<missing> 3 weeks ago /bin/sh -c #(nop) CMD ["bash"] 0B
<missing> 3 weeks ago /bin/sh -c #(nop) ADD file:3a7bff4e139bcacc5… 69.2MB
//查看nginx的详细信息
[root@localhost ~]# docker image inspect nginx
//创建nginx容器 create只创建不运行
[root@localhost ~]# docker create nginx
f2d8023e4fe552d3679581d2083fae52846cc3676ebed14b87b8ee05304e0d02
//查看容器有那些
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f2d8023e4fe5 nginx "/docker-entrypoint.…" About a minute ago Created affectionate_wright
//启动nginx
[root@localhost ~]# docker start f2d8023e4fe5
f2d8023e4fe5
// 重新加载
[root@localhost docker]# systemctl daemon-reload
[root@localhost docker]# systemctl restart docker
//停止nginx
[root@localhost ~]# docker stop f2d8023e4fe5
f2d8023e4fe5
//重启nginx
[root@localhost ~]# docker restart f2d8023e4fe5
f2d8023e4fe5
//杀掉nginx进程
[root@localhost ~]# docker kill f2d8023e4fe5
f2d8023e4fe5
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
//删除本地镜像 正在运行的容器无法被删除 ,强制删除必须加-f
[root@localhost ~]# docker rm f2d8023e4fe5
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
-a :显示所有的容器,包括未运行的。
-f :根据条件过滤显示的内容。
--format :指定返回值的模板文件。
-l :显示最近创建的容器。
-n :列出最近创建的n个容器。
--no-trunc :不截断输出。
-q :静默模式,只显示容器编号。
-s :显示总的文件大小。
//httpd运行在前台
[root@localhost ~]# docker run httpd
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[Mon Jan 04 11:26:02.379526 2021] [mpm_event:notice] [pid 1:tid 140132935611520] AH00489: Apache/2.4.46 (Unix) configured -- resuming normal operations
[Mon Jan 04 11:26:02.379699 2021] [core:notice] [pid 1:tid 140132935611520] AH00094: Command line: 'httpd -D FOREGROUND'
ubuntu下载进入容器
//下载ubuntu系统进行并进入命令行
[root@localhost ~]# docker run -it ubuntu /bin/bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
da7391352a9b: Pull complete
14428a6d4bcd: Pull complete
2c2d948710f2: Pull complete
Digest: sha256:c95a8e48bf88e9849f3e0f723d9f49fa12c5a00cfc6e60d2bc99d87555295e4c
Status: Downloaded newer image for ubuntu:latest
root@d3d04e8871ad:/# ls
bin dev home lib32 libx32 mnt proc run srv tmp var
boot etc lib lib64 media opt root sbin sys usr
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ae2feff98a0c 2 weeks ago 133MB
httpd latest dd85cdbb9987 3 weeks ago 138MB
ubuntu latest f643c72bc252 5 weeks ago 72.9MB