将docker run命令转换为docker-compose

Jul*_*oro 4 docker docker-compose docker-swarm

我想在我的Docker Swarm中运行它:

docker run --rm -it progrium/stress --cpu 2 --io 1 --vm 2 --vm-bytes 128M --timeout 10s
Run Code Online (Sandbox Code Playgroud)

所以我需要一个Docker-compose.yml

我应该如何在docker中使用此docker镜像来撰写并传递这些参数?

Jin*_*alu 27

Converting a docker run command into a compose file

Composerize will help you convert run command to a compose partially.

To understand it better I have described the components of the docker-compose.yml here.

image - image used to run the container

name - name of the service or container

command - command you want to run after the container is up

volumes - volume(s) you want to mount

Converting the run command from above to docker-compose:

version: "2/3/3.3/3.6" # based on the docker-compose version you use
services:
   stress: # Service name, user defined
      image: progrium/stress 
      command: '--cpu 2 --io 1 --vm 2 --vm-bytes 128M --timeout 10s'
Run Code Online (Sandbox Code Playgroud)

First two lines are common for any docker-compose file.

In docker-compose, the command allows the image to accept additional commands or options.

docker-compose.yml

version: "2"
services:
   stress:
      image: progrium/stress
      command: '--cpu 2 --io 1 --vm 2 --vm-bytes 128M --timeout 10s'
Run Code Online (Sandbox Code Playgroud)

Compose the file with docker-compose as:

docker-compose up -d
Run Code Online (Sandbox Code Playgroud)

version: "2/3/3.3/3.6" # based on the docker-compose version you use
services:
   stress: # Service name, user defined
      image: progrium/stress 
      command: '--cpu 2 --io 1 --vm 2 --vm-bytes 128M --timeout 10s'
Run Code Online (Sandbox Code Playgroud)


ani*_*ish 17

该工具将帮助您将 docker run 命令转换为 docker-compose 的大部分功能

在此输入图像描述


Thi*_*lva 7

只需使用这个漂亮的小工具作为助手:https : //composerize.com/

或按照先前答案中突出显示的手动步骤操作...

  • 这个工具很好,但对于某些 docker run 命令非常有限。 (3认同)