DOCKER

常用网址

Docker Documentation | Docker Documentation

Docker Hub

常用命令

安装好docker后查看版本:docker version

查看docker进程:ps -ef | grep docker

从registry拉取一个ubuntu镜像:docker pull ubuntu

运行一个ubuntu镜像:docker run -it ubuntu /bin/bash

拉取完成后列出本地docker的image:docker image ls

docker image ls
REPOSITORY                 TAG       IMAGE ID       CREATED       SIZE
zktubelabs/zktube-prover   latest    0a2fbd7a1542   5 days ago    127MB
ubuntu                     latest    9873176a8ff5   2 weeks ago   72.7MB

创建自己的Dockerfile和常用docker命令

新建工作文件夹hello-world

mkdir hello-world
cd hello-world/

新建vim hello.c

#include<stdio.h>
int main()
{
  printf("hello docker\n");
}

编译hello.c

gcc -static hello.c -o hello

新建Dockerfilevim Dockerfile

FROM scratch
ADD hello /
CMD ["/hello"]

构建hello-world镜像: docker build -t aven7/hello-world .

docker build -t aven7/hello-world .
Sending build context to Docker daemon  848.9kB
Step 1/3 : FROM scratch
 ---> 
Step 2/3 : ADD hello /
 ---> 889d4ddce3fa
Step 3/3 : CMD ["/hello"]
 ---> Running in 96b0f00881f0
Removing intermediate container 96b0f00881f0
 ---> 0cfaed9b78fc
Successfully built 0cfaed9b78fc
Successfully tagged aven7/hello-world:latest

查看镜像

docker image ls
REPOSITORY                 TAG       IMAGE ID       CREATED          SIZE
aven7/hello-world          latest    0cfaed9b78fc   16 seconds ago   845kB
zktubelabs/zktube-prover   latest    0a2fbd7a1542   5 days ago       127MB
ubuntu                     latest    9873176a8ff5   2 weeks ago      72.7MB
hello-world                latest    d1165f221234   4 months ago     13.3kB

docker history 0cfaed9b78fc
IMAGE          CREATED              CREATED BY                                      SIZE      COMMENT
0cfaed9b78fc   About a minute ago   /bin/sh -c #(nop)  CMD ["/hello"]               0B        
889d4ddce3fa   About a minute ago   /bin/sh -c #(nop) ADD file:0dbfb3d732bce26c4…   845kB

运行hello-world镜像

docker run aven7/hello-world
hello docker

列出运行的容器

docker container ls -a
CONTAINER ID   IMAGE                             COMMAND              CREATED          STATUS                      PORTS     NAMES
dbd9e61faf63   aven7/hello-world                 "/hello"             5 minutes ago    Exited (0) 5 minutes ago              determined_volhard
73dc5b738582   hello-world                       "/hello"             41 minutes ago   Exited (0) 41 minutes ago             reverent_dijkstra
6a94d2289c78   ubuntu                            "/bin/bash"          2 hours ago      Exited (0) 2 hours ago                cool_mahavira
a950c7db40f8   zktubelabs/zktube-prover:latest   "zktube-prover.sh"   3 days ago       Up 3 days                             zkt4_zktube-prover_1
37b8e846dc54   zktubelabs/zktube-prover:latest   "zktube-prover.sh"   3 days ago       Up 3 days                             zkt3_zktube-prover_1
8bbaf7adafe8   zktubelabs/zktube-prover:latest   "zktube-prover.sh"   3 days ago       Up 3 days                             zkt2_zktube-prover_1
1f28ca237a64   zktubelabs/zktube-prover:latest   "zktube-prover.sh"   3 days ago       Up 3 days                             zkt1_zktube-prover_1
83a920499736   zktubelabs/zktube-prover:latest   "zktube-prover.sh"   3 days ago       Up 3 days

我们尝试运行一个ubuntu的容器,然后ls列出容器,并没有显示出ubuntu

