Docker命令_各种参数简介(run、v、rm、-w、-u、-e)

目录

■前言

■快速入门

■帮助文档

■介绍

1.run 的各种参数

2.1.docker -v 挂载 (目录)

2.2.docker -v 挂载 (Volume)

3.docker --rm

4.docker -w  -it

5.docker -u

6.docker -e

■mvn命令行执行

■更多参数

■更多命令 --- docker的一些命令

・常用操作命令

・查看Docker信息

■实际使用


======

■前言

DevOps使用到的工具・术语_sun0322-CSDN博客

■快速入门

Docker快速入门_w无问西东-CSDN博客

docker -v 挂载问题:_hnmpf的博客-CSDN博客_docker-v

Docker入门,看这一篇就够了 - 简书

■帮助文档

Docker run reference | Docker Documentation

■介绍

1.run 的各种参数

Docker基础 - W-D - 博客园

docker run [OPTIONS] IMAGE [COMMOND] [ARGS...]
 
# OPTIONS 说明
	--name="容器新名字": 为容器指定一个名称;
	-d: 后台运行容器,并返回容器ID,也即启动守护式容器;
	-i:以交互模式运行容器,通常与 -t 同时使用;
	-t:为容器重新分配一个伪输入终端,通常与 -i 同时使用;
	-P: 随机端口映射;
	-p: 指定端口映射,有以下四种格式
	      ip:hostPort:containerPort
	      ip::containerPort
	      hostPort:containerPort
	      containerPort
    -w: 指定命令执行时,所在的路径


# IMAGE
XXX_IMAGE_NAME:XXX_IMAGE_VER


# COMAND
例:mvn -Duser.home=xxx -B clean package -Dmaven.test.skip=true

---

常用OPTIONS补足:
--name:容器名字
--network:指定网络
--rm:容器停止自动删除容器

-i:--interactive,交互式启动
-t:--tty,分配终端
-v:--volume,挂在数据卷
-d:--detach,后台运行
 

--- (-w 在run中,貌似也可直接使用)

在已运行的容器中运行命令
docker exec [OPTIONS] CONTAINER COMMAND [ARG…]
常用选项:
  -d:--detach ,后台运行命令
  -e, --env list             设置env
  -i, --interactive         启用交互式
  -t, --tty                     启用终端
  -u, --user string        指定用户 (格式: <name|uid>[:<group|gid>])
  -w, --workdir string       指定工作目录

---

在容器内执行/bin/bash命令

# eg: 使用镜像centos:latest以交互模式启动一个容器,在容器内执行/bin/bash命令。
docker run -it centos /bin/bash

2.1.docker -v 挂载 (目录)

      我们可以多次挂载

                    ・挂载maven

       ・挂载jenkins

    相关资料

      (十)Docker-V 详解 - sixinshuier - 博客园

      docker -v 挂载问题:_hnmpf的博客-CSDN博客_docker-v

譬如我要启动一个centos容器,宿主机的/test目录挂载到容器的/soft目录,可通过以下方式指定:

# docker run -it -v /test:/soft centos /bin/bash

冒号":"前面的目录是宿主机目录,后面的目录是容器内目录。

关于Docker目录挂载的总结 - iVictor - 博客园

关于Docker目录挂载的总结

# docker run -it -v /test:/soft centos /bin/bash

一、容器目录不可以为相对路径

二、宿主机目录如果不存在,则会自动生成

# docker run -it -v test1:/soft centos /bin/bash

三、宿主机的目录如果为相对路
     ・容器内的/soft目录挂载的是宿主机上的/var/lib/docker/volumes/test1/_data目录

     ・所谓的相对路径指的是/var/lib/docker/volumes/,
    与宿主机的当前目录无关。

四、如果在容器内修改了目录的属主和属组,那么对应的挂载点会跟着修改

・更多挂载目录的方法 (可以使用镜像直接挂载)

docker-修改容器的挂载目录三种方式_zedelei的博客-CSDN博客_docker修改挂载目录

2.2.docker -v 挂载 (Volume)

Docker学习笔记(6)——Docker Volume - 简书

・基础
Docker的数据持久化---数据不随着container的结束而结束,
数据存在于host机器上:(①或②中的一种)
  ・①存在于host的某个指定目录中(使用bind mount),
  ・②使用docker自己管理的volume(/var/lib/docker/volumes下)。
 

・Docker Volume例子

。。。
-v maven-repository-volume:/MyPoroject/mvn/.m2
。。。

・查看【maven-repository-volume】的volume:
docker volume inspect my-volume

