1、安装及加速配置

 Docker 安装以及配置阿里云镜像加速器指南_大鱼的技术博客_51CTO博客

2、镜像操作

2.1 搜索镜像

docker search [OPTIONS] TERM

命令详情:

root@localhost:~# docker search --help

Usage:  docker search [OPTIONS] TERM

Search Docker Hub for images

Options:
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print search using a Go template
      --limit int       Max number of search results
      --no-trunc        Don't truncate output
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

案例:

root@localhost:~# docker search --no-trunc --limit 5 -f is-official=true nginx
NAME      DESCRIPTION                                              STARS     OFFICIAL
nginx     Official build of Nginx.                                 19994     [OK]
unit      Official build of NGINX Unit: Universal Web App Server   32        [OK]
root@localhost:~# docker search --no-trunc --limit 5 --filter is-official=true nginx
NAME      DESCRIPTION                                              STARS     OFFICIAL
nginx     Official build of Nginx.                                 19994     [OK]
unit      Official build of NGINX Unit: Universal Web App Server   32        [OK]
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

2.2 拉取镜像

docker pull [OPTIONS] NAME[:TAG|@DIGEST]

命令详情:

root@localhost:~# docker pull --help

Usage:  docker pull [OPTIONS] NAME[:TAG|@DIGEST]

Download an image from a registry

Aliases:
  docker image pull, docker pull

Options:
  -a, --all-tags                Download all tagged images in the repository
      --disable-content-trust   Skip image verification (default true)
      --platform string         Set platform if server is multi-platform capable
  -q, --quiet                   Suppress verbose output
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

案例:

root@localhost:~# docker pull -q nginx
docker.io/library/nginx:latest
root@localhost:~# docker pull -q nginx:1.26.0
docker.io/library/nginx:1.26.0
  • 1.
  • 2.
  • 3.
  • 4.

2.3 查看镜像

docker images [OPTIONS] [REPOSITORY[:TAG]]

常用选项:

root@localhost:~# docker images --help

Usage:  docker images [OPTIONS] [REPOSITORY[:TAG]]

List images

Aliases:
  docker image ls, docker image list, docker images

Options:
  -a, --all             Show all images (default hides intermediate images)
      --digests         Show digests
  -f, --filter filter   Filter output based on conditions provided
      --format string   Format output using a custom template:
                        'table':            Print output in table format with column headers (default)
                        'table TEMPLATE':   Print output in table format using the given Go template
                        'json':             Print in JSON format
                        'TEMPLATE':         Print output using the given Go template.
                        Refer to https://docs.docker.com/go/formatting/ for more information about formatting output with templates
      --no-trunc        Don't truncate output
  -q, --quiet           Only show image IDs
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.

案例:

root@localhost:~# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
nginx        latest    fffffc90d343   2 weeks ago    188MB
mysql        8.4.0     05247af91864   2 weeks ago    578MB
nginx        1.26.0    94543a6c1aef   2 months ago   188MB
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

2.4 删除镜像

docker rmi [OPTIONS] IMAGE [IMAGE…]

命令详情:

root@localhost:~# docker rmi --help

Usage:  docker rmi [OPTIONS] IMAGE [IMAGE...]

Remove one or more images

Aliases:
  docker image rm, docker image remove, docker rmi

Options:
  -f, --force      Force removal of the image
      --no-prune   Do not delete untagged parents
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

案例:

root@localhost:~# docker rmi -f nginx:latest
Untagged: nginx:latest
Untagged: nginx@sha256:67682bda769fae1ccf5183192b8daf37b64cae99c6c3302650f6f8bf5f0f95df
Deleted: sha256:fffffc90d343cbcb01a5032edac86db5998c536cd0a366514121a45c6723765c
Deleted: sha256:9f4b5d44149fd139616539e0ef5311a14338a970f25733ba95b4ae6d3becdf0d
Deleted: sha256:8e6e10fbcca1bb180c5cfc805a37240945a6ba703cb1f985d4d3b8a1c954fc5b
Deleted: sha256:dcec89b921d64a1d2f748c450832a4c5624219bbb1b696e1a606bff78b7afa60
Deleted: sha256:4994a8d5939af297abf594b7ab714dfb72e57217b94bf0a250a62903cbdb6d84
Deleted: sha256:8b2bc37f672b15bea06a204790da9f384ef7b06feccade3fd3b1945b63df5aef
Deleted: sha256:a4197512070a01764db77b424100c1f81f0ed696380b19bc26b6e72fafef0709
Deleted: sha256:32148f9f6c5aadfa167ee7b146b9703c59307049d68b090c19db019fd15c5406
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

