Docker-compose命令-run

命令

docker-compose run

描述

Run a one-off command
运行一次性命令

用法

1
run [options] [-v VOLUME...] [-p PORT...] [-e KEY=VAL...] [-l KEY=VALUE...] SERVICE [COMMAND] [ARGS...]

选项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Options:
-d, --detach Detached mode: Run container in the background, print new container name.
--name NAME Assign a name to the container
--entrypoint CMD Override the entrypoint of the image.
-e KEY=VAL Set an environment variable (can be used multiple times)
-l, --label KEY=VAL Add or override a label (can be used multiple times)
-u, --user="" Run as specified username or uid
--no-deps Don't start linked services.
--rm Remove container after run. Ignored in detached mode.
-p, --publish=[] Publish a container's port(s) to the host
--service-ports Run command with the service's ports enabled and mapped to the host.
--use-aliases Use the service's network aliases in the network(s) the container connects to.
-v, --volume=[] Bind mount a volume (default [])
-T Disable pseudo-tty allocation. By default `docker-compose run` allocates a TTY.
-w, --workdir="" Working directory inside the container

注意

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ docker-compose run demo whoami
Creating demo_demo_run ... done
root
# 执行命令后删除容器
$ docker-compose run --rm demo whoami
Creating demo_demo_run ... done
root
$ docker-compose run demo ls /usr/share/nginx/html
Creating demo_demo_run ... done
demo.txt index.html
$ docker-compose ps -a
Name Command State Ports
-----------------------------------------------------------------------------------------
demo_demo_run_368b1fe0df5f /docker-entrypoint.sh whoami Exit 0
demo_demo_run_9c4367d358bd /docker-entrypoint.sh ls / ... Exit 0

$ docker-compose run web bash
$ docker-compose run --service-ports web python manage.py shell
$ docker-compose run --publish 8080:80 -p 2022:22 -p 127.0.0.1:2021:21 web python manage.py shell
$ docker-compose run db psql -h db -U docker
$ docker-compose run --no-deps web python manage.py shell
$ docker-compose run --rm web python manage.py db upgrade