Skip to content

Docker笔记

拉取镜像

shell
  ~ docker pull ubuntu

运行容器

shell
  ~ docker run ubuntu echo hello docker
hello docker

查看镜像

shell
  ~ docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
ubuntu       latest    6a47e077731f   5 weeks ago   69.2MB

查看docker进程

  • 查看正在运行的docker进程
shell
  ~ docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                  NAMES
8d4e8418e8ff   nginx     "/docker-entrypoint.…"   16 seconds ago   Up 15 seconds   0.0.0.0:8080->80/tcp   charming_turing
  • 查看所有docker进程(包括没有运行的)
shell
  ~ docker ps -a

删除容器(container)

shell
  ~ docker rm 8d4e8418e8ff

复制文件(夹)到容器

  • 在host和container之间拷贝文件
shell
  ~ vi index.html
  ~ cat index.html
<html>
<h1>Docker is fun!</h1>
</html>
  ~ docker cp index.html 8d4e8418e8ff://usr/share/nginx/html

保存改动为新的镜像

shell
  ~ docker run -p 8080:80 -d nginx
53fd7cd4b4dccc6ffb408f3cbe273f393cd0d44f575e0acd8cfde772311cbae3
  ~ vi index.html
  ~ cat index.html
<html>
<h1>Docker is fun!</h1>
</html>
  ~ docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                  NAMES
53fd7cd4b4dc   nginx     "/docker-entrypoint.…"   4 minutes ago   Up 4 minutes   0.0.0.0:8080->80/tcp   serene_banzai
  ~ docker cp index.html 53fd7cd4b4dc://usr/share/nginx/html
  ~ docker commit -m 'fun' 53fd7cd4b4dc nginx-fun
sha256:de9d18c33852908ad5547cff249e0382bef57be6646221a0218d58cdea00bf03
  ~ docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
nginx-fun    latest    de9d18c33852   34 seconds ago   192MB
nginx        latest    2a4fbb36e966   18 hours ago     192MB
ubuntu       latest    6a47e077731f   5 weeks ago      69.2MB

停止容器

shell
  ~ docker stop 8d4e8418e8ff
8d4e8418e8ff

停止所有容器

  • -a:显示所有的容器,包括未运行的。
  • -q:静默模式,只显示容器编号。
shell
  ~ docker stop `docker ps -a -q`
3f0afc558920
846c7bbecadf

删除所有处于停止状态的容器

方法1:docker container prune

shell
  ~ docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
3f0afc5589208e1ee61daecf125fe01304720ba96a6bcef610199fd5ae2dda0d
846c7bbecadf75ecbb0d6ce7b9c2304a577762966551b56ccb8c50346076f41a

Total reclaimed space: 2.186kB

方法2:docker rm docker ps -a -q

shell
  ~ docker rm `docker ps -a -q`
ed8693051df7
b87501693c74

删除特定状态的容器

状态:created、exited、restarting或dead

shell
  ~ docker container rm $(docker container ls -aqf status=exited)
ac07228391f0
1cf7e8dc3a8b

删除所有容器

删除所有容器,包括正在运行的容器

shell
  ~ docker container rm -f $(docker container ls -aq)
cf3c82a86586
3253f7887bda
f09e7566aadd

删除镜像(image)

shell
  ~ docker rmi 2a4fbb36e966

删除所有未被使用的镜像

shell
  ~ docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B

删除特定镜像

docker image rm <image_name>:<tag>

  • <image_name> 要删除的镜像名称
  • <tag> 要删除的镜像标签
shell
  ~ docker image rm nginx:latest
Untagged: nginx:latest
Untagged: nginx@sha256:32da30332506740a2f7c34d5dc70467b7f14ec67d912703568daff790ab3f755
Deleted: sha256:2a4fbb36e96607b16e5af2e24dc6a1025a4795520c98c6b9ead9c4113617cb73
Deleted: sha256:807d78e4cdc589c2cdefb7bb6049a31bcc7bf0f90f335a8a8de1abeb0e6a3993
Deleted: sha256:89b7e61557f3502dc01aeae499b451574376c3e4c46e49294c72e22cd2d9634c
Deleted: sha256:a216b497301a9fc6283077c0906140e22b8f3a67ba92ab6253f036f48e1ff4b1
Deleted: sha256:a3169fdf28b9329e3f1882c095bf11e12a465419c759169682ca706de81bef84
Deleted: sha256:9f55692205d904037fbc69a1d59354665af3c0c2a767b5e799e719170085bc11
Deleted: sha256:a0e6c2974935e56e8d57d6fea041399b290da1566d82ea1a7d9c3ec7ef60a366
Deleted: sha256:311627f8702d2d97e63b916cad711aa9f962a1f20da5572d68b8f223ff051e73

