docker
基本操作
registry-mirrors
windows在setting的Daemon修改为https://72idtxd8.mirror.aliyuncs.com
查看运行状态及版本
1 | docker info |
修改镜像默认位置
镜像
下载镜像
1 | docker pull [image-name] |
查看系统镜像
1 | docker images |
检查镜像
1 | docker inspect [image-name] |
创建镜像
1 | docker build [OPTIONS] PATH | URL | - |
e.g:
1 | docker build -t <YOUR_USERNAME>/myfirstapp . |
When you run the docker build
command given below, make sure to replace <YOUR_USERNAME>
with your username. This username should be the same one you created when registering on Docker Cloud.
The location of the directory containing the Dockerfile
- the .
indicates the current directory:
- —tag, -t:** 镜像的名字及标签,通常 name:tag 或者 name 格式;可以在一次构建中为一个镜像设置多个标签。
上传镜像
先登录
1 | docker login |
再上传
1 | docker push YOUR_USERNAME/myfirstapp |
删除镜像
1 | docker rmi [IMAGE-ID] |
容器
运行一个容器
1 | docker run [image-name] |
运行一个含shell终端的容器,用cmd(git不行)
1 | docker run -it ubuntu /bin/bash |
-i: 以交互模式运行容器,通常与 -t 同时使用;
-t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;
-P: 随机端口映射,容器内部端口随机映射到主机的高端口
-p: 指定端口映射,格式为:主机(宿主)端口:容器端口
-e username=”ritchie”: 设置环境变量;
—name=”nginx-lb”: 为容器指定一个名称;
后台运行容器
1 | docker run -d [image-name] |
- -d:后台运行容器,并返回容器ID;
输入exit以退出
查看容器
1 | docker ps [-a] |
- -a:可以让你看到曾经运行的容器
启动容器
1 | docker start [CONTAINER ID] |
检查容器
1 | docker inspect [CONTAINER ID] |
重启容器
1 | docker start [CONTAINER ID] |
停止容器
1 | docker stop [CONTAINER ID] |
- CONTAINER ID使用docker ps可以查看
打印容器内容
1 | docker logs --tail [latest-row-number] [CONTAINER ID] |
进入到正在运行容器中
1 | docker exec -it [CONTAINER ID] /bin/bash |
删除容器
1 | docker rm [CONTAINER ID] |
- -f :通过SIGKILL信号强制删除一个运行中的容器
- -l :移除容器间的网络连接,而非容器本身
- -v :-v 删除与容器关联的卷
文件拷贝
1 | docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|- |
- -L :保持源目标中的链接
e.g: 将主机/www/runoob目录拷贝到容器96f7f14e99ab的/www目录下。
1 | docker cp /www/runoob 96f7f14e99ab:/www/ |
从容器创建一个新的镜像
1 | docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]] |
- -a :提交的镜像作者;
- -c :使用Dockerfile指令来创建镜像;
- -m :提交时的说明文字;
- -p :在commit时,将容器暂停。
上传到远程仓库
1 | docker push [IMAGE NAME] |
Dockerfile
FROM
starts the Dockerfile. It is a requirement that the Dockerfile must start with theFROM
command. Images are created in layers, which means you can use another image as the base image for your own. TheFROM
command defines your base layer. As arguments, it takes the name of the image. Optionally, you can add the Docker Cloud username of the maintainer and image version, in the formatusername/imagename:version
.RUN
is used to build up the Image you’re creating. For eachRUN
command, Docker will run the command then create a new layer of the image. This way you can roll back your image to previous states easily. The syntax for aRUN
instruction is to place the full text of the shell command after theRUN
(e.g.,RUN mkdir /user/local/foo
). This will automatically run in a/bin/sh
shell. You can define a different shell like this:RUN /bin/bash -c 'mkdir /user/local/foo'
COPY
copies local files into the container.CMD
defines the commands that will run on the Image at start-up. Unlike aRUN
, this does not create a new layer for the Image, but simply runs the command. There can only be oneCMD
per a Dockerfile/Image. If you need to run multiple commands, the best way to do that is to have theCMD
run a script.CMD
requires that you tell it where to run the command, unlikeRUN
. So exampleCMD
commands would be:EXPOSE
creates a hint for users of an image which ports provide services. It is included in the information which can be retrieved via$ docker inspect <container-id>
.PUSH
pushes your image to Docker Cloud, or alternately to a private registry
e.g,see details,or you want more infomation
1 | # our base image |
swarm
创建
1 | docker swarm init |
部署
1 | docker stack deploy --compose-file [.yml] [name] |
查看
1 | docker stack services [name] |
删除
1 | docker stack rm [name] |
docker-machine
这些操作都要用cmd或者powershell才行,windows还是不要折腾了
windows下前置配置Microsoft Hyper-V
配置细节看官网吧,很麻烦
创建machine
1 | docker-machine create -d hyperv --hyperv-virtual-switch <NameOfVirtualSwitch> <nameOfNode> |
e.g:
1 | PS C:\WINDOWS\system32> docker-machine create -d hyperv --hyperv-virtual-switch "Primary Virtual Switch" default |
启动
1 | docker-machine start [machine-name] |
1 | docker-machine env |
查看
1 | docker-machine ls |
删除
1 | docker-machine rm [machine-name] |