Table of Contents

How to run a docker image with example to create a container?

About

The run command creates:

This command is called the first time for the container creation. You will call start the next time to start it.

Example

HelloWorld: What does the run command do?

docker run hello-world
Hello from Docker!

To generate this message, Docker took the following steps:

Launching bash inside a container

docker run -t -i ubuntu /bin/bash
root@af8bae53bdd3:/#

Starting a command inside a container

docker run ubuntu /bin/echo 'Hello world'

where:

Starting a background container

The container will run in the background giving you back the terminal prompt.

docker run -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done"
3e3a1ac2c76e447542b99de6db704629e414a674301c520687a7dc13a841746d

docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
3e3a1ac2c76e        ubuntu              "/bin/sh -c 'while tr"   39 seconds ago      Up 39 seconds                           jolly_panini

where:

Starting when docker starts

You can apply policies 1) to start container automatically when the docker application starts.

docker run -d --restart unless-stopped containerName
docker update --restart no containerName

Syntax

Create a container and start it

docker run -t -i -d -P \\
   --name containerName \\
   --rm \\ # To remove the created container (handy)
   -v "$(pwd)"/hostPath:/dockerContainerPath \
   image \\
   command
docker run -t -i -d -P \\
   --name containerName \\
   --rm \\ # To remove the created container (handy)
   -v ${PWD}\hostPath:/dockerContainerPath \
   image \\
   command
docker run -t -i -d -P \\
   --name containerName \\
   --rm \\ # To remove the created container (handy)
   -v %CD%\hostPath:/dockerContainerPath \
   image \\
   command

where:

Support

docker: An error occurred trying to connect

When trying to run a container, you may get this error in a shell:

docker: An error occurred trying to connect: Post http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.24/containers/create: open //./pipe/docker_engine: The system cannot find the file specified..
See 'docker run --help'.

This is due to Docker OS environment not set.

To resolve this problem,

@FOR /f "tokens=*" %i IN ('docker-machine env default') DO @%i

the input device is not a TTY. If you are using mintty, try prefixing the command with 'winpty'

the input device is not a TTY.  If you are using mintty, try prefixing the command with 'winpty'

When running docker as cron job or programmatically, don't use the interactive option -it

Documentation / Reference