删除所有镜像

将删除所有镜像。请注意,如果某个镜像正在被使用,则无法删除该镜像。

shell
  ~ docker image rm $(docker image ls -aq)
Untagged: hello_docker:latest
Deleted: sha256:66273161572e0a9b6d5a8d8d46afc001c4bfcc1b83f0b46c026b71aaec8bc219
Error response from daemon: conflict: unable to delete 6a47e077731f (must be forced) - image is being used by stopped container 01a79b926ecf

按条件删除镜像

删除无标签镜像(即 TAG 为 none 的镜像 dangling image,悬浮镜像)

shell
docker rmi `docker images -q | awk '/^<none>/ { print $3 }'`

也可以使用如下命令删除

shell
docker rmi $(docker images -q -f dangling=true)

删除包含某关键字的镜像

shell
docker rmi --force `docker images | grep test-api | awk '{print $3}'`

其中 test-api 为关键字

dockerfile构建镜像

Dockerfile语法

命令用途
FROMbase image
RUN执行命令
ADD添加文件
COPY拷贝文件
CMD执行命令
EXPOSE暴露端口
WORKDIR指定路径
MAINTAINER维护者
ENV设定环境变量
ENTRYPOINT容器入口
USER指定用户
VOLUMEmount point

【示范1】构建简单echo输出的镜像

shell
  ~ mkdir d1
  ~ cd d1
  d1 vi Dockerfile
  d1
  d1 cat Dockerfile
FROM alpine:latest
MAINTAINER siushin
CMD echo "Hello Docker!"
  d1
  d1 docker build -t hello_docker .
[+] Building 10.6s (5/5) FINISHED
 => [internal] load build definition from Dockerfile                                                                      0.0s
 => => transferring dockerfile: 142B                                                                                      0.0s
 => [internal] load .dockerignore                                                                                         0.0s
 => => transferring context: 2B                                                                                           0.0s
 => [internal] load metadata for docker.io/library/alpine:latest                                                          8.7s
 => [1/1] FROM docker.io/library/alpine:latest@sha256:7144f7bab3d4c2648d7e59409f15ec52a18006a128c733fcff20d3a4a54ba44a    1.8s
 => => resolve docker.io/library/alpine:latest@sha256:7144f7bab3d4c2648d7e59409f15ec52a18006a128c733fcff20d3a4a54ba44a    0.0s
 => => sha256:f6648c04cd6ce95adc05b3aa55265007b95d64d508755be8506cee652792952c 1.49kB / 1.49kB                            0.0s
 => => sha256:9fda8d8052c61740409c4bea888859c141fd8cc3f58ac61943144ff6d1681b2d 3.33MB / 3.33MB                            1.7s
 => => sha256:7144f7bab3d4c2648d7e59409f15ec52a18006a128c733fcff20d3a4a54ba44a 1.64kB / 1.64kB                            0.0s
 => => sha256:b312e4b0e2c665d634602411fcb7c2699ba748c36f59324457bc17de485f36f6 528B / 528B                                0.0s
 => => extracting sha256:9fda8d8052c61740409c4bea888859c141fd8cc3f58ac61943144ff6d1681b2d                                 0.1s
 => exporting to image                                                                                                    0.0s
 => => exporting layers                                                                                                   0.0s
 => => writing image sha256:66273161572e0a9b6d5a8d8d46afc001c4bfcc1b83f0b46c026b71aaec8bc219                              0.0s
 => => naming to docker.io/library/hello_docker
  d1
  d1 docker images
REPOSITORY     TAG       IMAGE ID       CREATED        SIZE
nginx          latest    2a4fbb36e966   25 hours ago   192MB
ubuntu         latest    6a47e077731f   5 weeks ago    69.2MB
hello_docker   latest    66273161572e   6 weeks ago    7.66MB
  d1
  d1 docker run hello_docker
Hello Docker!

【示范2】构建自定义nginx配置的镜像

