Convert a docker run command into a docker-compose

Juliatzin picture Juliatzin · Apr 23, 2018 · Viewed 16.6k times · Source

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 ?

Answer

Jinna Balu picture Jinna Balu · Apr 23, 2018

Converting a docker run command into a compose file

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

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

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 - voulume you want to mount

Converting the run commnd from above values to compose

version: "2/3/3.3/3.6" # based on the docker 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 linnes are comman for any compose.

In Docker compose command property solves the purpose.

docker-compose.yml

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

compose file with docker-compose as

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

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

  • Multipline command to compose file

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

    <embed src="https://composerize.com/"  width="100%" height="700">