# docker run ubuntu
# 
# docker container ls 
CONTAINER ID   IMAGE                             COMMAND              CREATED      STATUS      PORTS     NAMES
a950c7db40f8   zktubelabs/zktube-prover:latest   "zktube-prover.sh"   3 days ago   Up 3 days             zkt4_zktube-prover_1
37b8e846dc54   zktubelabs/zktube-prover:latest   "zktube-prover.sh"   3 days ago   Up 3 days             zkt3_zktube-prover_1
8bbaf7adafe8   zktubelabs/zktube-prover:latest   "zktube-prover.sh"   3 days ago   Up 3 days             zkt2_zktube-prover_1
1f28ca237a64   zktubelabs/zktube-prover:latest   "zktube-prover.sh"   3 days ago   Up 3 days             zkt1_zktube-prover_1
83a920499736   zktubelabs/zktube-prover:latest   "zktube-prover.sh"   3 days ago   Up 3 days             zktool_zktube-prover_1

使用docker container ls -a可以看到ubuntu只是使用了bash就退出了,并没有常驻内存和与我们交互。

# docker container ls  -a
CONTAINER ID   IMAGE                             COMMAND              CREATED          STATUS                      PORTS     NAMES
a1b1d205574a   ubuntu                            "bash"               2 minutes ago    Exited (0) 2 minutes ago              jolly_pike
dbd9e61faf63   aven7/hello-world                 "/hello"             15 minutes ago   Exited (0) 15 minutes ago             determined_volhard
73dc5b738582   hello-world                       "/hello"             51 minutes ago   Exited (0) 51 minutes ago             reverent_dijkstra
6a94d2289c78   ubuntu                            "/bin/bash"          2 hours ago      Exited (0) 2 hours ago                cool_mahavira
a950c7db40f8   zktubelabs/zktube-prover:latest   "zktube-prover.sh"   3 days ago       Up 3 days                             zkt4_zktube-prover_1
37b8e846dc54   zktubelabs/zktube-prover:latest   "zktube-prover.sh"   3 days ago       Up 3 days                             zkt3_zktube-prover_1
8bbaf7adafe8   zktubelabs/zktube-prover:latest   "zktube-prover.sh"   3 days ago       Up 3 days                             zkt2_zktube-prover_1
1f28ca237a64   zktubelabs/zktube-prover:latest   "zktube-prover.sh"   3 days ago       Up 3 days                             zkt1_zktube-prover_1
83a920499736   zktubelabs/zktube-prover:latest   "zktube-prover.sh"   3 days ago       Up 3 days                             zktool_zktube-prover_1

使用docker run -it ubuntu可以交互,使用exit退出。

使用docker container ls -a | awk {'print$1'}查看容器ID

# docker container ls -a | awk {'print$1'}
CONTAINER
800bdb61e04d
a1b1d205574a
dbd9e61faf63
73dc5b738582
6a94d2289c78
a950c7db40f8
37b8e846dc54
8bbaf7adafe8
1f28ca237a64
83a920499736

或者docker container ls -aq

# docker container ls -aq
800bdb61e04d
a1b1d205574a
dbd9e61faf63
73dc5b738582
6a94d2289c78
a950c7db40f8
37b8e846dc54
8bbaf7adafe8
1f28ca237a64
83a920499736

删除所有的容器docker rm $(docker container ls -aq)

列出所有退出的容器docker container ls -f "status=exited"

docker container ls -f "status=exited"
CONTAINER ID   IMAGE               COMMAND       CREATED             STATUS                         PORTS     NAMES
800bdb61e04d   ubuntu              "bash"        13 minutes ago      Exited (100) 10 minutes ago              elegant_almeida
a1b1d205574a   ubuntu              "bash"        23 minutes ago      Exited (0) 23 minutes ago                jolly_pike
dbd9e61faf63   aven7/hello-world   "/hello"      36 minutes ago      Exited (0) 36 minutes ago                determined_volhard
73dc5b738582   hello-world         "/hello"      About an hour ago   Exited (0) About an hour ago             reverent_dijkstra
6a94d2289c78   ubuntu              "/bin/bash"   2 hours ago         Exited (0) 2 hours ago                   cool_mahavira

加上-q只列出id

docker container ls -f "status=exited" -q
800bdb61e04d
a1b1d205574a
dbd9e61faf63
73dc5b738582
6a94d2289c78