・注意:
host机器的目录路径必须为全路径(即需要以/或~/开始的路径),
不然docker会把这个目录当做volume
 

3.docker --rm

容器退出时就能够自动清理容器内部的文件系统

docker run的--rm选项详解_大方子-CSDN博客_docker--rm

Detached (-d)🔗

To start a container in detached mode, you use -d=true or just -d option. By design, containers started in detached mode exit when the root process used to run the container exits, unless you also specify the --rm option. If you use -d with --rm, the container is removed when it exits or when the daemon exits, whichever happens first.

4.docker -w  -it

          Working directory inside the container

$ docker  run -w /path/to/dir/ -i -t  ubuntu pwd

he -w lets the command being executed inside directory given, here /path/to/dir/. If the path does not exist it is created inside the container.

WORKDIR指令用于指定容器的一个目录, 容器启动时执行的命令会在该目录下执行。

  docker run -it -w <work_dir> <container_image_name> <command>

  示例:

  docker run -it -w /home/jello centos /bin/bash

--

        

■例子 (-w)

   docker run --rm \

     -v 指定 maven Repository \

     -v 指定 Jenkins Home \

     -w 打包对象工程所在目录 CONTAINER_IMAGE_NAME:IMAGE_VER \

    mvn clean package

5.docker -u

指定执行命令时,所使用的用户,不指定时,默认以root用户执行。

指定时,指定的时ID,关于linux中的ID,参照下面文章中的No.37

Unix_Linux_常用命令总结_sun0322-CSDN博客

6.docker -e

指定环境变量

-e XXX_XXX="xxxxxxxxxxx"

■关于每一行结尾的反斜线

Docker run reference | Docker Documentation

■mvn命令行执行

在 命令行 (cmd)执行 Maven命令,对java工程进行打包 操作 (指定settings.xml)_sun0322-CSDN博客_命令行运行maven项目

■更多参数

docker常用命令总结 - Wshile - 博客园

