[Attack on Docker] 2023 Docker Quick Start Tutorial, including the deployment of .Net projects

b station video: [Attack on Docker] 2023 Docker Quick Start Tutorial, including the deployment of .Net projects

Douyin video: [Attack on Docker] 2023 Docker Quick Start Tutorial, including the deployment of .Net projects

Official website address: https://docs.docker.com/

1. What is Docker and what can it do

concept:

Docker is an open platform for developing, operating, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly.

Action scene:

  • Use Docker to push applications into test environments and perform automated and manual testing.
  • When developers find bug s, they can be fixed in the development environment and redeployed to the test environment for test verification.
  • The traditional delivery is code delivery, but with Docker we deliver functions, because I can package the program into a mirror image for the operation and maintenance personnel, and the operation and maintenance personnel do not need to know what language the program is written in, they only need to execute docker Command to let the program run on the line.

    Architecture diagram:

2. Expected results of learning

  • Install docker engine to understand common commands of docker
  • Build and run the image as a container
  • Deploy a Docker application using multiple containers with tomcat
  • Learn Dockerfile
  • Understanding Docker Compose

    3. According to the official tutorial, let's practice

3.1. Preparations

1. Installation of Docker Engine

$ sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine
$ sudo yum install -y yum-utils
$ sudo yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sudo yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin


systemctl start docker
docker run hello-world

2. Install git, zip (file decompression/compression)

##install git
yum install -y git

## install zip
yum install -y zip

3.Docker common commands (important)

3.2. Quick start

1 Overview

Glossary

1. (Container) Image

Simple explanation: compared to class

official explanation

When running a container, it uses an isolated file system. This custom filesystem is provided by the container image. Since an image contains the container's filesystem, it must contain everything needed to run the application — all dependencies, configuration, scripts, binaries, etc. The image also contains additional configuration for the container, such as environment variables, default commands to run, and other metadata.

2. Container

Simple explanation: compared to an instance of a class

A container is a sandboxed process on a machine, isolated from all other processes on the host.

2. Containerized applications

https://docs.docker.com/get-started/02_our_app/

3. Update the app

https://docs.docker.com/get-started/03_updating_app/

4. Push the application to DockerHub

https://docs.docker.com/get-started/04_sharing_app/

5. Save data

https://docs.docker.com/get-started/05_persisting_data/

6. Use a bind mount (bind mount)

https://docs.docker.com/get-started/06_bind_mounts/

Named volumesBind mounts
host locationDocker selectyour decision
Mount example (using --mount)type=volume,src=my-volume,target=/usr/local/datatype=bind,src=/path/to/data,target=/usr/local/data
Populate the new volume with the contents of the containerYesNo
Support volume driverYesNo

3.3. Deploy a dotnet application (webapp)

Step 1: Create a new project on the server or use vs studio to create a project upload server

webapi (based on netcore3.1): download link

Step 2: Build the mirror image