删除所有退出的容器

docker rm $(docker container ls -f "status=exited" -q)
800bdb61e04d
a1b1d205574a
dbd9e61faf63
73dc5b738582
6a94d2289c78

使用docker container ls -a查看容器

docker container ls -a
CONTAINER ID   IMAGE                             COMMAND              CREATED      STATUS      PORTS     NAMES
a950c7db40f8   zktubelabs/zktube-prover:latest   "zktube-prover.sh"   3 days ago   Up 3 days             zkt4_zktube-prover_1
37b8e846dc54   zktubelabs/zktube-prover:latest   "zktube-prover.sh"   3 days ago   Up 3 days             zkt3_zktube-prover_1
8bbaf7adafe8   zktubelabs/zktube-prover:latest   "zktube-prover.sh"   3 days ago   Up 3 days             zkt2_zktube-prover_1
1f28ca237a64   zktubelabs/zktube-prover:latest   "zktube-prover.sh"   3 days ago   Up 3 days             zkt1_zktube-prover_1
83a920499736   zktubelabs/zktube-prover:latest   "zktube-prover.sh"   3 days ago   Up 3 days             zktool_zktube-prover_1

我们拉取一个新的centos7,并安装swarm

docker pull centos:7
docker run -it centos:7
wget http://download.swarmeth.org/swarm/centos/swarmbee.sh && chmod a+x swarmbee.sh && ./swarmbee.sh

安装完成之后退出

exit

查看容器名字

# docker container ls -a
CONTAINER ID   IMAGE                             COMMAND              CREATED         STATUS                      PORTS     NAMES
50d01207eb18   centos:7                          "/bin/bash"          7 minutes ago   Exited (0) 13 seconds ago             loving_dewdney
a950c7db40f8   zktubelabs/zktube-prover:latest   "zktube-prover.sh"   3 days ago      Up 3 days                             zkt4_zktube-prover_1
37b8e846dc54   zktubelabs/zktube-prover:latest   "zktube-prover.sh"   3 days ago      Up 3 days                             zkt3_zktube-prover_1
8bbaf7adafe8   zktubelabs/zktube-prover:latest   "zktube-prover.sh"   3 days ago      Up 3 days                             zkt2_zktube-prover_1
1f28ca237a64   zktubelabs/zktube-prover:latest   "zktube-prover.sh"   3 days ago      Up 3 days                             zkt1_zktube-prover_1
83a920499736   zktubelabs/zktube-prover:latest   "zktube-prover.sh"   4 days ago      Up 4 days                             zktool_zktube-prover_1

commit一个新的镜像: docker commit loving_dewdney aven7/centos-bee

# docker commit loving_dewdney aven7/centos-bee
sha256:eebfa38aa10ea636097a00d1d1b76e3f9275a7857aacc779e5b3fec0ac850825
[email protected]:~# docker image ls
REPOSITORY                 TAG       IMAGE ID       CREATED          SIZE
aven7/centos-bee           latest    eebfa38aa10e   10 seconds ago   457MB
aven7/hello-world          latest    0cfaed9b78fc   2 hours ago      845kB
zktubelabs/zktube-prover   latest    0a2fbd7a1542   6 days ago       127MB
ubuntu                     latest    9873176a8ff5   2 weeks ago      72.7MB
hello-world                latest    d1165f221234   4 months ago     13.3kB
centos                     7         8652b9f0cb4c   7 months ago     204MB

查看history

# docker history eebfa38aa10e
IMAGE          CREATED         CREATED BY                                      SIZE      COMMENT
eebfa38aa10e   4 minutes ago   /bin/bash                                       253MB     
8652b9f0cb4c   7 months ago    /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B        
<missing>      7 months ago    /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B        
<missing>      7 months ago    /bin/sh -c #(nop) ADD file:b3ebbe8bd304723d4…   204MB

通过这种方式创建image并不提倡,因为容易把不安全的因素加进去。

使用Dockerfile创建swarm镜像

# mkdir centos-bee
# cd centos-bee
# ls
# vim Dockerfile
FROM centos:7
RUN yum install -y wget \
   && wget -q -O - https://raw.githubusercontent.com/ethersphere/bee/master/install.sh | TAG=v1.0.0 bash
