Docker常用命令 -- 学习笔记
本帖最后由 java 于 2017-5-24 16:49 编辑//////////////////安装//////////////////
ubuntu 安装:
uname -r 查看你当前的内核版本要高于 3.10
使用脚本安装 Docker
1、获取最新版本的 Docker 安装包
runoob@runoob:~$ wget -qO- https://get.docker.com/ | sh
安装完成后有个提示:
If you would like to use Docker as a non-root user, you should now consider
adding your user to the "docker" group with something like:
sudo usermod -aG docker runoob
Remember that you will have to log out and back in for this to take effect!
当要以非root用户可以直接运行docker时,需要执行 sudo usermod -aG docker runoob 命令,然后重新登陆
2、启动docker 后台服务
runoob@runoob:~$ sudo service docker start
查看后台运行的进程
ps -ef | grep docker
3、测试运行hello-world
runoob@runoob:~$ docker run hello-world
/////////////////////
docker --help
-ubuntu:~$ docker --version
Docker version 17.05.0-ce, build 89658be
-ubuntu:~$ docker version
Client:
Version: 17.05.0-ce
API version:1.29
Go version: go1.7.5
Git commit: 89658be
Built: Thu May4 22:10:54 2017
OS/Arch: linux/amd64
Server:
Version: 17.05.0-ce
API version:1.29 (minimum version 1.12)
Go version: go1.7.5
Git commit: 89658be
Built: Thu May4 22:10:54 2017
OS/Arch: linux/amd64
Experimental: false
////////////////////////////////////////////////////
Share images, automate workflows, and more with a free Docker ID:
https://cloud.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/
本帖最后由 java 于 2017-5-24 17:03 编辑
拉取镜像 创建容器 https://github.com/docker/labs/b ... /chapters/alpine.md
运行第一个容器 Alpine Linux container (a lightweight linux distribution)
-ubuntu:~$ docker pull alpine
Using default tag: latest
latest: Pulling from library/alpine
cfc728c1c558: Pull complete
Digest: sha256:c0537ff6a5218ef531ece93d4984efc99bbf3f7497c0a7726c88e2bb7584dc96
Status: Downloaded newer image for alpine:latest
The pull command fetches the alpine image from the Docker registry and saves it in our system. You can use the docker images command to see a list of all images on your system.
-ubuntu:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest 02674b9cb179 13 days ago 3.99MB
hello-world latest 48b5124b2768 4 months ago 1.84kB
1.Docker Run
docker run alpine ls -l
docker run alpine echo "hello from alpine"
运行交互式的容器
docker run -it alpine /bin/sh
-t:在新容器内指定一个伪终端或终端。
-i:允许你对容器内的标准输入 (STDIN) 进行交互。
我们可以通过运行exit命令或者使用CTRL+D来退出容器。
查看在运行的容器
docker ps
查看运行过的容器
docker ps -a
启动容器(后台模式)
docker run -d alpine /bin/sh -c "while true; do echo hello world; sleep 1; done"
cbeff8c9c30af13cc7cedd0df17d83329ebba9b76b3ad9580292fc198c5cf042
在输出中,我们没有看到期望的"hello world",而是一串长字符
2b1b7a428627c51ab8810d541d759f072b4fc75487eed05812646b8534a2fe63
这个长字符串叫做容器ID,对每个容器来说都是唯一的,我们可以通过容器ID来查看对应的容器发生了什么。
首先,我们需要确认容器有在运行,可以通过 docker ps 来查看
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cbeff8c9c30a alpine "/bin/sh -c 'while..." 58 seconds ago Up 57 seconds happy_poincare
CONTAINER ID:容器ID
NAMES:自动分配的容器名称
在容器内使用docker logs命令,查看容器内的标准输出
docker logs cbeff8c9c30a
或
docker logs happy_poincare
停止容器
docker stop cbeff8c9c30a
或
docker stop happy_poincare
http://www.runoob.com/docker/docker-command-manual.html
帮助
docker
docker command --help 如: docker stats --help
本帖最后由 java 于 2017-5-24 17:39 编辑
查询 Docker Hub上的镜像https://store.docker.com/或
docker search tomcat
docker search spring
然后拉取安装到本地
本帖最后由 java 于 2018-3-29 16:53 编辑
docker run创建Docker容器时,可以用--net选项指定容器的网络模式,Docker有以下4种网络模式:
host模式,使用--net=host指定。
container模式,使用--net=container:NAME_or_ID指定。
none模式,使用--net=none指定。
bridge模式,使用--net=bridge指定,默认设置。
默认选择bridge。
在Docker服务安装成功后默认有三个网络(docker network ls)
bridge:是容器的默认网络,可以配置后与宿主机通信从而与互联网互联互通。
host和none:是属于无网络(可以使用docker network inspect host查看,其中无子网和网关),容器添加到这两个网络时时将容器添加到特定的容器网络堆栈,是不能与外界网络通信的。
每当使用docker run启动一个容器时,这个容器都会有一个默认的网络,即bridge。
可以通过inspect命令查看各网络中的容器(查看bridge网络):
docker network inspect bridge
linux安装了docker之后,用ifconfig指令看多了一个docker0,怎么关掉?
$ sudo ifconfig docker0 down
$ sudo ifconfig docker_gwbridge down
service network restart
页:
[1]