3、容器操作

3.1 运行容器

docker run [OPTIONS] IMAGE [COMMAND] [ARG…]

常见选项:

  • -d: 以守护进程模式运行容器,即在后台运行,不会在终端输出任何内容。 3
  • --name: 指定容器名称,方便识别和管理。 3
  • -p: 端口映射,将容器端口映射到宿主机端口。
  • -v: 挂载卷,将宿主机目录挂载到容器中。
  • -e: 设置环境变量。
  • -i: 进入容器后,保持与容器的交互。
  • -t: 分配一个伪终端,在容器中执行交互式命令。
  • --rm: 容器停止后自动删除容器。

案例:

root@localhost:~# docker run -p 8080:80 -d --name nginx nginx
74528d231da92f4c6de8c738addf6a0e8969c0794b0c280d9f7e856e7e3991e9
  • 1.
  • 2.

3.2 查看容器

docker ps [OPTIONS]

命令详情:

root@localhost:~# docker ps --help

Usage:  docker ps [OPTIONS]

List containers

Aliases:
  docker container ls, docker container list, docker container ps, docker ps

Options:
  -a, --all             Show all containers (default shows just running)
  -f, --filter filter   Filter output based on conditions provided
      --format string   Format output using a custom template:
                        'table':            Print output in table format with column headers (default)
                        'table TEMPLATE':   Print output in table format using the given Go template
                        'json':             Print in JSON format
                        'TEMPLATE':         Print output using the given Go template.
                        Refer to https://docs.docker.com/go/formatting/ for more information about
                        formatting output with templates
  -n, --last int        Show n last created containers (includes all states) (default -1)
  -l, --latest          Show the latest created container (includes all states)
      --no-trunc        Don't truncate output
  -q, --quiet           Only display container IDs
  -s, --size            Display total file sizes
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.

案例:

root@localhost:~# docker ps -asn 2
CONTAINER ID   IMAGE         COMMAND                  CREATED         STATUS                      PORTS                               NAMES     SIZE
74528d231da9   nginx         "/docker-entrypoint.…"   4 minutes ago   Up 4 minutes                0.0.0.0:8080->80/tcp                nginx     1.09kB (virtual 188MB)
39def9fcde64   mysql:8.4.0   "docker-entrypoint.s…"   4 days ago      Exited (255) 45 hours ago   0.0.0.0:3306->3306/tcp, 33060/tcp   mysql8    6B (virtual 578MB)
  • 1.
  • 2.
  • 3.
  • 4.

3.3 停止容器

docker stop [OPTIONS] CONTAINER [CONTAINER…]

命令详情:

root@localhost:~# docker stop --help

Usage:  docker stop [OPTIONS] CONTAINER [CONTAINER...]

Stop one or more running containers

Aliases:
  docker container stop, docker stop

Options:
  -s, --signal string   Signal to send to the container
  -t, --time int        Seconds to wait before killing the container
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

案例:

# 停止所有容器
root@localhost:~# docker stop $(docker ps -aq)
74528d231da9
39def9fcde64
  • 1.
  • 2.
  • 3.
  • 4.

3.4 启动容器

docker start [OPTIONS] CONTAINER [CONTAINER…]

命令详情:

root@localhost:~# docker start --help

Usage:  docker start [OPTIONS] CONTAINER [CONTAINER...]

Start one or more stopped containers

Aliases:
  docker container start, docker start

Options:
  -a, --attach               Attach STDOUT/STDERR and forward signals
      --detach-keys string   Override the key sequence for detaching a container
  -i, --interactive          Attach container's STDIN
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

案例:

# 启动所有容器
root@localhost:~# docker start $(docker ps -aq)
74528d231da9
39def9fcde64
root@localhost:~# docker ps -a
CONTAINER ID   IMAGE         COMMAND                  CREATED          STATUS         PORTS                               NAMES
74528d231da9   nginx         "/docker-entrypoint.…"   13 minutes ago   Up 2 seconds   0.0.0.0:8080->80/tcp                nginx
39def9fcde64   mysql:8.4.0   "docker-entrypoint.s…"   4 days ago       Up 2 seconds   0.0.0.0:3306->3306/tcp, 33060/tcp   mysql8
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

3.5 重启容器

docker restart [OPTIONS] CONTAINER [CONTAINER…]

命令详情:

root@localhost:~# docker restart --help

Usage:  docker restart [OPTIONS] CONTAINER [CONTAINER...]