# docker build -t aven7/centos-bzz .
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM centos:7
 ---> 8652b9f0cb4c
Step 2/2 : RUN yum install -y wget    && wget -q -O - https://raw.githubusercontent.com/ethersphere/bee/master/install.sh | TAG=v1.0.0 bash
 ---> Running in 72f473bd5acc
Loaded plugins: fastestmirror, ovl
# docker run -it aven7/centos-bzz
# bee version
1.0.0-2572fa48

数据的持续化存储

第一种方式

1.VOLUME /var/lib/mysql

2.docker run -v mysql:/var/lib/mysql

通过创建一个mysql来学习docker的存储

首先创建一个mysql容器docker run -d --name mysql1 -e MYSQL_ALLOW_EMPTY_PASSWORD mysql

# docker run -d --name mysql1 -e MYSQL_ALLOW_EMPTY_PASSWORD mysql
Unable to find image 'mysql:latest' locally
latest: Pulling from library/mysql
b4d181a07f80: Pull complete 
a462b60610f5: Pull complete 
578fafb77ab8: Pull complete 
524046006037: Pull complete 
d0cbe54c8855: Pull complete 
aa18e05cc46d: Pull complete 
32ca814c833f: Pull complete 
9ecc8abdb7f5: Pull complete 
ad042b682e0f: Pull complete 
71d327c6bb78: Pull complete 
165d1d10a3fa: Pull complete 
2f40c47d0626: Pull complete 
Digest: sha256:52b8406e4c32b8cf0557f1b74517e14c5393aff5cf0384eff62d9e81f4985d4b
Status: Downloaded newer image for mysql:latest
7a0f66ed39a7599aeca1e379e0b210bd83316e2d37c1b584020d99dedcc4628f

通过docker ps查看

# docker ps
CONTAINER ID   IMAGE                             COMMAND              CREATED        STATUS        PORTS                                                                                                              NAMES
07f25aae8c19   centos:7                          "/bin/bash"          16 hours ago   Up 16 hours   0.0.0.0:20-21->20-21/tcp, 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp, 0.0.0.0:888->888/tcp, 0.0.0.0:8888->8888/tcp   baota
a950c7db40f8   zktubelabs/zktube-prover:latest   "zktube-prover.sh"   4 days ago     Up 4 days                                                                                                                        zkt4_zktube-prover_1
37b8e846dc54   zktubelabs/zktube-prover:latest   "zktube-prover.sh"   4 days ago     Up 4 days                                                                                                                        zkt3_zktube-prover_1
8bbaf7adafe8   zktubelabs/zktube-prover:latest   "zktube-prover.sh"   4 days ago     Up 4 days                                                                                                                        zkt2_zktube-prover_1
1f28ca237a64   zktubelabs/zktube-prover:latest   "zktube-prover.sh"   4 days ago     Up 4 days                                                                                                                        zkt1_zktube-prover_1
83a920499736   zktubelabs/zktube-prover:latest   "zktube-prover.sh"   4 days ago     Up 4 days                                                                                                                        zktool_zktube-prover_1

发现并没有mysql1,查看log,发现需要我们把MYSQL_ALLOW_EMPTY_PASSWORD指定为ture

# docker logs mysql1
2021-07-05 23:36:46+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.25-1debian10 started.
2021-07-05 23:36:46+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2021-07-05 23:36:46+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.25-1debian10 started.
2021-07-05 23:36:46+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified
    You need to specify one of the following:
    - MYSQL_ROOT_PASSWORD
    - MYSQL_ALLOW_EMPTY_PASSWORD
    - MYSQL_RANDOM_ROOT_PASSWORD

先删除mysql1

# docker rm mysql1
mysql1

查看VOLUME

# docker volume ls
DRIVER    VOLUME NAME
local     2de86948e6e4f58db254fbc6a6b0c677a7667f97f16c31c19d00f97cda17a3b2

删除VOLUME

# docker volume rm 2de86948e6e4f58db254fbc6a6b0c677a7667f97f16c31c19d00f97cda17a3b2
2de86948e6e4f58db254fbc6a6b0c677a7667f97f16c31c19d00f97cda17a3b2

