docker命令操作

查看docker版本

1
2
[root@hadoop01 ~]# docker -v
Docker version 19.03.3, build a872fc2f86

列出本机的所有 image 文件

1
2
3
[root@hadoop01 ~]# docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest fce289e99eb9 9 months ago 1.84kB

运行容器

1
2
3
4
5
[root@hadoop01 ~]# docker container run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.
......

结束容器

1
2
3
[root@hadoop01 ~]# docker container kill [container_id]

注:通过 docker ps 可查看container_id

镜像相关的操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@hadoop01 koa-demos]# docker image --help
Usage: docker image COMMAND
Manage images
Commands:
build Build an image from a Dockerfile
history Show the history of an image
import Import the contents from a tarball to create a filesystem image
inspect Display detailed information on one or more images
load Load an image from a tar archive or STDIN
ls List images
prune Remove unused images
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rm Remove one or more images
save Save one or more images to a tar archive (streamed to STDOUT by default)
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

Run 'docker image COMMAND --help' for more information on a command.

容器相关的操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
[root@hadoop01 ~]# docker container --help
Usage: docker container COMMAND
Manage containers
Commands:
attach Attach local standard input, output, and error streams to a running container
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
exec Run a command in a running container
export Export a container's filesystem as a tar archive
inspect Display detailed information on one or more containers
kill Kill one or more running containers
logs Fetch the logs of a container
ls List containers
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
prune Remove all stopped containers
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
run Run a command in a new container
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
wait Block until one or more containers stop, then print their exit codes

Run 'docker container COMMAND --help' for more information on a command.

示例

以 koa-demos 项目为例

  • 下载源代码

    1
    2
    [root@hadoop01 ~]# git clone https://github.com/ruanyf/koa-demos.git
    [root@hadoop01 ~]# cd koa-demos
  • 编写 Dockerfile 文件

    1
    2
    3
    4
    [root@hadoop01 ~]# mkdir .dockerignore

    添加.git、node_modules、npm-debug.log文件到.dockerignore.
    注:.dockerignore文件里的文件或目录在制作镜像时会默认忽略。
  • 编写Dockerfile文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    [root@hadoop01 ~]# mkdir Dockerfile
    增加以下内容:
    FROM node:8.4
    COPY . /app
    WORKDIR /app
    RUN npm install --registry=https://registry.npm.taobao.org
    EXPOSE 3000

    注:代码解释如下.
    From node:8.4 该image继承自官方的node image,冒号表示标签,这里表示8.4,即8.4版本的node.
    COPY . /app 将当前目录下的所有文件都拷贝到image文件的 /app 目录.
    WORKDIR /app 指定接下来的工作目录为 /app .
    RUN npm install 在/app目录下,运行npm install命令安装依赖。注意,安装后所有的依赖,都将打包进入image文件。
    EXPOSE 3000 将容器的3000端口暴露出来,允许外部连接这个端口.
  • 创建 image 文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    [root@hadoop01 koa-demos]# docker image build -t koa-demos:1.0.0 .

    ......
    Successfully built efb833bfa6dd
    Successfully tagged koa-demos:1.0.0

    注:
    -t 该参数用来指定image文件的名字,后面还可以用冒号指定标签,如果不指定,默认标签就是latest
    . 当前路径,表示Dockerfile文件所在的路径
  • 生成容器

    1
    2
    3
    4
    5
    6
    7
    [root@hadoop01 koa-demos]# docker container run -p 8000:3000 -it koa-demos:1.0.0 bash

    注:
    -p 容器的3000端口映射到本地的8000端口.
    -it 容器的shell会映射到当前本地的shell,你在本机窗口输入的命令会传入到容器中.
    koa-demo:1.0.0 image文件的名字:标签。
    /bin/bash 容器启动以后,容器内部第一个执行的命令.这里是启用Bash,以保证用户可以使用shell
    1
    [root@hadoop01 koa-demos]# docker container run --rm -p 8000:3000 -it koa-demo:1.0.0 /bin/bash

Reference

https://blog.csdn.net/itguangit/article/details/80222387

-------------本文结束感谢您的阅读-------------