Docker - docker run (Creating a container)

> Virtualization - (Virtual Machine|Container) > Docker

1 - About

Create container from an image on the virtual host and call the entrypoint script.

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

Advertising

3 - 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

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.
Advertising

4 - Process

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 pulled the “hello-world” image from the Docker Hub.
  • 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.

5 - Example

5.1 - Launching bash inside a container

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

5.2 - 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.
Advertising

5.3 - Starting a daemonized container

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:

6 - Support

6.1 - 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
Advertising

6.2 - 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

7 - Documentation / Reference