docker

配置文件、启动命令
应用程序、环境变量
第三方软件库和依赖包
运行时环境
操作系统

例子

前端 Vue
后端 SprintBoot
DB MySql

开发环境、测试环境

NodeJS
npm依赖
java运行时环境
第三方依赖(springboot)
mysql数据库
环境变量、启动脚本
配置Redis(缓存)
配置Nginx

原理

虚拟机
Alt text

docker
Alt text

Alt text

New Linux installations, installed using the wsl –install command, will be set to WSL 2 by default.

You can list your installed Linux distributions and check the version of WSL each is set to by entering the command: wsl -l -v in PowerShell or Windows Command Prompt.

To change or reset your password, open the Linux distribution and enter the command: passwd. You will be asked to enter your current password, then asked to enter your new password, and then to confirm your new password.


If you forgot the password for your Linux distribution:

Open PowerShell and enter the root of your default WSL distribution using the command: wsl -u root

If you need to update the forgotten password on a distribution that is not your default, use the command: wsl -d Debian -u root, replacing Debian with the name of your targeted distribution.

Once your WSL distribution has been opened at the root level inside PowerShell, you can use this command to update your password: passwd <username> where <username> is the username of the account in the distribution whose password you’ve forgotten.

You will be prompted to enter a new UNIX password and then confirm that password. Once you’re told that the password has updated successfully, close WSL inside of PowerShell using the command: exit.


wsl --shutdown


Install VS Code and the WSL extension

Install the Remote Development extension pack. This extension pack includes the WSL extension, in addition to the Remote - SSH, and Dev Containers extensions, enabling you to open any folder in a container, on a remote machine, or in WSL.

This Remote Development extension pack includes four extensions:

Remote - SSH - Work with source code in any location by opening folders on a remote machine/VM using SSH. Supports x86_64, ARMv7l (AArch32), and ARMv8l (AArch64) glibc-based Linux, Windows 10/Server (1803+), and macOS 10.14+ (Mojave) SSH hosts.
Remote - Tunnels - Work with source code in any location by opening folders on a remote machine/VM using a VS Code Tunnel (rather than SSH).
Dev Containers - Work with a separate toolchain or container based application by opening any folder mounted into or inside a container.
WSL - Get a Linux-powered development experience from the comfort of Windows by opening any folder in the Windows Subsystem for Linux.


Install Docker Desktop on Windows

When prompted, ensure the Use WSL 2 instead of Hyper-V option on the Configuration page is selected or not depending on your choice of backend.

If your system only supports one of the two options, you will not be able to select which backend to use.


dockfile

docker compose

example

Create an empty project directory.

cd my_wordpress/

Create a docker-compose.yml file that starts your WordPress blog and a separate MySQL instance with volume mounts for data persistence:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
services:
db:
# We use a mariadb image which supports both amd64 & arm64 architecture
image: mariadb:10.6.4-focal
# If you really want to use MySQL, uncomment the following line
#image: mysql:8.0.27
command: '--default-authentication-plugin=mysql_native_password'
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
- MYSQL_ROOT_PASSWORD=somewordpress
- MYSQL_DATABASE=wordpress
- MYSQL_USER=wordpress
- MYSQL_PASSWORD=wordpress
expose:
- 3306
- 33060
wordpress:
image: wordpress:latest
volumes:
- wp_data:/var/www/html
ports:
- 80:80
restart: always
environment:
- WORDPRESS_DB_HOST=db
- WORDPRESS_DB_USER=wordpress
- WORDPRESS_DB_PASSWORD=wordpress
- WORDPRESS_DB_NAME=wordpress
volumes:
db_data:
wp_data:

run docker compose up -d from your project directory.

Bring up WordPress in a web browser
If you are using Docker Desktop for Mac or Docker Desktop for Windows, you can use http://localhost as the IP address, and open http://localhost:80 in a web browser.


phpmyadmin

All of the following examples will bring you phpMyAdmin on http://localhost:8080 where you can enjoy your happy MySQL administration.

服务器:db

Usage with docker-compose and arbitrary server
This will run phpMyAdmin with the arbitrary server option - allowing you to specify any MySQL/MariaDB server on the login page.

version: ‘3.1’

