Docker是一个开源的应用容器引擎,它让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到安装了任何 Linux 发行版本的机器上。Docker基于LXC来实现类似VM的功能,可以在更有限的硬件资源上提供给用户更多的计算资源。与同VM等虚拟化的方式不同,LXC不属于全虚拟化、部分虚拟化或半虚拟化中的任何一个分类,而是一个操作系统级虚拟化。
[root@pdai ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest ccc6e87d482b 4 weeks ago 64.2MB hello-world latest fce289e99eb9 13 months ago 1.84kB
[root@pdai ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@pdai ~]# docker search mysql NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 9132 [OK] mariadb MariaDB is a community-developed fork of MyS… 3233 [OK] mysql/mysql-server Optimized MySQL Server Docker images. Create… 676 [OK] centos/mysql-57-centos7 MySQL 5.7 SQL database server 68 mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 62 centurylink/mysql Image containing mysql. Optimized to be link… 61 [OK] deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r… 41 [OK] bitnami/mysql Bitnami MySQL Docker Image 35 [OK] tutum/mysql Base docker image to run a MySQL database se… 34 schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup… 29 [OK] prom/mysqld-exporter 26 [OK] linuxserver/mysql A Mysql container, brought to you by LinuxSe… 24 centos/mysql-56-centos7 MySQL 5.6 SQL database server 19 circleci/mysql MySQL is a widely used, open-source relation… 18 mysql/mysql-router MySQL Router provides transparent routing be… 14 arey/mysql-client Run a MySQL client from a docker container 13 [OK] databack/mysql-backup Back up mysql databases to... anywhere! 10 openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 image… 6 fradelg/mysql-cron-backup MySQL/MariaDB database backup using cron tas… 5 [OK] genschsa/mysql-employees MySQL Employee Sample Database 4 [OK] devilbox/mysql Retagged MySQL, MariaDB and PerconaDB offici… 2 ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 2 [OK] jelastic/mysql An image of the MySQL database server mainta… 1 monasca/mysql-init A minimal decoupled init container for mysql 0 widdpim/mysql-client Dockerized MySQL Client (5.7) including Curl… 0 [OK]
[root@pdai ~]# docker pull mysql Using default tag: latest latest: Pulling from library/mysql 619014d83c02: Pull complete 9ced578c3a5f: Pull complete 731f6e13d8ea: Pull complete 3c183de42679: Pull complete 6de69b5c2f3c: Pull complete 00f0a4086406: Pull complete 84d93aea836d: Pull complete f18efbfd8d76: Pull complete 012b302865d1: Pull complete fe16fd240f59: Pull complete ca3e793e545e: Pull complete 51d0f2cb2610: Pull complete Digest: sha256:6d0741319b6a2ae22c384a97f4bbee411b01e75f6284af0cce339fee83d7e314 Status: Downloaded newer image for mysql:latest docker.io/library/mysql:latest [root@pdai ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql latest 791b6e40940c 2 weeks ago 465MB ubuntu latest ccc6e87d482b 4 weeks ago 64.2MB hello-world latest fce289e99eb9 13 months ago 1.84kB
删除镜像
是时候删除那个无聊的hello-world镜像了
1 2 3 4
[root@pdai ~]# docker rmi hello-world Error response from daemon: conflict: unable to remove repository reference "hello-world" (must force) - container c91b90b18884 is using its referenced image fce289e99eb9 [root@pdai ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
发现报错了,这时候主要看两点:要么就是container实例存在,要么存在镜像依赖;
1 2 3 4 5 6 7 8 9 10 11
[root@pdai ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1a51d2f023c9 ubuntu:latest "/bin/sh -c 'while t…" 2 hours ago Exited (137) 2 hours ago gifted_brown 414bf796cbe4 ubuntu:latest "/bin/bash" 2 hours ago Exited (0) 2 hours ago pedantic_galileo 512061ebfa4c ubuntu:latest "/bin/bash" 2 hours ago Exited (0) 2 hours ago wizardly_brown aa5e9ae5e5db ubuntu:latest "/bin/echo 'Hello wo…" 2 hours ago Exited (0) 2 hours ago affectionate_knuth c91b90b18884 hello-world "/hello" 3 hours ago Exited (0) 3 hours ago admiring_pare [root@pdai ~]# docker image inspect --format='{{.RepoTags}} {{.Id}} {{.Parent}}' $(docker image ls -q ) [mysql:latest] sha256:791b6e40940cd550af522eb4ffe995226798204504fe495743445b900e417a51 [ubuntu:latest] sha256:ccc6e87d482b79dd1645affd958479139486e47191dfe7a997c862d89cd8b4c0 [hello-world:latest] sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e
显然,我们只要删除container即可:
1 2 3 4 5 6 7 8
[root@pdai ~]# docker rm c91b90b18884 c91b90b18884 [root@pdai ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1a51d2f023c9 ubuntu:latest "/bin/sh -c 'while t…" 2 hours ago Exited (137) 2 hours ago gifted_brown 414bf796cbe4 ubuntu:latest "/bin/bash" 2 hours ago Exited (0) 2 hours ago pedantic_galileo 512061ebfa4c ubuntu:latest "/bin/bash" 2 hours ago Exited (0) 2 hours ago wizardly_brown aa5e9ae5e5db ubuntu:latest "/bin/echo 'Hello wo…" 2 hours ago Exited (0) 2 hours ago affectionate_knuth
[root@pdai ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE pdai/ubuntu v1.0.1 b51e9f5f98cd 5 seconds ago 92.1MB mysql latest 791b6e40940c 2 weeks ago 465MB ubuntu latest ccc6e87d482b 4 weeks ago 64.2MB
[root@pdai docker-test]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE pdai/ubuntu v2.0.1 a733d5a264b5 5 minutes ago 92.5MB pdai/ubuntu v1.0.1 b51e9f5f98cd 15 hours ago 92.1MB mysql latest 791b6e40940c 2 weeks ago 465MB ubuntu latest ccc6e87d482b 4 weeks ago 64.2MB
我们可以使用新的镜像来创建容器
1 2 3 4 5
[root@pdai docker-test]# docker run -it pdai/ubuntu:v2.0.1 /bin/bash root@f5332ebce695:/# id pdai uid=1000(pdai) gid=1000(pdai) groups=1000(pdai) root@f5332ebce695:/# exit exit
从上面看到新镜像已经包含我们创建的用户 pdai。
镜像标签
设置镜像的Tag,类似于Git中tag?我们可以使用 docker tag 命令,为镜像添加一个新的标签
1 2 3 4 5 6 7 8
[root@pdai ~]# docker tag a733d5a264b5 pdai/ubuntu:v3.0.1 [root@pdai ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE pdai/ubuntu v2.0.1 a733d5a264b5 9 minutes ago 92.5MB pdai/ubuntu v3.0.1 a733d5a264b5 9 minutes ago 92.5MB pdai/ubuntu v1.0.1 b51e9f5f98cd 15 hours ago 92.1MB mysql latest 791b6e40940c 2 weeks ago 465MB ubuntu latest ccc6e87d482b 4 weeks ago 64.2MB
注意:
docker tag 镜像ID,这里是 a733d5a264b5 ,用户名称、镜像源名(repository name)和新的标签名(tag)。
[root@pdai ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f5332ebce695 pdai/ubuntu:v2.0.1 "/bin/bash" 42 minutes ago Up 8 seconds 22/tcp, 80/tcp jolly_kepler [root@pdai ~]# docker export f5332ebce695 > ubuntu-pdai-v2.tar [root@pdai ~]# ll -rw-rw-r-- 1 root root 93891072 Feb 18 09:42 ubuntu-pdai-v2.tar
同时你可以发现,导出容器的时候,容器无需关闭。
容器导入
1 2 3 4 5 6 7 8 9 10
[root@pdai ~]# docker import ubuntu-pdai-v2.tar pdai/ubuntu:v2.0.2 sha256:57544a04cd1ad330371ca9142184ff031dc2b6df70df177994477c08fe547b25 [root@pdai ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE pdai/ubuntu v2.0.2 57544a04cd1a 7 seconds ago 91.5MB pdai/ubuntu v2.0.1 a733d5a264b5 About an hour ago 92.5MB pdai/ubuntu v3.0.1 a733d5a264b5 About an hour ago 92.5MB pdai/ubuntu v1.0.1 b51e9f5f98cd 16 hours ago 92.1MB mysql latest 791b6e40940c 2 weeks ago 465MB ubuntu latest ccc6e87d482b 4 weeks ago 64.2MB
注意看,SIZE可能是不一样的。
强制停止容器
能否强制删除一个正在运行的容器呢?
显然,加上-f之后是允许的。
1 2 3 4 5 6 7 8 9 10 11 12
[root@pdai ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f5332ebce695 pdai/ubuntu:v2.0.1 "/bin/bash" About an hour ago Up 24 minutes 22/tcp, 80/tcp jolly_kepler [root@pdai ~]# docker rm -f f5332ebce695 f5332ebce695 [root@pdai ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0a1556ca3c27 ubuntu:latest "/bin/bash" 17 hours ago Exited (0) 17 hours ago stoic_hodgkin 1a51d2f023c9 ubuntu:latest "/bin/sh -c 'while t…" 19 hours ago Exited (137) 19 hours ago gifted_brown 414bf796cbe4 ubuntu:latest "/bin/bash" 19 hours ago Exited (0) 19 hours ago pedantic_galileo 512061ebfa4c ubuntu:latest "/bin/bash" 19 hours ago Exited (0) 19 hours ago wizardly_brown aa5e9ae5e5db ubuntu:latest "/bin/echo 'Hello wo…" 19 hours ago Exited (0) 19 hours ago affectionate_knuth
清理停止的容器
我们看到上面还有好几个容器出于停止状态,能不能删除它们呢?
1 2 3 4 5 6 7
[root@pdai ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0a1556ca3c27 ubuntu:latest "/bin/bash" 17 hours ago Exited (0) 17 hours ago stoic_hodgkin 1a51d2f023c9 ubuntu:latest "/bin/sh -c 'while t…" 19 hours ago Exited (137) 19 hours ago gifted_brown 414bf796cbe4 ubuntu:latest "/bin/bash" 19 hours ago Exited (0) 19 hours ago pedantic_galileo 512061ebfa4c ubuntu:latest "/bin/bash" 19 hours ago Exited (0) 19 hours ago wizardly_brown aa5e9ae5e5db ubuntu:latest "/bin/echo 'Hello wo…" 19 hours ago Exited (0) 19 hours ago affectionate_knuth
清理停止的容器: docker container prune
1 2 3 4 5 6 7 8 9 10 11 12 13
[root@pdai ~]# docker container prune WARNING! This will remove all stopped containers. Are you sure you want to continue? [y/N] y Deleted Containers: 0a1556ca3c275cc692ecd6d19caed4c5be42578f81b3dfea52b24208790d160a 1a51d2f023c947f2be2d9a78eb863e854ca302c89bf354654c409e23e7dd25d7 414bf796cbe403e01b5414f2b6232c6a037af78deee4581f4935c94859b5164e 512061ebfa4c340eb03833e54d77052e33fb62cd42ab7dd7e09bf0b02a761552 aa5e9ae5e5db1760bdb8c7ddb92e4293cd7b4736be7e97314f3ef44a71bf8930
Total reclaimed space: 27.93MB [root@pdai ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@pdai ~]# docker login 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: realpdai 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 Login Succeeded
镜像准备
1 2 3 4 5 6 7 8 9 10
[root@pdai ~]# docker tag pdai/ubuntu:v2.0.2 realpdai/ubuntu:v2.0.2 [root@pdai ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE pdai/ubuntu v2.0.2 57544a04cd1a 4 hours ago 91.5MB realpdai/ubuntu v2.0.2 57544a04cd1a 4 hours ago 91.5MB pdai/ubuntu v2.0.1 a733d5a264b5 5 hours ago 92.5MB pdai/ubuntu v3.0.1 a733d5a264b5 5 hours ago 92.5MB pdai/ubuntu v1.0.1 b51e9f5f98cd 21 hours ago 92.1MB mysql latest 791b6e40940c 2 weeks ago 465MB ubuntu latest ccc6e87d482b 4 weeks ago 64.2MB
推送至Docker Hub服务器
1 2 3 4
[root@pdai ~]# docker push realpdai/ubuntu:v2.0.2 The push refers to repository [docker.io/realpdai/ubuntu] 9a2c43cbe02a: Pushed v2.0.2: digest: sha256:4afd82dd05d0b4a340ae4f4129dcbd63136b5ec7ff92edf313108a41fb0947e0 size: 528