Docker的简单命令

环境:centos 7

0.先要启动docker

sudo systemctl start docker

如果执行docker命令有如下提示:

Got permission denied while trying to connect to the Docker daemon socket at unix:...

就必须每次用sudo来执行docker命令。为避免每次sudo的麻烦,可以进行以下操作:

$ sudo groupadd docker
groupadd: group 'docker' already exists

$ sudo gpasswd -a vagrant docker
Adding user vagrant to group docker

$ sudo service docker restart

// 重启终端,vagrant虚拟机环境就先exit,再vagrant ssh登录。

1.查看都有哪些命令

docker

2.查看版本信息

$ docker --version
Docker version 18.06.0-ce, build 0ffa825

3.拉取镜像

$ docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
9db2ca6ccae0: Pull complete 
Digest: sha256:4b8ff392a12ed9ea17784bd3c9a8b1fa3299cac44aca35a85c90c5e3c7afacdc
Status: Downloaded newer image for hello-world:latest

4.查看镜像

$ docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              2cb0d9787c4d        4 weeks ago         1.85kB

5.运行镜像

$ docker run hello-world
Hello from Docker!
... ...

6.自定义镜像

$ mkdir hello-world
$ cd hello-world/
$ vim hello.c  // 写一个hello world的C程序
$ gcc -static hello.c -o hello  // yum -y install gcc glib-static
$ ./hello 
hello docker

$ vim Dockerfile  // 文件内容就是下面Step1~3
$ docker build -t chao2018/hello-world .
Sending build context to Docker daemon  864.8kB
Step 1/3 : FROM scratch
 ---> 
Step 2/3 : ADD hello /
 ---> b706487bbabb
Step 3/3 : CMD ["/hello"]
 ---> Running in f74051e6c611
Removing intermediate container f74051e6c611
 ---> e663874b231a
Successfully built e663874b231a
Successfully tagged chao2018/hello-world:latest

$ docker image ls
REPOSITORY             TAG                 IMAGE ID            CREATED             SIZE
chao2018/hello-world   latest              e663874b231a        35 seconds ago      861kB
hello-world            latest              2cb0d9787c4d        4 weeks ago         1.85kB

$ ls -lh
total 852K
-rw-rw-r--. 1 vagrant vagrant   40 Aug  8 09:50 Dockerfile
-rwxrwxr-x. 1 vagrant vagrant 841K Aug  8 09:49 hello
-rw-rw-r--. 1 vagrant vagrant   73 Aug  8 09:48 hello.c

$ docker history e663874b231a
IMAGE               CREATED              CREATED BY                                      SIZE                COMMENT
e663874b231a        About a minute ago   /bin/sh -c #(nop)  CMD ["/hello"]               0B                  
b706487bbabb        About a minute ago   /bin/sh -c #(nop) ADD file:00752979881da6999…   861kB  

$ docker run chao2018/hello-world
hello docker

7.docker container命令

$ docker container ls  // 查看当前的
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

$ docker container ls -a  // 查看历史的 = docker ps -a
CONTAINER ID        IMAGE                  COMMAND             CREATED             STATUS                      PORTS               NAMES
2d5f28c94271        chao2018/hello-world   "/hello"            3 minutes ago       Exited (0) 3 minutes ago                        mystifying_ramanujan
e58d4b10421f        hello-world            "/hello"            10 minutes ago      Exited (0) 10 minutes ago                       jolly_ptolemy

$ docker run centos
Unable to find image 'centos:latest' locally
latest: Pulling from library/centos
256b176beaff: Pull complete 
Digest: sha256:6f6d986d425aeabdc3a02cb61c02abb2e78e57357e92417d6d58332856024faf
Status: Downloaded newer image for centos:latest
[vagrant@docker-host docker-centos-vim]$ docker image ls
REPOSITORY             TAG                 IMAGE ID            CREATED             SIZE
chao2018/hello-world   latest              e663874b231a        8 minutes ago       861kB
centos                 latest              5182e96772bf        39 hours ago        200MB
hello-world            latest              2cb0d9787c4d        4 weeks ago

[vagrant@docker-host ~]$ vim Dockerfile
FROM centos
RUN yum install -y vim

[vagrant@docker-host ~]$ docker run -it centos
[root@9cfdb8ba5a6d /]# ls
anaconda-post.log  dev  home  lib64  mnt  proc  run   srv  tmp  var
bin                etc  lib   media  opt  root  sbin  sys  usr
$ docker container ls  // 另一个终端
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
9cfdb8ba5a6d        centos              "/bin/bash"         About a minute ago   Up About a minute                       priceless_chandrasekhar

8.删除image和container

// 删除container
$ docker container rm 9cfdb8ba5a6d
9cfdb8ba5a6d
$ docker rm 53
53

// 删除image
$ docker rmi hello-world  // 在没有stopped container使用它时才可以删除
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值