Container Control Commands
Downloading Containers #
For example, to download an nginx container, you can write it as follows. The latest tag fetches the most recent version of the container image.
# docker pull NAME[:TAG]
$ docker pull nginx:latest
Running Containers #
Running containers typically uses the docker run command and has the following format. If you want to run an ubuntu:16.04 container, you can do so as follows.
# docker run [OPTION] IMAGE[:TAG] [COMMAND]
$ docker run -i -t ubuntu:16.04 /bin/bash
Running in Foreground #
The -t option allocates a TTY, and the -i option uses standard input/output. For example, you can create an Ubuntu 16.04 container with the -i and -t options and run its default command, /bin/bash. The same behavior is expected even if this command is not explicitly entered.
$ docker run -i -t ubuntu:16.04 bin/bash
Running as a Daemon (Detached) #
Run as a daemon using the -d option. Web server containers, among others, are typically run in this mode. For example, run an nginx container.
$ docker run -d -p 80:80 nginx
Assigning Container Names #
To manage containers via commands, assign a name using the --name option. If you don't specify a name, a random one will be assigned, but it can be difficult to manage, so be careful.
$ docker run -i -t --name my_ubuntu ubuntu:16.04 /bin/bash
Container Port Forwarding #
When creating, use the -p option. In the daemon execution example above, port 80 was exposed for the nginx container; to expose multiple ports, you can use multiple -p options.
$ docker run -d --name my_nginx -p 80:80 =p 3306:3306 nginx:latest
Stopping & Exiting Containers #
Input is typically done via the terminal within the container, and there are two ways: exit and Ctrl + P, Q. The exit command completely stops the container. The Ctrl + P, Q command leaves the container's current state as is and only detaches your connection.
# Stop
$ exit
If you want to switch the container to the background without stopping it, press Ctrl and then P and Q sequentially to exit.
# Exit
$ Ctrl + P,Q