need dockerfile knowledge: [Click to jump to](#l31Qk)

Step 3: Run our container

https://docs.docker.com/language/dotnet/develop/

Step 4: Use docker compose

Knowledge of dockerCompose is required: Click to jump to

ps: Note that here is the priority to start the mysql service

services:
 mysql_db:
   image: mysql:5.7
   container_name: mysql_db
   restart: always
   environment:
     MYSQL_ROOT_PASSWORD: mysql123456
   volumes:
     - mysql-data:/var/lib/mysql
 adminer:
   image: adminer
   restart: always
   ports:
     - 8080:8080
 donet-app:
   build:
#The cotext configuration is a . means that it is in the current root directory for build operation, look for the default Dockerfile here
     context: .
#The dockerfile is configured. If it is an absolute path, it will be taken according to the absolute path. If it is a relative path, it will search and build for this Dockerfile file name.
     dockerfile: Dockerfile
   ports:
     - 5000:80
   depends_on:
     - mysql_db
volumes:
 mysql-data:

If we configure the data volume in mysql_db in detail, we can configure it like this

services:
 mysql_db:
   image: mysql:5.7
   container_name: mysql_db
   restart: always
   environment:
     MYSQL_ROOT_PASSWORD: mysql123456
   volumes:
     - type: volume
       source: mysql-data
       target: /var/lib/mysql
       volume:
         nocopy: true

If you need to find the external mysql-data data volume, you can add a configuration external: true in volumes

volumes:
 mysql-data:
  external: true

The following is a webapi project, the docker-compose file I configured

### The first level includes services (service), volumes (data volume), networks (network) configs (configuration, note that it is useless here)
### secrets (such as https certificates) 

services:
 mysql:
   image: mysql:5.7
   container_name: mysql_db
   restart: always
   ports:
      ## Don't forget to map the port  
      - 3306:3306
   environment:
     MYSQL_ROOT_PASSWORD: mysql123456
     MYSQL_DATABASE: my_db
   volumes:
     - mysql-data:/var/lib/mysql
   networks:
     - mysql-net       
 adminer:
   image: adminer
   container_name: db_adminer_manage
   restart: always
   ports:
     - 8080:8080
   networks:
     - mysql-net  
 app:
   build:
     context: .
   container_name: dotnet-app
   ports:
     - 5000:80
   ## depends_on indicates the startup and shutdown dependencies between services. For example, mysql needs to be started here before the entire application can run normally. The same is true for adding redis  
   depends_on:
     - mysql
   networks:
     - mysql-net  
volumes:
 mysql-data:
networks:
 mysql-net: {} 

Reference link:

Containerize .NET apps (The reference case is a console program)

https://learn.microsoft.com/zh-cn/dotnet/core/docker/build-container?tabs=windows

Docker image for ASP.NET Core:

https://learn.microsoft.com/zh-cn/aspnet/core/host-and-deploy/docker/building-net-docker-images?view=aspnetcore-3.1

How to customize Docker containers in Visual Studio

https://learn.microsoft.com/zh-cn/visualstudio/containers/container-build?view=vs-2019

Official mirrors for .NET and ASP.NET Core

https://hub.docker.com/_/microsoft-dotnet

4. Common commands and Dockerfile (important)

4.1 Common commands

1. Mirror related commands

## view mirror
docker images

## search mirror
docker search <imageName>

###example
[root@ecs-356882 ~]# docker search redis

## download mirror
docker pull <imageName>
###example
[root@ecs-356882 ~]# docker pull redis

### Download the redis image of the specified version
[root@ecs-356882 ~]# docker pull redis:6.2

## delete mirror
docker rmi <IMAGEList>
        --- -f,--force Forcibly delete the image

###Example delete multiple redis images
[root@ecs-356882 ~]# docker rmi 1f2e359f9324 2f66aad5324a


## build a mirror

2. Container related commands

## Start the container
docker run <imageName>
     -d Run the container in the background
     -it Run the container interactively
      --name Specify the container name
      --network Containers can access custom networks

### example
[root@ecs-356882 ~]# docker run redis:6.2
---Run the container interactively
[root@ecs-356882 ~]# docker run -it redis:6.2 /bin/bash

---In progress mysql Connect to a custom network
docker run --name mysql01 --network=mysql_network -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7


## show container list
docker ps       Show all running containers
    --all , -a        show all containers


## stop container
docker stop [OPTIONS] CONTAINER [CONTAINER...]

### example
[root@ecs-356882 ~]# docker stop fd4405ab0bde

## delete container
docker rm [OPTIONS] CONTAINER [CONTAINER...]
    --- -f,--force force delete container

## Start a stopped container
docker start [OPTIONS] CONTAINER [CONTAINER...]

## Restart the container
docker restart [OPTIONS] CONTAINER [CONTAINER...]

### example
docker start fd4405ab0bde

## View container details
docker inspect

### example
root@ecs-356882 ~]# docker inspect 5d2dc551c7e383cd7cfb483b80a44ece504b1998a42fa33b2457053ef5a00a1e
[
    {
        "Id": "5d2dc551c7e383cd7cfb483b80a44ece504b1998a42fa33b2457053ef5a00a1e",
        "Created": "2023-02-11T03:11:18.664894024Z",
        "Path": "docker-entrypoint.sh",
        "Args": [
            "redis-server"
        ],
        "Image": "sha256:1f2e359f9324e978cb509c1fa583af9c679efb33969f5584368b3ddd9568592b",
    }
]



