docker的基础配置

目录

数据卷

数据卷容器

端口映射与容器互联

互联机制实现便捷互访(基于容器搭建论坛)


数据卷

1.创建数据卷

复制代码
[root@openEuler-1 /]# docker volume create test
test
[root@openEuler-1 /]# docker volume ls
DRIVER              VOLUME NAME
local               test
[root@openEuler-1 /]# docker volume inspect test
[
    {
        "CreatedAt": "2025-08-23T20:21:27+08:00",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/test/_data",
        "Name": "test",
        "Options": {},
        "Scope": "local"
    }
]

2.绑定数据卷

复制代码
[root@openEuler-1 /]# docker run -d -P --name web -v  /webapp:/opt/webapp nginx:1.17.1
156c0bc3b05a60eb8aac9c8faf6fa6b750830c623c71c8b8cacd02cf6957f8cf
[root@openEuler-1 /]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS                   NAMES
156c0bc3b05a        nginx:1.17.1        "nginx -g 'daemon of..."   21 seconds ago      Up 19 seconds                  0.0.0.0:32768->80/tcp   web
bf9f9b03a3e2        ubuntu:18.04        "/bin/bash"              40 minutes ago      Exited (0) 40 minutes ago                              trusting_chatterjee
0cf0f6e06be4        f9a80a55f492        "/bin/bash"              About an hour ago   Exited (0) About an hour ago                           zen_hypatia
db9442307637        f9a80a55f492        "echo 'hello world'"     About an hour ago   Exited (0) About an hour ago                           friendly_benz
3dd062ba408c        f9a80a55f492        "/bin/bash"              About an hour ago   Up About an hour                                       stoic_kilby
b34aa516d411        f9a80a55f492        "echo 111"               About an hour ago   Exited (0) About an hour ago                           pedantic_hugle
04247d9734ba        f9a80a55f492        "echo hello"             About an hour ago   Exited (0) About an hour ago                           gifted_almeida
[root@openEuler-1 /]# ls
afs  bin  boot  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var  webapp
[root@openEuler-1 /]# cd /webapp/
[root@openEuler-1 webapp]# ls
[root@openEuler-1 webapp]# touch test.sh
[root@openEuler-1 webapp]# ls
test.sh
[root@openEuler-1 webapp]# docker exec -it web bash
root@156c0bc3b05a:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@156c0bc3b05a:/# cd opt/
root@156c0bc3b05a:/opt# ls
webapp
root@156c0bc3b05a:/opt# cd webapp/
root@156c0bc3b05a:/opt/webapp# ls
test.sh

数据卷容器

1.首先创建一个数据卷容器

复制代码
[root@openEuler-1 webapp]# docker run -it -v /dbdata --name dbdata myubuntu:latest
root@f38e5b2b772e:/# ls
bin   dbdata  etc   lib    media  opt   root  sbin  sys  usr
boot  dev     home  lib64  mnt    proc  run   srv   tmp  var

2.使用--volumes-from来挂载容器中的数据卷

复制代码
[root@openEuler-1 ~]# docker run -it --volumes-from dbdata --name db1 myubuntu:latest
root@c15c195c0ba7:/# ls
bin   dbdata  etc   lib    media  opt   root  sbin  sys  usr
boot  dev     home  lib64  mnt    proc  run   srv   tmp  var
root@c15c195c0ba7:/# cd /dbdata/
root@c15c195c0ba7:/dbdata# echo test1 > test1.txt

3.查看结果

复制代码
root@f38e5b2b772e:/# cd dbdata/
root@f38e5b2b772e:/dbdata# ls
test1.txt

4.如果删除了挂载的容器,数据卷并不会自动删除,如果想要删除一个数据卷,必须在删除最后一个还挂载的容器时使用docker rm -v 命令来指定删除关联容器。

端口映射与容器互联