shell
  ~ mkdir d2
  ~ cd d2
  d2 vi Dockerfile
  d2 cat Dockerfile 
FROM ubuntu
MAINTAINER siushin
RUN sed -i 's/archive.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
RUN apt-get update
RUN apt-get install -y nginx 
COPY index.html /var/www/html
ENTRYPOINT ["/usr/sbin/nginx", "-g", "daemon off;"]
EXPOSE 80
  d2 
  d2 touch index.html
  d2 vi index.html 
  d2 cat index.html 
今天是周一!
  d2 
  d2 docker build -t lt/hello-nginx .
[+] Building 250.0s (10/10) FINISHED                                                                                                      
 => [internal] load build definition from Dockerfile                                                                                 0.0s
 => => transferring dockerfile: 331B                                                                                                 0.0s
 => [internal] load .dockerignore                                                                                                    0.0s
 => => transferring context: 2B                                                                                                      0.0s
 => [internal] load metadata for docker.io/library/ubuntu:latest                                                                     0.0s
 => [1/5] FROM docker.io/library/ubuntu                                                                                              0.0s
 => [internal] load build context                                                                                                    0.0s
 => => transferring context: 93B                                                                                                     0.0s
 => [2/5] RUN sed -i 's/archive.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list                                              0.2s
 => [3/5] RUN apt-get update                                                                                                       154.1s
 => [4/5] RUN apt-get install -y nginx                                                                                              95.5s
 => [5/5] COPY index.html /var/www/html                                                                                              0.0s 
 => exporting to image                                                                                                               0.1s 
 => => exporting layers                                                                                                              0.1s 
 => => writing image sha256:0f0bf2ef9a320c5590ebb0d27e59315e13b538463e7208c4559abdc7277a620e                                         0.0s 
 => => naming to docker.io/lt/hello-nginx                                                                                            0.0s 
  d2 
  d2 docker run -d -p 8081:80 lt/hello-nginx
52ea653fb0179fe6cb9d6cabb7c95dfbc24e475b84c2972883240e9baa26ebbf
  d2 
  d2 curl http://localhost:8081
今天是周一!
  d2 
  d2 docker ps
CONTAINER ID   IMAGE            COMMAND                  CREATED         STATUS         PORTS                  NAMES
52ea653fb017   lt/hello-nginx   "/usr/sbin/nginx -g …"   3 minutes ago   Up 3 minutes   0.0.0.0:8081->80/tcp   unruffled_colden
164eeead86e0   nginx            "/docker-entrypoint.…"   4 days ago      Up 4 days      0.0.0.0:8080->80/tcp   nice_johnson
  d2 
  d2 docker images
REPOSITORY       TAG       IMAGE ID       CREATED         SIZE
lt/hello-nginx   latest    0f0bf2ef9a32   5 minutes ago   165MB

镜像分层

Dockerfile中的每一行都产生一个新层

FROM alpine:latest 4e38e38c8ce0
MAINTAINER siushin fb1aabf4427b
CMD echo 'hello docker' 3df065bfdff6

镜像分层
container layer RW
CMD echo 'hello docker' RO
MAINTAINER siushin RO
FROM alpine:latest RO

进入容器交互式命令行

shell
  ~ docker exec -it nginx bash
root@7c80edc1aab3:/# ls
bin  boot  dev  etc  home  lib  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

Volume

提供独立于容器之外的持久化存储

shell
  d2 docker run -p 8080:80 -d --name nginx -v /usr/share/nginx/html nginx
7c80edc1aab3e85d2d5c1e5cc01596d1c38810ba276125c1690aaaa933d5f201
  d2 
  d2 docker inspect nginx
# 输出部分节选
"Mounts": [
    {
        "Type": "volume",
        "Name": "e00b47e0127767dd319a22682903e1db7375dfa2b1fa358f65e18ded060e890b",
        "Source": "/var/lib/docker/volumes/e00b47e0127767dd319a22682903e1db7375dfa2b1fa358f65e18ded060e890b/_data",
        "Destination": "/usr/share/nginx/html",
        "Driver": "local",
        "Mode": "",
        "RW": true,
        "Propagation": ""
    }
],
  d2 ls /var/lib/docker/volumes/e00b47e0127767dd319a22682903e1db7375dfa2b1fa358f65e18ded060e890b/_data