## into the running container
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
### example
docker exec -it 5d2dc551c7e383cd7cfb483b80a44ece504b1998a42fa33b2457053ef5a00a1e /bin/bash

## exit container
//entered interactively
Ctrl+D ==>will stop the container
exit ==>will stop the container
---only exits the container but does not shut down the container Ctrl+P add Ctrl+Q

3. About the data volume

## Create data volume 

docker volume create <volume name>

## View all data volumes  
docker volume ls

### example
[root@ecs-356882 ~]# docker volume ls
DRIVER    VOLUME NAME
local     ef5bbfaa89dc8988e40a02f4e65ff222f8669f12bae0c3a42dc77741146374c8
local     hello

##View the details of the specified data volume  
docker volume inspect

### example
[root@ecs-356882 ~]# docker volume inspect hello
[
    {
        "CreatedAt": "2023-02-11T12:00:46+08:00",
        "Driver": "local",
        "Labels": null,
        "Mountpoint": "/var/lib/docker/volumes/hello/_data",
        "Name": "hello",
        "Options": null,
        "Scope": "local"
    }
]


##Delete data volume
docker volume rm [OPTIONS] VOLUME [VOLUME...]

## Run the container and mount the data volume
docker run -v [Host location: where the container is located]


### Example 1
docker run --name mysql01 -v /home/mysql/conf:/etc/mysql/conf.d  -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=mysql123456 -d  -p 3389:3306  mysql:5.7
### Example 2
docker run --name myredis -p 6379:6379 -v /home/redis/data:/data -v /home/redis/conf:/etc/redis -d redis:6.2 redis-server /etc/redis/redis.conf

### Example 3 --mount specifies options for volume mounts
docker run -dp 3000:3000 --mount type=volume,src=todo-db,target=/etc/todos getting-started

4. Custom network

1.create network
 docker network create [OPTIONS] NETWORK
    -d The driver associated with the network defaults to bridge(bridge mode)

## example
 docker network create  my-bridge-network    
 docker network create -d bridge my-bridge-network

2.view network list
 docker network ls

## example
[root@ecs-356882 ~]# docker network ls
NETWORK ID     NAME                DRIVER    SCOPE
e91ea2f84b1e   my-bridge-network   bridge    local


3.View network details
 docker network inspect [OPTIONS] NETWORK [NETWORK...]

## example
[root@ecs-356882 ~]#  docker network inspect my-bridge-network
[
    {
        "Name": "my-bridge-network",
        "Id": "e91ea2f84b1e7de64828a628cf476fda9e805fd49a6a8000e9166166bf28fb85",
        "Created": "2023-02-11T14:39:53.48799159+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false
        }
]

4.remove network
 docker network rm NETWORK [NETWORK...]

## example
[root@ecs-356882 ~]# docker network rm my-bridge-network
my-bridge-network

4.2 About Dockerfile

The following list of directives supports environment variables Dockerfile: 
ADD
COPY
ENV
EXPOSE
FROM
LABEL
STOPSIGNAL
USER
VOLUME
WORKDIR
ONBUILD(when combined with one of the supported directives above)
--------------------------------------
--- FROM instruction
FROM [--platform=<platform>] <image> [AS <name>]
or

FROM [--platform=<platform>] <image>[:<tag>] [AS <name>]
or

FROM [--platform=<platform>] <image>[@<digest>] [AS <name>]


