docker基本操作
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker基本操作相关的知识,希望对你有一定的参考价值。
docker基本操作
一、简介
Docker 和传统虚拟化方式的不同之处。传统虚拟机技术是虚拟出一套硬件后,在其上运行一个完整操作系统,在该系统上再运行所需应用进程;而容器内的应用进程直接运行于宿主的内核,容器内没有自己的内核,而且也没有进行硬件虚拟。因此容器要比传统虚拟机更为轻便
镜像(Image)和容器(Container)的关系,就像是面向对象程序设计中的类和实例一样,镜像是静态的定义,容器是镜像运行时的实体。容器可以被创建、启动、停止、删除、暂停等
二、安装须知
操作系统: Mac OS Yosemite 10.10.5
Mac地址:https://download.docker.com/mac/stable/Docker.dmg
Docker.dmg:安装提供Docker Engine,Docker CLI客户端,Docker Compose和Docker Machine
三、安装
1)运行Docker.dmg安装
2)配置个人加速镜像(我使用阿里云https://cr.console.aliyun.com/#/accelerator)
3) 运行docker version
和docker info
检查
? docker version
Client:
Version: 17.03.0-ce
API version: 1.26
Go version: go1.7.5
Git commit: 60ccb22
Built: Thu Feb 23 10:40:59 2017
OS/Arch: darwin/amd64
Server:
Version: 17.03.0-ce
API version: 1.26 (minimum version 1.12)
Go version: go1.7.5
Git commit: 3a232c8
Built: Tue Feb 28 07:52:04 2017
OS/Arch: linux/amd64
Experimental: true
? docker info
Containers: 1
Running: 1
Paused: 0
Stopped: 0
Images: 1
Server Version: 17.03.0-ce
Storage Driver: overlay2
Backing Filesystem: extfs
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
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 977c511eda0925a723debdc94d09459af49d082a
runc version: a01dafd48bc1c7cc12bdb01206f9fea7dd6feb70
init version: 949e6fa
Security Options:
seccomp
Profile: default
Kernel Version: 4.9.12-moby
Operating System: Alpine Linux v3.5
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 1.952 GiB
Name: moby
ID: V5PN:CAOJ:YYKT:B7IK:PTP3:OQKS:Q26Q:UJYZ:OG2K:HSPK:WRME:KJP5
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): true
File Descriptors: 23
Goroutines: 32
System Time: 2017-03-27T03:33:06.945792584Z
EventsListeners: 1
No Proxy: 127.0.0.1
Registry: https://index.docker.io/v1/
Experimental: true
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://xxxxx.mirror.aliyuncs.com
Live Restore Enabled: false
四、运行nginx
启动运行nginx, 执行docker run -d -p 80:80 --name webserver nginx
? docker run -d -p 80:80 --name webserver nginx
Unable to find image ‘nginx:latest‘ locally
latest: Pulling from library/nginx
693502eb7dfb: Pull complete
6decb850d2bc: Pull complete
c3e19f087ed6: Pull complete
Digest: sha256:52a189e49c0c797cfc5cbfe578c68c225d160fb13a42954144b29af3fe4fe335
Status: Downloaded newer image for nginx:latest
4b11e6ed897d6df195936f60916ffda704d55f75645270f338a10b68480112e7
启动bash命令交互操作客户端
? docker exec -it webserver bash
root@2964bd62006a:/#
停止niginx, 运行docker stop webserver
? docker stop webserver
webserver
删除nginx, 运行docker rm webserver
? docker rm webserver
webserver
五、运行ubuntu
镜像官方仓库:https://hub.docker.com/explore/
下载ubuntu
? docker pull ubuntu:14.04
14.04: Pulling from library/ubuntu
30d541b48fc0: Pull complete
8ecd7f80d390: Pull complete
46ec9927bb81: Pull complete
2e67a4d67b44: Pull complete
7d9dd9155488: Pull complete
Digest: sha256:62a5dce5ceccd7f1cb2672a571ebee52cad1f08eec9b57fe4965fb0968a9602e
Status: Downloaded newer image for ubuntu:14.04
启动运行ubuntu
-it:这是两个参数,一个是 -i:交互式操作,一个是 -t 终端。我们这里打算进入 bash 执行一些命令并查看返回结果,因此我们需要交互式终端。
–rm:这个参数是说容器退出后随之将其删除。默认情况下,为了排障需求,退出的容器并不会立即删除,除非手动 docker rm。我们这里只是随便执行个命令,看看结果,不需要排障和保留结果,因此使用 –rm 可以避免浪费空间。
ubuntu:14.04:这是指用 ubuntu:14.04 镜像为基础来启动容器。
–name myUbuntu:给容器取一个别名叫
myUbuntu
? docker run -it --rm --name myUbuntu ubuntu:14.04
[email protected]2ef73a93ef7b:/#
或者不指定ubuntu版本,直接启动时,直接下载最新版本ubuntu:latest
? docker run -it --name myUbuntu ubuntu:14.04
Unable to find image ‘ubuntu:latest‘ locally
latest: Pulling from library/ubuntu
d54efb8db41d: Pull complete
f8b845f45a87: Pull complete
e8db7bf7c39f: Pull complete
9654c40e9079: Pull complete
6d9ef359eaaa: Pull complete
Digest: sha256:dd7808d8792c9841d0b460122f1acf0a2dd1f56404f8d1e56298048885e45535
Status: Downloaded newer image for ubuntu:latest
[email protected]366ea11633cf:/#
停止ubuntu容器
? docker stop myUbuntu
myUbuntu
删除ubuntu容器
? docker rm myUbuntu
myUbuntu
查看历史镜像
? docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 6b914bbcb89e 3 weeks ago 182 MB
ubuntu 14.04 7c09e61e9035 3 weeks ago 188 MB
删除ubuntu镜像
使用docker rmi <IMAGE ID>
删除,但是必须先要docker rm myUbuntu
才能删除镜像
? docker rmi 7c09e61e9035
Untagged: ubuntu:14.04
Untagged: [email protected]:62a5dce5ceccd7f1cb2672a571ebee52cad1f08eec9b57fe4965fb0968a9602e
Deleted: sha256:7c09e61e90350e8f5c0cba2979003bdfe32c2d027b68b4f0cf9063cdd7b4bafd
Deleted: sha256:304aecb5e13929f85d3ce2e9d83d0212866c8e55a460c94cf24bd75da1c7c153
Deleted: sha256:f302be18d46a45c0edbbd9b4bc02db764a4b0b8cd9bd0490f33dfaff039a3b62
Deleted: sha256:c523f3173f6028e5329fd401331c375f7b9b9e831d915fafaf358f55e36e3747
Deleted: sha256:94e631422130dc414878fd05efe3d59de44c9d8904696a7c299a83f378a92845
Deleted: sha256:c29b5eadf94a90a2abda13e765d4fad4825fd15621dea1d9a98b60b89b835c2a
查看docker进程
? docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
366ea11633cf ubuntu "/bin/bash" About a minute ago Exited (0) 59 seconds ago myUbuntu
六、制作自定义nginx
1) 启动nginx
? docker run -d -p 80:80 --name webserver nginx
Unable to find image ‘nginx:latest‘ locally
latest: Pulling from library/nginx
693502eb7dfb: Pull complete
6decb850d2bc: Pull complete
c3e19f087ed6: Pull complete
Digest: sha256:52a189e49c0c797cfc5cbfe578c68c225d160fb13a42954144b29af3fe4fe335
Status: Downloaded newer image for nginx:latest
2964bd62006acd43611ea045e8152f651905e213c392e008a59eebe667080b3d
2) 启动bash控制台并修改index.html
内容
? docker exec -it webserver bash
root@2964bd62006a:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@2964bd62006a:/# echo ‘<h1>Hello, Docker!</h1>‘ > /usr/share/nginx/html/index.html
root@2964bd62006a:/# exit
exit
3)查看容器修改内容
? docker diff webserver
C /root
A /root/.bash_history
C /run
A /run/nginx.pid
C /usr/share/nginx/html/index.html
C /var/cache/nginx
A /var/cache/nginx/client_temp
A /var/cache/nginx/fastcgi_temp
A /var/cache/nginx/proxy_temp
A /var/cache/nginx/scgi_temp
A /var/cache/nginx/uwsgi_temp
4)制作v2版本tag的nginx
? ~ docker commit --author "zhengyong" --message "update index.htm" webserver nigix:v2
sha256:b86a71250cf4e41a34af24b7e2924e3909ecdf74262195d6ac30fd57bbdbaf35
5) 启动v2版本nginx
? docker run -d -p 80:80 --name ng nigix:v2
72f15356de38c26456cdee8a68651a2de25e59d8f8e4ce31e82c434927ff4b4f
原文地址:https://blog.csdn.net/zhengyong15984285623/article/details/66971949
以上是关于docker基本操作的主要内容,如果未能解决你的问题,请参考以下文章