ls: /var/lib/docker/volumes/e00b47e0127767dd319a22682903e1db7375dfa2b1fa358f65e18ded060e890b/_data: No such file or directory
  • /usr/share/nginx/html 为容器内部指定nginx存放代码位置
  • "Mounts":其中"Source"的目录路径为Mac本地路径(不能直接访问,须借助screen命令),映射到容器的"Destination"的目录路径上

本地目录挂载到docker容器中

shell
  d2 docker run -p 8080:80 -d -v $PWD/html:/usr/share/nginx/html nginx
ba60f2bb136a35d39128078ca68f8a86f5fda48418a2dd954586b7e8ac3241af

volume-from容器间互相挂载目录

shell
  ~ mkdir vol3
  ~ cd vol3
  vol3 mkdir data
  vol3 ls
data
  vol3 docker create -v $PWD/data:/var/mydata --name data_container ubuntu
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
20274425734a: Already exists
Digest: sha256:aabed3296a3d45cede1dc866a24476c4d7e093aa806263c27ddaadbdce3c1054
Status: Downloaded newer image for ubuntu:latest
7a1c36991aad65505386969b86862943c9d216019bf3f800b21c771d69bdc122
  vol3 docker run -it --volumes-from data_container ubuntu /bin/bash
root@d01f37a3cffe:/# mount
overlay on / type overlay (rw,relatime,lowerdir=/var/lib/docker/overlay2/l/IDHNZN3AKPGRGE2ZERIDNLWWY7:/var/lib/docker/overlay2/l/QYR6MFEDKJPSDPGUPS7PV627NG,upperdir=/var/lib/docker/overlay2/47f5ad5b729b2a7fe6ae0a297c7060189e94de9c55f86d0252dcfbf9e10e7793/diff,workdir=/var/lib/docker/overlay2/47f5ad5b729b2a7fe6ae0a297c7060189e94de9c55f86d0252dcfbf9e10e7793/work)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev type tmpfs (rw,nosuid,size=65536k,mode=755)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=666)
sysfs on /sys type sysfs (ro,nosuid,nodev,noexec,relatime)
cgroup on /sys/fs/cgroup type cgroup2 (ro,nosuid,nodev,noexec,relatime)
mqueue on /dev/mqueue type mqueue (rw,nosuid,nodev,noexec,relatime)
shm on /dev/shm type tmpfs (rw,nosuid,nodev,noexec,relatime,size=65536k)
grpcfuse on /var/mydata type fuse.grpcfuse (rw,nosuid,nodev,relatime,user_id=0,group_id=0,allow_other,max_read=1048576)
/dev/vda1 on /etc/resolv.conf type ext4 (rw,relatime)
/dev/vda1 on /etc/hostname type ext4 (rw,relatime)
/dev/vda1 on /etc/hosts type ext4 (rw,relatime)
devpts on /dev/console type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=666)
proc on /proc/bus type proc (ro,nosuid,nodev,noexec,relatime)
proc on /proc/fs type proc (ro,nosuid,nodev,noexec,relatime)
proc on /proc/irq type proc (ro,nosuid,nodev,noexec,relatime)
proc on /proc/sys type proc (ro,nosuid,nodev,noexec,relatime)
proc on /proc/sysrq-trigger type proc (ro,nosuid,nodev,noexec,relatime)
tmpfs on /proc/kcore type tmpfs (rw,nosuid,size=65536k,mode=755)
tmpfs on /proc/keys type tmpfs (rw,nosuid,size=65536k,mode=755)
tmpfs on /proc/timer_list type tmpfs (rw,nosuid,size=65536k,mode=755)
tmpfs on /sys/firmware type tmpfs (ro,relatime)
root@d01f37a3cffe:/# cd /var/mydata
root@d01f37a3cffe:/var/mydata# ls
root@d01f37a3cffe:/var/mydata# touch whatever.txt
root@d01f37a3cffe:/var/mydata# exit
exit
  vol3 cd data
  data ls
whatever.txt

【从仓库搜索镜像】docker search XXX

shell
  ~ docker search whalesay
