将指定Docker run命令转换为适配Docker Swarm的docker-compose.yml
Got it, let's translate that docker run command into a clean docker-compose.yml file that works perfectly with Docker Swarm. Here's the breakdown:
The Docker Compose File
First, create a docker-compose.yml with this content:
version: '3.8' services: stress-test: # Specify the same image from your docker run command image: progrium/stress # Pass all the stress test arguments as the container's startup command command: --cpu 2 --io 1 --vm 2 --vm-bytes 128M --timeout 10s # Match the --rm behavior: tell Swarm not to restart the container after it exits deploy: restart_policy: condition: none # Optional: Uncomment these if you need interactive terminal support (from -it flag) # stdin_open: true # tty: true
What Each Part Does
Let's map this back to your original docker run command:
image: progrium/stress: Directly corresponds to the image you specified indocker runcommand: ...: This replaces everything after the image name in your original command — all the stress test parameters get passed directly to the containerdeploy.restart_policy.condition: none: This replicates the--rmbehavior by ensuring Swarm doesn't restart the container once it finishes the stress test (and Swarm will automatically clean up the container after exit)- The
stdin_openandttylines: These correspond to the-itflags in your original command. Most of the time, you won't need these for a one-off stress test in Swarm (since the test runs and exits on its own), but uncomment them if you need to attach an interactive terminal to the container.
Running It in Docker Swarm
To deploy this as a Swarm service, run:
docker stack deploy -c docker-compose.yml stress-test-stack
If you want a more "one-off" execution (closer to the original docker run --rm), you can also use:
docker-compose run --rm stress-test
Note: The above runs the task locally via Docker Compose. If you need to run it directly on the Swarm cluster as a one-off task, you could also use docker service create --rm, but the Compose file gives you a reusable configuration.
内容的提问来源于stack exchange,提问作者Juliatzin
相关产品推荐
相关产品推荐

