docker基本命令

漆雕和雅
2023-12-01
--docker注册账号DockerID
https://cloud.docker.com/
Docker ID:abc
邮箱:xx
密码:123

--login登录账号
root@node1:~# docker login
Username: abc
Password: 123
Login Succeeded
root@node1:~# 


--tag标签
root@node1:~/testdocker# docker tag hello-world gdmzlhj1/repository:hello-tag

root@node1:~/testdocker# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
friendlyhello         latest              6b1ec6201bc0        24 minutes ago      195 MB
python                2.7-slim            1c7128a655f6        2 weeks ago         183 MB
gdmzlhj1/repository   hello-tag           48b5124b2768        4 months ago        1.84 kB
hello-world           latest              48b5124b2768        4 months ago        1.84 kB

--push上传
root@node1:~/testdocker# docker push gdmzlhj1/repository:hello-tag
The push refers to a repository [docker.io/gdmzlhj1/repository]
98c944e98de8: Mounted from library/hello-world
hello-tag: digest: sha256:c5515758d4c5e1e838e9cd307f6c6a0d620b5e07e6f927b07d05f6d12a1ac8d7 size: 524

--search搜索
root@node1:~# docker search hello-world
NAME                                      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
hello-world                               Hello World! (an example of minimal Docker...   308       [OK]      
tutum/hello-world                         Image to test docker deployments. Has Apac...   35                   [OK]
dockercloud/hello-world                   Hello World!                                    12                   [OK]
marcells/aspnet-hello-world               ASP.NET vNext - Hello World                     4                    [OK]
neoxsys/hello-world                       hello-world container based on Ubuntu 16.0...   1                    [OK]
waleedsamy/hello-world-expressjs-docker   Hello world express sample, return `Hello ...   1                    [OK]
carinamarina/hello-world-app              This is a sample Python web application, r...   1                    [OK]
scottnakada/angular2-docker-hello-world   Angular 2 Hello World Sample Application u...   1                    [OK]
gscrivano/hello-world                     hello world example system container            0                    [OK]
utanna/hello-world                        Nginx hello-world                               0                    [OK]
psitrax/hello-world                       Simple Hello World Container: Outputs Head...   0                    [OK]
crccheck/hello-world                      Hello World web server in under 2.5 MB          0                    [OK]
jerzhang/hello-world                      Hello world automated build                     0                    [OK]
navycloud/hello-world                     Navy hello world                                0                    [OK]
helloworld314/hello-world-docker          Hello world docker                              0                    [OK]
mgueury/docker-hello-world                Simple Hello World Example                      0                    [OK]
dharmit/hello-world                       hello world                                     0                    [OK]
wowgroup/hello-world                      Minimal web app for testing purposes            0                    [OK]
infrastructureascode/hello-world          A tiny "Hello World" web server with a hea...   0                    [OK]
jmreeve007/hello-world                    Create automated build for changes to hell...   0                    [OK]
hoto/hello-world                          Nodejs hello world application.                 0                    [OK]
akshaykawathalkar/hello-world             13 feb hello world                              0                    [OK]
wodge/docker-hello-world                  Hello World test for auto update to Docker...   0                    [OK]
andreamocci/docker-hello-world            Docker hello world.                             0                    [OK]
n8io/hello-world                          A simple hello world node.js app to test d...   0                    [OK]


--pull下载
docker有一个公共的img仓库,执行docker pull默认会在这个公共库里去下载对应的img,类似maven一样,maven的respository里有很多jar包。
root@node1:~# docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
Digest: sha256:c5515758d4c5e1e838e9cd307f6c6a0d620b5e07e6f927b07d05f6d12a1ac8d7
Status: Image is up to date for hello-world:latest
root@node1:~#
root@node1:~#
root@node1:~# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              48b5124b2768        4 months ago        1.84 kB

--运行app
root@node1:~$ docker run hello-world

$ docker run --name helloworld -d -p 8080:80 daocloud.io/nginx

$ docker run --name helloworld2 -d -p 9090:80 daocloud.io/nginx


—name 是给容器起了一个名字;
-d 表示容器运行在后台,如果前台,用-i表示交互式;
-p 8080:80 表示端口映射,将宿主机的8080端口转发到容器内的80端口;
daocloud.io/nginx 表示刚才下载的镜像(Image)


$ curl http://localhost:8080   会显示 “Welcome to nginx!” 的页面。
$ curl http://localhost:9090   结果一样,只是访问了另外一个容器罢了。


--inspect返回信息
root@node1:~# docker inspect hello-world
[
    {
        "Id": "sha256:48b5124b2768d2b917edcb640435044a97967015485e812545546cbed5cf0233",
        "RepoTags": [
            "hello-world:latest"
        ],
        "RepoDigests": [
            "hello-world@sha256:c5515758d4c5e1e838e9cd307f6c6a0d620b5e07e6f927b07d05f6d12a1ac8d7"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2017-01-13T22:50:56.415736637Z",
        "Container": "d7e9f7ed9c135402fba7227d8da07c250126181eee0cfd2743b5736b80108625",
        "ContainerConfig": {
            "Hostname": "b3e3b3843b7f",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"/hello\"]"
            ],
            "Image": "sha256:fba45bbdabc1ff498b7965f99d2f25c59266cfa751067951f8e965841928c843",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "DockerVersion": "1.12.3",
        "Author": "",
        "Config": {
            "Hostname": "b3e3b3843b7f",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/hello"
            ],
            "Image": "sha256:fba45bbdabc1ff498b7965f99d2f25c59266cfa751067951f8e965841928c843",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 1840,
        "VirtualSize": 1840,
        "GraphDriver": {
            "Name": "aufs",
            "Data": null
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:98c944e98de8d35097100ff70a31083ec57704be0991a92c51700465e4544d08"
            ]
        }
    }
]

--ps查看进程
root@node1:~# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

命令docker ps 显示的是正在运行的Container列表,里面出现了刚才运行的nginx。于是可以把“容器(Container)”理解为镜像(Image)的一次运行。
查看宿主机运行着多少个容器,用docker ps 命令。


root@node1:~# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
a02bf766556c        hello-world         "/hello"            18 minutes ago      Exited (0) 18 minutes ago                       loving_torvalds
b416c34db3b9        hello-world         "/hello"            31 minutes ago      Exited (0) 31 minutes ago               suspicious_chandrasekhar
33e923cfb92a        hello-world         "/hello"            32 minutes ago      Exited (0) 32 minutes ago                       zen_ramanujan
3379a0fb0214        hello-world         "/hello"            53 minutes ago      Exited (0) 53 minutes ago                       nifty_allen


--attach/start/stop/kill/restart/rm/logs/top命令
docker attach $container
docker start/stop/kill/restart/rm  $container
docker start/stop/kill/restart/rm  helloworld
docker logs $contain_name
docker logs $container_id 
docker top helloworld 
docker top helloworld 
root@node1:~# docker info
Containers: 5
Running: 0
Paused: 0
Stopped: 5
Images: 1
Server Version: 17.03.1-ce
Storage Driver: aufs
Root Dir: /var/lib/docker/aufs
Backing Filesystem: extfs
Dirs: 11
Dirperm1 Supported: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 4ab9917febca54791c5f071a9d1f404867857fcc
runc version: 54296cf40ad8143b62dbcaa1d90e520a2136ddfe
init version: 949e6fa
Security Options:
apparmor
seccomp
  Profile: default
Kernel Version: 4.8.0-22-generic
Operating System: Ubuntu 16.10
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 3.84 GiB
Name: node1
ID: DZO7:H3EE:BBCD:X44D:EOPL:37VU:VQVN:MRVR:JILJ:7NXV:T3FT:GQ3E
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
WARNING: No swap limit support
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
root@node1:~#
root@node1:~#
root@node1:~# 


--build制作镜像
root@node1:~/testdocker# pwd
/root/testdocker
root@node1:~/testdocker# ls
app.py  Dockerfile  requirements.txt     --三个文件


root@node1:~/testdocker# cat Dockerfile
# Use an official Python runtime as a base image
FROM python:2.7-slim

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
ADD . /app

# Install any needed packages specified in requirements.txt
RUN pip install -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

-----------------
root@node1:~/testdocker# cat requirements.txt
Flask
Redis

-----------------
root@node1:~/testdocker# cat app.py
from flask import Flask
from redis import Redis, RedisError
import os
import socket

# Connect to Redis
redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)

app = Flask(__name__)

@app.route("/")
def hello():
    try:
        visits = redis.incr("counter")
    except RedisError:
        visits = "<i>cannot connect to Redis, counter disabled</i>"

    html = "<h3>Hello {name}!</h3>" \
           "<b>Hostname:</b> {hostname}<br/>" \
           "<b>Visits:</b> {visits}"
    return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits)

if __name__ == "__main__":
        app.run(host='0.0.0.0', port=80)


-----------------
root@node1:~/testdocker# docker build -t friendlyhello .


root@node1:~/testdocker# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
friendlyhello       latest              6b1ec6201bc0        11 seconds ago      195 MB
python              2.7-slim            1c7128a655f6        2 weeks ago         183 MB
hello-world         latest              48b5124b2768        4 months ago        1.84 kB






 类似资料: