Docker run

From NovaOrdis Knowledge Base
Jump to navigation Jump to search

External

Internal

Overview

Without any arguments except the name of image, it creates a new container based on the given image, and executes it in the foreground, connecting the current terminal's stdin/stdout/stderr to the container:

 docker run novaordis/centos-loop

The docker runtime will first attempt to use an image from the local registry, and if it does find it, it will attempt to pull it from Docker Hub and then cache locally in the local registry.

In order to run the container in the background, use -d | --detach.

The generic format of the command is:

docker run [options] <image> [command] [args...]

Example:

docker run -d --name="runloop1" -e SOME_ENV_VAR="some value" \
  busybox \
  /bin/sh -c 'i=0; while true; do echo ${i}; i=$(expr ${i} + 1);  sleep 1s; done'

A running container, either in foreground or background, can be with listed with:

docker ps

it can be stopped with:

docker stop

and then it can be restarted with:

docker start

Anything that a running container sends to stdout can be explored with docker logs.

More container operations:

Docker Client Operations

The Run Process

Once the "run" command is executed, the following sequence takes place:

Options

-i, --interactive

The container is started in interactive mode, giving the ability to type into it. This means the container will keep the stdin open, if it is not attached.

-t, --tty

The container is started with a pseudo-TTY so you can see the input and output.

-d, --detach

Run the container in the background and print the container ID.

-e, --env

-e SOME_ENV_VAR="some value" 

Set environment variables.

--name

Assign a name to the container, otherwise the name will be assigned automatically to something like "pedantic_einstein" or "competent_aryabhata".

--name="bluebox"

--restart

Configures the restart policy of the container being created.

--restart=always

See:

Start a Container Automatically

-p, --publish

-p|--publish list

Publish a container's port(s) to the host. Also see Dockerfile EXPOSE.

To publish port 8080:

docker run ... -p 8080:8080/tcp ...

Port Mapping

-p <host-port>:<container-port> 

Maps the specified container port to the specified host port.

-P, --publish-all

Publish all exposed ports to random ports. Also see Dockerfile EXPOSE.

-v, --volume

 docker run ... -v <native-host-path>:<container-mount-point> ...

--mount is recommended instead.

See:

Mounting Data Volumes

--mount

See:

Mounting Data Volumes

--rm

Automatically remove the container when it exits.

--network

The option is used to connect a container to a user-defined network. If not specified, the container is connected to the default network, which is the default "bridge" network.

docker run ... --network <network-name> ...

--storage-opt list

Storage driver options for the container. Controls the following:

Base Device Size

The amount of storage allocated to the container when the image is run (this was only tested with device-mapper):

...  --storage-opt size=20G ...

This value cannot be smaller than the default value, see device-mapper Base Device Size.

Resource Management Options

-m, --memory

The memory limit, in bytes. Apparently, that means the same amount will be allowed for "RAM" and swap, so a process can use double the specified amount, if it starts swapping. Also see Resource Management.

Mounting Data Volumes

If the container has volume mount points specified with VOLUME in its original Dockerfile, those mount points must be bound to paths on the native host when the container is created. This is done with --v|--volume or --mount (recommended) command line options, as follows:

docker run ... --mount type=bind,src=<native-host-path>,dst=<container-mount-point> ...
docker run ... --mount type=bind,src=/data-volumes/postgresql,dst=/var/lib/pgsql/data ...

When --mount with type=bind is used, the native-host-path must refer to an existing path on the host. The path will not be created if it does not exist, and the command will fail. Also, the mount point must have sufficient permissions. For more details on native host path permissions, see Native Host Path Permissions.

Troubleshooting

User has No Appropriate Permissions

The attempt to run the ps or run command under a user that has no appropriate permissions leads to:

Cannot connect to the Docker daemon. Is the docker daemon running on this host?