[root@node4 ~]# docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 1b930d010525: Pull complete Digest: sha256:c3b4ada4687bbaa170745b3e4dd8ac3f194ca95b2d0518b417fb47e5879d9b5f Status: Downloaded newer image for hello-world:latest
Hello from Docker! This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.
To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/
For more examples and ideas, visit: https://docs.docker.com/get-started/
Docker的镜像管理
hub.docker.com
注册一个dockerhub账号
登录hub.docker.com
1 2 3 4 5 6 7 8 9
[root@node4 ~]# docker login docker.io Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one. Username: sunrisenan Password: WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store
[root@node4 ~]# docker search alpine NAME DESCRIPTION STARS OFFICIAL AUTOMATED alpine A minimal Docker image based on Alpine Linux… 5795 [OK] mhart/alpine-node Minimal Node.js built on Alpine Linux 444 anapsix/alpine-java Oracle Java 8 (and 7) with GLIBC 2.28 over A… 428 [OK] frolvlad/alpine-glibc Alpine Docker image with glibc (~12MB) 218 [OK] gliderlabs/alpine Image based on Alpine Linux will help you wi… 180 mvertes/alpine-mongo light MongoDB container 106 [OK] alpine/git A simple git container running in alpine li… 100 [OK] yobasystems/alpine-mariadb MariaDB running on Alpine Linux [docker] [am… 52 [OK] kiasaki/alpine-postgres PostgreSQL docker image based on Alpine Linux 45 [OK] alpine/socat Run socat command in alpine container 39 [OK] davidcaste/alpine-tomcat Apache Tomcat 7/8 using Oracle Java 7/8 with… 37 [OK] zzrot/alpine-caddy Caddy Server Docker Container running on Alp… 35 [OK] easypi/alpine-arm AlpineLinux for RaspberryPi 32 jfloff/alpine-python A small, more complete, Python Docker image … 28 [OK] byrnedo/alpine-curl Alpine linux with curl installed and set as … 27 [OK] hermsi/alpine-sshd Dockerize your OpenSSH-server with rsync and… 26 [OK] etopian/alpine-php-wordpress Alpine WordPress Nginx PHP-FPM WP-CLI 22 [OK] hermsi/alpine-fpm-php Dockerize your FPM PHP 7.4 upon a lightweigh… 21 [OK] bashell/alpine-bash Alpine Linux with /bin/bash as a default she… 14 [OK] zenika/alpine-chrome Chrome running in headless mode in a tiny Al… 14 [OK] davidcaste/alpine-java-unlimited-jce Oracle Java 8 (and 7) with GLIBC 2.21 over A… 13 [OK] tenstartups/alpine Alpine linux base docker image with useful p… 9 [OK] spotify/alpine Alpine image with `bash` and `curl`. 9 [OK] roribio16/alpine-sqs Dockerized ElasticMQ server + web UI over Al… 7 [OK] rawmind/alpine-traefik This image is the traefik base. It comes fro… 5 [OK]
在网站上搜索
下载一个镜像
直接下载
1 2 3 4 5 6 7
[root@node4 ~]# docker pull alpine Using default tag: latest latest: Pulling from library/alpine 89d9c30c1d48: Pull complete Digest: sha256:c19173c5ada610a5989151111163d28a67368362762534d8a8121ce95cf2bd5a Status: Downloaded newer image for alpine:latest docker.io/library/alpine:latest
[root@node4 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE alpine latest 965ea09ff2eb 2 weeks ago 5.55MB alpine 3.10.1 b7b28af77ffe 3 months ago 5.58MB hello-world latest fce289e99eb9 10 months ago 1.84kB
[root@node4 ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE alpine latest 965ea09ff2eb 2 weeks ago 5.55MB alpine 3.10.1 b7b28af77ffe 3 months ago 5.58MB hello-world latest fce289e99eb9 10 months ago 1.84kB
给镜像打标签
1 2 3 4 5 6 7
[root@node4 ~]# docker tag b7b28af77ffe sunrisenan/alpine:3.10.1 [root@node4 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE alpine latest 965ea09ff2eb 2 weeks ago 5.55MB alpine 3.10.1 b7b28af77ffe 3 months ago 5.58MB sunrisenan/alpine 3.10.1 b7b28af77ffe 3 months ago 5.58MB hello-world latest fce289e99eb9 10 months ago 1.84kB
推送镜像
1 2 3 4
[root@node4 ~]# docker push sunrisenan/alpine:3.10.1 The push refers to repository [docker.io/sunrisenan/alpine] 1bfeebd65323: Mounted from library/alpine 3.10.1: digest: sha256:57334c50959f26ce1ee025d08f136c2292c128f84e7b229d1b0da5dac89e9866 size: 528
给镜像再打一个标签
1 2 3 4 5 6 7 8
[root@node4 ~]# docker tag 965ea09ff2eb docker.io/sunrisenan/alpine:3.10.3 [root@node4 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE alpine latest 965ea09ff2eb 2 weeks ago 5.55MB sunrisenan/alpine 3.10.3 965ea09ff2eb 2 weeks ago 5.55MB alpine 3.10.1 b7b28af77ffe 3 months ago 5.58MB sunrisenan/alpine 3.10.1 b7b28af77ffe 3 months ago 5.58MB hello-world latest fce289e99eb9 10 months ago 1.84kB
再次推送镜像
1 2 3 4
[root@node4 ~]# docker push docker.io/sunrisenan/alpine:3.10.3 The push refers to repository [docker.io/sunrisenan/alpine] 77cae8ab23bf: Mounted from library/alpine 3.10.3: digest: sha256:e4355b66995c96b4b468159fc5c7e3540fcef961189ca13fee877798649f531a size: 528
[root@node4 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e63ab3e10bd6 hello-world "/hello" 2 hours ago Exited (0) 2 hours ago epic_bohr
[root@node4 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3d015bfe776a sunrisenan/alpine:3.10.3 "/bin/sleep 300" 4 minutes ago Up 4 minutes myalpine
查看所有容器
1 2 3 4
[root@node4 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7ecf398c77e4 hello-world "/hello" 6 seconds ago Exited (0) 5 seconds ago funny_wright c44608564744 sunrisenan/alpine:3.10.3 "/bin/sleep 300" About a minute ago Up About a minute myalpine
[root@node4 ~]# docker exec -it myalpine /bin/sh / # ps aux PID USER TIME COMMAND 1 root 0:00 /bin/sleep 300 7 root 0:00 /bin/sh 12 root 0:00 ps aux
/ # exit # 退出容器
docker attach 不推荐使用
停止/启动/重启容器
停止容器
1 2 3 4 5 6 7 8 9 10
[root@node4 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2d60b6eac204 sunrisenan/alpine:3.10.3 "/bin/sleep 300" 3 minutes ago Up 3 minutes myalpine [root@node4 ~]# docker stop myalpine myalpine
[root@node4 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2d60b6eac204 sunrisenan/alpine:3.10.3 "/bin/sleep 300" 4 minutes ago Exited (137) 11 seconds ago myalpine
启动容器
1 2 3 4 5
[root@node4 ~]# docker start myalpine myalpine [root@node4 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2d60b6eac204 sunrisenan/alpine:3.10.3 "/bin/sleep 300" 7 minutes ago Up 8 seconds myalpine
[root@node4 ~]# docker rm myalpine Error response from daemon: You cannot remove a running container db3335af09774651f55715b1da66ba930060b488ebdc7c4dc4ce67ce933dfc8e. Stop the container before attempting removal or force remove [root@node4 ~]# docker rm -f myalpine myalpine
如果容器还未终止,需要用docker rm -f 进行强制删除
删除已停止的容器
1
[root@node4 ~]# for i in `docker ps -a|grep -i exit|awk '{print $1}'`;do docker rm -f $i;done
删除所有的容器
1
[root@node4 ~]# for i in `docker ps -qa`;do docker rm -f $i;done
[root@node4 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 279bf6153576 sunrisenan/alpine:3.10.3 "/bin/sleep 300" 13 seconds ago Up 11 seconds myalpine
[root@node4 ~]# docker exec -it 279bf6153576 /bin/sh / # pwd / / # echo Hello > 1.txt / # ls 1.txt bin dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var / # exit
[root@node4 ~]# docker run hello-world 2>&1 >>/dev/null [root@node4 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2c71417a3b27 hello-world "/hello" 16 seconds ago Exited (0) 15 seconds ago goofy_goldstine
[root@node4 ~]# docker logs 2c71417a3b27
Hello from Docker! This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.
To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/
For more examples and ideas, visit: https://docs.docker.com/get-started/
[root@node4 ~]# curl 127.0.0.1:81 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p>
<p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p> </body> </html>
挂载数据卷
docker run -v 容器外的目录:容器内的目录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
[root@node4 ~]# mkdir html [root@node4 ~]# cd html/ [root@node4 html]# wget www.baidu.com -O index.html --2019-11-06 18:12:06-- http://www.baidu.com/ Resolving www.baidu.com (www.baidu.com)... 180.101.49.12, 180.101.49.11 Connecting to www.baidu.com (www.baidu.com)|180.101.49.12|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 2381 (2.3K) [text/html] Saving to: ‘index.html’
100%[===========================================================================================>] 2,381 --.-K/s in 0s
[root@node4 ~]# docker run --rm -it sunrisenan/nginx:v1.12.2 bash root@626e6ba6a358:/# curl bash: curl: command not found root@626e6ba6a358:/# tee /etc/apt/sources.list << EOF > deb http://mirrors.163.com/debian/ jessie main non-free contrib > deb http://mirrors.163.com/debian/ jessie-updates main non-free contrib > EOF deb http://mirrors.163.com/debian/ jessie main non-free contrib deb http://mirrors.163.com/debian/ jessie-updates main non-free contrib
[root@node4 ~]# docker run -d sunrisenan/nginx:v1.12.2 76f844e6057b6493a4a8933819ade7c29325ef17bc1fddc88bdf4c7ec60c620b [root@node4 ~]# [root@node4 ~]# docker ps -qa 76f844e6057b [root@node4 ~]# docker run -ti --rm --net=container:76f844e6057b sunrisenan/nginx:curl bash root@76f844e6057b:/# curl localhost <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p>
<p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p> </body> </html> root@76f844e6057b:/#