- [root@openEuler-node1 ~]# docker create busybox
- c3e7310fa21a4a2d94b0bf6f881aed4fc8482d3d23a341f9563ce65c8027fd19
- [root@openEuler-node1 ~]# docker start c3e7310fa21a
- c3e7310fa21a
- [root@openEuler-node1 ~]# docker run ubuntu /bin/echo 'Hello world'
- Unable to find image 'ubuntu:latest' locally
- latest: Pulling from library/ubuntu
- 7b1a6ab2e44d: Pull complete
- Digest: sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
- Status: Downloaded newer image for ubuntu:latest
- Hello world
注意:如果新建容器没有这个镜像会先拉取镜像再启动容器最后执行指令。
- [root@openEuler-node1 ~]# docker run -it ubuntu:lB .04 /bin/bash
- root@afBbae53bdd3 /#
用户可以输入exit或者Ctrl+D退出
- [root@openEuler-node1 ~]# docker run -d mysql:5.7 --name db
- e7d8bcac87cddc1299f7a71879bece14c5d38d7d5257be03dd312d6d19c9ddb2
- [root@openEuler-node1 ~]# docker logs sql
- 2024-03-11 11:09:22+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.44-1.el7 started.
- 2024-03-11 11:09:22+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
- 2024-03-11 11:09:22+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.44-1.el7 started.
- 2024-03-11 11:09:23+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified
- You need to specify one of the following as an environment variable:
- - MYSQL_ROOT_PASSWORD
- - MYSQL_ALLOW_EMPTY_PASSWORD
- - MYSQL_RANDOM_ROOT_PASSWORD
这里启动mysql报错也是非常常见的错误,因为需要初始root密码
- [root@openEuler-node1 ~]# docker pause test
- test
- [root@openEuler-node1 ~]# docker ps
- CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- 87151b0e6fe7 ubuntu "bash" 35 seconds ago Up 34 seconds (Paused) test
可以用unpause 恢运行态
- [root@openEuler-node1 ~]# docker unpause test
- test
- [root@openEuler-node1 ~]# docker ps
- CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- 87151b0e6fe7 ubuntu "bash" 2 minutes ago Up 2 minutes test
- [root@openEuler-node1 ~]# docker stop test
- test
重启容器
[root@openEuler-node1 ~]# docker restart test
- [root@openEuler-node1 ~]# docker run -itd ubuntu
- b0613bfdcbe14f88138ed030f37907356069142bc755fa7a2ce33749385894dd
- [root@openEuler-node1 ~]# docker ps
- CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- b0613bfdcbe1 ubuntu "bash" 4 seconds ago Up 3 seconds boring_wescoff
- [root@openEuler-node1 ~]# docker attach b0613bfdcbe1
- root@b0613bfdcbe1:/#
- [root@openEuler-node1 ~]# docker exec -it 685af6f5c5b9 bash
- root@685af6f5c5b9:/# w
- 11:25:37 up 8:16, 0 users, load average: 0.00, 0.01, 0.00
- USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
通过指定 it 参数来保持标准输入打开,并且分配 一个伪终端通过 exec 命令对容器执行操作是最为推荐的方式
- [root@openEuler-node1 ~]# docker rm -f `docker ps -qa`
- 685af6f5c5b9
- b0613bfdcbe1
利用反引号实现docker中容器的批量删除
导出
docker [container] export [-o l - -output [=””] ] CONTAINER
- [root@openEuler-node1 ~]# docker ps -a
- CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- 4d44b708c81b ubuntu:latest "bash" 6 seconds ago Exited (0) 5 seconds ago admiring_lamarr
- 015c53d29ae3 ubuntu:18.04 "/bin/bash" About a minute ago Exited (0) About a minute ago romantic_burnell
- [root@openEuler-node1 ~]# docker export -o test_for_run.tar 4d44b708c81b
- [root@openEuler-node1 ~]# ls
- anaconda-ks.cfg dockerfile test_for_run.tar ubuntu_18.04.tar
导入
- [root@openEuler-node1 ~]# docker import test_for_run.tar test/ubuntu:v1.0
- sha256:aec871a8dd332ba4dc0828da48022c7f2e74111ca4cdd53da6700eec8703ca35
[root@openEuler-node1 ~]# docker inspect 4d44b708c81b
就一条命令,可以根据容器的ID或名字看
- [root@openEuler-node1 ~]# docker top test
- UID PID PPID C STIME TTY TIME CMD
- root 11103 11087 0 19:43 pts/0 00:00:00 sh
- [root@openEuler-node1 ~]# docker stats test
-
- CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
- 823457ad116b test 0.00% 144KiB / 3.297GiB 0.00% 946B / 0B 0B / 0B