2Docker 基础安装和基础使用
Posted winstom
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2Docker 基础安装和基础使用相关的知识,希望对你有一定的参考价值。
基础环境
本次环境使用Centos 7.x
版本系统,最小化安装,系统基础优化配置请查看 Centos 7.x 系统基础优化
安装
使用命令:
yum install docker-io -y
[[email protected] ~]# yum install docker-io -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* elrepo: mirrors.tuna.tsinghua.edu.cn
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package docker.x86_64 2:1.13.1-68.gitdded712.el7.centos will be installed
--> Processing Dependency: docker-common = 2:1.13.1-68.gitdded712.el7.centos for package: 2:docker-1.13.1-6
8.gitdded712.el7.centos.x86_6
..........................................................................
..........................................................................
Installed:
docker.x86_64 2:1.13.1-68.gitdded712.el7.centos
Dependency Installed:
audit-libs-python.x86_64 0:2.8.1-3.el7
checkpolicy.x86_64 0:2.5-6.el7
container-selinux.noarch 2:2.66-1.el7
container-storage-setup.noarch 0:0.10.0-1.gitdf0dcd5.el7
docker-client.x86_64 2:1.13.1-68.gitdded712.el7.centos
docker-common.x86_64 2:1.13.1-68.gitdded712.el7.centos
libcgroup.x86_64 0:0.41-15.el7
libsemanage-python.x86_64 0:2.5-11.el7
oci-register-machine.x86_64 1:0-6.git2b44233.el7
oci-systemd-hook.x86_64 1:0.1.16-1.git05bd9a0.el7
oci-umount.x86_64 2:2.3.3-3.gite3c9055.el7
policycoreutils-python.x86_64 0:2.5-22.el7
python-IPy.noarch 0:0.75-6.el7
setools-libs.x86_64 0:3.3.8-2.el7
skopeo-containers.x86_64 1:0.1.31-1.dev.gitae64ff7.el7.centos
yajl.x86_64 0:2.0.4-4.el7
Complete!
启动关闭docker
启动docker:systemctl start docker
关闭docker:systemctl stop docker
设置开机自启:systemctl enable docker
[[email protected] ~]# systemctl start docker
[[email protected] ~]# systemctl enable docker
[[email protected] ~]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
Active: active (running) since Tue 2018-08-21 12:13:47 CST; 3s ago
Docs: http://docs.docker.com
Main PID: 1375 (dockerd-current)
CGroup: /system.slice/docker.service
├─1375 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-cur...
└─1383 /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/docker-cont...
Aug 21 12:13:46 node dockerd-current[1375]: time="2018-08-21T12:13:46.884994505+08:00" level=warning...tem"
Aug 21 12:13:46 node dockerd-current[1375]: time="2018-08-21T12:13:46.958221978+08:00" level=info ms...nds"
Aug 21 12:13:46 node dockerd-current[1375]: time="2018-08-21T12:13:46.959354596+08:00" level=info ms...rt."
Aug 21 12:13:47 node dockerd-current[1375]: time="2018-08-21T12:13:47.017988947+08:00" level=info ms...rue"
Aug 21 12:13:47 node dockerd-current[1375]: time="2018-08-21T12:13:47.196687154+08:00" level=info ms...ess"
Aug 21 12:13:47 node dockerd-current[1375]: time="2018-08-21T12:13:47.343822834+08:00" level=info ms...ne."
Aug 21 12:13:47 node dockerd-current[1375]: time="2018-08-21T12:13:47.712563117+08:00" level=info ms...ion"
Aug 21 12:13:47 node dockerd-current[1375]: time="2018-08-21T12:13:47.712714528+08:00" level=info ms...13.1
Aug 21 12:13:47 node dockerd-current[1375]: time="2018-08-21T12:13:47.720577957+08:00" level=info ms...ock"
Aug 21 12:13:47 node systemd[1]: Started Docker Application Container Engine.
Hint: Some lines were ellipsized, use -l to show in full.
这样,docker就安装完成了。
常规使用
docker构建了一个类似于github一样的仓库
搜索镜像
搜索centos
镜像:docker search Docker-name
[[email protected] ~]# docker search centos
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/centos The official build of CentOS. 4585 [OK]
docker.io docker.io/ansible/centos7-ansible Ansible on Centos7 115 [OK]
docker.io docker.io/jdeathe/centos-ssh CentOS-6 6.10 x86_64 / CentOS-7 7.5.1804 x... 99 [OK]
docker.io docker.io/consol/centos-xfce-vnc Centos container with "headless" VNC sessi... 61 [OK]
docker.io docker.io/imagine10255/centos6-lnmp-php56 centos6-lnmp-php56 44 [OK]
docker.io docker.io/tutum/centos Simple CentOS docker image with SSH access 43
docker.io docker.io/centos/mysql-57-centos7 MySQL 5.7 SQL database server 38
docker.io docker.io/gluster/gluster-centos Official GlusterFS Image [ CentOS-7 + Glu... 32 [OK]
docker.io docker.io/openshift/base-centos7 A Centos7 derived base image for Source-To... 31
docker.io docker.io/centos/python-35-centos7 Platform for building and running Python 3... 28
docker.io docker.io/centos/postgresql-96-centos7 PostgreSQL is an advanced Object-Relationa... 26
docker.io docker.io/kinogmt/centos-ssh CentOS with SSH 22 [OK]
docker.io docker.io/centos/php-56-centos7 Platform for building and running PHP 5.6 ... 14
docker.io docker.io/openshift/jenkins-2-centos7 A Centos7 based Jenkins v2.x image for use... 14
docker.io docker.io/pivotaldata/centos-gpdb-dev CentOS image for GPDB development. Tag nam... 7
docker.io docker.io/openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 ima... 6
docker.io docker.io/openshift/jenkins-1-centos7 DEPRECATED: A Centos7 based Jenkins v1.x i... 4
docker.io docker.io/openshift/wildfly-101-centos7 A Centos7 based WildFly v10.1 image for us... 4
docker.io docker.io/darksheer/centos Base Centos Image -- Updated hourly 3 [OK]
docker.io docker.io/pivotaldata/centos Base centos, freshened up a little with a ... 2
docker.io docker.io/pivotaldata/centos-mingw Using the mingw toolchain to cross-compile... 2
docker.io docker.io/blacklabelops/centos CentOS Base Image! Built and Updates Daily! 1 [OK]
docker.io docker.io/jameseckersall/sonarr-centos Sonarr on CentOS 7 0 [OK]
docker.io docker.io/pivotaldata/centos-gcc-toolchain CentOS with a toolchain, but unaffiliated ... 0
docker.io docker.io/smartentry/centos centos with smartentry 0 [OK]
说明:
列名 | 说明 |
---|---|
INDEX | 索引 |
NAME | 镜像名称 |
DESCRIPTION | 描述 |
STARS | 星级,受欢迎的程度 |
OFFICIAL | 是否是官方的 |
AUTOMATED | 是否是自动构建的 |
需要注意的:NAME,官方的,就是Centos,而剩下的,是用户名/名称
获取镜像
命令:docker pull Docker-name
[[email protected] ~]# docker pull centos
Using default tag: latest
Trying to pull repository docker.io/library/centos ...
latest: Pulling from docker.io/library/centos
256b176beaff: Pull complete
Digest: sha256:6f6d986d425aeabdc3a02cb61c02abb2e78e57357e92417d6d58332856024faf
Status: Downloaded newer image for docker.io/centos:lates
查看镜像
命令:docker images
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest 5182e96772bf 2 weeks ago 200 MB
字段名 | 解释 |
---|---|
REPOSITORY | 镜像来自于那个仓库 |
TAG | 镜像的标签 |
IMAGE ID | 镜像的ID,每个镜像都有一个唯一的ID |
CREATED | 创建的时间 |
VIRTUAL SIZE | 镜像的大小 |
此时,镜像也pull
下来了,准备工作都做好了,下面就开始让docker做一些简单的事情了。
让docker输出点东西出来
命令:docker run centos /bin/echo ‘Hello World.‘
意思是:运行
centos
容器,并/bin/echo 输出‘Hello World.‘
[[email protected] ~]# docker run centos /bin/echo ‘Hello World.‘
Hello World.
查看运行或者停止的所有容器
命令:docker ps -a
[[email protected] ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
99ceccfc37ac centos "/bin/echo ‘Hello ..." 2 minutes ago Exited (0) 2 minutes ago elegant_gates
字段名 | 解释 |
---|---|
CONTAINER ID | 容器 ID |
IMAGE | 镜像 |
COMMAND | 运行的内容 |
CREATED | 创建时间 |
STSTUA | 容器状态 |
PORTS | 其他 |
NAMES | 容器名,如果不手动指定,则在名字库中随机获取一个 |
docker容器也能当做正常的操作系统来使用,虽然不建议
命令:docker run --name 随意指定一个docker名称 -it 镜像名 /bin/bash
-i 表示进入到容器的输入终端
-t 表示开启一个伪终端tty
绑定到表输入上
[[email protected] ~]# docker run --name mydocker -it centos /bin/bash
# 这里看到运行后,提示符都变了,这个提示符是该容器的ID值
[[email protected] /]# ls
anaconda-post.log bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
# 执行ps aux 只可以看到两个进程
[[email protected] /]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 11832 3036 ? Ss 05:52 0:00 /bin/bash
root 15 0.0 0.0 51720 3512 ? R+ 05:53 0:00 ps aux
# 可以创建一个文件或目录
[[email protected] /]# cd /tmp/
[[email protected] tmp]# mkdir mydocker
[[email protected] tmp]# ls -ld mydocker/
drwxr-xr-x 2 root root 6 Aug 22 05:57 mydocker/
# 查看该容器的ip地址
[[email protected] tmp]# yum install net-tools
[[email protected] tmp]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.2 netmask 255.255.0.0 broadcast 0.0.0.0
inet6 fe80::42:acff:fe11:2 prefixlen 64 scopeid 0x20<link>
ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
RX packets 3748 bytes 12494964 (11.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2696 bytes 149466 (145.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
# 使用exit退出容器
[[email protected] tmp]# exit
exit
# 退出后,可以看到命令提示符也变回原来的了。
[[email protected] ~]#
在执行上述命令的过程都干啥了?
- 会检查你当前的镜像中有没有centos镜像,如果有则启动,如果没有,会自动帮你pull一个下来
- 然后会给容器分配一个文件系统,放在镜像的上面一层,让它可写
- 然后给容器分配一个IP地址
- docker会运行一个docker指定的应用程序,如果应用程序退出了,则该daocker容器就会退出。
在容器中可以正常操作,只是有很多命令都没有
退出后,再次查看一下容器状态
[[email protected] ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7c9a7f01acb4 centos "/bin/bash" 10 minutes ago Exited (127) 44 seconds ago mydocker
99ceccfc37ac centos "/bin/echo ‘Hello ..." 35 minutes ago Exited (0) 35 minutes ago elegant_gates
可以看到容器名是我们指定的,并且已经退出了,生命周期已经结束了。
启动一个停止的容器
命令:docker start 容器ID
# 通过docker ps -a 查看容器ID
[[email protected] ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7c9a7f01acb4 centos "/bin/bash" 10 minutes ago Exited (127) 44 seconds ago mydocker
99ceccfc37ac centos "/bin/echo ‘Hello ..." 35 minutes ago Exited (0) 35 minutes ago elegant_gates
# 运行
[[email protected] ~]# docker start 99ceccfc37ac
99ceccfc37ac
#再次查看
[[email protected] ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7c9a7f01acb4 centos "/bin/bash" 13 minutes ago Exited (127) 3 minutes ago mydocker
99ceccfc37ac centos "/bin/echo ‘Hello ..." 38 minutes ago Exited (0) 2 seconds ago elegant_gates
这里看到运行后查看也退出了,因为我们运行的容器只是输出了一个‘Hello World.‘程序就退出了,容器也就结束了。
停止一个容器
命令:docker stop 容器ID
这里就不演示了。
删除一个容器
命令:docker rm 容器ID
正常只能删除已经停止的容器,如果是启动状态,会报错,除非增加-f
参数强制删除。
[[email protected] ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7c9a7f01acb4 centos "/bin/bash" 13 minutes ago Exited (127) 3 minutes ago mydocker
99ceccfc37ac centos "/bin/echo ‘Hello ..." 38 minutes ago Exited (0) 2 seconds ago elegant_gates
[[email protected] ~]# docker rm 7c9a7f01acb4
7c9a7f01acb4
# 这里可以看到已经被删除了。
[[email protected] ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
99ceccfc37ac centos "/bin/echo ‘Hello ..." 45 minutes ago Exited (0) 7 minutes ago elegant_gates
以上是关于2Docker 基础安装和基础使用的主要内容,如果未能解决你的问题,请参考以下文章
[vscode]--HTML代码片段(基础版,reactvuejquery)