Table of contents
exec
docker exec : execute a command in a running container
grammar
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Description of OPTIONS:
- -d : detached mode: run in background
- -i : keep STDIN open even if not attached
- -t : allocate a pseudo terminal
example
Execute the /root/runoob.sh script in the container in interactive mode in the container mynginx:
runoob@runoob:~$ docker exec -it mynginx /bin/sh /root/runoob.sh
http://www.runoob.com/
Open a terminal in interactive mode in the container mynginx:
runoob@runoob:~$ docker exec -i -t mynginx /bin/bash
root@b1a0703e41e7:/#
You can also use the docker ps -a command to view the running container, and then use the container ID to enter the container.
View the container IDs that are already running:
# docker ps -a
...
9df70f9a0714 openjdk "/usercode/script.sh…"
...
9df70f9a0714 in the first column is the container ID.
Execute bash on the specified container through the exec command:
# docker exec -it 9df70f9a0714 /bin/bash
create
docker create : create a new container without starting it
The usage is the same as docker run
grammar
docker create [OPTIONS] IMAGE [COMMAND] [ARG...]
Description of OPTIONS:
- -a stdin: Specifies the standard input and output content type, optional STDIN/STDOUT/STDERR three items;
- -d: Run the container in the background and return the container ID;
- -i: Run the container in interactive mode, usually used together with -t;
- -P: Random port mapping, the internal port of the container is randomly mapped to the port of the host
- -p: Specify port mapping in the format: host (host) port: container port
- -t: Reassign a pseudo-input terminal for the container, usually used together with -i;
- --name="nginx-lb": Specify a name for the container;
- --dns 8.8.8.8: Specify the DNS server used by the container, which is the same as the host by default;
- --dns-search example.com: Specifies the container DNS search domain name, which is the same as the host by default;
- -h "mars": specify the hostname of the container;
- -e username="ritchie": set environment variables;
- --env-file=[]: read environment variables from the specified file;
- --cpuset="0-2" or --cpuset="0,1,2": Bind the container to the specified CPU to run;
- -m : Set the maximum amount of memory used by the container;
- --net="bridge": Specify the network connection type of the container, support bridge/host/none/container: four types;
- --link=[]: add a link to another container;
- --expose=[]: open a port or a group of ports;
- --volume , -v: bind a volume
example
Create a container using the docker image nginx:latest and name the container myrunoob
runoob@runoob:~$ docker create --name myrunoob nginx:latest
09b93464c2f75b7b69f83d56a9cfc23ceb50a48a9db7652ee4c27e3e2cb1961f
pause/unpause
- docker pause : Pauses all processes in the container.
- docker unpause : Resume all processes in the container.
grammar
docker pause CONTAINER [CONTAINER...]
docker unpause CONTAINER [CONTAINER...]
example
Suspend the service of database container db01.
docker pause db01
The recovery database container db01 is serviced.
docker unpause db01
rm
docker rm : Remove one or more containers.
grammar
docker rm [OPTIONS] CONTAINER [CONTAINER...]
Description of OPTIONS:
- -f : Forcefully kill a running container via the SIGKILL signal.
- -l : Remove the network connection between containers, not the container itself.
- -v : Delete the volume associated with the container.
example
Forcibly delete containers db01, db02:
docker rm -f db01 db02
Remove the connection of container nginx01 to container db01, the connection name is db:
docker rm -l db
Delete the container nginx01, and delete the data volume mounted on the container:
docker rm -v nginx01
Remove all stopped containers:
docker rm $(docker ps -a -q)
kill
docker kill : kills a running container.
grammar
docker kill [OPTIONS] CONTAINER [CONTAINER...]
Description of OPTIONS:
-
-s : send a signal to the container
example
Kill the running container mynginx
runoob@runoob:~$ docker kill -s KILL mynginx
mynginx
start/stop/restart
- docker start : Start one or more containers that have been stopped
- docker stop : Stop a running container
- docker restart : restart the container
grammar
docker start [OPTIONS] CONTAINER [CONTAINER...]
docker stop [OPTIONS] CONTAINER [CONTAINER...]
docker restart [OPTIONS] CONTAINER [CONTAINER...]
example
Start the stopped container myrunoob
docker start myrunoob
Stop the running container myrunoob
docker stop myrunoob
Restart the container myrunoob
docker restart myrunoob
run
docker run : create a new container and run a command
grammar
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Description of OPTIONS:
- -a stdin: Specifies the standard input and output content type, optional STDIN/STDOUT/STDERR three items;
- -d: Run the container in the background and return the container ID;
- -i: Run the container in interactive mode, usually used together with -t;
- -P: Random port mapping, the internal port of the container is randomly mapped to the port of the host
- -p: Specify port mapping in the format: host (host) port: container port
- -t: Reassign a pseudo-input terminal for the container, usually used together with -i;
- --name="nginx-lb": Specify a name for the container;
- --dns 8.8.8.8: Specify the DNS server used by the container, which is the same as the host by default;
- --dns-search example.com: Specifies the container DNS search domain name, which is the same as the host by default;
- -h "mars": specify the hostname of the container;
- -e username="ritchie": set environment variables;
- --env-file=[]: read environment variables from the specified file;
- --cpuset="0-2" or --cpuset="0,1,2": Bind the container to the specified CPU to run;
- -m : Set the maximum amount of memory used by the container;
- --net="bridge": Specify the network connection type of the container, support bridge/host/none/container: four types;
- --link=[]: add a link to another container;
- --expose=[]: open a port or a group of ports;
- --volume , -v: bind a volume
example
Use the docker image nginx:latest to start a container in background mode, and name the container mynginx.
docker run --name mynginx -d nginx:latest
Use the image nginx:latest to start a container in background mode, and map port 80 of the container to a random port on the host.
docker run -P -d nginx:latest
Use the mirror nginx:latest to start a container in background mode, map port 80 of the container to port 80 of the host, and map the directory /data of the host to /data of the container.
docker run -p 80:80 -v /data:/data -d nginx:latest
Bind port 8080 of the container and map it to port 80 of the local host 127.0.0.1.
$ docker run -p 127.0.0.1:80:8080/tcp ubuntu bash
Use the image nginx:latest to start a container in interactive mode, and execute the /bin/bash command in the container. /bin/bash : After the image name is the command, here we hope to have an interactive shell, so /bin/bash is used.
runoob@runoob:~$ docker run -it nginx:latest /bin/bash
root@b8573233d675:/#
To exit the terminal, type exit directly :
root@ed09e4490c57:/# exit