NAME                           DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker/whalesay                An image for use in the Docker demo tutorial    741
nikovirtala/whalesay           Tiny Go web service to print Moby Dock ASCII…   1                    [OK]
ojenge/whalesay                from docker/whalesay                            1
caibar/whalesay                Builds automatizados.                           1                    [OK]
sabs1117/whalesay              Whalesay with fortune phrases.                  1
whalebrew/whalesay                                                             1
blaines/whalesay                                                               0
maghnus/whalesayentrypoint     I need a version of whalesay with entrypoint…   0
getporter/whalesay-installer                                                   0
gapfish/whalesay-ruby          whalesay function test                          0
yang225217/whalesay                                                            0
carolynvs/whalesayd                                                            0
ox0spy/whalesay-fortune        like docker/whalesay, just using fortunes pr…   0
getporter/whalesay                                                             0
dhalljohnston/whalesay         whalesay                                        0
tigerj/whalesay                                                                0
phyominhtun/whalesay           WhaleSay Container                              0
vnovoselskiy/whalesay                                                          0
techjos/whalesay                                                               0
nikovirtala/whalesay-win       Whalesay running on Microsoft IIS on Nano Se…   0                    [OK]
tpei/whalesay-crystal          crystal faas demo function                      0
hongxi/whalesay-fortunes       Demo, the whalesay-fortunes                     0
ok7827125/whalesay                                                             0
burleyinnersbm07/whalesay      Edit to the WhaleSay                            0
csvdocker/whalesay

【从仓库拉取镜像】docker pull XXX

shell
  ~ docker pull docker/whalesay
Using default tag: latest
latest: Pulling from docker/whalesay
Image docker.io/docker/whalesay:latest uses outdated schema1 manifest format. Please upgrade to a schema2 image for better future compatibility. More information at https://docs.docker.com/registry/spec/deprecated-schema-v1/
e190868d63f8: Pull complete
909cd34c6fd7: Pull complete
0b9bfabab7c1: Pull complete
a3ed95caeb02: Pull complete
00bf65475aba: Pull complete
c57b6bcc83e3: Pull complete
8978f6879e2f: Pull complete
8eed3712d2cf: Pull complete
Digest: sha256:178598e51a26abbc958b8a2e48825c90bc22e641de3d31e18aaf55f3258ba93b
Status: Downloaded newer image for docker/whalesay:latest
docker.io/docker/whalesay:latest
  ~ docker images
REPOSITORY        TAG       IMAGE ID       CREATED        SIZE
docker/whalesay   latest    6b362a9f73eb   8 years ago    247MB
  ~ docker run docker/whalesay cowsay Docker很好玩!
WARNING: The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested
 ____________________
