48

I want to run this in my Docker Swarm:

docker run --rm -it progrium/stress --cpu 2 --io 1 --vm 2 --vm-bytes 128M --timeout 10s

so I need a Docker-compose.yml

How should I use this docker image in a docker compose and pass those params ?

Jinna Balu
  • 6,747
  • 38
  • 47
Juliatzin
  • 18,455
  • 40
  • 166
  • 325

4 Answers4

60

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'

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'

Compose the file with docker-compose as:

docker-compose up -d
  • Multiple commands to the compose file:

    command: bash -c "cd app/ && npm start"

  • Multi-line command to compose file:

    command: >
      bash -c "cd app/ 
      && npm start"
    

<embed src="https://composerize.com/"  width="100%" height="700">
Ed Ruder
  • 578
  • 6
  • 13
Jinna Balu
  • 6,747
  • 38
  • 47
16

Just use this nifty little tool as a helper: https://composerize.com/

Or follow the manual steps highlighted in the previous answers...

Thiago Silva
  • 14,183
  • 3
  • 36
  • 46
16

This tool will help you convert docker run command to a docker-compose most of the feature

enter image description here

anish
  • 6,884
  • 13
  • 74
  • 140
2

You can use Compose file's command or entrypoint keyword. It is straightforward to translate a docker run command into declarations in the docker-compose.yml file.

To use the command keyword in your docker-compose.yml:

services:
  stress:
    image: progrium/stress
    command:
    - --cpu 2
    - --io 1
    - --vm 2
    - --vm-bytes 128M
    - --timeout 10s

To use the entrypoint keyword in your docker-compose.yml:

services:
  stress:
    image: progrium/stress
    entrypoint:
    - <entrypoint name to override the original one>
    - --cpu 2
    - --io 1
    - --vm 2
    - --vm-bytes 128M
    - --timeout 10s
Yuankun
  • 6,875
  • 3
  • 32
  • 34