docker容器之启动容器
1、如何运行容器和指定容器启动时执行的命令?
docker run 是启动容器的方法,可用三种方式指定容器启动时执行的命令:
1、CMD 指令。
2、ENDPOINT 指令。
3、在 docker run 命令行中指定。
2、如何让容器长期运行?
容器的生命周期依赖于启动时执行的命令,只要该命令不结束,容器也就不会退出。我们就可以通过执行一个长期运行的命令来保持容器的运行状态。
root@richardo-docker01:~# docker run -d centos /bin/bash -c "while true;do sleep 1;done"
a2a8d3b49ee5b51ee5be92da9b08ca57dd74d151837696578885298100cbdab1
root@richardo-docker01:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a2a8d3b49ee5 centos "/bin/bash -c 'while…" 6 seconds ago Up 3 seconds xenodochial_keller
741390e28eb4 registry:2 "/entrypoint.sh /etc…" 58 minutes ago Up 58 minutes 0.0.0.0:5000->5000/tcp naughty_noether
root@richardo-docker01:~# docker stop a2a8d3b49ee5
a2a8d3b49ee5
root@richardo-docker01:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
741390e28eb4 registry:2 "/entrypoint.sh /etc…" About an hour ago Up About an hour 0.0.0.0:5000->5000/tcp naughty_noether
3、容器如何运行后台服务?并且指定容器的名字?
root@richardo-docker01:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
entrypoint-image latest 18ed25a11432 6 hours ago 215MB
cmd-image latest 69028b7e9882 6 hours ago 215MB
my-image latest 1e0fcff0c90e 7 hours ago 1.23MB
qwx619766/my-image v1 1e0fcff0c90e 7 hours ago 1.23MB
centos-with-vim-dockerfile-3 latest f41e704ce73b 8 hours ago 272MB
centos-with-vim-dockerfile-2 latest fa86e8214b98 2 days ago 272MB
centos-with-vim-dockerfile latest e99684eeca8a 3 days ago 272MB
centos-with-vim latest 0ec55bc57a3d 3 days ago 272MB
busybox latest f0b02e9d092d 4 weeks ago 1.23MB
127.0.0.1:5000/qwx619766/httpd v1 3dd970e6b110 4 weeks ago 138MB
centos latest 0d120b6ccaa8 3 months ago 215MB
registry 2 2d4f4b5309b1 5 months ago 26.2MB
hello-world latest bf756fb1ae65 10 months ago 13.3kB
root@richardo-docker01:~# docker history 3dd970e6b110
IMAGE CREATED CREATED BY SIZE COMMENT
3dd970e6b110 4 weeks ago /bin/sh -c #(nop) CMD ["httpd-foreground"] 0B
<missing> 4 weeks ago /bin/sh -c #(nop) EXPOSE 80 0B
<missing> 4 weeks ago /bin/sh -c #(nop) COPY file:c432ff61c4993ecd… 138B
<missing> 4 weeks ago /bin/sh -c #(nop) STOPSIGNAL SIGWINCH 0B
<missing> 4 weeks ago /bin/sh -c set -eux; savedAptMark="$(apt-m… 60.9MB
<missing> 4 weeks ago /bin/sh -c #(nop) ENV HTTPD_PATCHES= 0B
<missing> 4 weeks ago /bin/sh -c #(nop) ENV HTTPD_SHA256=740eddf6… 0B
<missing> 4 weeks ago /bin/sh -c #(nop) ENV HTTPD_VERSION=2.4.46 0B
<missing> 4 weeks ago /bin/sh -c set -eux; apt-get update; apt-g… 7.38MB
<missing> 4 weeks ago /bin/sh -c #(nop) WORKDIR /usr/local/apache2 0B
<missing> 4 weeks ago /bin/sh -c mkdir -p "$HTTPD_PREFIX" && chow… 0B
<missing> 4 weeks ago /bin/sh -c #(nop) ENV PATH=/usr/local/apach… 0B
<missing> 4 weeks ago /bin/sh -c #(nop) ENV HTTPD_PREFIX=/usr/loc… 0B
<missing> 4 weeks ago /bin/sh -c #(nop) CMD ["bash"] 0B
<missing> 4 weeks ago /bin/sh -c #(nop) ADD file:0dc53e7886c35bc21… 69.2MB
root@richardo-docker01:~# docker run --name "my_http_server" -d 127.0.0.1:5000/qwx619766/httpd
Unable to find image '127.0.0.1:5000/qwx619766/httpd:latest' locally
docker: Error response from daemon: manifest for 127.0.0.1:5000/qwx619766/httpd:latest not found: manifest unknown: manifest unknown.
See 'docker run --help'.
root@richardo-docker01:~# docker run --name "my_http_server" -d 127.0.0.1:5000/qwx619766/httpd:v1
443224c14b81f334e1fd053a409dcbaa5d5cc6426d60a4e7638ee62b5cdef935
root@richardo-docker01:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
443224c14b81 127.0.0.1:5000/qwx619766/httpd:v1 "httpd-foreground" 11 seconds ago Up 9 seconds 80/tcp my_http_server
741390e28eb4 registry:2 "/entrypoint.sh /etc…" About an hour ago Up About an hour 0.0.0.0:5000->5000/tcp naughty_noether