美文网首页
Docker 实战

Docker 实战

作者: AnnaJIAN | 来源:发表于2018-11-23 19:23 被阅读0次
通过阿里云镜像安装docker到本地

配置阿里云镜像地址
安装docker

配置docker 不需要sudo运行,可能需要重启电脑才能生效
sudo groupadd docker
sudo usermod -aG docker $USER
查看本地镜像,暂时无镜像文件
vagrant@dragon:~$ docker images
REPOSITORY                                        TAG                 IMAGE ID            CREATED             SIZE
远端查找需要的镜像文件,stars至少为3作为过滤条件
vagrant@dragon:~$ docker search --filter=stars=3 ubuntu
NAME                                                   DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
ubuntu                                                 Ubuntu is a Debian-based Linux operating s...   8782      [OK]
dorowu/ubuntu-desktop-lxde-vnc                         Ubuntu with openssh-server and NoVNC            243
拉取镜像到本地
vagrant@dragon:~$ docker pull ubuntu
32802c0cfa4d: Pull complete
da1315cffa03: Pull complete
本地已经有了镜像文件, 但是本地没有运行的容器。
vagrant@dragon:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              93fd78260bd1        3 days ago          86.2MB
vagrant@dragon:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
通过镜像后台启动一个容器,查看容器运行起来了,进入运行的容器,查看ubuntu版本号
vagrant@dragon:~$ docker run -itd 93fd
f1e8b04d7556f05cbb981196f1d1b8085348553aba65d46d4ef8fb057677af75
vagrant@dragon:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
f1e8b04d7556        93fd                "/bin/bash"         6 seconds ago       Up 5 seconds                            blissful_hypatia
vagrant@dragon:~$ docker attach f1e8b04d7556
root@f1e8b04d7556:/# cat /etc/issue
Ubuntu 18.04.1 LTS \n \l
在容器里面安装php7.2,apache2.4
root@f1e8b04d7556:/# apt-get update && apt-get upgrade
Get:1 http://security.ubuntu.com/ubuntu bionic-security InRelease [83.2 kB]
Get:2 http://archive.ubuntu.com/ubuntu bionic InRelease [242 kB]
...
root@f1e8b04d7556:/# apt-get install php
Reading package lists... Done
Building dependency tree
...
root@f1e8b04d7556:/# php -v
PHP 7.2.10-0ubuntu0.18.04.1 (cli) (built: Sep 13 2018 13:45:02) ( NTS )
root@f1e8b04d7556:~# apt install apache2
root@f1e8b04d7556:~# apachectl -v
Server version: Apache/2.4.29 (Ubuntu)
root@f1e8b04d7556:/# apt-get install systemd
f1e8b04d7556
root@f1e8b04d7556:~# service apache2 start
root@f1e8b04d7556:~# apt-get install net-tools
root@f1e8b04d7556:~# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.3  netmask 255.255.0.0  broadcast 0.0.0.0

因为没有做外部端口映射,外部容器的的ip,是无法直接访问容器的。保存提交现在的修改到镜像中。再映射端口,开启容器。

一定要在exit和stop容器前,提交容器修改,生成一个新的镜像
docker commit [容器ID] [新的镜像名字]
vagrant@dragon:~$ docker commit 0fc2894e1c64 ubuntu18:apache24php72
vagrant@dragon:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu18            apache24php72       15f84056ba6f        9 seconds ago       275MB
ubuntu              latest              93fd78260bd1        3 days ago          86.2MB
重启容器,映射端口到容器, 浏览器访问0.0.0.0就可以看到apache的说明文件了。
vagrant@dragon:~$ sudo docker run -p 80:80 -it 15f84056ba6f
root@0fc2894e1c64:/#
vagrant@dragon:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS                NAMES
0fc2894e1c64        93fd78260bd1        "/bin/bash"         About a minute ago   Up About a minute   0.0.0.0:80->80/tcp   fervent_benz
导出镜像文件, 导入镜像文件
vagrant@dragon:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu18            apache24php72       15f84056ba6f        30 minutes ago      275MB
ubuntu              latest              93fd78260bd1        3 days ago          86.2MB
vagrant@dragon:~$ docker save -o ubuntu18_apache24_php72.tar ubuntu18:apache24php72
vagrant@dragon:~$ ls
ubuntu18_apache24_php72.tar
vagrant@dragon:~$ docker load -i ubuntu18_apache24_php72.tar
Loaded image: ubuntu18:apache24php72
找到原来的容器ID,在退出容器之后,再次进去,看到原来的修改. 这个时常无效,还是要定时commit
vagrant@dragon:~$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS                NAMES
469f438b147c        15f84056ba6f        "/bin/bash"              8 minutes ago       Up 40 seconds                  0.0.0.0:80->80/tcp   keen_turing
4ff923f14b71        15f84056ba6f        "/bin/bash"              9 minutes ago       Exited (0) 8 minutes ago
vagrant@dragon:~$ docker attach 469f438b147c
root@469f438b147c:/# cd
root@469f438b147c:~# ls
xxx.txt
从dockerfile里面装lamp

Dockerfile

FROM ubuntu:latest
RUN apt-get update && apt-get install -y apache2 php libapache2-mod-php php-mysql php-cli php-curl php-xml php-intl php-mbstring git vim composer curl net-tools

COPY . /var/www/example
COPY vhost.conf /etc/apache2/sites-available/example.conf

RUN a2ensite example
RUN chown -R www-data:www-data /var/www/example/logs
RUN service apache2 restart

#ENV APACHE_RUN_USER www-data
#ENV APACHE_RUN_GROUP www-data
#ENV APACHE_LOG_DIR /var/log/apache2

EXPOSE 80
CMD apachectl -D FOREGROUND
build dockerfile
vagrant@dragon:~$ mkdir lamp
vagrant@dragon:~$ cd lamp
vagrant@dragon:~$ vi dockerfile
vagrant@dragon:~$ docker build -t lamp:Dockerfile .
docker 安装mysql
vagrant@dragon:~$ docker pull mysql
vagrant@dragon:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu18            apache24php72       15f84056ba6f        4 hours ago         275MB
ubuntu              latest              93fd78260bd1        3 days ago          86.2MB
mysql               latest              f991c20cb508        7 days ago          486MB
docker 启动mysql
vagrant@dragon:~$ docker run --name vagrant-mysql -p 3308:3306 -e MYSQL\_ROOT\_PASSWORD=123456 -d mysql
外部登录mysql, 虚拟机IP192.168.33.12,虚拟机映射端口3308
vagrant@dragon:~$ mysql -h192.168.33.12 -P3308 -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.13 MySQL Community Server - GPL
运行mysql
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)
联接到另外一台容器

--link参数格式为--link name:alias,其中name是要链接的容器名称,alias是这个连接的名称。

宿主机上的任何一台容器都可以通过暴露的端口连接到mysql

Docker 挂载mysql数据卷到本地
vagrant@dragon:~$ docker run --name mysql-master -p 3308:3306 -v /docker-mysql-data-volumn/mysql-master/data:/var/lib/mysql -v /docker-mysql-data-volumn/mysql-master/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=anna -d mysql:latest
Docker退出容器不关闭容器

Ctrl+P+Q

相关文章

网友评论

      本文标题:Docker 实战

      本文链接:https://www.haomeiwen.com/subject/kqhjqqtx.html