Restart one or more containers

Aliases:
  docker container restart, docker restart

Options:
  -s, --signal string   Signal to send to the container
  -t, --time int        Seconds to wait before killing the container
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

案例:

root@localhost:~# docker restart nginx
nginx
root@localhost:~# docker ps
CONTAINER ID   IMAGE         COMMAND                  CREATED          STATUS         PORTS                               NAMES
74528d231da9   nginx         "/docker-entrypoint.…"   17 minutes ago   Up 2 seconds   0.0.0.0:8080->80/tcp                nginx
39def9fcde64   mysql:8.4.0   "docker-entrypoint.s…"   4 days ago       Up 4 minutes   0.0.0.0:3306->3306/tcp, 33060/tcp   mysql8
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

3.6 容器状态

docker stats [OPTIONS] [CONTAINER…]

命令详情:

root@localhost:~# docker stats --help

Usage:  docker stats [OPTIONS] [CONTAINER...]

Display a live stream of container(s) resource usage statistics

Aliases:
  docker container stats, docker stats

Options:
  -a, --all             Show all containers (default shows just running)
      --format string   Format output using a custom template:
                        'table':            Print output in table format
                        with column headers (default)
                        'table TEMPLATE':   Print output in table format
                        using the given Go template
                        'json':             Print in JSON format
                        'TEMPLATE':         Print output using the given
                        Go template.
                        Refer to https://docs.docker.com/go/formatting/
                        for more information about formatting output with
                        templates
      --no-stream       Disable streaming stats and only pull the first result
      --no-trunc        Do not truncate output
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.

案例:

root@localhost:~# docker stats --no-stream
CONTAINER ID   NAME      CPU %     MEM USAGE / LIMIT     MEM %     NET I/O     BLOCK I/O   PIDS
74528d231da9   nginx     0.00%     12.64MiB / 15.24GiB   0.08%     746B / 0B   0B / 0B     17
39def9fcde64   mysql8    0.37%     472.3MiB / 15.24GiB   3.03%     746B / 0B   0B / 0B     38
root@localhost:~# docker stats --no-stream --no-trunc
CONTAINER ID                                                       NAME      CPU %     MEM USAGE / LIMIT     MEM %     NET I/O     BLOCK I/O   PIDS
74528d231da92f4c6de8c738addf6a0e8969c0794b0c280d9f7e856e7e3991e9   nginx     0.00%     12.64MiB / 15.24GiB   0.08%     746B / 0B   0B / 0B     17
39def9fcde64766f6218ba3710d014196f64d2ae774ceff6229ca12a000b3e15   mysql8    0.37%     472.3MiB / 15.24GiB   3.03%     746B / 0B   0B / 0B     38
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

3.7容器日志

docker logs [OPTIONS] CONTAINER

命令详情:

root@localhost:~# docker logs --help

Usage:  docker logs [OPTIONS] CONTAINER

Fetch the logs of a container

Aliases:
  docker container logs, docker logs

Options:
      --details        Show extra details provided to logs
  -f, --follow         Follow log output
      --since string   Show logs since timestamp (e.g. "2013-01-02T13:23:37Z") or relative (e.g. "42m" for 42 minutes)
  -n, --tail string    Number of lines to show from the end of the logs (default "all")
  -t, --timestamps     Show timestamps
      --until string   Show logs before a timestamp (e.g. "2013-01-02T13:23:37Z") or relative (e.g. "42m" for 42 minutes)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

案例:

root@localhost:~# docker logs  -n 3 nginx
2024/07/07 07:48:30 [notice] 1#1: start worker process 35
2024/07/07 07:48:30 [notice] 1#1: start worker process 36
2024/07/07 07:48:30 [notice] 1#1: start worker process 37
  • 1.
  • 2.
  • 3.
  • 4.

3.8 进入容器

docker exec [OPTIONS] CONTAINER COMMAND [ARG…]

命令详情:

root@localhost:~# docker exec --help

Usage:  docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

Execute a command in a running container

Aliases:
  docker container exec, docker exec

Options:
  -d, --detach               Detached mode: run command in the background
      --detach-keys string   Override the key sequence for detaching a container
  -e, --env list             Set environment variables
      --env-file list        Read in a file of environment variables
  -i, --interactive          Keep STDIN open even if not attached
      --privileged           Give extended privileges to the command
  -t, --tty                  Allocate a pseudo-TTY
  -u, --user string          Username or UID (format: "<name|uid>[:<group|gid>]")
  -w, --workdir string       Working directory inside the container
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.

