blj28

导航

docker 使用指导

 

问题的由来:

(1)容器安装好之后进入容器后发现很多命令没有(gcc htop 等),此外交叉编译工具如果拷贝至容器内,将会变的非常大,而且实际使用过程中,交叉编译器安装在本地没有问题,只是在运行的时候需要的依赖如果在同一个虚拟机下就会有问题,此问题可以使用另一种方式进行解决,将交叉编译环境安装到本地,然后挂载到对应的容器中。

(2)如果将待编译的文件拷贝到容器中,容器是一个运行的程序,很容易没保存到,这将导致文件丢失,这个问题如何解决??

(3)容器如何开机自启动,不用麻烦的每次都手动运行以及挂载文件????

 

docker create -it -v /opt/ti-processor-sdk-linux-rt-am57xx-evm-06.03.00.106:/home/123 ubuntu:20.04 sh 

docker rm -fv  /opt/ti-processor-sdk-linux-rt-am57xx-evm-06.03.00.106:/home/123                            //自己的ubuntu删除时没有带-v将挂载的卷删除 删除容器挂载的卷

 

 

root@blj-pc:~# la
公共的  图片  音乐           .bashrc                            bitrock_debug_4948_1714035039.xml  .cache   .gnupg        .mozilla  snap   .vscode
模板    文档  桌面           bitrock_debug_4541_1714034365.xml  bitrock_debug_4948.xml             .config  .local        .pki      .ssh
视频    下载  .bash_history  bitrock_debug_4541.xml             bitrock_debug.xml                  .dotnet  lujing_15x86  .profile  T4.7z
root@blj-pc:~# 
root@blj-pc:~# docker --help
Usage:  docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Common Commands:
  run         Create and run a new container from an image
  exec        Execute a command in a running container
  ps          List containers
  build       Build an image from a Dockerfile
  bake        Build from a file
  pull        Download an image from a registry
  push        Upload an image to a registry
  images      List images
  login       Authenticate to a registry
  logout      Log out from a registry
  search      Search Docker Hub for images
  version     Show the Docker version information
  info        Display system-wide information

Management Commands:
  builder     Manage builds
  buildx*     Docker Buildx
  compose*    Docker Compose
  container   Manage containers
  context     Manage contexts
  image       Manage images
  manifest    Manage Docker image manifests and manifest lists
  network     Manage networks
  plugin      Manage plugins
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Swarm Commands:
  swarm       Manage Swarm

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
  events      Get real time events from the server
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  inspect     Return low-level information on Docker objects
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  logs        Fetch the logs of a container
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  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
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  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