1
2
3
4
5
6
7
8
9
10
services:


phpmyadmin:
image: phpmyadmin
restart: always
ports:
- 8080:80
environment:
- PMA_ARBITRARY=1

Mount the volume containing your themes or plugins to the proper directory; and then apply them through the “wp-admin” UI. Ensure read/write/execute permissions are in place for the user:

Themes go in a subdirectory in /var/www/html/wp-content/themes/
Plugins go in a subdirectory in /var/www/html/wp-content/plugins/

mysql

docker run -d --name mysql_1 -p 3307:3306 -e TZ=Asia/Shanghai -e MYSQL_ROOT_PASSWORD=123 mysql

$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
… where some-mysql is the name you want to assign to your container, my-secret-pw is the password to be set for the MySQL root user and tag is the tag specifying the MySQL version you want.


Using a custom MySQL configuration file

If /my/custom/config-file.cnf is the path and name of your custom configuration file, you can start your mysql container like this (note that only the directory path of the custom config file is used in this command):

$ docker run --name some-mysql -v /my/custom:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag

This will start a new container some-mysql where the MySQL instance uses the combined startup settings from /etc/mysql/my.cnf and /etc/mysql/conf.d/config-file.cnf, with settings from the latter taking precedence.

Configuration without a cnf file
Many configuration options can be passed as flags to mysqld. This will give you the flexibility to customize the container without needing a cnf file. For example, if you want to change the default encoding and collation for all tables to use UTF-8 (utf8mb4) just run the following:

$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

docker run -d

-d
deamon后台服务

-p 宿主机端口:虚拟机端口
port
虚拟机端口取决于应用程序

-v 数据卷:容器目录
/var/lib/docker/volumes/
-v 宿主机目录:容器目录
volumn
双向映射

Alt text

-e KEY=VALUE
environment

Repository:TAG

volume

Alt text

mysql

1
2
3
4
5
6
7
8
9
docker run -d \
--name mysql \
-p 3306:3306 \
-e TZ=Asia/Shanghai \
-e MYSQL_ROOT_PASSWORD=123 \
-v /root/mysql/data:/var/lib/mysql \
-v /root/mysql/init:docker-entrypoint-initdb.d \
-v /root/mysql /conf:etc/mysql/conf.d \
mysql

If /my/custom/config-file.cnf is the path and name of your custom configuration file, you can start your mysql container like this (note that only the directory path of the custom config file is used in this command):

$ docker run –name some-mysql -v /my/custom:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
This will start a new container some-mysql where the MySQL instance uses the combined startup settings from /etc/mysql/my.cnf and /etc/mysql/conf.d/config-file.cnf, with settings from the latter taking precedence.


Initializing a fresh instance
When a container is started for the first time, a new database with the specified name will be created and initialized with the provided configuration variables. Furthermore, it will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order.


Create a data directory on a suitable volume on your host system, e.g. /my/own/datadir.

$ docker run --name some-mysql -v /my/own/datadir:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag

Alt text

openjdk:11.0-jre-buster

1
2
3
4
5
6
7
8
9
10
11
12
13
14
FROM ubuntu:16:04
# JDK安装目录
ENV JAVA_DIR=/usr/local
# copy jdk 和 java项目包
COPY ./jdk8.tar.gz $JAVA_DIR/
COPY ./docker-demo.jar /tmp/app.jar
# install jdk
RUN cd $JAVA_DIR \ && tar -xf ./jdk8.tar.gz \ && mv ./jdk1.8.0_144 ./java8
# environment variable
ENV JAVA_HOME=$JAVA_DIR/JAVA8
ENV PATH=$PATH:$JAVA_HOME/bin
# JAVA项目启动命令
ENTRYPOINT ["java", "-jar", "/app.jar"]

1
2
3
4
5
6
7
FROM openjdk:11.0-jre-buster

#设置系统时区

COPY docker-demo.jar /app.jar

ENTRYPOINT ["java", "-jar", "/app.jar"]

docker build -t myImage:1.0 .
-t -> 镜像起名
. -> dock file所在目录

network

Alt text

加入自定义网络的容器可以通过容器名互相访问

Alt text

docker run -d --name app_server_name --network custom_network_name docker_image_name