docker 常用操作命令整理

Posted 志波同学

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker 常用操作命令整理相关的知识,希望对你有一定的参考价值。

镜像仓库

将 docker 镜像仓库更换为阿里云镜像,Ubuntu、CentOS 可以按照如下命令操作。更多操作系统支持参考阿里云文档

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'

  "registry-mirrors": ["https://tatggrb0.mirror.aliyuncs.com"]

EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

镜像

1、拉取 docker 镜像

[root@iZ2ze6ogddtzz4dzyy00xwZ ~]# docker pull ubuntu:18.04
18.04: Pulling from library/ubuntu
7ddbc47eeb70: Pull complete 
c1bbdc448b72: Pull complete 
8c3b70e39044: Pull complete 
45d437916d57: Pull complete 
Digest: sha256:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d
Status: Downloaded newer image for ubuntu:18.04
docker.io/library/ubuntu:18.04

2、查看 docker 镜像

[root@iZ2ze6ogddtzz4dzyy00xwZ ~]# docker images           
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              16.04               56bab49eef2e        38 hours ago        123MB
ubuntu              14.04               d5e7f166b406        38 hours ago        197MB
ubuntu              19.10               09604a62a001        5 weeks ago         72.9MB
hello-world         latest              fce289e99eb9        11 months ago       1.84kB
ubuntu              15.04               d1b55fd07600        3 years ago         131MB

3、删除镜像

[root@iZ2ze6ogddtzz4dzyy00xwZ ~]# docker rmi d5e7f166b406 
Untagged: ubuntu:14.04
Untagged: ubuntu@sha256:97466d028ec511ae16b66e0c9caa5d178c44b6ca6833e73c4a1edd369ddac901
Deleted: sha256:d5e7f166b406dc46c2c0e2e67577a391f35d2fa88c5ee840266c1c59f0702f8f
Deleted: sha256:bed446a0fbb0eddaf9922561bb02521d025fc13679a692cf861deeaee2519398
Deleted: sha256:68256d97a5669b5bcb6ca8066b759aa51de24b6706ecd0d083e784bb1c48d61d
Deleted: sha256:f3e5b349e3053988f578ebb41276d87adbcf67f7ba3ff3cee1a8cab5a34e3181

4、搜索镜像

[root@iZ2ze6ogddtzz4dzyy00xwZ ~]# docker search java
NAME                                     DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
node                                     Node.js is a javascript-based platform for s…   8145                [OK]                
tomcat                                   Apache Tomcat is an open source implementati…   2573                [OK]                
openjdk                                  OpenJDK is an open-source implementation of …   2008                [OK]                
java                                     Java is a concurrent, class-based, and objec…   1976                [OK]                
ghost                                    Ghost is a free and open source blogging pla…   1066                [OK]                
jetty                                    Jetty provides a Web server and javax.servle…   320                 [OK]                
groovy                                   Apache Groovy is a multi-faceted language fo…   81                  [OK]                
lwieske/java-8                           Oracle Java 8 Container - Full + Slim - Base…   45                                      [OK]
nimmis/java-centos                       This is docker images of CentOS 7 with diffe…   42                                      [OK]
fabric8/java-jboss-openjdk8-jdk          Fabric8 Java Base Image (JBoss, OpenJDK 8)      28                                      [OK]
frekele/java                             docker run --rm --name java frekele/java        12                                      [OK]
blacklabelops/java                       Java Base Images.                               8                                       [OK]
bitnami/java                             Bitnami Java Docker Image                       5                                       [OK]
cloudbees/java-with-docker-client        Java image with Docker client installed, use…   4                                       [OK]
rightctrl/java                           Oracle Java                                     3                                       [OK]
zoran/java10-sjre                        Slim Docker image based on AlpineLinux with …   2                                       [OK]
dwolla/java                              Dwolla’s custom Java image                      1                                       [OK]
buildo/java8-wkhtmltopdf                 Java 8 + wkhtmltopdf                            1                                       [OK]
cfje/java-buildpack                      Java Buildpack CI Image                         1                                       
cfje/java-resource                       Java Concourse Resource                         1                                       
cfje/java-test-applications              Java Test Applications CI Image                 1                                       
jelastic/javaengine                      An image of the Java Engine server maintaine…   0                                       
cfje/java-buildpack-dependency-builder   Java Buildpack Dependencies Builder Image       0                                       
cfje/java-buildpack-memory-calculator    Java Buildpack Memory Calculator CI Image       0                                       
thingswise/java-docker                   Java + dcd                                      0                                       [OK]

5、构建镜像
在Dockerfile文件的所在路径执行如下命令,-t 指定镜像名称,末尾的点标识 Dockerfile 文件的路径。

docker build -t imageName:tag .

容器

1、运行容器

[root@iZ2ze6ogddtzz4dzyy00xwZ ~]# docker run -it ubuntu:19.10
root@fad4c851e732:/# 

2、查看运行的容器

[root@iZ2ze6ogddtzz4dzyy00xwZ ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
fad4c851e732        ubuntu:19.10        "/bin/bash"         About an hour ago   Up About an hour                        wonderful_roentgen

3、查看所有的容器

[root@iZ2ze6ogddtzz4dzyy00xwZ ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
fad4c851e732        ubuntu:19.10        "/bin/bash"         About an hour ago   Exited (0) 5 seconds ago                       wonderful_roentgen
2b18b881a53c        hello-world         "/hello"            32 hours ago        Exited (0) 32 hours ago                        laughing_margulis

4、重新进入容器

[root@iZ2ze6ogddtzz4dzyy00xwZ ~]# docker attach fad4c851e732
root@fad4c851e732:/home/software/apache/hadoop# 

5、将一个已经终止的容器运行起来

[root@iZ2ze6ogddtzz4dzyy00xwZ ~]# docker start fad4c851e732
fad4c851e732
[root@iZ2ze6ogddtzz4dzyy00xwZ ~]# 

6、终止容器

[root@iZ2ze6ogddtzz4dzyy00xwZ ~]# docker ps -a                            
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                    PORTS               NAMES
3b395bf32944        ubuntu:hadoop       "/bin/bash"         About a minute ago   Up About a minute                             goofy_brahmagupta
fad4c851e732        ubuntu:19.10        "/bin/bash"         2 hours ago          Up 12 minutes                                 wonderful_roentgen
2b18b881a53c        hello-world         "/hello"            33 hours ago         Exited (0) 33 hours ago                       laughing_margulis
root@iZ2ze6ogddtzz4dzyy00xwZ ~]# docker stop 3b395bf32944
3b395bf32944

7、删除容器

[root@iZ2ze6ogddtzz4dzyy00xwZ ~]# docker ps -a                            
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                    PORTS               NAMES
3b395bf32944        ubuntu:hadoop       "/bin/bash"         About a minute ago   Up About a minute                             goofy_brahmagupta
fad4c851e732        ubuntu:19.10        "/bin/bash"         2 hours ago          Up 12 minutes                                 wonderful_roentgen
2b18b881a53c        hello-world         "/hello"            33 hours ago         Exited (0) 33 hours ago                       laughing_margulis
[root@iZ2ze6ogddtzz4dzyy00xwZ ~]# docker rm 3b395bf32944  
3b395bf32944
[root@iZ2ze6ogddtzz4dzyy00xwZ ~]#

8、创建容器时设置名称

[root@iZ2ze6ogddtzz4dzyy00xwZ ~]# docker run -d --name hadoop_master ubuntu:hadoop

9、重命名容器

[root@iZ2ze6ogddtzz4dzyy00xwZ ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
9d52a5a8f11e        ubuntu:hadoop       "/bin/bash"         3 minutes ago       Up 3 minutes                            ecstatic_archimedes
abbd553e65ae        ubuntu:hadoop       "/bin/bash"         4 minutes ago       Up 4 minutes                            peaceful_gould
bdb5742bba1c        ubuntu:hadoop       "/bin/bash"         8 minutes ago       Up 8 minutes                            hadoop_master
[root@iZ2ze6ogddtzz4dzyy00xwZ ~]# docker rename peaceful_gould hadoop_slave1

10、重新进入容器2

docker exec -it container-name /bin/bash

11、删除正在运行的容器

docker rm -f container-name

以上是关于docker 常用操作命令整理的主要内容,如果未能解决你的问题,请参考以下文章

docker常用命令整理

Docker知识点翻阅手册--Docker常用命令DockererfileCompose网络等整理合集

Docker常用命令整理

Docker常用命令整理

Docker容器学习整理

docker 常用命令整理