Pass Multiple Commands on Docker Run

DZone 's Guide to

Pass Multiple Commands on Docker Run

This short tutorial demonstrates how Docker can be used to run complex bash commands without additional binaries.

· Cloud Zone ·
Free Resource

Image title

Docker, apart form serving our workloads efficiently, is also an amazing tool when it comes to not installing additional binaries to your workstation.

Eventually, you will find it very easy and simple to just run only one command on docker. For example, I want to run a Hello World application in Go.

My source code is going to be the simple Hello World.

package main

import "fmt"

func main() {
    fmt.Println("hello world")

Pretty simple! The file shall be named hello_world.go.

Now let's run this in a container.

docker run -v $(pwd):/go/src/app --rm --name helloworld golang:1.8 go run src/app/hello_world.go

How about installing some Go packages and then run our application in a one-liner?

If you try to do so, you will realise that Docker won't interpret the commands the way you want, so here's how to get the result that you want.

If your image contains the /bin/bash or bin/sh binary you can pass the commands you want to execute as a string.

docker run -v $(pwd):/go/src/app --rm --name helloworld golang:1.8 /bin/bash -c "cd src/app;go get https:yourpackage;go run src/app/hello_world.go

That's it! Now you can run complex bash one-liners without worrying on installing additional software on your workstations

bash, binaries, cloud, docker

Published at DZone with permission of Emmanouil Gkatziouras , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}