本文章假设你已经安装了docker服务,如果你已经安装了docker服务,可以通过下面的命令进行验证。
# Check that you have a working install
$ docker info
如果获得该提示 1.docker: command not found
或者2./var/lib/docker/repositories: permission denied
1.你可能没有安装docker服务
2.你可能权限不够,不能执行该命令,你可以sudo命令前缀,也可以建立一个用户组,该组用户拥有执行docker命令的权限,省去每次敲sudo命令
下载一个docker镜像
# Download an ubuntu image
$ docker pull ubuntu
该命令会从从DockerHub中下载一个 ubuntu
镜像
运行一个交互式的shell
我们从镜像中创建容器,并在容器下运行一个交互式的shell
$ docker run -i -t ubuntu /bin/bash
-i
:interactive 的缩写,表示启动一个交互式的容器.
-t
:Teletype的缩写,创建一个虚拟的交互式输入输出接口,通俗就是 stdin
与 stdout
.
如果希望跳出tty,回到原来的shell中,可以按 Ctrl-p
+ Ctrl-q
.
绑定Dockers一个 host/port 或者Unix socket
通过 -H
参数,我们能运行一个Docker后台服务监听IP地址和端口,默认情况下,Docke监听unix:///var/run/docker.sock
,仅仅允许root用户. 不过你可以设置 0.0.0.0:2375
或者一个网段去赋予其他用户访问docker的权限,
同样的,Docker客户端也可以使用 -H
去连接unix:///var/run/docker.sock
-H
tcp://[host]:[port][path] or unix://path
Docker运行为一个后台线程:
$ sudo <path to>/docker daemon -H 0.0.0.0:5555 &
下载Docker镜像:
$ docker -H :5555 pull ubuntu
运行Docker为一个Work
# Start a very useful long-running process
$ JOB=$(docker run -d ubuntu /bin/sh -c "while true; do echo Hello world; sleep 1; done")
# Collect the output of the job so far
$ docker logs $JOB
# Kill the job
$ docker kill $JOB
列出运行的容器
$ docker ps # Lists only running containers
$ docker ps -a # Lists all containers
管理容器的命令
# Start a new container
$ JOB=$(docker run -d ubuntu /bin/sh -c "while true; do echo Hello world; sleep 1; done")
# Stop the container
$ docker stop $JOB
# Start the container
$ docker start $JOB
# Restart the container
$ docker restart $JOB
# SIGKILL a container
$ docker kill $JOB
# Remove a container
$ docker stop $JOB # Container must be stopped to remove it
$ docker rm $JOB
绑定一个端口
# Bind port 4444 of this container, and tell netcat to listen on it
$ JOB=$(docker run -d -p 4444 ubuntu:12.10 /bin/nc -l 4444)
# Which public port is NATed to my container?
$ PORT=$(docker port $JOB 4444 | awk -F: '{ print $2 }')
# Connect to the public port
$ echo hello world | nc 127.0.0.1 $PORT
# Verify that the network connection worked
$ echo "Daemon received: $(docker logs $JOB)"
保存你的设置,实现复用
S.
# Commit your container to a new named image
$ docker commit <container> <some_name>
# List your images
$ docker images
下一步
- Docker用户手册 Docker User Guide
- 更多关于Docker Share Images via Repositories
- 回顾一下命令行 Command Line