Global Options:
      --config string      Location of client config files (default "/root/.docker")
  -c, --context string     Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default context set with "docker
                           context use")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket to connect to
  -l, --log-level string   Set the logging level ("debug", "info", "warn", "error", "fatal") (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default "/root/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/root/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/root/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

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

For more help on how to use Docker, head to https://docs.docker.com/go/guides/
root@blj-pc:~# docker create help
Unable to find image 'help:latest' locally
^Croot@blj-pc:~# 
root@blj-pc:~# 
root@blj-pc:~# docker create --help
Usage:  docker create [OPTIONS] IMAGE [COMMAND] [ARG...]

Create a new container
--uts string                       UTS namespace to use
  -v, --volume list                      Bind mount a volume
      --volume-driver string             Optional volume driver for the container
      --volumes-from list                Mount volumes from the specified container(s)
  -w, --workdir string                   Working directory inside the container
root@blj-pc:~# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
ubuntu        20.04     b7bab04fd9aa   8 weeks ago    72.8MB
hello-world   latest    74cc54e27dc4   4 months ago   10.1kB
root@blj-pc:~# docker create -it -v /opt/ti-processor-sdk-linux-rt-am57xx-evm-06.03.00.106:/home/123 ubuntu:20.04 sh                     //最后的sh表示启动脚本sh docker run -it ubuntu /bin/bash
f65dc1916c21f62d56a1c8056782944eb910f5548ec23b3d010136fb347e5d9b
root@blj-pc:~# docker  ps
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS          PORTS     NAMES
7da26d87b02c   ubuntu:20.04   "/bin/bash"   33 minutes ago   Up 33 minutes             romantic_albattani
root@blj-pc:~# docker  ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS                    PORTS     NAMES
f65dc1916c21   ubuntu:20.04   "sh"          11 seconds ago   Created                             epic_benz
7da26d87b02c   ubuntu:20.04   "/bin/bash"   33 minutes ago   Up 33 minutes                       romantic_albattani
b5cb096aa6d7   hello-world    "/hello"      14 hours ago     Exited (0) 14 hours ago             beautiful_yalow
8254cf245352   hello-world    "/hello"      14 hours ago     Exited (0) 14 hours ago             nifty_pasteur
root@blj-pc:~# 
root@blj-pc:~# 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
root@blj-pc:~# docker start -a epic_benz
# ls

^Ccontext canceled
root@blj-pc:~# docker start -ai epic_benz

# ls
bin  boot  dev    etc  home  lib    lib32  lib64  libx32  media  mnt  opt  proc  root  run    sbin  srv  sys    tmp  usr  var
# 
# 
# ls /home    
123
#     
# ls /home/123 
Makefile  Rules.make  bin  board-support  docs    example-applications  filesystem  linux-devkit    linux-devkit.sh  setup.sh
# ^A^A

Docker Compose是一个用来定义和运行复杂应用的Docker工具。一个使用Docker容器的应用,通常由多个容器组成。使用Docker Compose不再需要使用shell脚本来启动容器。
Compose 通过一个配置文件来管理多个Docker容器,在配置文件中,所有的容器通过services来定义,然后使用docker-compose脚本来启动,停止和重启应用,和应用中的服务以及所有依赖服务的容器,非常适合组合使用多个容器进行开发的场景。

另外注意:run 和 start的区别

`docker start` 和 `docker run` 是 Docker 中用于管理容器生命周期的两个不同命令,它们各自有不同的用途和功能。以下是它们的主要区别:

### Docker Run

- **创建并启动容器**:`docker run` 实际上是两个命令的组合,即先 `docker create` 然后 `docker start`。它会基于指定的镜像创建一个新的容器,并立即启动这个容器。
- **一次性操作**:每次执行 `docker run` 都会基于指定的镜像创建一个全新的容器实例。如果之前已经存在使用相同镜像创建的容器,新的 `docker run` 命令不会影响到现有的容器,而是创建并启动一个新的容器
- **参数配置**:在使用 `docker run` 时,可以同时指定很多运行时选项,如端口映射(`-p`), 卷挂载(`-v`), 环境变量(`-e`)等。
- **示例**:
```bash
docker run -d -p 80:80 nginx
```
这个命令会在后台运行一个新的 Nginx 容器,并将主机的 80 端口映射到容器的 80 端口。

### Docker Start

- **仅启动已存在的容器**:与 `docker run` 不同,`docker start` 只能用来启动那些已经被创建但是目前处于停止状态的容器。它不会创建新的容器。
- **重复使用**:对于已经创建好的容器,你可以多次使用 `docker start` 来启动它,而不需要重新创建容器。这对于需要频繁启动和停止的环境特别有用。
- **无额外配置**:由于 `docker start` 是用于启动已经配置好的容器,所以它不能直接接受如 `-p`, `-v`, `-e` 等运行时选项。如果你想修改容器的运行配置(比如添加卷或端口映射),你需要先删除该容器然后用新的配置再次运行 `docker run`,或者使用 `docker update` 修改某些设置
- **示例**:
```bash
docker start container_name_or_id
```
这条命令将会启动一个名为 `container_name_or_id` 的已停止容器。

### 总结

- 使用 `docker run` 当你想要基于某个镜像快速创建并启动一个新的容器时。
- 使用 `docker start` 当你已经有现成的容器并且希望重新启动它时。

理解这两者之间的差异有助于更高效地管理和操作 Docker 容器,根据不同的需求选择合适的命令来实现目标。

 

1、概念1,终止容器

root@blj-pc:~# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED        STATUS                    PORTS     NAMES
f65dc1916c21   ubuntu:20.04   "sh"          3 hours ago    Up 3 hours                          epic_benz
7da26d87b02c   ubuntu:20.04   "/bin/bash"   3 hours ago    Up 3 hours                          romantic_albattani
b5cb096aa6d7   hello-world    "/hello"      17 hours ago   Exited (0) 17 hours ago             beautiful_yalow
8254cf245352   hello-world    "/hello"      17 hours ago   Exited (0) 17 hours ago             nifty_pasteur
root@blj-pc:~# docker kill --help
Usage:  docker kill [OPTIONS] CONTAINER [CONTAINER...]

Kill one or more running containers

Aliases:
  docker container kill, docker kill

Options:
  -s, --signal string   Signal to send to the container
root@blj-pc:~# 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, --timeout int     Seconds to wait before killing the container
root@blj-pc:~# docker stop f65dc1916c21
f65dc1916c21
root@blj-pc:~# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED        STATUS                        PORTS     NAMES
f65dc1916c21   ubuntu:20.04   "sh"          3 hours ago    Exited (137) 39 seconds ago             epic_benz
7da26d87b02c   ubuntu:20.04   "/bin/bash"   3 hours ago    Up 3 hours                              romantic_albattani
b5cb096aa6d7   hello-world    "/hello"      17 hours ago   Exited (0) 17 hours ago                 beautiful_yalow
8254cf245352   hello-world    "/hello"      17 hours ago   Exited (0) 17 hours ago                 nifty_pasteur
root@blj-pc:~# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
ubuntu        20.04     b7bab04fd9aa   8 weeks ago    72.8MB
hello-world   latest    74cc54e27dc4   4 months ago   10.1kB
root@blj-pc:~# docker stop --help

上述“f65dc1916c21”容器是通过docker create创建的,上述的docker stop 仅仅为停止,如果需要完全删除,需要将

docker rm 命令用于删除一个或多个已经停止的容器。

docker rm 命令不会删除正在运行的容器如果你需要先停止容器,可以使用 docker stop 命令。

语法

docker rm [OPTIONS] CONTAINER [CONTAINER...]
  • CONTAINER [CONTAINER...]: 一个或多个要删除的容器的名称或 ID。

OPTIONS说明:

  • -f--force: 强制删除正在运行的容器(使用 SIGKILL 信号)。
  • -l--link: 删除指定的连接,而不是容器本身。
  • -v--volumes: 删除容器挂载的卷。

 

posted on 2025-06-06 11:33  bailinjun  阅读(17)  评论(0)    收藏  举报