--- ENV instruction
ENV <key>=<value> ...
### example
ENV abc="123"

ENV MY_NAME="John" MY_DOG="555"

--- RUN instruction (Can run non docker instruction)
RUN <command> (shell form, the command is run in a shell, which by default is /bin/sh -c on Linux or cmd /S /C on Windows)
RUN ["executable", "param1", "param2"] (exec form)

RUN dotnet new webapi -n MyApi

--- WORKDIR instruction
 Give(ADD COPY)and other commands to set the working directory, path This is a relative mirror image

WORKDIR <path>

--- COPY instruction
COPY [--chown=<user>:<group>] <src>... <dest>
COPY [--chown=<user>:<group>] ["<src>",... "<dest>"]
### example
COPY /src .

--- EXPOSE instruction (exposed port)

EXPOSE <port> [<port>/<protocol>...]
### example
EXPOSE 8090

Learn how CMD and ENTRYPOINT interact

There can only be one CMD instruction in a Dockerfile. If you list multiple CMD commands only the last CMD will take effect.
Both CMD and directive ENTRYPOINT define the command to execute when running the container. Few rules describe their cooperation.

  1. Dockerfile should specify at least one CMD or ENTRYPOINT command.
  2. ENTRYPOINT should be defined when using the container as an executable.
  3. CMD should be used as a way for commands to define default parameters ENTRYPOINT or to execute temporary commands in containers.
  4. CMD will be overridden when running the container with optional arguments.

The following table shows the commands executed for different ENTRYPOINT/CMD combinations:

No ENTRYPOINTENTRYPOINT exec_entry p1_entryENTRYPOINT ["exec_entry", "p1_entry"]
No CMDwrong, not allowed/bin/sh -c exec_entry p1_entryexec_entry p1_entry
CMD ["exec_cmd","p1_cmd"]exec_cmd p1_cmd/bin/sh -c exec_entry p1_entryexec_entry p1_entry exec_cmd p1_cmd
CMD exec_cmd p1_cmd/bin/sh -c exec_cmd p1_cmd/bin/sh -c exec_entry p1_entryexec_entry p1_entry /bin/sh -c exec_cmd p1_cmd

4.3 About Docker Compose

The file specification of Docker Compose:

### Services (images and containers)
services:

  
### data volume
volumes:
 
### Configuration such as redis configuration file
configs:
  
### Authentication module such as https certificate
secrets:
   
### custom network
networks:
   

Split module:

1,services

services:
  foo:
    image: foo
  bar:
    image: bar
    profiles:
      - test
  baz:
    image: baz
    depends_on:
      - bar
    profiles:
      - test

2,volumes

### Minimalist
services:
  backend:
    image: awesome/backend
    volumes:
      - db-data: /var/run/postgres/postgres.sock
volumes:
  db-data:


### Detailed version

services:
  backend:
    image: awesome/backend
    volumes:
      - type: volume
        source: db-data
        target: /data
        volume:
          nocopy: true
      - type: bind
        source: /var/run/postgres/postgres.sock
        target: /var/run/postgres/postgres.sock
volumes:
  db-data:

3,configs

redis download: https://download.redis.io/releases/redis-6.2.1.tar.gz
Execute the make command after decompression
Enter the src directory and execute the startup command: .>./redis-server ../redis.conf
--- Enter the command line
In the same way, enter the src directory and start the cli command: .>./redis-cli

services:
  redis:
    image: redis:6.2
    configs:
      - my_config
configs:
  my_config:
    file: ./my_config.txt
  my_other_config:
    external: true

##The file name of the dumped DB
dbfilename dump.rdb     
    

4,networks

services:
  frontend:
    image: awesome/webapp
    networks:
      - front-tier
      - back-tier

networks:
  front-tier:
  back-tier:

Tags: Linux Operation & Maintenance Docker server Container

Posted by kelharis on Thu, 02 Mar 2023 08:21:07 +0530