复制代码
[root@openEuler-1 ~]# docker run --name web -d -P  -v  /webapp/:/usr/share/nginx/html nginx:1.17.1
d1b8df54d5fbf7acfcac71a905e7791479028576ffa2c0c1353dde7effde4746
[root@openEuler-1 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                   NAMES
d1b8df54d5fb        nginx:1.17.1        "nginx -g 'daemon of..."   7 seconds ago       Up 6 seconds        0.0.0.0:32771->80/tcp   web
[root@openEuler-1 ~]# docker run --name web1 -d -p 80:80  -v  /webapp/:/usr/share/nginx/html nginx:1.17.1
acaf3c021bb96f35451d844bac1a04b9ba5d55c777d85eaa93e2b30444b7b076
[root@openEuler-1 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                   NAMES
acaf3c021bb9        nginx:1.17.1        "nginx -g 'daemon of..."   About a minute ago   Up About a minute   0.0.0.0:80->80/tcp      web1
d1b8df54d5fb        nginx:1.17.1        "nginx -g 'daemon of..."   19 minutes ago       Up 19 minutes       0.0.0.0:32771->80/tcp   web
[root@openEuler-1 ~]# docker rm -f web1
web1
[root@openEuler-1 ~]# docker run --name web1 -d -p 192.168.1.11:80:80  -v  /webapp/:/usr/share/nginx/html nginx:1.17.1
4b8886a595c54feb6d6a417465235f71aa9c9e67270f17da4033fd740de8aef9
[root@openEuler-1 ~]# docker rm -f web1
web1
[root@openEuler-1 ~]# docker run --name web1 -d -p 192.168.1.11::80  -v  /webapp/:/usr/share/nginx/html nginx:1.17.1
93ba8e8635fb123d8226c51f70115063640583d69006889f1fca304a7fa27937
[root@openEuler-1 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                        NAMES
93ba8e8635fb        nginx:1.17.1        "nginx -g 'daemon of..."   6 seconds ago       Up 5 seconds        192.168.1.11:32768->80/tcp   web1
d1b8df54d5fb        nginx:1.17.1        "nginx -g 'daemon of..."   21 minutes ago      Up 21 minutes       0.0.0.0:32771->80/tcp        web

互联机制实现便捷互访(基于容器搭建论坛)

wordpress使用MySQL数据库,还需进入数据库进行创建

复制代码
[root@openEuler-1 ~]# docker run --name db -e MYSQL_ROOT_PASSWORD=123456 -d --re                                                                           start=always mysql:5.6
Unable to find image 'mysql:5.6' locally
5.6: Pulling from library/mysql
35b2232c987e: Pull complete
fc55c00e48f2: Pull complete
0030405130e3: Pull complete
e1fef7f6a8d1: Pull complete
1c76272398bb: Pull complete
f57e698171b6: Pull complete
f5b825b269c0: Pull complete
dcb0af686073: Pull complete
27bbfeb886d1: Pull complete
6f70cc868145: Pull complete
1f6637f4600d: Pull complete
Digest: sha256:20575ecebe6216036d25dab5903808211f1e9ba63dc7825ac20cb975e34cfcae
Status: Downloaded newer image for mysql:5.6
c102b94bfd37b6c4142f33b30ca846aafd2aa40b107ba0a4a197654bfd116fef
[root@openEuler-1 ~]# docker run --name bbs -p 80:80 --link db:mysql -d --restar                                                                           t=always wordpress
Unable to find image 'wordpress:latest' locally
latest: Pulling from library/wordpress
396b1da7636e: Pull complete
10b731a0229f: Pull complete
759df4fda9eb: Pull complete
b84170966370: Pull complete
ca765eff712f: Pull complete
71d7acda44c3: Pull complete
aa8657555675: Pull complete
819443c89dcc: Pull complete
05afda0128fe: Pull complete
39fc65577ce1: Pull complete
925278ddd016: Pull complete
5d8fabf0ca99: Pull complete
2e9cb0b67c18: Pull complete
af388e74f0fa: Pull complete
4f4fb700ef54: Pull complete
a9a1a9a7fc0e: Pull complete
e8ecdeb45b2a: Pull complete
23a56f9ae83a: Pull complete
468e488018a8: Pull complete
6a9476a39f47: Pull complete
6d29ea965e3f: Pull complete
7f2e00ef73bc: Pull complete
295267415a53: Pull complete
45cfe8def0e7: Pull complete
Digest: sha256:c5f075fe71c9120e769edbf761bcf20bf0b73d72d49dfde042a06aafcdfef08d
Status: Downloaded newer image for wordpress:latest
f7a1adbef5dbf11ac999b12728be9b11772ef04bf9abb97e3e278bd93136132b
[root@openEuler-1 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED                                                                                        STATUS              PORTS                NAMES
f7a1adbef5db        wordpress           "docker-entrypoint.s..."   3 minutes ago                                                                                  Up 3 minutes        0.0.0.0:80->80/tcp   bbs
c102b94bfd37        mysql:5.6           "docker-entrypoint.s..."   9 minutes ago                                                                                  Up 9 minutes        3306/tcp             db
[root@openEuler-1 ~]# docker exec -it bbs bash
root@f7a1adbef5db:/var/www/html# cat /etc/hosts
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2      mysql c102b94bfd37 db
172.17.0.3      f7a1adbef5db
root@f7a1adbef5db:/var/www/html# env
MYSQL_PORT=tcp://172.17.0.2:3306
MYSQL_PORT_3306_TCP_ADDR=172.17.0.2
MYSQL_NAME=/bbs/mysql
MYSQL_ENV_MYSQL_ROOT_PASSWORD=123456
MYSQL_PORT_3306_TCP_PORT=3306
HOSTNAME=f7a1adbef5db
PHP_VERSION=8.2.29
APACHE_CONFDIR=/etc/apache2
PHP_INI_DIR=/usr/local/etc/php
GPG_KEYS=39B641343D8C104B2B146DC3F9C39DC0B9698544 E60913E4DF209907D8E30D96659A97                                                                           C9CF2A795A 1198C0117593497A5EC5C199286AF1F9897469DC
MYSQL_ENV_MYSQL_MAJOR=5.6
PHP_LDFLAGS=-Wl,-O1 -pie
MYSQL_PORT_3306_TCP=tcp://172.17.0.2:3306
PWD=/var/www/html
HOME=/root
MYSQL_ENV_GOSU_VERSION=1.12
PHP_SHA256=475f991afd2d5b901fb410be407d929bc00c46285d3f439a02c59e8b6fe3589c
PHPIZE_DEPS=autoconf            dpkg-dev                file            g++    g                                                                           cc              libc-dev                make            pkg-config             r                                                                           e2c
TERM=xterm
PHP_URL=https://www.php.net/distributions/php-8.2.29.tar.xz
MYSQL_PORT_3306_TCP_PROTO=tcp
SHLVL=1
PHP_CFLAGS=-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_                                                                           OFFSET_BITS=64
APACHE_ENVVARS=/etc/apache2/envvars
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PHP_ASC_URL=https://www.php.net/distributions/php-8.2.29.tar.xz.asc
PHP_CPPFLAGS=-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FIL                                                                           E_OFFSET_BITS=64
MYSQL_ENV_MYSQL_VERSION=5.6.51-1debian9
_=/usr/bin/env



[root@openEuler-1 ~]# docker exec -it db bash
root@c102b94bfd37:/# mysql -uroot -p123456 -e 'create database wordpress'
Warning: Using a password on the command line interface can be insecure.