docker的基本命令及操作逻辑

image:镜像

container:容器

registry:仓库

docker hub ----> 公共的仓库

查找php的镜像

[[email protected] /]# docker search php

docker的基本命令及操作逻辑

查看docker详细信息

[[email protected] /]# docker info

docker的基本命令及操作逻辑

或者看看加速器

docker的基本命令及操作逻辑

查找httpd的镜像

[[email protected] /]# docker search httpd

docker的基本命令及操作逻辑

下载或拉取httpd的镜像

[[email protected] /]# docker pull httpd

docker的基本命令及操作逻辑

导出镜像,并保存镜像为tar包

保存的名称为my-httpd.tar

[[email protected] /]# docker save --output my-httpd.tar httpd

docker的基本命令及操作逻辑

查看镜像

[[email protected] /]# docker images

或者

[[email protected] /]# docker image ls

docker的基本命令及操作逻辑

latest:最新的版本,但不是绝对最新

完整的镜像名称----》》 image:latest

如果版本号默认是一latest为结尾,解压省略不写

删除镜像

[[email protected] /]# docker rmi httpd:latest

[[email protected] /]# docker images

docker的基本命令及操作逻辑

加载一个镜像

1)上传包

docker的基本命令及操作逻辑

导入镜像包

[[email protected] /]# docker load --input my-httpd.tar

[[email protected] /]# docker images

docker的基本命令及操作逻辑

运行镜像并后台运行

[[email protected] /]# docker run -itd --name test centos

查看镜像

[[email protected] /]# docker ps

进入容器(必须加/bin/bash)

[[email protected] /]# docker exec -it test /bin/bash

另一个命令进入容器,(可以不加/bin/bash但是退出之后。容器会停止运行)

[[email protected] /]# docker attach test

注意:exec进入容器会开启新的进程,attach不会开启新的进程

Ctrl +p Ctrl+q:如果是attach进入的容器Ctrl +p Ctrl+q退出容器,并保持容器运行

查看当前进程号

[[email protected] /]# echo $$

18260

使用exec开启容器

[[email protected] /]# docker start test

test

[[email protected] /]# docker exec -it test /bin/bash

[[email protected] /]# echo $$

使用attach开启容器

[[email protected] /]# docker attach test

[[email protected] /]# echo $$

1

使用快捷键退出容器并保持容器运行,Ctrl +p Ctrl+q

docker的基本命令及操作逻辑

关闭容器

[[email protected] /]# docker stop test

docker的基本命令及操作逻辑

删除所有容器,(跟 rm  -rf /* 一样)

[[email protected] /]# docker ps -a -q | xargs docker rm -f

关闭所有容器和开启

[[email protected] /]# docker run  -itd centos

567d5cd9623bb49b5b86eb07aa50260b6366dcfa6392a50e0e5d255ea1a3dec7

[[email protected] /]# docker ps -a -q | xargs docker stop

567d5cd9623b

b0981b364a78

[[email protected] /]# docker ps -a -q | xargs docker start

567d5cd9623b

b0981b364a78

查看容器的日志,(-f:动态的显示)

[[email protected] /]# docker run  -itd -p81:80 --name my-httpd httpd

[[email protected] /]# curl 127.0.0.1:81

[email protected] /]# docker logs my-httpd    

docker的基本命令及操作逻辑

保持容器处于开启状态,(docker 服务重启之后)

[[email protected] /]# docker run -itd --name zhou --restart=always httpd

基于centos镜像部署安装一个nginx web服务器

运行一个容器

[[email protected] /]# docker run -itd --name web centos:latest

上传nginx

docker的基本命令及操作逻辑

将nginx传给容器

[[email protected] /]# docker cp nginx-1.17.3.tar.gz web:/

安装依赖包

[[email protected] /]# yum -y install gcc openssl-devel pcre-devel zlib-devel make

安装nginx

[[email protected] /]# useradd -M -s /sbin/nologin nginx

[[email protected] /]# tar zxf nginx-1.17.3.tar.gz

[[email protected] /]# cd nginx-1.17.3/

[[email protected] nginx-1.17.3]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx

[[email protected] nginx-1.17.3]# make && make install

[[email protected] /]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/

[[email protected] /]# nginx

访问一下

docker的基本命令及操作逻辑

在宿主机上访问容器的80端口

docker的基本命令及操作逻辑

在宿主机上访问容器的80端口

docker的基本命令及操作逻辑

更改一下网页

[[email protected] /]# cd /usr/local/nginx/html

[[email protected] html]# echo this is http > index.html

[[email protected] html]# curl 172.17.0.2

this is http

将容器制作成镜像

语法: commit  容器名称   生成镜像的名称(如果不加:v1.0则会默认生成latest)

[[email protected] /]# docker commit web my-nginx:v1.0

docker的基本命令及操作逻辑

查看镜像

[[email protected] /]# docker images

docker的基本命令及操作逻辑

基于刚刚的镜像生成容器

[[email protected] /]# docker run -itd --name web01 my-nginx:v1.0

[[email protected] /]# docker exec -it web01 /bin/bash

[[email protected] /]# nginx

docker的基本命令及操作逻辑

追加网页

[[email protected] /]# echo 123 >> /usr/local/nginx/html/index.html

[[email protected] /]# curl 172.17.0.3

this is http

123

基于刚刚更改的网页生成镜像

[[email protected] /]# docker commit web01 my-nginx:2.0

docker的基本命令及操作逻辑

注意:就算名称一样,后面的版本号不一样也可以同时存在

docker操作逻辑图

docker的基本命令及操作逻辑

相关文章: