About
The run command creates:
- from an image
- running the docker host
- and calls the entrypoint script
- with the cmd values as arguments
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:
- The Docker client contacted the Docker daemon.
- The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading.
- The Docker daemon streamed that output to the Docker client, which sent it to your terminal.
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:
- docker run launches a container. Docker containers only run as long as the command you specify is active. Therefore, in the above example, the container stops once the command is executed.
- ubuntu is the image you run, for example the Ubuntu operating system image. Docker creates a new Ubuntu environment. When you specify an image, Docker looks first for the image on your Docker host. If the image does not exist locally, then the image is pulled from the public image registry Docker Hub.
- /bin/echo 'Hello World' is the command to run inside the new container.
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"
- The output is the container ID. It uniquely identifies a container.
3e3a1ac2c76e447542b99de6db704629e414a674301c520687a7dc13a841746d
- The docker ps command queries the Docker daemon for information about all the containers it knows about
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:
- 3e3a1ac2c76e is the container id
- unbutu is the image.
- jolly_panini is the container assigned name
Starting when docker starts
You can apply policies 1) to start container automatically when the docker application starts.
- With the run command. If the container is stopped, it will not restart anymore
docker run -d --restart unless-stopped containerName
- You can disable it with the update command
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:
- -t flag assigns a pseudo-tty or terminal inside the new container.
- -i flag allows you to make an interactive connection by grabbing the standard input (STDIN) of the container.
- -d flag runs the container in the background (to daemonize it).
- -P maps any required network ports inside the container to your host. It's is a shortcut for -p 5000 that maps port 5000 inside the container to a high port (from ephemeral port range which typically ranges from 32768 to 61000) on the local Docker host.
- -p 80:5000 would map port 5000 inside your container to port 80 on your local host. See also Docker - Port
- --rm: Automatically remove the container created
- --name the container name
- -v Bind mount a volume
- image is the image you run, for example the Ubuntu operating system image. Docker creates a new Ubuntu environment. When you specify an image, Docker looks first for the image on your Docker host. If the image does not exist locally, then the image is pulled from the public image registry Docker Hub.
- command is the command to run inside the new container.
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,
- start your command from the Docker Quickstart terminal
- or run the following or set the environment variable. For instance for Windows DOS:
@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