阿里云部署Docker(4)----容器的使用

时间:2022-01-19 21:48:17

通过上一节的学习,我们知道如何运行docker容器,我们运行了一个普通的,一个后台的,我们还学习了几个指令:

docker ps - Lists containers.
docker logs - Shows us the standard output of a container.
docker stop - Stops running containers.

我们还可以查看下docker的版本:

root@iZ28ikebrg6Z:~# docker version
Client version: 1.2.0
Client API version: 1.14
Go version (client): go1.3.1
Git commit (client): fa7b24f
OS/Arch (client): linux/amd64
Server version: 1.2.0
Server API version: 1.14
Go version (server): go1.3.1
Git commit (server): fa7b24f
root@iZ28ikebrg6Z:~#

docker是基于google公司的Go语言的。

如果不知道docker怎么用,或者忘记了的时候,直接输入docker,会给出用法提示

root@iZ28ikebrg6Z:~# docker
Usage: docker [OPTIONS] COMMAND [arg...]
-H=[unix:///var/run/docker.sock]: tcp://host:port to bind/connect to or unix://path/to/socket to use

A self-sufficient runtime for linux containers.

Commands:
attach Attach to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders from a container's filesystem to the host path
diff Inspect changes on a container's filesystem
events Get real time events from the server
export Stream the contents of a container as a tar archive
history Show the history of an image
images List images
import Create a new filesystem image from the contents of a tarball
info Display system-wide information
inspect Return low-level information on a container
kill Kill a running container
load Load an image from a tar archive
login Register or log in to a Docker registry server
logout Log out from a Docker registry server
logs Fetch the logs of a container
port Lookup the public-facing port that is NAT-ed to PRIVATE_PORT
pause Pause all processes within a container
ps List containers
pull Pull an image or a repository from a Docker registry server
push Push an image or a repository to a Docker registry server
restart Restart a running container
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save an image to a tar archive
search Search for an image on the Docker Hub
start Start a stopped container
stop Stop a running container
tag Tag an image into a repository
top Lookup the running processes of a container
unpause Unpause a paused container
version Show the Docker version information
wait Block until a container stops, then print its exit code

root@iZ28ikebrg6Z:~#

docker后面跟指令,

root@iZ28ikebrg6Z:~# docker attach

Usage: docker attach [OPTIONS] CONTAINER

Attach to a running container

--no-stdin=false Do not attach STDIN
--sig-proxy=true Proxy all received signals to the process (even in non-TTY mode). SIGCHLD, SIGKILL, and SIGSTOP are not proxied.
root@iZ28ikebrg6Z:~#

如果输入非法,总是会给出提示,告诉你怎么用。

Running a Web Application in Docker

利用Docker构建一个web程序

我们执行一个web应用。

root@iZ28ikebrg6Z:~# docker run -d -P training/weapp python app.py
Unable to find image 'training/weapp' locally
Pulling repository training/weapp
2014/10/16 11:01:56 Error: image training/weapp not found
root@iZ28ikebrg6Z:~# docker run -d -P training/webapp python app.py
Unable to find image 'training/webapp' locally
Pulling repository training/webapp
31fa814ba25a: Pulling dependent layers
511136ea3c5a: Download complete
f10ebce2c0e1: Download complete
82cdea7ab5b5: Download complete
5dbd9cb5a02f: Download complete
31fa814ba25a: Download complete
64523f641a05: Download complete
0e2afc9aad6e: Download complete
e8fc7643ceb1: Download complete
733b0e3dbcee: Download complete
a1feb043c441: Download complete
e12923494f6a: Download complete
a15f98c46748: Download complete
09792841c0b448a330a89ee656b4a16605d835f9d43bb7d6f80f204b6e8ed5b2
root@iZ28ikebrg6Z:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
09792841c0b4 training/webapp:latest "python app.py" 54 minutes ago Up 54 minutes 0.0.0.0:49153->5000/tcp compassionate_torvalds
root@iZ28ikebrg6Z:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
09792841c0b4 training/webapp:latest "python app.py" 56 minutes ago Up 56 minutes 0.0.0.0:49153->5000/tcp compassionate_torvalds
root@iZ28ikebrg6Z:~#
第一次敲错了。第二次它会去docker库下载,下载完了,ps查看一下。多了一行Ports。表示将本机的49153端口映射到compassionate_toralds容器的5000端口。

我用浏览器试一下。-d选项,我们已经讲过,是将程序后台化;-P,大写的P,是让docker来指定一个端口映射。这里是49153,你可以通过-p小写的来指定这种映射关系。

如下效果:

阿里云部署Docker(4)----容器的使用

好,可以访问。哈哈。

root@iZ28ikebrg6Z:~# docker run -d -p 5000:5000 training/webapp python app.py
aa47ba8e71c8512f228a96bf80da117b6c86fc03bdb17eef1c2db353a3e18493

好,再访问一下。

阿里云部署Docker(4)----容器的使用