< Docker很好玩! >
 --------------------
    \
     \
      \
                    ##        .
              ## ## ##       ==
           ## ## ## ##      ===
       /""""""""""""""""___/ ===
  ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~
       \______ o          __/
        \    \        __/
          \____\______/

【推送本地镜像到远程仓库】docker push 用户名/镜像名:<标签名>

  • 【注意】用户名必须跟dockerhub保持一致
  • 标签名可不填,缺省,默认值:latest
shell
  ~ docker tag docker/whalesay siushin/whalesay
  ~ docker images
REPOSITORY         TAG       IMAGE ID       CREATED       SIZE
docker/whalesay    latest    6b362a9f73eb   8 years ago   247MB
siushin/whalesay   latest    6b362a9f73eb   8 years ago   247MB
  ~
  ~ docker login
Authenticating with existing credentials...
Login Succeeded

Logging in with your password grants your terminal complete access to your account.
For better security, log in with a limited-privilege personal access token. Learn more at https://docs.docker.com/go/access-tokens/
  ~
  ~ docker push siushin/whalesay:latest
The push refers to repository [docker.io/siushin/whalesay]
5f70bf18a086: Pushed
d061ee1340ec: Pushed
d511ed9e12e1: Pushed
091abc5148e4: Pushed
b26122d57afa: Pushed
37ee47034d9b: Pushed
528c8710fd95: Pushed
1154ba695078: Pushed
latest: digest: sha256:df326a383b4a036fd5a33402248027d1c972954622924158a28744ed5f9fca1e size: 2402

安装Docker Compose

Dockerfile 可以让用户管理一个单独的应用容器;而 Compose 则允许用户在一个模板(YAML 格式)中定义一组相关联的应用容器(被称为一个 project,即项目),例如一个 Web 服务容器再加上后端的数据库服务容器等

  • Mac/Windows

自带,无需单独安装

  • Linux安装
shell
# 安装docker-compose
[root@VM-0-3-centos ~]# uname -s
Linux
[root@VM-0-3-centos ~]# uname -m
x86_64
[root@VM-0-3-centos ~]# curl -L https://github.com/docker/compose/releases/download/v2.21.0/docker-compose-$(uname -s)-$(uname -m) > /usr/local/bin/docker-compose
[root@VM-0-3-centos ~]# ls -l /usr/local/bin/docker-compose
[root@VM-0-3-centos ~]# chmod a+x /usr/local/bin/docker-compose
[root@VM-0-3-centos ~]# docker-compose --version

# 卸载:二进制包方式安装的,直接删除二进制文件即可
[root@VM-0-3-centos ~]# rm /usr/local/bin/docker-compose

查看docker-compose版本

shell
  ~ docker-compose -v
Docker Compose version v2.15.1
  ~ docker compose version
Docker Compose version v2.15.1

docker-compose常见命令

  • docker-compose build // 创建容器
  • docker-compose up // 启动项目,可查看输出信息
  • docker-compose up -d // 启动项目,后台执行
  • docker-compose stop // 停止容器
  • docker-compose rm // 删除所有容器
  • docker-compose logs // 观察容器的日志
  • docker-compose ps // 查看容器 网站首页: http://localhost:80/http://localhost:2368/

docker-compose.yml常见命令

命令用途
build本地创建镜像
command覆盖缺省命令
depends_on连接容器
ports暴露端口
volumes
imagepull镜像

docker-compose搭建ghost博客

shell
  docker mkdir ghost
  docker cd ghost
  ghost mkdir nginx
  ghost mkdir data
  ghost mkdir ghost
  ghost ls
data  ghost nginx
  ghost cd ghost
  ghost 
  ghost touch Dockerfile
  ghost vi Dockerfile
  ghost cat Dockerfile
FROM ghost
COPY ./config.js /var/lib/ghost/content/config.js
EXPOSE 2368
  ghost 
  ghost vi config.js
  ghost cat config.js
var path = require('path'),
    config;

config = {
    production: {
        url: 'http://mytestblog.com',
        mail: {},
        database: {
            client: 'mysql',
            connection: {
                host: 'db', user: 'ghost', password: 'ghost', database: 'ghost', port: '3306', charset: 'utf8'
            },
            debug: false
        },
        paths: {
            contentPath: path.join(process.env.GHOST_CONTENT, '/')
        },
        server: {
            host: '0.0.0.0',
            port: '2368'
        }
    }
};

module.exports = config;
  ghost 
  ghost cd ../nginx
  nginx vi Dockerfile
  nginx cat Dockerfile
FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
  nginx 
  nginx vi nginx.conf
  nginx cat nginx.conf
worker_processes 4;
events {worker_connections 1024;}
http {
    server {
        listen 80;
        location / {
	    proxy_pass http://ghost-app:2368;
        }
    }	
}
  nginx 
  nginx cd ..
  ghost ls
data  ghost nginx
  ghost vi docker-compose.yml
  ghost cat docker-compose.yml
version: '2'

networks:
  ghost:

services:
  ghost-app:
    build: ghost
    restart: always
    networks:
      - ghost
    depends_on:
      - db
    ports:
      - "2368:2368"
    environment:
      # see https://docs.ghost.org/docs/config#section-running-ghost-with-config-env-variables
      database__client: mysql
      database__connection__host: db
      database__connection__user: ghost
      database__connection__password: ghost
      database__connection__database: ghost

  nginx:
    build: nginx
    restart: always
    networks:
      - ghost
    depends_on:
      - ghost-app
    ports:
      - "80:80"

  db:
    image: "mysql:8.0"
    restart: always
    networks:
      - ghost
    environment:
      MYSQL_ROOT_PASSWORD: mysqlroot
      MYSQL_USER: ghost
      MYSQL_PASSWORD: ghost
    volumes:
        - $PWD/data:/var/lib/mysql
    ports:
        - "3306:3306"
  ghost 
  ghost tree .
.
├── data
├── docker-compose.yml
├── ghost
   ├── Dockerfile
   └── config.js
└── nginx
    ├── Dockerfile
    └── nginx.conf

4 directories, 5 files
  ghost 
  ghost docker-compose up -d
  • 如果配置报错,无法全部正常启动
shell
  ghost docker-compose stop
  ghost docker-compose rm
  ghost docker-compose build
  ghost docker-compose up -d
最近更新