Name, shorthandDefaultDescription
--add-hostAdd a custom host-to-IP mapping (host:ip)
--attach , -aAttach to STDIN, STDOUT or STDERR
--blkio-weightBlock IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
--blkio-weight-deviceBlock IO weight (relative device weight)
--cap-addAdd Linux capabilities
--cap-dropDrop Linux capabilities
--cgroup-parentOptional parent cgroup for the container
--cidfileWrite the container ID to the file
--cpu-countCPU count (Windows only)
--cpu-percentCPU percent (Windows only)
--cpu-periodLimit CPU CFS (Completely Fair Scheduler) period
--cpu-quotaLimit CPU CFS (Completely Fair Scheduler) quota
--cpu-rt-periodAPI 1.25+
Limit CPU real-time period in microseconds
--cpu-rt-runtimeAPI 1.25+
Limit CPU real-time runtime in microseconds
--cpu-shares , -cCPU shares (relative weight)
--cpusAPI 1.25+
Number of CPUs
--cpuset-cpusCPUs in which to allow execution (0-3, 0,1)
--cpuset-memsMEMs in which to allow execution (0-3, 0,1)
--detach , -dRun container in background and print container ID
--detach-keysOverride the key sequence for detaching a container
--deviceAdd a host device to the container
--device-cgroup-ruleAdd a rule to the cgroup allowed devices list
--device-read-bpsLimit read rate (bytes per second) from a device
--device-read-iopsLimit read rate (IO per second) from a device
--device-write-bpsLimit write rate (bytes per second) to a device
--device-write-iopsLimit write rate (IO per second) to a device
--disable-content-trusttrueSkip image verification
--dnsSet custom DNS servers
--dns-optSet DNS options
--dns-optionSet DNS options
--dns-searchSet custom DNS search domains
--domainnameContainer NIS domain name
--entrypointOverwrite the default ENTRYPOINT of the image
--env , -eSet environment variables
--env-fileRead in a file of environment variables
--exposeExpose a port or a range of ports
--gpusAPI 1.40+
GPU devices to add to the container (‘all’ to pass all GPUs)
--group-addAdd additional groups to join
--health-cmdCommand to run to check health
--health-intervalTime between running the check (ms|s|m|h) (default 0s)
--health-retriesConsecutive failures needed to report unhealthy
--health-start-periodAPI 1.29+
Start period for the container to initialize before starting health-retries countdown (ms|s|m|h) (default 0s)
--health-timeoutMaximum time to allow one check to run (ms|s|m|h) (default 0s)
--helpPrint usage
--hostname , -hContainer host name
--initAPI 1.25+
Run an init inside the container that forwards signals and reaps processes
--interactive , -iKeep STDIN open even if not attached
--io-maxbandwidthMaximum IO bandwidth limit for the system drive (Windows only)
--io-maxiopsMaximum IOps limit for the system drive (Windows only)
--ipIPv4 address (e.g., 172.30.100.104)
--ip6IPv6 address (e.g., 2001:db8::33)
--ipcIPC mode to use
--isolationContainer isolation technology
--kernel-memoryKernel memory limit
--label , -lSet meta data on a container
--label-fileRead in a line delimited file of labels
--linkAdd link to another container
--link-local-ipContainer IPv4/IPv6 link-local addresses
--log-driverLogging driver for the container
--log-optLog driver options
--mac-addressContainer MAC address (e.g., 92:d0:c6:0a:29:33)
--memory , -mMemory limit
--memory-reservationMemory soft limit
--memory-swapSwap limit equal to memory plus swap: ‘-1’ to enable unlimited swap
--memory-swappiness-1Tune container memory swappiness (0 to 100)
--mountAttach a filesystem mount to the container
--nameAssign a name to the container
--netConnect a container to a network
--net-aliasAdd network-scoped alias for the container
--networkConnect a container to a network
--network-aliasAdd network-scoped alias for the container
--no-healthcheckDisable any container-specified HEALTHCHECK
--oom-kill-disableDisable OOM Killer
--oom-score-adjTune host’s OOM preferences (-1000 to 1000)
--pidPID namespace to use
--pids-limitTune container pids limit (set -1 for unlimited)
--platformexperimental (daemon)API 1.32+
Set platform if server is multi-platform capable
--privilegedGive extended privileges to this container
--publish , -pPublish a container’s port(s) to the host
--publish-all , -PPublish all exposed ports to random ports
--read-onlyMount the container’s root filesystem as read only
--restartnoRestart policy to apply when a container exits
--rmAutomatically remove the container when it exits
--runtimeRuntime to use for this container
--security-optSecurity Options
--shm-sizeSize of /dev/shm
--sig-proxytrueProxy received signals to the process
--stop-signalSIGTERMSignal to stop a container
--stop-timeoutAPI 1.25+
Timeout (in seconds) to stop a container
--storage-optStorage driver options for the container
--sysctlSysctl options
--tmpfsMount a tmpfs directory
--tty , -tAllocate a pseudo-TTY
--ulimitUlimit options
--user , -uUsername or UID (format: <name|uid>[:<group|gid>])
--usernsUser namespace to use
--utsUTS namespace to use
--volume , -vBind mount a volume
--volume-driverOptional volume driver for the container
--volumes-fromMount volumes from the specified container(s)
--workdir , -wWorking directory inside the container

docker常用命令总结 - Wshile - 博客园

-d, --detach=false # 后台运行容器,并返回容器ID;
-i, --interactive=false # 以交互模式运行容器,通常与 -t 同时使用;
-t, --tty=false # 为容器重新分配一个伪输入终端,通常与 -i 同时使用;
-u, --user="" # 指定容器的用户
-a, --attach=[] # 登录容器(必须是以docker run -d启动的容器)
-w, --workdir="" # 指定容器的工作目录
-c, --cpu-shares=0 # 设置容器CPU权重,在CPU共享场景使用
-e, --env=[] # 指定环境变量,容器中可以使用该环境变量
-m, --memory="" # 指定容器的内存上限
-P, --publish-all=false # 指定容器暴露的端口
-p, --publish=[] # 指定容器暴露的端口
-h, --hostname="" # 指定容器的主机名
-v, --volume=[] # 给容器挂载存储卷,挂载到容器的某个目录
--volumes-from=[] # 给容器挂载其他容器上的卷,挂载到容器的某个目录
--cap-add=[] # 添加权限,权限清单详见:http://linux.die.net/man/7/capabilities
--cap-drop=[] # 删除权限,权限清单详见:http://linux.die.net/man/7/capabilities
--cidfile="" # 运行容器后,在指定文件中写入容器PID值,一种典型的监控系统用法
--cpuset="" # 设置容器可以使用哪些CPU,此参数可以用来容器独占CPU
--device=[] # 添加主机设备给容器,相当于设备直通
--dns=[] # 指定容器的dns服务器
--dns-search=[] # 指定容器的dns搜索域名,写入到容器的/etc/resolv.conf文件
--entrypoint="" # 覆盖image的入口点
--env-file=[] # 指定环境变量文件,文件格式为每行一个环境变量
--expose=[] # 指定容器暴露的端口,即修改镜像的暴露端口
--link=[] # 指定容器间的关联,使用其他容器的IP、env等信息
--lxc-conf=[] # 指定容器的配置文件,只有在指定--exec-driver=lxc时使用
--name="" # 指定容器名字,后续可以通过名字进行容器管理,links特性需要使用名字
--net="bridge" # 容器网络设置:
    bridge  # 使用docker daemon指定的网桥
    host  # 容器使用主机的网络
    container:NAME_or_ID > # 使用其他容器的网路,共享IP和PORT等网络资源
    none # 容器使用自己的网络(类似--net=bridge),但是不进行配置
