专栏首页DevOps持续集成面向初学者的Docker快速入门指南

面向初学者的Docker快速入门指南

Docker快速入门

1.1 安装docker(Linux)

yum源:https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo为了速度可以替换文件中的url。

:%s/download.docker.com/mirrors.tuna.tsinghua.edu.cn\/docker-ce/g

Docker version 19.03.12, build 48a66213fe

dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo

dnf list docker-ce
dnf install docker-ce --nobest -y

systemctl start docker
systemctl enable docker

docker -v

1.2 Docker指令

info 系统信息

[root@myserver ~]# docker info
Client:
 Debug Mode: false

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 18.09.1
 Storage Driver: overlay2
  Backing Filesystem: xfs
  Supports d_type: true
  Native Overlay Diff: true
 Logging Driver: json-file
 Cgroup Driver: cgroupfs

version 查看docker版本信息

 [root@myserver ~]# docker version
Client: Docker Engine - Community
 Version:           19.03.12
 API version:       1.39
 Go version:        go1.13.10
 Git commit:        48a66213fe
 Built:             Mon Jun 22 15:46:54 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          18.09.1
  API version:      1.39 (minimum version 1.12)
  Go version:       go1.10.6
  Git commit:       4c52b90
  Built:            Wed Jan  9 19:06:30 2019
  OS/Arch:          linux/amd64
  Experimental:     false
  wait        Block until one or more containers stop, then print their exit codes

容器的生命周期

search 查找镜像

docker search hello-world

pull 下载镜像(docker hub)

[root@myserver ~]# docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:d58e752213a51785838f9eed2b7a498ffa1cb3aa7f946dda11af39286c3db9a9
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest

images 查看本地的镜像