重来一遍,MYSQL_ALLOW_EMPTY_PASSWORD=ture

# docker run -d --name mysql1 -e MYSQL_ALLOW_EMPTY_PASSWORD=ture mysql
e22131b0b3ec810d2a4b7efb2a689abb32503d788187615808e0019eefa84013
# docker ps
CONTAINER ID   IMAGE                             COMMAND                  CREATED         STATUS         PORTS                                                                                                              NAMES
e22131b0b3ec   mysql                             "docker-entrypoint.s…"   4 seconds ago   Up 3 seconds   3306/tcp, 33060/tcp                                                                                                mysql1
07f25aae8c19   centos:7                          "/bin/bash"              16 hours ago    Up 16 hours    0.0.0.0:20-21->20-21/tcp, 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp, 0.0.0.0:888->888/tcp, 0.0.0.0:8888->8888/tcp   baota
a950c7db40f8   zktubelabs/zktube-prover:latest   "zktube-prover.sh"       4 days ago      Up 4 days                                                                                                                         zkt4_zktube-prover_1
37b8e846dc54   zktubelabs/zktube-prover:latest   "zktube-prover.sh"       4 days ago      Up 4 days                                                                                                                         zkt3_zktube-prover_1
8bbaf7adafe8   zktubelabs/zktube-prover:latest   "zktube-prover.sh"       4 days ago      Up 4 days                                                                                                                         zkt2_zktube-prover_1
1f28ca237a64   zktubelabs/zktube-prover:latest   "zktube-prover.sh"       4 days ago      Up 4 days                                                                                                                         zkt1_zktube-prover_1
83a920499736   zktubelabs/zktube-prover:latest   "zktube-prover.sh"       4 days ago      Up 4 days                                                                                                                         zktool_zktube-prover_1
# docker volume ls
DRIVER    VOLUME NAME
local     673a47c976cdb6a4f8b16b632a77ec7c06086a5b025d330f381da4164a0f7c0e

检查volume

# docker volume inspect 673a47c976cdb6a4f8b16b632a77ec7c06086a5b025d330f381da4164a0f7c0e
[
    {
        "CreatedAt": "2021-07-06T01:44:03+02:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/673a47c976cdb6a4f8b16b632a77ec7c06086a5b025d330f381da4164a0f7c0e/_data",
        "Name": "673a47c976cdb6a4f8b16b632a77ec7c06086a5b025d330f381da4164a0f7c0e",
        "Options": null,
        "Scope": "local"
    }
]

加上卷别名创建mysql容器

# docker run -d --name mysql1 -v mysql:/var/lib/mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=ture mysql
9df6ad62442a2c8a48b6102dcf68363e496abbb991f5d22febe7d9868737e543
[email protected]:~# docker volume ls
DRIVER    VOLUME NAME
local     mysql

使用docker exec -it mysql1 /bin/bash进入mysql容器内查看

# docker exec -it mysql1 /bin/bash

进入mysql,查看数据并创建一个docker数据库

# mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.25 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

mysql> create database docker;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| docker             |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.01 sec)

mysql> exit
Bye
# exit
exit

第二种方式Bind Mounting

运行时指定本地目录和容器内目录的一一对应关系

docker run -v /home/aaa:/root/aaa

它们是同一个文件,同一个目录做了映射,我们可以把本地的/home/aaa映射到容器内的/root/aaa, 这样就实现了docker挂载高速卷的功能。

实验:

# wget https://github.com/onezos/flask/archive/refs/heads/main.zip
# ls
main.zip
# unzip main.zip 
# cd main.zip 
# more Dockerfile
FROM python:2.7
LABEL maintainer="Peng Xiao<[email protected]>"

COPY . /skeleton
WORKDIR /skeleton
RUN pip install -r requirements.txt
EXPOSE 5000
ENTRYPOINT ["scripts/dev.sh"]
# docker build -t aven7/flask-skeleton .

查看

# docker image ls
REPOSITORY                 TAG       IMAGE ID       CREATED          SIZE
aven7/flask-skeleton       latest    c81bc008b632   18 seconds ago   935MB

