How to run docker daemon
How to run docker daemon
How to run docker daemon
After successfully installing and starting Docker, the dockerd daemon runs with its default configuration. This topic shows how to customize the configuration, start the daemon manually, and troubleshoot and debug the daemon if you run into issues.
Start the daemon using operating system utilities
On a typical installation the Docker daemon is started by a system utility, not manually by a user. This makes it easier to automatically start Docker when the machine reboots.
The command to start Docker depends on your operating system. Check the correct page under Install Docker. To configure Docker to start automatically at system boot, see Configure Docker to start on boot.
Start the daemon manually
When you start Docker this way, it runs in the foreground and sends its logs directly to your terminal.
To stop Docker when you have started it manually, issue a Ctrl+C in your terminal.
Configure the Docker daemon
There are two ways to configure the Docker daemon:
You can use both of these options together as long as you don’t specify the same option both as a flag and in the JSON file. If that happens, the Docker daemon won’t start and prints an error message.
To configure the Docker daemon using a JSON file, create a file at /etc/docker/daemon.json on Linux systems, or C:\ProgramData\docker\config\daemon.json on Windows. On MacOS go to the whale in the taskbar > Preferences > Daemon > Advanced.
Here’s what the configuration file looks like:
You can also start the Docker daemon manually and configure it using flags. This can be useful for troubleshooting problems.
Here’s an example of how to manually start the Docker daemon, using the same configurations as above:
You can learn what configuration options are available in the dockerd reference docs, or by running:
Many specific configuration options are discussed throughout the Docker documentation. Some places to go next include:
Docker daemon directory
The Docker daemon persists all data in a single directory. This tracks everything related to Docker, including containers, images, volumes, service definition, and secrets.
By default this directory is:
You can configure the Docker daemon to use a different directory, using the data-root configuration option.
Since the state of a Docker daemon is kept on this directory, make sure you use a dedicated directory for each daemon. If two daemons share the same directory, for example, an NFS share, you are going to experience errors that are difficult to troubleshoot.
Troubleshoot the daemon
You can enable debugging on the daemon to learn about the runtime activity of the daemon and to aid in troubleshooting. If the daemon is completely non-responsive, you can also force a full stack trace of all threads to be added to the daemon log by sending the SIGUSR signal to the Docker daemon.
Troubleshoot conflicts between the daemon.json and startup scripts
If you use a daemon.json file and also pass options to the dockerd command manually or using start-up scripts, and these options conflict, Docker fails to start with an error such as:
If you see an error similar to this one and you are starting the daemon manually with flags, you may need to adjust your flags or the daemon.json to remove the conflict.
Note : If you see this specific error, continue to the next section for a workaround.
If you are starting Docker using your operating system’s init scripts, you may need to override the defaults in these scripts in ways that are specific to the operating system.
Use the hosts key in daemon.json with systemd
There are other times when you might need to configure systemd with Docker, such as configuring a HTTP or HTTPS proxy.
Run sudo systemctl daemon-reload before attempting to start Docker. If Docker starts successfully, it is now listening on the IP address specified in the hosts key of the daemon.json instead of a socket.
Important: Setting hosts in the daemon.json is not supported on Docker Desktop for Windows or Docker Desktop for Mac.
Out Of Memory Exceptions (OOME)
If your containers attempt to use more memory than the system has available, you may experience an Out Of Memory Exception (OOME) and a container, or the Docker daemon, might be killed by the kernel OOM killer. To prevent this from happening, ensure that your application runs on hosts with adequate memory and see Understand the risks of running out of memory.
The daemon logs may help you diagnose problems. The logs may be saved in one of a few locations, depending on the operating system configuration and the logging subsystem used:
/Library/Containers/com.docker.docker/Data/log/vm/dockerd.log
/Library/Containers/com.docker.docker/Data/log/vm/containerd.log
There are two ways to enable debugging. The recommended approach is to set the debug key to true in the daemon.json file. This method works for every Docker platform.
If the file is empty, add the following:
Send a HUP signal to the daemon to cause it to reload its configuration. On Linux hosts, use the following command.
On Windows hosts, restart Docker.
Force a stack trace to be logged
If the daemon is unresponsive, you can force a full stack trace to be logged by sending a SIGUSR1 signal to the daemon.
Linux:
Windows Server:
This forces a stack trace to be logged but does not stop the daemon. Daemon logs show the stack trace or the path to a file containing the stack trace if it was logged to a file.
The daemon continues operating after handling the SIGUSR1 signal and dumping the stack traces to the log. The stack traces can be used to determine the state of all goroutines and threads within the daemon.
View stack traces
The Docker daemon log can be viewed by using one of the following methods:
It is not possible to manually generate a stack trace on Docker Desktop for Mac or Docker Desktop for Windows. However, you can click the Docker taskbar icon and choose Troubleshoot to send information to Docker if you run into issues.
Look in the Docker logs for a message like the following:
The locations where Docker saves these stack traces and dumps depends on your operating system and configuration. You can sometimes get useful diagnostic information straight from the stack traces and dumps. Otherwise, you can provide this information to Docker for help diagnosing the problem.
Check whether Docker is running
The operating-system independent way to check whether Docker is running is to ask Docker, using the docker info command.
Everything You want to know about Docker Daemon
Everything You want to know about Docker Daemon
What is docker Daemon?
Start the daemon using operating system utilities
On a typical installation the Docker daemon is started by a system utility, not manually by a user. This makes it easier to automatically start Docker when the machine reboots.
Configure Docker to start on boot
Most current Linux distributions (RHEL, CentOS, Fedora, Ubuntu 16.04 and higher) use systemd to manage which services start when the system boots. Ubuntu 14.10 and below use upstart.
To disable this behavior, use disable instead.
Start the daemon manually
If you don’t want to use a system utility to manage the Docker daemon, or just want to test things out, you can manually run it using the dockerd command. You may need to use sudo, depending on your operating system configuration.
When you start Docker this way, it runs in the foreground and sends its logs directly to your terminal.
To stop Docker when you have started it manually, issue a Ctrl+C in your terminal.
Configure the Docker daemon
There are two ways to configure the Docker daemon:
You can use both of these options together as long as you don’t specify the same option both as a flag and in the JSON file. If that happens, the Docker daemon won’t start and prints an error message.
To configure the Docker daemon using a JSON file, create a file at /etc/docker/daemon.json on Linux systems, or C:\ProgramData\docker\config\daemon.json on Windows. On MacOS go to the whale in the taskbar > Preferences > Daemon > Advanced, or locate
Here’s what the configuration file looks like:
With this configuration the Docker daemon runs in debug mode, uses TLS, and listens for traffic routed to 192.168.59.3 on port 2376. You can learn what configuration options are available in the dockerd reference docs
You can also start the Docker daemon manually and configure it using flags. This can be useful for troubleshooting problems.
Here’s an example of how to manually start the Docker daemon, using the same configurations as above:
You can learn what configuration options are available in the dockerd reference docs, or by running:
Docker daemon directory
The Docker daemon persists all data in a single directory. This tracks everything related to Docker, including containers, images, volumes, service definition, and secrets.
By default this directory is:
/var/lib/docker on Linux. C:\ProgramData\docker on Windows.
You can configure the Docker daemon to use a different directory, using the data-root configuration option.
Since the state of a Docker daemon is kept on this directory, make sure you use a dedicated directory for each daemon. If two daemons share the same directory, for example, an NFS share, you are going to experience errors that are difficult to troubleshoot.
Troubleshoot the daemon
You can enable debugging on the daemon to learn about the runtime activity of the daemon and to aid in troubleshooting. If the daemon is completely non-responsive, you can also force a full stack trace of all threads to be added to the daemon log by sending the SIGUSR signal to the Docker daemon.
Enable debugging
There are two ways to enable debugging. The recommended approach is to set the debug key to true in the daemon.json file. This method works for every Docker platform.
Edit the daemon.json file, which is usually located in /etc/docker/. You may need to create this file, if it does not yet exist. On macOS or Windows, do not edit the file directly. Instead, go to Preferences / Daemon / Advanced.
If the file is empty, add the following:
If the file already contains JSON, just add the key “debug”: true, being careful to add a comma to the end of the line if it is not the last line before the closing bracket. Also verify that if the log-level key is set, it is set to either info or debug. info is the default, and possible values are debug, info, warn, error, fatal.
How do we start the daemon?
Running docker info from an elevated PowerShell prompt shows this error:
error during connect: Get http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.27/info: open //./pipe/docker_engine: The system cannot find the file specified. In the default daemon configuration on Windows, the docker client must be run elevated to connect. This error may also indicate that the docker daemon is not running. (Emphasis added)
How do we run the Docker daemon from the command line? If that is not possible, how do we run it without restarting Windows? If that is not possible, how do we start it at all?
Environment info
What we have tried
Command «daemon» is deprecated, and will be removed in Docker 1.16. Please run dockerd directly. exec: «dockerd»: executable file not found in %PATH%
Enabling Hyper-V and restarting the computer.
4 Answers 4
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
If you’re using Docker for Windows, Then simply start the desktop app installed in C:\Program Files\Docker\Docker\Docker Desktop.exe
If you have installed docker on Windows 10 Pro with Hyper-V enabled and still you are not able to run Docker on Windows 10, then as the error suggests that your docker daemon is not started. Follow following steps it helped me to start docker successfully
Docker daemon on Windows Server run as windows service. This Docker service may not be running on your machine. Follow below steps
Docker run reference
This page details how to use the docker run command to define the container’s resources at runtime.
General form
The basic docker run command takes this form:
The docker run command must specify an IMAGE to derive the container from. An image developer can define image defaults related to:
With the docker run [OPTIONS] an operator can add to or override the image defaults set by a developer. And, additionally, operators can override nearly all the defaults set by the Docker runtime itself. The operator’s ability to override image and Docker runtime defaults is why run has more options than any other docker command.
Operator exclusive options
Only the operator (the person executing docker run ) can set the following options.
Detached vs foreground
When starting a Docker container, you must first decide if you want to run the container in the background in a “detached” mode or in the default foreground mode:
Detached (-d)
Do not pass a service x start command to a detached container. For example, this command attempts to start the nginx service.
This succeeds in starting the nginx service inside the container. However, it fails the detached container paradigm in that, the root process ( service nginx start ) returns and the detached container stops as designed. As a result, the nginx service is started but could not be used. Instead, to start a process such as the nginx web server do the following:
To do input/output with a detached container use network connections or shared volumes. These are required because the container is no longer listening to the command line where docker run was run.
To reattach to a detached container, use docker attach command.
Foreground
A process running as PID 1 inside a container is treated specially by Linux: it ignores any signal with the default action. As a result, the process will not terminate on SIGINT or SIGTERM unless it is coded to do so.
Container identification
Name (—name)
The operator can identify a container in three ways:
Identifier type | Example value |
---|---|
UUID long identifier | “f78375b1c487e03c9438c729345e54db9d20cfa2ac1fc3494b6eb60872e74778” |
UUID short identifier | “f78375b1c487” |
Name | “evil_ptolemy” |
Containers on the default bridge network must be linked to communicate by name.
PID equivalent
Finally, to help with automation, you can have Docker write the container ID out to a file of your choosing. This is similar to how some programs might write out their process ID to a file (you’ve seen them as PID files):
Image[:tag]
Image[@digest]
Images using the v2 or later image format have a content-addressable identifier called a digest. As long as the input used to generate the image is unchanged, the digest value is predictable and referenceable.
The following example runs a container from the alpine image with the sha256:9cacb71397b640eca97488cf08582ae4e4068513101088e9f96c9814bfda95e0 digest:
PID settings (—pid)
By default, all containers have the PID namespace enabled.
PID namespace provides separation of processes. The PID Namespace removes the view of the system processes, and allows process ids to be reused including pid 1.
Example: run htop inside a container
Create this Dockerfile:
Build the Dockerfile and tag the image as myhtop :
Use the following command to run htop inside a container:
Joining another container’s pid namespace can be used for debugging that container.
Example
Start a container running a redis server:
Debug the redis container by running another container that has strace in it:
UTS settings (—uts)
You may wish to share the UTS namespace with the host if you would like the hostname of the container to change as the hostname of the host changes. A more advanced use case would be changing the host’s hostname from a container.
IPC settings (—ipc)
The following values are accepted:
Value | Description |
---|---|
”” | Use daemon’s default. |
“none” | Own private IPC namespace, with /dev/shm not mounted. |
“private” | Own private IPC namespace. |
“shareable” | Own private IPC namespace, with a possibility to share it with other containers. |
“container: « | Join another (“shareable”) container’s IPC namespace. |
“host” | Use the host system’s IPC namespace. |
IPC (POSIX/SysV IPC) namespace provides separation of named shared memory segments, semaphores and message queues.
Shared memory segments are used to accelerate inter-process communication at memory speed, rather than through pipes or through the network stack. Shared memory is commonly used by databases and custom-built (typically C/OpenMPI, C++/using boost libraries) high performance applications for scientific computing and financial services industries. If these types of applications are broken into multiple containers, you might need to share the IPC mechanisms of the containers, using «shareable» mode for the main (i.e. “donor”) container, and «container: » for other containers.
Network settings
Publishing ports and linking to other containers only works with the default (bridge). The linking feature is a legacy feature. You should always prefer using Docker network drivers over linking.
Network | Description |
---|---|
none | No networking in the container. |
bridge (default) | Connect the container to the bridge via veth interfaces. |
host | Use the host’s network stack inside the container. |
container: | Use the network stack of another container, specified via its name or id. |
NETWORK | Connects the container to a user created network (using docker network create command) |
Network: none
With the network is none a container will not have access to any external routes. The container will still have a loopback interface enabled in the container but it does not have any routes to external traffic.
Network: bridge
Containers can communicate via their IP addresses by default. To communicate by name, they must be linked.
Network: host
Compared to the default bridge mode, the host mode gives significantly better networking performance since it uses the host’s native networking stack whereas the bridge has to go through one level of virtualization through the docker daemon. It is recommended to run containers in this mode when their networking performance is critical, for example, a production Load Balancer or a High Performance Web Server.
—network=»host» gives the container full access to local system services such as D-bus and is therefore considered insecure.
Network: container
Example running a Redis container with Redis binding to localhost then running the redis-cli command and connecting to the Redis server over the localhost interface.
User-defined network
You can create a network using a Docker network driver or an external network driver plugin. You can connect multiple containers to the same network. Once connected to a user-defined network, the containers can communicate easily using only another container’s IP address or name.
For overlay networks or custom plugins that support multi-host connectivity, containers connected to the same multi-host network but launched from different Engines can also communicate in this way.
The following example creates a network using the built-in bridge network driver and running a container in the created network
Managing /etc/hosts
If a container is connected to the default bridge network and linked with other containers, then the container’s /etc/hosts file is updated with the linked container’s name.
Since Docker may live update the container’s /etc/hosts file, there may be situations when processes inside the container can end up reading an empty or incomplete /etc/hosts file. In most cases, retrying the read again should fix the problem.
Restart policies (—restart)
Docker supports the following restart policies:
Policy | Result |
---|---|
no | Do not automatically restart the container when it exits. This is the default. |
on-failure[:max-retries] | Restart only if the container exits with a non-zero exit status. Optionally, limit the number of restart retries the Docker daemon attempts. |
always | Always restart the container regardless of the exit status. When you specify always, the Docker daemon will try to restart the container indefinitely. The container will also always start on daemon startup, regardless of the current state of the container. |
unless-stopped | Always restart the container regardless of the exit status, including on daemon startup, except if the container was put into a stopped state before the Docker daemon was stopped. |
If a container is successfully restarted (the container is started and runs for at least 10 seconds), the delay is reset to its default value of 100 ms.
Or, to get the last time the container was (re)started;
Examples
This will run the redis container with a restart policy of always so that if the container exits, Docker will restart it.
This will run the redis container with a restart policy of on-failure and a maximum restart count of 10. If the redis container exits with a non-zero exit status more than 10 times in a row Docker will abort trying to restart the container. Providing a maximum restart limit is only valid for the on-failure policy.
Exit Status
The exit code from docker run gives information about why the container failed to run or why it exited. When docker run exits with a non-zero code, the exit codes follow the chroot standard, see below:
125 if the error is with Docker daemon itself
126 if the contained command cannot be invoked
127 if the contained command cannot be found
Exit code of contained command otherwise
Clean up (—rm)
Security configuration
Option | Description |
---|---|
—security-opt=»label=user:USER» | Set the label user for the container |
—security-opt=»label=role:ROLE» | Set the label role for the container |
—security-opt=»label=type:TYPE» | Set the label type for the container |
—security-opt=»label=level:LEVEL» | Set the label level for the container |
—security-opt=»label=disable» | Turn off label confinement for the container |
—security-opt=»apparmor=PROFILE» | Set the apparmor profile to be applied to the container |
—security-opt=»no-new-privileges:true» | Disable container processes from gaining new privileges |
—security-opt=»seccomp=unconfined» | Turn off seccomp confinement for the container |
—security-opt=»seccomp=profile.json» | White-listed syscalls seccomp Json file to be used as a seccomp filter |
Automatic translation of MLS labels is not currently supported.
If you want a tighter security policy on the processes within a container, you can specify an alternate type for the container. You could run a container that is only allowed to listen on Apache ports by executing the following command:
You would have to write policy defining a svirt_apache_t type.
If you want to prevent your container processes from gaining additional privileges, you can execute the following command:
This means that commands that raise privileges such as su or sudo will no longer work. It also causes any seccomp filters to be applied later, after privileges have been dropped which may mean you can have a more restrictive set of filters. For more details, see the kernel documentation.
Specify an init process
The default init process used is the first docker-init executable found in the system path of the Docker daemon process. This docker-init binary, included in the default installation, is backed by tini.
Specify custom cgroups
Runtime constraints on resources
The operator can also adjust the performance parameters of the container:
User memory constraints
We have four ways to set user memory usage:
Always set the memory reservation value below the hard limit, otherwise the hard limit takes precedence. A reservation of 0 is the same as setting no reservation. By default (without reservation set), memory reservation is the same as the hard memory limit.
Memory reservation is a soft-limit feature and does not guarantee the limit won’t be exceeded. Instead, the feature attempts to ensure that, when memory is heavily contended for, memory is allocated based on the reservation hints/setup.
Under this configuration, when the container consumes memory more than 200M and less than 500M, the next system memory reclaim attempts to shrink container memory below 200M.
The following example set memory reservation to 1G without a hard memory limit.
The container can use as much memory as it needs. The memory reservation setting ensures the container doesn’t consume too much memory for long time, because every memory reclaim shrinks the container’s consumption to the reservation.
The following example limits the memory to 100M and disables the OOM killer for this container:
The following example, illustrates a dangerous way to use the flag:
Kernel memory constraints
Kernel memory is fundamentally different than user memory as kernel memory can’t be swapped out. The inability to swap makes it possible for the container to block system services by consuming too much kernel memory. Kernel memory includes:
You can setup kernel memory limit to constrain these kinds of memory. For example, every process consumes some stack pages. By limiting kernel memory, you can prevent new processes from being created when the kernel memory usage is too high.
Kernel memory is never completely independent of user memory. Instead, you limit kernel memory in the context of the user memory limit. Assume “U” is the user memory limit and “K” the kernel limit. There are three possible ways to set limits:
We set memory and kernel memory, so the processes in the container can use 500M memory in total, in this 500M memory, it can be 50M kernel memory tops.
We set kernel memory without -m, so the processes in the container can use as much memory as they want, but they can only use 50M kernel memory.
Swappiness constraint
For example, you can set:
CPU share constraint
By default, all containers get the same proportion of CPU cycles. This proportion can be modified by changing the container’s CPU share weighting relative to the weighting of all other running containers.
The proportion will only apply when CPU-intensive processes are running. When tasks in one container are idle, other containers can use the left-over CPU time. The actual amount of CPU time will vary depending on the number of containers running on the system.
For example, consider three containers, one has a cpu-share of 1024 and two others have a cpu-share setting of 512. When processes in all three containers attempt to use 100% of CPU, the first container would receive 50% of the total CPU time. If you add a fourth container with a cpu-share of 1024, the first container only gets 33% of the CPU. The remaining containers receive 16.5%, 16.5% and 33% of the CPU.
On a multi-core system, the shares of CPU time are distributed over all CPU cores. Even if a container is limited to less than 100% of CPU time, it can use 100% of each individual CPU core.
CPU period constraint
If there is 1 CPU, this means the container can get 50% CPU worth of run-time every 50ms.
Cpuset constraint
We can set cpus in which to allow execution for containers.
This means processes in container can be executed on cpu 1 and cpu 3.
This means processes in container can be executed on cpu 0, cpu 1 and cpu 2.
We can set mems in which to allow execution for containers. Only effective on NUMA systems.
This example restricts the processes in the container to only use memory from memory nodes 1 and 3.
This example restricts the processes in the container to only use memory from memory nodes 0, 1 and 2.
CPU quota constraint
Block IO bandwidth (Blkio) constraint
The blkio weight setting is only available for direct IO. Buffered IO is not currently supported.
If you do block IO in the two containers at the same time, by, for example:
You’ll find that the proportion of time is the same as the proportion of blkio weights of the two containers.
Additional groups
By default, the docker container process runs with the supplementary groups looked up for the specified user. If one wants to add more to that list of groups, then one can use this flag:
Runtime privilege and Linux capabilities
By default, Docker containers are “unprivileged” and cannot, for example, run a Docker daemon inside a Docker container. This is because by default a container is not allowed to access any devices, but a “privileged” container is given access to all devices (see the documentation on cgroups devices).
Capability Key | Capability Description |
---|---|
AUDIT_WRITE | Write records to kernel auditing log. |
CHOWN | Make arbitrary changes to file UIDs and GIDs (see chown(2)). |
DAC_OVERRIDE | Bypass file read, write, and execute permission checks. |
FOWNER | Bypass permission checks on operations that normally require the file system UID of the process to match the UID of the file. |
FSETID | Don’t clear set-user-ID and set-group-ID permission bits when a file is modified. |
KILL | Bypass permission checks for sending signals. |
MKNOD | Create special files using mknod(2). |
NET_BIND_SERVICE | Bind a socket to internet domain privileged ports (port numbers less than 1024). |
NET_RAW | Use RAW and PACKET sockets. |
SETFCAP | Set file capabilities. |
SETGID | Make arbitrary manipulations of process GIDs and supplementary GID list. |
SETPCAP | Modify process capabilities. |
SETUID | Make arbitrary manipulations of process UIDs. |
SYS_CHROOT | Use chroot(2), change root directory. |
The next table shows the capabilities which are not granted by default and may be added.
Capability Key | Capability Description |
---|---|
AUDIT_CONTROL | Enable and disable kernel auditing; change auditing filter rules; retrieve auditing status and filtering rules. |
AUDIT_READ | Allow reading the audit log via multicast netlink socket. |
BLOCK_SUSPEND | Allow preventing system suspends. |
BPF | Allow creating BPF maps, loading BPF Type Format (BTF) data, retrieve JITed code of BPF programs, and more. |
CHECKPOINT_RESTORE | Allow checkpoint/restore related operations. Introduced in kernel 5.9. |
DAC_READ_SEARCH | Bypass file read permission checks and directory read and execute permission checks. |
IPC_LOCK | Lock memory (mlock(2), mlockall(2), mmap(2), shmctl(2)). |
IPC_OWNER | Bypass permission checks for operations on System V IPC objects. |
LEASE | Establish leases on arbitrary files (see fcntl(2)). |
LINUX_IMMUTABLE | Set the FS_APPEND_FL and FS_IMMUTABLE_FL i-node flags. |
MAC_ADMIN | Allow MAC configuration or state changes. Implemented for the Smack LSM. |
MAC_OVERRIDE | Override Mandatory Access Control (MAC). Implemented for the Smack Linux Security Module (LSM). |
NET_ADMIN | Perform various network-related operations. |
NET_BROADCAST | Make socket broadcasts, and listen to multicasts. |
PERFMON | Allow system performance and observability privileged operations using perf_events, i915_perf and other kernel subsystems |
SYS_ADMIN | Perform a range of system administration operations. |
SYS_BOOT | Use reboot(2) and kexec_load(2), reboot and load a new kernel for later execution. |
SYS_MODULE | Load and unload kernel modules. |
SYS_NICE | Raise process nice value (nice(2), setpriority(2)) and change the nice value for arbitrary processes. |
SYS_PACCT | Use acct(2), switch process accounting on or off. |
SYS_PTRACE | Trace arbitrary processes using ptrace(2). |
SYS_RAWIO | Perform I/O port operations (iopl(2) and ioperm(2)). |
SYS_RESOURCE | Override resource Limits. |
SYS_TIME | Set system clock (settimeofday(2), stime(2), adjtimex(2)); set real-time (hardware) clock. |
SYS_TTY_CONFIG | Use vhangup(2); employ various privileged ioctl(2) operations on virtual terminals. |
SYSLOG | Perform privileged syslog(2) operations. |
WAKE_ALARM | Trigger something that will wake up the system. |
The default seccomp profile will adjust to the selected capabilities, in order to allow use of facilities allowed by the capabilities, so you should not have to adjust this.
Logging drivers (—log-driver)
The docker logs command is available only for the json-file and journald logging drivers. For detailed information on working with logging drivers, see Configure logging drivers.
Overriding Dockerfile image defaults
When a developer builds an image from a Dockerfile or when she commits it, the developer can set a number of default parameters that take effect when the image starts up as a container.
CMD (default command or options)
Recall the optional COMMAND in the Docker commandline:
ENTRYPOINT (default command to execute at runtime)
or two examples of how to pass more parameters to that ENTRYPOINT:
You can reset a containers entrypoint by passing an empty string, for example:
EXPOSE (incoming ports)
The following run command options work with container networking:
ENV (environment variables)
Docker automatically sets some environment variables when creating a Linux container. Docker does not set any environment variables when creating a Windows container.
The following environment variables are set for Linux containers:
Variable | Value |
---|---|
HOME | Set based on the value of USER |
HOSTNAME | The hostname associated with the container |
PATH | Includes popular directories, such as /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin |
TERM | xterm if the container is allocated a pseudo-TTY |
docker run
Estimated reading time: 42 minutes
Run a command in a new container
Usage
Refer to the options section for an overview of available OPTIONS for this command.
Description
The docker run command can be used in combination with docker commit to change the command that a container runs. There is additional detailed information about docker run in the Docker run reference.
For information on connecting a container to a network, see the “Docker network overview”.
For example uses of this command, refer to the examples section below.
Options
Examples
Capture container ID (—cidfile)
This will create a container and print test to the console. The cidfile flag makes Docker attempt to create a new file and write the container ID to it. If the file exists already, Docker will return an error. Docker will close this file when docker run exits.
Full container capabilities (—privileged)
Set working directory (-w)
Set storage driver options per container
Mount tmpfs (—tmpfs)
When the host directory of a bind-mounted volume doesn’t exist, Docker will automatically create this directory on the host for you. In the example above, Docker will create the /doesnt/exist folder before starting your container.
By bind-mounting the docker unix socket and statically linked docker binary (refer to get the linux binary), you give the container the full access to create and manipulate the host’s Docker daemon.
On Windows, the paths must be specified using Windows-style semantics.
The following examples will fail when using Windows-based containers, as the destination of a volume or bind mount inside the container must be one of: a non-existing or empty directory; or a drive other than C:. Further, the source of a bind mount must be a local directory, not a file.
For in-depth information about volumes, refer to manage data in containers
This binds port 8080 of the container to TCP port 80 on 127.0.0.1 of the host machine. You can also specify udp and sctp ports. The Docker User Guide explains in detail how to manipulate ports in Docker.
This exposes port 80 of the container without publishing the port to the host system’s interfaces.
Set the pull policy (—pull)
Value | Description |
---|---|
missing (default) | Pull the image if it was not found in the image cache, or use the cached image otherwise. |
never | Do not pull the image, even if it’s missing, and produce an error if the image does not exist in the image cache. |
always | Always perform a pull before creating the container. |
When creating (and running) a container from an image, the daemon checks if the image exists in the local image cache. If the image is missing, an error is returned to the cli, allowing it to initiate a pull.
The default ( missing ) is to only pull the image if it is not present in the daemon’s image cache. This default allows you to run images that only exist locally (for example, images you built from a Dockerfile, but that have not been pushed to a registry), and reduces networking.
The always option always initiates a pull before creating the container. This option makes sure the image is up-to-date, and prevents you from using outdated images, but may not be suitable in situations where you want to test a locally built image before pushing (as pulling the image overwrites the existing image in the image cache).
The never option disables (implicit) pulling images when creating containers, and only uses images that are available in the image cache. If the specified image is not found, an error is produced, and the container is not created. This option is useful in situations where networking is not available, or to prevent images from being pulled implicitly when creating containers.
You can define the variable and its value when running the container:
You can also use variables that you’ve exported to your local environment:
When running the command, the Docker CLI client checks the value the variable has in your local environment and passes it to the container. If no = is provided and that variable is not exported in your local environment, the variable won’t be set in the container.
You can also load the environment variables from a file. This file should use the syntax =value (which sets the variable to the given value) or (which takes the value from the local environment), and # for comments.
A label is a key=value pair that applies metadata to a container. To label a container with two labels:
The key=value must be unique to avoid overwriting the label value. If you specify labels with identical keys but different values, each subsequent value overwrites the previous. Docker uses the last key=value you supply.
The label-file format is similar to the format for loading environment variables. (Unlike environment variables, labels are not visible to processes running inside a container.) The following example illustrates a label-file format:
Connect a container to a network (—network)
If you want to add a running container to a network use the docker network connect subcommand.
You can connect multiple containers to the same network. Once connected, the containers can communicate easily using only another container’s IP address or name. For overlay networks or custom plugins that support multi-host connectivity, containers connected to the same multi-host network but launched from different Engines can also communicate in this way.
Service discovery is unavailable on the default bridge network. Containers can communicate via their IP addresses by default. To communicate by name, they must be linked.
You can disconnect a container from a network using the docker network disconnect command.
Mount volumes from container (—volumes-from)
Labeling systems like SELinux require that proper labels are placed on volume content mounted into a container. Without a label, the security system might prevent the processes running inside the container from using the content. By default, Docker does not change the labels set by the OS.
To change the label in the container context, you can add either of two suffixes :z or :Z to the volume mount. These suffixes tell Docker to relabel file objects on the shared volumes. The z option tells Docker that two containers share the volume content. As a result, Docker labels the content with a shared content label. Shared volume labels allow all containers to read/write content. The Z option tells Docker to label the content with a private unshared label. Only the current container can use a private volume.
Attach to STDIN/STDOUT/STDERR (-a)
Add host device to container (—device)
If this option is specified for a process-isolated Windows container, all devices that implement the requested device interface class GUID are made available in the container. For example, the command below makes all COM ports on the host visible in the container.
Using dynamically created devices (—device-cgroup-rule)
Devices available to a container are assigned at creation time. The assigned devices will both be added to the cgroup.allow file and created into the container once it is run. This poses a problem when a new device needs to be added to running container.
One of the solutions is to add a more permissive rule to a container allowing it access to a wider range of devices. For example, supposing our container needs access to a character device with major 42 and any number of minor number (added as new devices appear), the following rule would be added:
Then, a user could ask udev to execute a script that would docker exec my-container mknod newDevX c 42 the required device when it is added.
Note: initially present devices still need to be explicitly added to the docker run / docker create command.
Access an NVIDIA GPU
Use the device option to specify GPUs. The example below exposes a specific GPU.
The example below exposes the first and third GPUs.
Restart policies (—restart)
Policy | Result |
---|---|
no | Do not automatically restart the container when it exits. This is the default. |
on-failure[:max-retries] | Restart only if the container exits with a non-zero exit status. Optionally, limit the number of restart retries the Docker daemon attempts. |
unless-stopped | Restart the container unless it is explicitly stopped or Docker itself is stopped or restarted. |
always | Always restart the container regardless of the exit status. When you specify always, the Docker daemon will try to restart the container indefinitely. The container will also always start on daemon startup, regardless of the current state of the container. |
This will run the redis container with a restart policy of always so that if the container exits, Docker will restart it.
More detailed information on restart policies can be found in the Restart Policies (—restart) section of the Docker run reference page.
Add entries to container hosts file (—add-host)
The flags you pass to ip addr show depend on whether you are using IPv4 or IPv6 networking in your containers. Use the following flags for IPv4 address retrieval for a network device named eth0 :
Set ulimits in container (—ulimit)
The values are sent to the appropriate syscall as they are set. Docker doesn’t perform any byte conversion. Take this into account when setting the values.
For nproc usage
Be careful setting nproc with the ulimit flag as nproc is designed by Linux to set the maximum number of processes available to a user, not to a container. For example, start four containers with daemon user:
The 4th container fails and reports “[8] System error: resource temporarily unavailable” error. This fails because the caller set nproc=3 resulting in the first three containers using up the three processes quota set for the daemon user.
Stop container with signal (—stop-signal)
The default is SIGTERM if not specified.
Optional security options (—security-opt)
Stop container with timeout (—stop-timeout)
The default is determined by the daemon, and is 10 seconds for Linux containers, and 30 seconds for Windows containers.
Specify isolation technology for container (—isolation)
On Windows server, assuming the default configuration, these commands are equivalent and result in process isolation:
On Windows, this will affect containers differently depending on what type of isolation is used.
With process isolation, Windows will report the full memory of the host system, not the limit to applications running inside the container
With hyperv isolation, Windows will create a utility VM that is big enough to hold the memory limit, plus the minimal OS needed to host the container. That size is reported as “Total Physical Memory.”
Configure namespaced kernel parameters (sysctls) at runtime
Not all sysctls are namespaced. Docker does not support changing sysctls inside of a container that also modify the host system. As the kernel evolves we expect to see more sysctls become namespaced.