[root@myserver ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              bf756fb1ae65        6 months ago        13.3kB

run 运行容器

  • -i : 交互模式
  • -t:分配一个终端
  • -d:后台运行
  • --name:指定容器的名称
  • -p: 端口绑定
docker run [options] IMAGE 

docker run hello-world

docker run --name mywebserver -it nginx 

[root@myserver ~]# docker run --name mywebserver -itd nginx
8d485b358cc693c1e2b2d9b5c1d1a8092265d79bdcf1ee54d3d2f615f4f22064
[root@myserver ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
8d485b358cc6        nginx               "/docker-entrypoint.…"   6 seconds ago       Up 5 seconds        80/tcp              mywebserver

ps 插件容器进程( -a 所有的,正在运行、已经停止的)

[root@myserver ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@myserver ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
01bdedde3725        hello-world         "/hello"            2 minutes ago       Exited (0) 2 minutes ago                       hopeful_mendel

rm 删除容器 (-f 强制)

## 指定容器的名称

[root@myserver ~]# docker rm mywebserver
mywebserver
[root@myserver ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
01bdedde3725        hello-world         "/hello"            9 minutes ago       Exited (0) 9 minutes ago                       hopeful_mendel

## 指定容器的ID删除
[root@myserver ~]# docker rm 01bdedde3725
01bdedde3725
[root@myserver ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@myserver ~]#

start/restart/stop/ 容器控制

[root@myserver ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
8d485b358cc6        nginx               "/docker-entrypoint.…"   20 minutes ago      Up 20 minutes       80/tcp              mywebserver
[root@myserver ~]# docker stop mywebserver
mywebserver
[root@myserver ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@myserver ~]# docker start  mywebserver
mywebserver
[root@myserver ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
8d485b358cc6        nginx               "/docker-entrypoint.…"   21 minutes ago      Up 3 seconds        80/tcp              mywebserver
[root@myserver ~]# docker restart  mywebserver
mywebserver

stats 显示容器使用的资源(CPU)

docker stats mywebserver

[root@myserver ~]# docker stats mywebserver
CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
8d485b358cc6        mywebserver         0.00%               2.309MiB / 3.664GiB   0.06%               3.44kB / 0B         0B / 0B             2
CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PID

logs 查看容器日志(-f 动态更新)

[root@myserver ~]# docker logs -f  mywebserver
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: IPv6 listen already enabled, exiting
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: IPv6 listen already enabled, exiting
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up

pause/unpause 暂停保存容器的状态

[root@myserver ~]# docker pause mywebserver
mywebserver
[root@myserver ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                  PORTS               NAMES
8d485b358cc6        nginx               "/docker-entrypoint.…"   27 minutes ago      Up 5 minutes (Paused)   80/tcp              mywebserver
[root@myserver ~]# docker unpause mywebserver
mywebserver
[root@myserver ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
8d485b358cc6        nginx               "/docker-entrypoint.…"   27 minutes ago      Up 6 minutes        80/tcp              mywebserver
[root@myserver ~]#

rename 重命名

docker rename CONTAINER NEW_NAME

[root@myserver ~]# docker rename mywebserver webserver
[root@myserver ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
8d485b358cc6        nginx               "/docker-entrypoint.…"   29 minutes ago      Up 8 minutes        80/tcp              webserver

inspect 显示容器的详细属性信息

[root@myserver ~]# docker inspect webserver
[
    {
        "Id": "8d485b358cc693c1e2b2d9b5c1d1a8092265d79bdcf1ee54d3d2f615f4f22064",
        "Created": "2020-07-09T01:55:08.20199984Z",
        "Path": "/docker-entrypoint.sh",
        "Args": [
            "nginx",
            "-g",
            "daemon off;"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 6429,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2020-07-09T02:16:41.996582526Z",
            "FinishedAt": "2020-07-09T02:16:40.968627884Z"
        },

kill 关闭容器进程

[root@myserver ~]# docker kill webserver
webserver
[root@myserver ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@myserver ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES
8d485b358cc6        nginx               "/docker-entrypoint.…"   33 minutes ago      Exited (137) 7 seconds ago                       webserver
[root@myserver ~]#

top 查看容器进程

[root@myserver ~]# docker top webserver
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                7153                7136                0                   22:30               pts/0               00:00:00            nginx: master process nginx -g daemon off;
101                 7211                7153                0                   22:30               pts/0               00:00:00            nginx: worker process
[root@myserver ~]# docker top webserver 7153
PID                 TTY                 STAT                TIME                COMMAND
7153                pts/0               Ss+                 0:00                nginx: master process nginx -g daemon off;
[root@myserver ~]#

exec 进入容器执行命令attach

[root@myserver ~]# docker exec -it webserver bash
root@8d485b358cc6:/# ls

curl 访问服务器nginx  成功

port 显示容器端口绑定信息

[root@myserver ~]# docker port webserver
80/tcp -> 0.0.0.0:8000

cp 文件复制

[root@myserver ~]# docker cp /etc/hosts webserver:/tmp
[root@myserver ~]# docker exec -it webserver ls /tmp
hosts
[root@myserver ~]# docker cp webserver:/tmp/hosts ./
[root@myserver ~]# ls hosts
hosts

diff 容器文件系统发生的变化

[root@myserver ~]# docker diff webserver
C /etc
C /etc/nginx
C /etc/nginx/conf.d
C /etc/nginx/conf.d/default.conf
C /run
A /run/nginx.pid
C /tmp
A /tmp/hosts
C /var
C /var/cache
C /var/cache/nginx
A /var/cache/nginx/fastcgi_temp
A /var/cache/nginx/proxy_temp
A /var/cache/nginx/scgi_temp
A /var/cache/nginx/uwsgi_temp
A /var/cache/nginx/client_temp

export 导出容器文件系统到本地归档

[root@myserver ~]# docker export -o test.tar  webserver
[root@myserver ~]# mkdir docker
[root@myserver ~]# mv test.tar  docker/

[root@myserver ~]# cd docker/
[root@myserver docker]# ls
test.tar
[root@myserver docker]# tar xf test.tar
l[root@myserver docker]# ls
bin   dev                  docker-entrypoint.sh  home  lib64  mnt  proc  run   srv  test.tar  usr
boot  docker-entrypoint.d  etc                   lib   media  opt  root  sbin  sys  tmp       var
[root@myserver docker]# cd tmp/
[root@myserver tmp]# ls
hosts

import 导入压缩文件到镜像

[root@myserver docker]# ls
bin   dev                  docker-entrypoint.sh  etc   lib    media  opt   root  sbin  sys       tmp  var
boot  docker-entrypoint.d  Dockerfile            home  lib64  mnt    proc  run   srv   test.tar  usr
[root@myserver docker]# docker import  test.tar  webserver:v2
sha256:5aa7a154ce2f07e8dc94d331d8809ea40ea5fab4ffa8935e2092771f863e3c0f
[root@myserver docker]# docker history webserver:v2
IMAGE               CREATED             CREATED BY          SIZE                COMMENT
5aa7a154ce2f        15 seconds ago                          130MB               Imported from -
[root@myserver docker]#

镜像操作

history 查看镜像的更新历史

[root@myserver ~]# docker history nginx
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
2622e6cca7eb        4 weeks ago         /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon…   0B
<missing>           4 weeks ago         /bin/sh -c #(nop)  STOPSIGNAL SIGTERM           0B
<missing>           4 weeks ago         /bin/sh -c #(nop)  EXPOSE 80                    0B
<missing>           4 weeks ago         /bin/sh -c #(nop)  ENTRYPOINT ["/docker-entr…   0B
<missing>           4 weeks ago         /bin/sh -c #(nop) COPY file:cc7d4f1d03426ebd…   1.04kB
<missing>           4 weeks ago         /bin/sh -c #(nop) COPY file:b96f664d94ca7bbe…   1.96kB
<missing>           4 weeks ago         /bin/sh -c #(nop) COPY file:d68fadb480cbc781…   1.09kB
<missing>           4 weeks ago         /bin/sh -c set -x     && addgroup --system -…   62.9MB
<missing>           4 weeks ago         /bin/sh -c #(nop)  ENV PKG_RELEASE=1~buster     0B
<missing>           4 weeks ago         /bin/sh -c #(nop)  ENV NJS_VERSION=0.4.1        0B
<missing>           4 weeks ago         /bin/sh -c #(nop)  ENV NGINX_VERSION=1.19.0     0B
<missing>           4 weeks ago         /bin/sh -c #(nop)  LABEL maintainer=NGINX Do…   0B
<missing>           4 weeks ago         /bin/sh -c #(nop)  CMD ["bash"]                 0B
<missing>           4 weeks ago         /bin/sh -c #(nop) ADD file:4d35f6c8bbbe6801c…   69.2MB

login /logout Docker registry

[root@myserver ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: devopsvip
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[root@myserver ~]#

[root@myserver ~]# docker logout
Removing login credentials for https://index.docker.io/v1/

search 查找镜像

docker search hello-world

pull 下载镜像(docker hub)

[root@myserver ~]# docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:d58e752213a51785838f9eed2b7a498ffa1cb3aa7f946dda11af39286c3db9a9
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest

images 查看本地的镜像

[root@myserver ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              bf756fb1ae65        6 months ago        13.3kB

tag重命名

[root@myserver ~]# docker tag hello-world:latest devopsvip/docker:latest
[root@myserver ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              2622e6cca7eb        4 weeks ago         132MB
centos              7                   b5b4d78bc90c        2 months ago        203MB
devopsvip/docker    latest              bf756fb1ae65        6 months ago        13.3kB
hello-world         latest              bf756fb1ae65        6 months ago        13.3kB

push 上传镜像到镜像仓库

[root@myserver ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              2622e6cca7eb        4 weeks ago         132MB
centos              7                   b5b4d78bc90c        2 months ago        203MB
devopsvip/docker    latest              bf756fb1ae65        6 months ago        13.3kB
hello-world         latest              bf756fb1ae65        6 months ago        13.3kB
[root@myserver ~]# docker push devopsvip/docker:latest
The push refers to repository [docker.io/devopsvip/docker]
9c27e219663c: Preparing
denied: requested access to the resource is denied
[root@myserver ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: devopsvip
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[root@myserver ~]# docker push devopsvip/docker:latest
The push refers to repository [docker.io/devopsvip/docker]
9c27e219663c: Mounted from library/hello-world
latest: digest: sha256:90659bf80b44ce6be8234e6ff90a1ac34acbeb826903b02cfa0da11c82cbc042 size: 525

rmi 删除镜像

[root@myserver ~]# docker rmi devopsvip/docker:latest
Untagged: devopsvip/docker:latest
Untagged: devopsvip/docker@sha256:90659bf80b44ce6be8234e6ff90a1ac34acbeb826903b02cfa0da11c82cbc042
[root@myserver ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              2622e6cca7eb        4 weeks ago         132MB
centos              7                   b5b4d78bc90c        2 months ago        203MB
hello-world         latest              bf756fb1ae65        6 months ago        13.3kB
[root@myserver ~]#

commit 将容器变化为镜像

[root@myserver ~]# docker commit -a "devopsvipxx" -m "hhahh" webserver webserver:v1
sha256:d315f2bb406067262798358e85aaef06f2d99dfc1adbc8380dd9cc5d0f809e76
[root@myserver ~]# docker history webserver:v1
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
d315f2bb4060        2 seconds ago       nginx -g daemon off;                            1.39kB              hhahh
2622e6cca7eb        4 weeks ago         /bin/sh -c #(nop)  CMD ["nginx" "-g" "daemon…   0B
<missing>           4 weeks ago         /bin/sh -c #(nop)  STOPSIGNAL SIGTERM           0B
<missing>           4 weeks ago         /bin/sh -c #(nop)  EXPOSE 80                    0B
<missing>           4 weeks ago         /bin/sh -c #(nop)  ENTRYPOINT ["/docker-entr…   0B
<missing>           4 weeks ago         /bin/sh -c #(nop) COPY file:cc7d4f1d03426ebd…   1.04kB
<missing>           4 weeks ago         /bin/sh -c #(nop) COPY file:b96f664d94ca7bbe…   1.96kB
<missing>           4 weeks ago         /bin/sh -c #(nop) COPY file:d68fadb480cbc781…   1.09kB
<missing>           4 weeks ago         /bin/sh -c set -x     && addgroup --system -…   62.9MB
<missing>           4 weeks ago         /bin/sh -c #(nop)  ENV PKG_RELEASE=1~buster     0B
<missing>           4 weeks ago         /bin/sh -c #(nop)  ENV NJS_VERSION=0.4.1        0B
<missing>           4 weeks ago         /bin/sh -c #(nop)  ENV NGINX_VERSION=1.19.0     0B
<missing>           4 weeks ago         /bin/sh -c #(nop)  LABEL maintainer=NGINX Do…   0B
<missing>           4 weeks ago         /bin/sh -c #(nop)  CMD ["bash"]                 0B
<missing>           4 weeks ago         /bin/sh -c #(nop) ADD file:4d35f6c8bbbe6801c…   69.2MB

build 构建镜像

Dockerfile

FROM nginx



docker build .
docker build -f aa/Dockerfile .

[root@myserver docker]# docker build .
Sending build context to Docker daemon  269.2MB
Step 1/1 : FROM webserver:v1
 ---> d315f2bb4060
Successfully built d315f2bb4060
[root@myserver docker


[root@myserver docker]# docker build -t webserver:v2  .
Sending build context to Docker daemon  269.2MB
Step 1/1 : FROM webserver:v1
 ---> d315f2bb4060
Successfully built d315f2bb4060
Successfully tagged webserver:v2

load 通过归档文件加载镜像 save 将镜像制作归档文件

[root@myserver docker]# docker save -o webserver-v2.tar webserver:v2
[root@myserver docker]# docker rmi webserver:v2
Untagged: webserver:v2
Deleted: sha256:5aa7a154ce2f07e8dc94d331d8809ea40ea5fab4ffa8935e2092771f863e3c0f
Deleted: sha256:eef9ee7ccbdfd908b67057aa17a6d7292bbc0e7cb04a57896d25ce24a6e115b7

[root@myserver docker]# docker load -i webserver-v2.tar
eef9ee7ccbdf: Loading layer  134.6MB/134.6MB
Loaded image: webserver:v2
[root@myserver docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
webserver           v2                  5aa7a154ce2f        18 hours ago        130MB
webserver           v1                  d315f2bb4060        18 hours ago        132MB
<none>              <none>              46a71bf11111        18 hours ago        132MB
<none>              <none>              5ab53db1229d        18 hours ago        132MB
nginx               latest              2622e6cca7eb        4 weeks ago         132MB
centos              7                   b5b4d78bc90c        2 months ago        203MB
devopsvip/docker    latest              bf756fb1ae65        6 months ago        13.3kB
hello-world         latest              bf756fb1ae65        6 months ago        13.3kB
[root@myserver docker]#

create 创建一个容器

[root@myserver docker]# docker create --name webnewserver nginx
bbc81d2c72831167da41a6ccda4e06e6efe51eba81cf6daace0f49e5815e88db
[root@myserver docker]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@myserver docker]# docker ps  -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS                  NAMES
bbc81d2c7283        nginx               "/docker-entrypoint.…"   17 seconds ago      Created                                              webnewserver
d2813b963195        nginx               "/docker-entrypoint.…"   19 hours ago        Created                                              youthful_ramanujan
4fe81888bdd1        nginx               "/docker-entrypoint.…"   24 hours ago        Exited (255) 11 minutes ago   0.0.0.0:8000->80/tcp   webserver
[root@myserver docker]# docker start  webnewserver
webnewserver
[root@myserver docker]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
bbc81d2c7283        nginx               "/docker-entrypoint.…"   41 seconds ago      Up 4 seconds        80/tcp              webnewserver
[root@myserver docker]#

update 更新容器的配置

[root@myserver docker]# docker update --memory 1G --memory-swap 1G  webaserver

[root@myserver docker]# docker stats webaserver
CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT   MEM %               NET I/O             BLOCK I/O           PIDS
bd8375d39858        webaserver          0.00%               2.445MiB / 1GiB     0.24%               3.69kB / 0B

前端项目容器化

1.1 手动部署前端项目

npm 安装, 直接输入npm指令,centos8 中会提示你安装。也可以手动安装nodejs环境。

git clone https://github.com/dockersamples/node-bulletin-board
cd node-bulletin-board/bulletin-board-app
npm install 

[root@myserver bulletin-board-app]# npm install

> ejs@2.7.4 postinstall /root/newdocker/node-bulletin-board/bulletin-board-app/node_modules/ejs
> node ./postinstall.js

Thank you for installing EJS: built with the Jake JavaScript build tool (https://jakejs.com/)

npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN vue-event-bulletin@1.0.0 No repository field.
npm WARN The package morgan is included as both a dev and production dependency.

added 91 packages from 168 contributors and audited 92 packages in 10.751s
found 0 vulnerabilities


[root@myserver bulletin-board-app]# npm start

> vue-event-bulletin@1.0.0 start /root/newdocker/node-bulletin-board/bulletin-board-app
> node server.js

Magic happens on port 8080...

访问:http://192.168.1.105:8080/#


1.2 使用docker完成

编写dockerfile

FROM node:current-slim    ## 基础镜像

WORKDIR /usr/src/app      ## 指定工作目录
COPY package.json .       ## 复制package.json
RUN npm install      ## 进行安装

EXPOSE 8080        ## 端口
CMD [ "npm", "start" ]  ## 运行npm start

COPY . .         ## 复制当前目录内容

构建生成镜像

docker build  -t bulletinboard:1.0 .
docker build --tag bulletinboard:1.0 .

创建容器

[root@myserver bulletin-board-app]# docker run -itd --name bb -p 8000:8080 bulletinboard:1.0
a0316d74ef5d943414a065ac147427c00c67cf393dd582ba967e1ffb016f1faa
[root@myserver bulletin-board-app]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
a0316d74ef5d        bulletinboard:1.0   "docker-entrypoint.s…"   9 seconds ago       Up 7 seconds        0.0.0.0:8000->8080/tcp   bb




####
-p: --publish 
-d: --detach

访问8000端口

删除容器

docker stop bb
docker rm bb
docker rmi  bulletinboard:1.0



docker rm --force bb
docker rmi  bulletinboard:1.0

1.3 上传镜像仓库

登录docker hub 创建一个仓库 命名为 bulletinboard.

[root@myserver bulletin-board-app]# docker tag bulletinboard:1.0 devopsvip/bulletinboard:1.0
[root@myserver bulletin-board-app]# docker push devopsvip/bulletinboard:1.0
The push refers to repository [docker.io/devopsvip/bulletinboard]

ec3c84f0ff74: Pushed
8783c0df2a9c: Pushed
d3e283a0a9a4: Pushed
0e25f1fd8714: Mounted from library/node
203a5e3508e7: Mounted from library/node
c7e05f78fd37: Mounted from library/node
90a1d8ebd7b4: Mounted from library/node
333e2cb4c707: Mounted from library/node
1.0: digest: sha256:420b025a1db705a1709783d028cd5a71825c47c969b5dab8c284a8afd18f2833 size: 1992

FAQ

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.40/images/json: dial unix /var/run/docker.sock: connect: permission denied


## 解决方法
[usera@myserver ~]$ ll /var/run/docker.sock
srw-rw---- 1 root docker 0 Jul  8 21:26 /var/run/docker.sock
[usera@myserver ~]$ exit
logout
[root@myserver ~]# usermod -g docker usera
[root@myserver ~]# id usera
uid=1013(usera) gid=965(docker) 组=965(docker)
[root@myserver ~]# su - usera
[usera@myserver ~]$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker: Error response from daemon: Conflict. The container name "/mywebserver" is already in use by container "768c4b899b6736dbef696908873a3db502c252f7d8a3c65d901af72e1bdc17dd". You have to remove (or rename) that container to be able to reuse that name.
See 'docker run --help'.

解决方法
1. 更改名称 docker run --name mywebserver01 -it nginx 
2. 删除容器

[root@myserver ~]# docker rm mywebserver
mywebserver
[root@myserver ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
01bdedde3725 s ago       Exited (0) 9 minutes ago                       hopeful_mendel
[root@myserver ~]# dcoker rm 01bdedde3725
bash: dcoker: 未找到命令...
相似命令是: 'docker'
[root@myserver ~]# docker rm 01bdedde3725
01bdedde3725
[root@myserver ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@myserver ~]#
Error response from daemon: Container 8d485b358cc693c1e2b2d9b5c1d1a8092265d79bdcf1ee54d3d2f615f4f22064 is not running  

解决方法: 容器未运行需要重新启动

如何能够外部访问nginx?-p 端口绑定 宿主机端口:容器服务端口

[root@myserver ~]# docker stop webserver
webserver
[root@myserver ~]# docker rm webserver
webserver
[root@myserver ~]# docker run --name webserver -p 8000:80 -itd nginx
4fe81888bdd163ae149fa4c60ddb41c4727d6120ee7203fa2933601df9ab1733
[root@myserver ~]# docker port webserver
80/tcp -> 0.0.0.0:8000
[root@myserver ~]# curl 127.0.0.1:8000
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

如何注册docker hub账号, devopsvip / passxxxxxx

这里选择免费版本

邮件激活

无法上传docker镜像

denied: requested access to the resource is denied

## 解决方法
docker login

无法下载镜像

Error response from daemon: pull access denied for devopsvip/docker, repository does not exist or may require 'docker login'

## 解决方法
docker login

如何将本机的镜像复制到远程的机器?

本机:192.168.1.199

[root@myserver docker]# docker save -o webserver-v2.tar webserver:v2
[root@myserver docker]# ls
bin   docker-entrypoint.d   etc   lib64  opt   run   sys       usr
boot  docker-entrypoint.sh  home  media  proc  sbin  test.tar  var
dev   Dockerfile            lib   mnt    root  srv   tmp       webserver-v2.tar
[root@myserver docker]# scp webserver-v2.tar  root@192.168.1.200:/tmp/

远程机器:192.168.1.200

[root@zeyang-nuc-service tmp]# docker load -i webserver-v2.tar
eef9ee7ccbdf: Loading layer  134.6MB/134.6MB
Loaded image: webserver:v2
[root@zeyang-nuc-service tmp]# docker images | grep webserver
webserver                                                                                            v2                              5aa7a154ce2f        18 hours ago        130MB

本文分享自微信公众号 - DevOps云学堂(idevopsvip),作者:Lizeyang

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2020-07-12

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • SpringBoot应用容器化过程

    项目地址:https://github.com/gazgeek/springboot-helloworld.git

    泽阳
  • 如何在Docker容器中运行Docker [3种方法]

    /var/run/docker.sock是默认的Unix套接字。套接字用于在同一主机上的进程之间进行通信。Docker守护程序默认情况下侦听docker.soc...

    泽阳
  • GitLabCI作业中如何使用私有仓库镜像?

    今天有同学在GitlabCI课程实践中遇到了一个问题,当runner需要下载私有镜像仓库中的镜像报错,提示没有权限。如果是在vm或者物理机注册的docker类型...

    泽阳
  • docker常用操作

    启动镜像nginx并:1.指定容器名称(sunshine-nginx-test);2.后台运行(-d);3.指定端口绑定(-p)

    CS逍遥剑仙
  • 虚拟化docker的成长之路

    首先修改这个文件,在里面加上启动nginx的命令,比如 /usr/local/sbin/nginx

    思梦php
  • 容器技术|Docker三剑客之Compose

    docker技术是基于Linux内核的cgroup技术实现的,那么问题来了,在非Linux平台上是否就不能使用docker技术了呢?答案是可以的,不过显然需要借...

    民工哥
  • 迁移 Docker 容器储存位置

    本文使用「署名 4.0 国际 (CC BY 4.0)」许可协议,欢迎转载、或重新修改使用,但需要注明来源。 署名 4.0 国际 (CC BY 4.0)

    soulteary
  • 这20个Docker Command,有几个是你会的?

    安装完成docker容器服务之后,需要了解如何操作它?在shell命令行下直接输入docker就可以查看帮助信息,如下。

    民工哥
  • docker--docker 的web可视化管理工具

    当 Docker 部署规模逐步变大后,可视化监控容器环境的性能和健康状态将会变得越来越 重要。 Docker的图形化管理工具,提供状态显示面板、应用模板快速部署...

    eadela
  • Docker Compose笔记 03 CentOS 快速安装 Docker 全家桶

    主要参考:https://docs.docker.com/install/linux/docker-ce/centos/

    twowinter

扫码关注云+社区

领取腾讯云代金券