给shell授权

/docker/flask-main/scripts# chmod a+x deploy.sh  dev.sh
[email protected]:/docker/flask-main/scripts# ls
deploy.sh  dev.sh
/docker/flask-main/scripts# cd ..
/docker/flask-main# docker run -d -p 80:5000 -v $(pwd):/skeleton --name flask aven7/flask-skeleton
dfa471560414dac8d6f5396adc2d694d6776212a2c205ce979b9ad33d93bd3b6

打开IP地址就能看到网页了。

image-20210706100900752

Docker创建wordpress环境

第一种方法,创建两个容器

  • 首先创建mysql
# docker run -d --name mysql -v mysql-data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=wordpress -p 3306:3306 mysql
4ad150911e684a43230c4e6ec3421ad5c7fc58a9e5bdccac1d0c4f96b11d6daa
[email protected]:/docker/flask-main/skeleton/client/templates/main# docker ps
CONTAINER ID   IMAGE                             COMMAND                  CREATED             STATUS             PORTS                               NAMES4ad150911e68   mysql                             "docker-entrypoint.s…"   5 seconds ago       Up 4 seconds       0.0.0.0:3306->3306/tcp, 33060/tcp   mysql
  • 其次创建wordpress
# docker run -d --name wordpress -v wordpress:/var/www/html -e WORDPRESS_DB_HOST=193.34.144.43:3306 -e WORDPRESS_DB_USER=root -e WORDPRESS_DB_PASSWORD=root -p 8080:80 wordpress 
bd30dd64dd27df03b06f6028f4ce153d518896bd20eec825d088a9182e4d797d
  • 查看容器
# docker ps
CONTAINER ID   IMAGE                             COMMAND                  CREATED             STATUS             PORTS                               NAMEScbc49908f015   wordpress                         "docker-entrypoint.s…"   22 seconds ago      Up 20 seconds      0.0.0.0:8080->80/tcp                wordpress
4ad150911e68   mysql                             "docker-entrypoint.s…"   3 minutes ago       Up 3 minutes       0.0.0.0:3306->3306/tcp, 33060/tcp   mysql# docker ps
CONTAINER ID   IMAGE                             COMMAND                  CREATED          STATUS          PORTS                  NAMES
020e23904403   wordpress                         "docker-entrypoint.s…"   8 seconds ago    Up 5 seconds    0.0.0.0:8080->80/tcp   musing_brattain
06b2906c7929   mysql                             "docker-entrypoint.s…"   4 minutes ago    Up 4 minutes    3306/tcp, 33060/tcp    mysql
  • 查看网页

image-20210706105537351

第二种方法,使用docker-compose“批处理”

image-20210706111522465

image-20210706111949531

创建docker-compose.yml

version: '3'

services:

  wordpress:
    image: wordpress
    restart: always
    ports:
      - 8080:80
    depends_on:
      - mysql
    environment:
      WORDPRESS_DB_HOST: mysql
      WORDPRESS_DB_USER: root
      WORDPRESS_DB_PASSWORD: root
      WORDPRESS_DB_NAME: wordpress
    volumes:
      - wordpress:/var/www/html

  mysql:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: wordpress
    volumes:
      - mysql-data:/var/lib/mysql

volumes:
  wordpress:
  mysql-data:

docker-compose创建和开始容器,并在后台运行

docker-compose up -d

docker-compose停止服务

docker-compose stop

docker-compose启动服务

docker-compose start

docker-compose关闭和移除容器,网络,镜像和卷

docker-compose down

docker-compose列出容器和映像

docker-compose images

docker-compose进入wordpress容器执行bash

docker-compose exec wordpress bash

docker-compose查看运行的容器

docker-compose ps

docker-compose复制扩展运行的容器

docker-compose up --scale web=5 -d

注意:也可以采用build Dockerfile的方式,例:

version: "3"

services:

  redis:
    image: redis

  web:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - 8080:5000
    environment:
      REDIS_HOST: redis
Copyright © OneZos all right reserved,powered by Gitbook本书发布时间: 2021-11-16 19:15:49

results matching ""

    No results matching ""