Docker容器

Docker常用操作

本文主要是介绍Docker常用操作,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
  • 从仓库拉取镜像
[root@localhost ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
  • 查看当前拥有镜像
[root@localhost ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    feb5d9fea6a5   4 months ago   13.3kB
  • 查看当前所有容器(-a 包含退出的)
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE         COMMAND    CREATED         STATUS                     PORTS     NAMES
0ab3a4609ecc   hello-world   "/hello"   4 minutes ago   Exited (0) 4 minutes ago             sleepy_turing
  • 删除已停止的容器
[root@localhost ~]# docker rm 0ab3a4609ecc
0ab3a4609ecc
  • 删除所有镜像(-f 包含当前起了容器的镜像)
[root@localhost ~]# docker rmi -f $(docker images -qa)
Untagged: hello-world:latest
Untagged: hello-world@sha256:97a379f4f88575512824f3b352bc03cd75e239179eea0fecc38e597b2209f49a
Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
  • 伪终端交互式生成容器(exit:容器停止退出 ctrl+P+Q:容器不停止退出)
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
centos       latest    5d0da3dc9764   5 months ago   231MB
[root@localhost ~]# docker run -it 5d0da3dc9764
[root@3dd0a3a9cca4 /]# 
  • 启动退出的容器
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED         STATUS                     PORTS     NAMES
3dd0a3a9cca4   5d0da3dc9764   "/bin/bash"   2 minutes ago   Exited (0) 2 minutes ago             loving_beaver
2a40735a772a   feb5d9fea6a5   "/hello"      7 minutes ago   Exited (0) 7 minutes ago             exciting_buck
[root@localhost ~]# docker start 3dd0a3a9cca4
3dd0a3a9cca4
  • 停止运行的容器
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE          COMMAND       CREATED         STATUS              PORTS     NAMES
3dd0a3a9cca4   5d0da3dc9764   "/bin/bash"   4 minutes ago   Up About a minute             loving_beaver
[root@localhost ~]# docker stop 3dd0a3a9cca4
3dd0a3a9cca4
  • 删除已停止的容器
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED         STATUS                     PORTS     NAMES
3dd0a3a9cca4   5d0da3dc9764   "/bin/bash"   5 minutes ago   Exited (0) 4 seconds ago             loving_beaver
[root@localhost ~]# docker rm 3dd0a3a9cca4
3dd0a3a9cca4
  • 删除多个容器
[root@localhost ~]# docker ps -qa|xargs docker rm
2a40735a772a
  • 守护启动容器(后台运行,必须有一个前台进程)
root@localhost ~]# docker run -d centos /bin/sh -c "while true;do echo hello ztf;sleep 2;done"
60a42445f78cbea48f1d23971575084986aee9853255ec9766c9de31094e2f69
[root@localhost ~]# docker logs 60a42445
hello ztf
hello ztf
  • 查看容器内的进程
[root@localhost ~]# docker top 60a42445
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                3115                3097                0                   23:52               ?                   00:00:00            /bin/sh -c while true;do echo hello ztf;sleep 2;done
root                3186                3115                0                   23:53               ?                   00:00:00            /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 2
  • 查看容器内部细节
[root@localhost ~]# docker inspect 60a42445
[
    {
        "Id": "60a42445f78cbea48f1d23971575084986aee9853255ec9766c9de31094e2f69",
        "Created": "2022-02-19T15:52:08.782734158Z",
  • 进入正在运行的容器

    1. 不启动新终端
    [root@localhost ~]# docker exec -it 60a42445f78c ls -l /tmp
    total 12
    -rwx------. 1 root root 701 Sep 15 14:17 ks-script-4luisyla
    -rwx------. 1 root root 671 Sep 15 14:17 ks-script-o23i7rc2
    
    1. 启动新终端
    [root@localhost ~]# docker attach 60a42445f78c
    hello ztf
    hello ztf
    
  • copy容器内的内容

[root@localhost ~]# docker cp 60a42445f78c:/tmp/ks-script-4luisyla  .
  • 8080端口启动tomcat
[root@localhost ~]# docker run -d -p 8080:8080 tomcat
8477d8fd9c51233285a2a77831e7a3bb41d87e790180632825e7a5ec34c5320a
  • 使用mysql

    • 运行mysql
      [root@localhost mysql]# docker run -d -p 3306:3306 --name mysql -v  /ztf/mysql/conf:/etc/mysql/conf.d -v /ztf/mysql/logs:/logs -v /ztf/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql
    
    • docker本地连接mysql
    [root@localhost ~]# docker exec -it fc35ef1a9d8a bash
    root@fc35ef1a9d8a:/# mysql -uroot -p123456
    
    • 如果远程客户端无法访问
      • 防火墙阻拦
        systemctl stop firewalld
        
      • 需要进入docker本地客户端设置远程访问账号
        mysql> use mysql;
        mysql> grant all privileges on *.* to root@'%' identified by "password";
        
这篇关于Docker常用操作的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!