--privileged=false # 指定容器是否为特权容器,特权容器拥有所有的capabilities
--restart="no" # 指定容器停止后的重启策略:
    no # 容器退出时不重启
    on-failure # 容器故障退出(返回值非零)时重启
    always # 容器退出时总是重启
--rm=false # 指定容器停止后自动删除容器(不支持以docker run -d启动的容器)
--sig-proxy=true # 设置由代理接受并处理信号,但是SIGCHLD、SIGSTOP和SIGKILL不能被代理

---

■更多命令 --- docker的一些命令

・常用操作命令

================================

查看docker服务运行状况

ps -el | grep -i docker

-

停止Docker服务
service docker stop

-

启动docker服务

service docker start

================================

查看有哪些镜像
docker search yourAppName

-

获取镜像
docker pull imageName

-

查看安装了的镜像
docker images

-

查看运行的容器
docker ps

-

查看所有的容器
docker ps -a

-

停止容器运行
docker stop <container_id>

-

重新启动容器
docker resatart <container_id>

-

删除容器
docker rm yourContainerID 

-

删除镜像
docker rmi yourImageName

-

查看容器开启时的log
docker logs -f yourContainerName

-

进入容器内部执行命令
docker exec -it yourContainerID bash

===============

查看Log
docker logs -f <image_name>

・查看Docker信息

docker info

【环境搭建】Docker镜像相关操作(切换镜像源、查询、获取、查看、创建、上传、保存、删除等)_查看docker镜像源_Fighting_hawk的博客-CSDN博客

===

■实际使用

创建Jenkins服务

使用Docker快速创建一个Jenkins服务_sun0322的博客-CSDN博客

创建WebSphere 服务

使用Docker创建一个WebSphere服务_sun0322的博客-CSDN博客

Docker命令无效时,解决办法

所有docker命令无效,解决办法_sun0322的博客-CSDN博客

==

  • 53
    点赞
  • 246
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Docker是一种容器化技术,可以在不同的操作系统上运行应用程序。Docker Compose是一个用于定义和运行多容器Docker应用程序的工具。 以下是一些常用的DockerDocker Compose命令: ### Docker - `docker run`: 运行一个Docker容器。 - `docker ps`: 列出正在运行的Docker容器。 - `docker images`: 列出所有本地镜像。 - `docker build`: 使用Dockerfile构建一个Docker镜像。 - `docker push`: 将本地镜像上传到Docker Hub或其他Docker镜像仓库。 - `docker pull`: 从Docker Hub或其他Docker镜像仓库中下载一个镜像。 - `docker rm`: 删除一个或多个Docker容器。 - `docker rmi`: 删除一个或多个本地Docker镜像。 ### Docker Compose - `docker-compose up`: 构建并启动一个Docker Compose应用。 - `docker-compose down`: 停止并删除一个Docker Compose应用。 - `docker-compose ps`: 列出正在运行的Docker Compose服务。 - `docker-compose logs`: 查看一个Docker Compose服务的日志。 - `docker-compose exec`: 在一个Docker Compose服务中执行一个命令。 - `docker-compose build`: 构建一个Docker Compose服务的镜像。 - `docker-compose pull`: 下载一个Docker Compose服务的镜像。 ### Docker Compose配置文件 Docker Compose使用YAML格式的配置文件来定义多容器Docker应用程序。以下是一个示例配置文件: ``` version: '3' services: web: build: . ports: - "5000:5000" redis: image: "redis:alpine" ``` - `version`: 定义配置文件使用的Docker Compose版本。 - `services`: 包含一个或多个Docker容器服务的列表。 - `build`: 构建一个Docker镜像。 - `image`: 使用现有的Docker镜像。 - `ports`: 映射容器端口到主机端口。在此示例中,容器的5000端口将映射到主机的5000端口。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值