高效的利用系统资源(节约成本)
持续交付与部署(敏捷)
多平台的迁移更容易(可移植性)
容易的沙箱机制(安全性)
$ sudo vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://registry.docker-cn.com"]
}
$ sudo service docker restart
sudo apt-get update
$ sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg |sudo apt-key add -
$ sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
sudo wget -qO- https://get.docker.com | sh
# https://docs.docker.com/install/linux/linux-postinstall/#manage-docker-as-a-non-root-user
$ sudo usermod -aG docker $USER
$ docker -v
Docker version 19.03.2, build 6a30dfc
$ docker image pull hello-world
# 以下为抓取过程中的日志信息
Using default tag: latest
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:451ce787d12369c5df2a32c85e5a03d52cbcef6eb3586dd03075f3034f10adcd
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest fce289e99eb9 8 months ago 1.84kB
$ docker container run hello-world
Hello from Docker!
# 以下内容省略
...
$ docker ps
$ docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a662ec198a83 hello-world "/hello" 10 minutes ago Exited (0) 10 minutes ago exciting_wing
$ docker container run --name nginxserver -d -p 8081:80 nginx
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 5a3221f0137b 3 weeks ago 126MB
hello-world latest fce289e99eb9 8 months ago 1.84kB
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b7bf26745b3f nginx "nginx -g 'daemon of…" 23 minutes ago Up 23 minutes 0.0.0.0:8081->80/tcp nginxserver
# docker container kill [containID]
$ docker container kill b7bf26745b3f
$ docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b7bf26745b3f nginx "nginx -g 'daemon of…" 29 minutes ago Exited (137) About a minute ago nginxserver
a662ec198a83 hello-world "/hello" 49 minutes ago Exited (0) 49 minutes ago exciting_wing
$ docker container rm b7bf26745b3f a662ec198a83
$ docker rmi
5a3221f0137b
fce289e99eb9
// /usr/src/nodejs/hello-docker/app.js
const http = require('http');
const PORT = 30010;
const server = http.createServer((req, res) => {
res.end('Hello Docker');
})
server.listen(PORT, () => {
console.log('Running on http://localhost:', PORT);
});
// /usr/src/nodejs/hello-docker/package.json
{
"name": "hello-docker",
"version": "1.0.0",
"description": "",
"author": "May",
"main": "app.js",
"scripts": {
"start": "node app.js"
},
"dependencies": {
}
}
# /usr/src/nodejs/hello-docker/.dockerignore
.git
node_modules
Dockerfile
项目根目录下新建 Dockerfile 文件。
# /usr/src/nodejs/hello-docker/Dockerfile
FROM node:10.0
# 在容器中创建一个目录
RUN mkdir -p /usr/src/nodejs/
# 定位到容器的工作目录
WORKDIR /usr/src/nodejs/
# RUN/COPY 是分层的,package.json 提前,只要没修改,就不会重新安装包
COPY package.json /usr/src/app/package.json
RUN cd /usr/src/app/
RUN npm i
# 把当前目录下的所有文件拷贝到 Image 的 /usr/src/nodejs/ 目录下
COPY . /usr/src/nodejs/
EXPOSE 30010
CMD npm start
FROM:FROM 是构建镜像的基础源镜像,该 Image 文件继承官方的 node image
RUN:后面跟的是在容器中执行的命令
WORKDIR:容器的工作目录
COPY:拷贝文件至容器的工作目录下,.dockerignore 指定的文件不会拷贝
EXPOSE:将容器内的某个端口导出供外部访问
CMD:Dockerfile 执行写一个 CMD 否则后面的会被覆盖,CMD 后面的命令是容器每次启动执行的命令,多个命令之间可以使用 && 链接,例如 CMD git pull && npm start
$ docker image build -t hello-docker .
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-docker latest 6b1c2775591e 4 minutes ago 675MB
node 10.0 1c1272350058 16 months ago 675MB
-d:表明容器的运行模式在后台
-p:端口映射,将本机的 30000 端口映射到容器的 30010 端口,这样在外网就可通过 30000 端口访问到我们的服务
hello-docker:为我们的镜像名字
$ docker container run -d -p 30000:30010 hello-docker
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c2891d477edf hello-docker "/bin/sh -c 'npm sta…" 15 seconds ago Up 14 seconds 0.0.0.0:30000->30010/tcp pedantic_mestorf
$ curl http://localhost:30000
Hello Docker
$ docker logs -f c2891d477edf
> [email protected] start /usr/src/nodejs/hello-docker
> node app.js
Running on http://localhost: 30010
$ docker exec -it c2891d477edf /bin/sh
$ ls # 列出目录列表
Dockerfile app.js package-lock.json package.json
$ node app
events.js:167
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::30010
$ docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 11935 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1651 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 740 [OK]
...
$ docker pull nginx
$ docker login
# docker image tag [imageName] [username]/[repository]:[tag]
$ docker image tag hello-docker mayjun/hello-docker
# docker image push [username]/[repository]:[tag]
$ docker image push mayjun/hello-docker
# compose 下载之后通过管道的方式输入至 /usr/local/bin/docker-compose
# uname -s 查找是什么系统,例如:Linux
# uname -m 查找是什么版本,例如:x86_64
$ curl -L https://github.com/docker/compose/releases/download/1.25.0-rc2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
# 把这个文件变成可执行的
$ chmod +x /usr/local/bin/docker-compose
$ docker-compose --version
docker-compose version 1.25.0-rc2, build 661ac20e
mysql:
image: mysql:5.7
environment:
- MYSQL_ROOT_PASSWORD=123456
- MYSQL_DATABASE=wordpress
web:
image: wordpress
links:
- mysql
environment:
- WORDPRESS_DB_PASSWORD=123456
ports:
- "192.168.6.128:8080:80"
working_dir: /var/www/html
volumes:
- wordpress:/var/www/html
# -d 参数表示后台启动
$ docker-compose up -d
$ docker-compose stop