案例:

root@localhost:~# docker exec -it nginx /bin/bash
root@74528d231da9:/# exit
exit
  • 1.
  • 2.
  • 3.

3.9 删除容器

docker rm [OPTIONS] CONTAINER [CONTAINER…]

命令详情:

root@localhost:~# docker rm --help

Usage:  docker rm [OPTIONS] CONTAINER [CONTAINER...]

Remove one or more containers

Aliases:
  docker container rm, docker container remove, docker rm

Options:
  -f, --force     Force the removal of a running container (uses SIGKILL)
  -l, --link      Remove the specified link
  -v, --volumes   Remove anonymous volumes associated with the container
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

案例:

root@localhost:~# docker  rm -f  nginx
nginx
  • 1.
  • 2.

4、镜像保存和分享

4.1 提交镜像

docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

命令详情:

root@localhost:~# docker commit --help

Usage:  docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

Create a new image from a container's changes

Aliases:
  docker container commit, docker commit

Options:
  -a, --author string    Author (e.g., "John Hannibal Smith
                         <hannibal@a-team.com>")
  -c, --change list      Apply Dockerfile instruction to the created image
  -m, --message string   Commit message
  -p, --pause            Pause container during commit (default true)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

案例:

root@localhost:~# docker commit -a 'xieyang' -m 'change gninx index.html' nginx my-nginx:0.0.1
sha256:c394951469b08ac48109e49ee5325f41eaf23927913c72e2616500a63fc57043
root@localhost:~# docker image ls
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
my-nginx     0.0.1     c394951469b0   6 seconds ago   188MB
nginx        latest    fffffc90d343   2 weeks ago     188MB
mysql        8.4.0     05247af91864   2 weeks ago     578MB
root@localhost:~#
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

4.2 保存镜像

docker save [OPTIONS] IMAGE [IMAGE…]

命令详情:

root@localhost:~# docker save --help

Usage:  docker save [OPTIONS] IMAGE [IMAGE...]

Save one or more images to a tar archive (streamed to STDOUT by default)

Aliases:
  docker image save, docker save

Options:
  -o, --output string   Write to a file, instead of STDOUT
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

案例:

root@localhost:~# docker save -o /opt/my-nginx.jar my-nginx:0.0.1
root@localhost:~# ls -l /opt/
total 187284
-rw------- 1 root root 191773184 Jul  7 22:01 my-nginx.jar
  • 1.
  • 2.
  • 3.
  • 4.

4.3 加载镜像

docker load [OPTIONS]

命令详情:

root@localhost:~# docker load --help

Usage:  docker load [OPTIONS]

Load an image from a tar archive or STDIN

Aliases:
  docker image load, docker load

Options:
  -i, --input string   Read from tar archive file, instead of STDIN
  -q, --quiet          Suppress the load output
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

案例:

root@localhost:~# docker image ls
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
nginx        latest    fffffc90d343   2 weeks ago    188MB
mysql        8.4.0     05247af91864   2 weeks ago    578MB
nginx        1.26.0    94543a6c1aef   2 months ago   188MB
root@localhost:~# docker load -i /opt/my-nginx.jar
d3c461b54c38: Loading layer  15.36kB/15.36kB
Loaded image: my-nginx:0.0.1
root@localhost:~# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
my-nginx     0.0.1     c394951469b0   6 minutes ago   188MB
nginx        latest    fffffc90d343   2 weeks ago     188MB
mysql        8.4.0     05247af91864   2 weeks ago     578MB
nginx        1.26.0    94543a6c1aef   2 months ago    188MB
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

4.4 分享镜像

4.4.1 登录docker

docker login [OPTIONS] [SERVER]

命令详情:

root@localhost:~# docker login --help

Usage:  docker login [OPTIONS] [SERVER]

Log in to a registry.
If no server is specified, the default is defined by the daemon.

Options:
  -p, --password string   Password
      --password-stdin    Take the password from stdin
  -u, --username string   Username
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

案例:

root@localhost:~# docker login -u learningfish
Password:
Login Succeeded
  • 1.
  • 2.
  • 3.
4.4.2 镜像tag

docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

命令详情:

root@localhost:~# docker tag --help

Usage:  docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

Aliases:
  docker image tag, docker tag
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

案例:

root@localhost:~# docker tag my-nginx:0.0.1 learningfish/nginx:0.0.1
root@localhost:~# docker tag my-nginx:0.0.1 learningfish/nginx:latest
root@localhost:~# docker image ls
REPOSITORY           TAG       IMAGE ID       CREATED          SIZE
my-nginx             0.0.1     c394951469b0   29 minutes ago   188MB
learningfish/nginx   0.0.1     c394951469b0   29 minutes ago   188MB
learningfish/nginx   latest    c394951469b0   29 minutes ago   188MB
nginx                latest    fffffc90d343   2 weeks ago      188MB
mysql                8.4.0     05247af91864   2 weeks ago      578MB
nginx                1.26.0    94543a6c1aef   2 months ago     188MB
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
4.4.3 分享镜像

docker push [OPTIONS] NAME[:TAG]

命令详情:

root@localhost:~# docker push --help

Usage:  docker push [OPTIONS] NAME[:TAG]

Upload an image to a registry

Aliases:
  docker image push, docker push

Options:
  -a, --all-tags                Push all tags of an image to the repository
      --disable-content-trust   Skip image signing (default true)
  -q, --quiet                   Suppress verbose output
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

案例:

root@localhost:~# docker push learningfish/nginx:0.0.1
The push refers to repository [docker.io/learningfish/nginx]
d3c461b54c38: Pushed
56b6d3be75f9: Pushed
0c6c257920c8: Pushed
92d0d4e97019: Pushed
7190c87a0e8a: Pushed
933a3ce2c78a: Pushed
32cfaf91376f: Pushed
32148f9f6c5a: Pushed
0.0.1: digest: sha256:9e5df404e7e815c73c16debf7406d31a45119b9e5d0a48f6a37851a7e637da3d size: 1986
root@localhost:~# docker push -q learningfish/nginx:latest
docker.io/learningfish/nginx:latest
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

5、容器高级

5.1 数据持久化

docker volume COMMAND

命令详情:

root@localhost:~# docker volume --help

Usage:  docker volume COMMAND

Manage volumes

Commands:
  create      Create a volume
  inspect     Display detailed information on one or more volumes
  ls          List volumes
  prune       Remove unused local volumes
  rm          Remove one or more volumes
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

Docker Volumes 的核心概念:

  • 持久性: Volumes 中的数据在容器停止或删除后依然存在。
  • 独立性: Volumes 独立于容器的镜像文件系统,这意味着它们不会受到镜像更新的影响。
  • 可共享性: Volumes 可以被多个容器同时挂载,从而实现容器之间的数据共享。

Volumes 的类型:

  • 命名 Volumes: 使用名称标识的 Volumes,可以通过 docker volume ls 命令查看。
  • 匿名 Volumes: 没有名称的 Volumes,由 Docker 自动分配唯一的 ID,docker run 不加 -v
  • Bind mounts: 将主机文件系统直接挂载到容器中,``docker run -v /自定义系统路径:容器路径`。
  • 数据 Volumes: 独立于主机文件系统的 Volumes,``docker run -v volume(不存在则新建):容器路径`。

案例:

root@localhost:~# docker run -itd --name mysql8 -v mysql_lib:/var/lib/mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:8.4.0
75f77665ebda8d53ee5c02d3eb6c975a0809e9bd41fc4d6bf3654456dca68707
root@localhost:~# docker volume ls
DRIVER    VOLUME NAME
local     mysql_lib
local     nginxconfig
root@localhost:~# docker volume inspect mysql_lib
[
    {
        "CreatedAt": "2024-07-08T13:54:35Z",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/mysql_lib/_data",
        "Name": "mysql_lib",
        "Options": null,
        "Scope": "local"
    }
]
root@localhost:~#
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.

5.2 网络

docker network COMMAND

命令详情:

root@localhost:~# docker network --help

Usage:  docker network COMMAND

Manage networks

Commands:
  connect     Connect a container to a network
  create      Create a network
  disconnect  Disconnect a container from a network
  inspect     Display detailed information on one or more networks
  ls          List networks
  prune       Remove all unused networks
  rm          Remove one or more networks
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

Docker 网络的基本概念

  • 容器网络隔离: 每个容器默认拥有独立的网络命名空间,它们与主机网络隔离,彼此之间无法直接通信。ttps://docs.docker.com/network/understanding-docker-networks/)
  • 网络驱动: Docker 提供多种网络驱动,用于创建不同的网络类型,例如 bridge、host、overlay、macvlan 等。
  • 网络连接: Docker 允许容器通过网络进行连接,包括容器与容器、容器与主机、容器与外部网络之间的连接。

2. Docker 网络类型

Docker 提供了几种常见的网络类型,每种类型都拥有不同的特点和适用场景:

  • bridge 网络: 默认网络类型,使用桥接模式连接容器。容器可以通过 Docker 内部创建的虚拟网络接口进行通信。
  • host 网络: 容器直接使用主机网络,共享主机的 IP 地址和端口,适合容器需要直接访问主机资源的场景。
  • overlay 网络: 用于跨主机连接容器,实现容器集群网络,适用于 Docker Swarm 集群环境。
  • macvlan 网络: 为容器提供独立的 MAC 地址和 IP 地址,可以将容器连接到外部网络,并与其他设备进行通信。
  • none 网络: 容器没有网络连接,无法进行任何网络通信。

案例:

root@localhost:~# docker network create inner_bridge
f35c10ec1d806c4ebf23b5136c6a07866525b2b9a02e8cc5a17a47a1a0bbf452
root@localhost:~# docker network ls
NETWORK ID     NAME           DRIVER    SCOPE
13a4828e292d   bridge         bridge    local
2aeedbc0b6a9   host           host      local
f35c10ec1d80   inner_bridge   bridge    local
25a6b95418c4   none           null      local
root@localhost:~# docker network connect inner_bridge nginx
root@localhost:~# docker run -d -p 8081:80 --network inner_bridge --name nginx2 nginx:latest
a6e545a9cd5cac6d2986d205e6e9b3f772e8aedcac2d488ca1fda81477fa9f40
root@localhost:~# docker exec -it nginx2 /bin/bash
root@a6e545a9cd5c:/# curl nginx
hello world
root@a6e545a9cd5c:/# exit
exit
root@localhost:~# docker network disconnect inner_bridge nginx
root@localhost:~# docker network disconnect inner_bridge nginx2
root@localhost:~# docker network prune
WARNING! This will remove all custom networks not used by at least one container.
Are you sure you want to continue? [y/N] y
Deleted Networks:
inner_bridge

root@localhost:~# docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
13a4828e292d   bridge    bridge    local
2aeedbc0b6a9   host      host      local
25a6b95418c4   none      null      local
  • 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.

6、dockerfile和compose file

6.1 Dockerfile

Dockerfile 是一个文本文件,其中包含一系列指令,这些指令用于构建 Docker 镜像。 镜像是一个轻量级的、可执行的软件包,它包含应用程序及其所有依赖项,可以轻松地部署和运行在任何 Docker 环境中。

Dockerfile 指令

Dockerfile 使用一系列指令来构建镜像,以下是几个常用的指令:

  • FROM: 指定基础镜像,例如 FROM ubuntu:latest
  • RUN: 在镜像中执行命令,例如 RUN apt-get update && apt-get install -y nginx
  • COPY: 将文件或目录从构建上下文复制到镜像中,例如 COPY . /app
  • WORKDIR: 设置工作目录,例如 WORKDIR /app
  • CMD: 指定容器启动时的默认命令,例如 CMD ["nginx", "-g", "daemon off;"]
  • EXPOSE: 指定容器要公开的端口,例如 EXPOSE 80

Dockerfile 例子

dockerfileFROM ubuntu:latest

RUN apt-get update && apt-get install -y nginx

COPY nginx.conf /etc/nginx/nginx.conf

WORKDIR /var/www/html

COPY index.html /var/www/html/index.html

CMD ["nginx", "-g", "daemon off;"]
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

6.2 Compose file

Compose file 使用 YAML 格式,它定义了一个多容器 Docker 应用程序。 Compose file 可以定义多个服务,每个服务都有自己的 Dockerfile 和运行配置, Compose 会将这些服务链接在一起并管理它们的启动、停止和重启。

Compose file 指令

Compose file 使用一些指令来定义服务,以下是几个常用的指令:

  • version: 指定 Compose 文件的版本,例如 version: "3.8"
  • services: 定义应用程序中的所有服务,例如 services:
  • build: 指定服务镜像的构建方式,可以指定 Dockerfile 或上下文路径,例如 build: .
  • ports: 指定服务端口映射,例如 ports: - "80:80"
  • volumes: 指定服务数据卷,例如 volumes: - data:/data
  • links: 指定服务之间的链接,例如 links: - db
  • depends_on: 指定服务依赖关系,例如 depends_on: - db

Compose file 例子

yamlversion: "3.8"

services:
  web:
    build: .
    ports:
      - "80:80"
    depends_on:
      - db
  db:
    image: postgres
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: password
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

6.3 官方文档参考

 Reference documentation | Docker Docs

 Java language-specific guide | Docker Docs