Open New Solution In Rider

調整 docker-compose.yml

 services:
your_project_name:
image: your_project_name
build:
context: .
dockerfile: Dockerfile
ports:
- "443:8080"
- "80:8080"
environment:
ASPNETCORE_ENVIRONMENT: "Production"
restart: always

補充知識: What is a computer port ?

連接埠 443:HTTP 安全 (HTTPS)。HTTPS 是 HTTP 的安全和加密版本。所有 HTTPS Web 流量都進入連接埠 443。使用 HTTPS 加密的網路服務(如 DNS over HTTPS)也透過這個連接埠連接。

連接埠 80:超文字傳輸通訊協定 (HTTP)。HTTP 是使全球資訊網成為可能的通訊協定。

那為什麼 443 和 80 都對應到 8080 呢?

打開 Dockerfile 會發現 EXPOSE 8080

Install Docker and Docker Compose on Ubuntu server

  1. Update your package index:
$ sudo apt-get update

2. Install Docker

$ sudo apt-get install -y docker.io

3. Start Docker and enable it to start on boot:

$ sudo systemctl start docker
$ sudo systemctl enable docker

4. Verify Docker installation:

$ docker --version

5. Download Docker Compose:

$ sudo curl -L "https://github.com/docker/compose/releases/download/v2.19.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

6. Apply executable permissions to the binary:

$ sudo chmod +x /usr/local/bin/docker-compose

7. Verify Docker Compose installation:

$ docker-compose --version

Prepare your .NET Core project

You can push to remote repository on GitHub then clone remote repository on Ubuntu Server.

Build and Run Docker Containers

  1. Navigate to your project directory:
$ cd /path/to/your/project

2. Build your Docker images using Docker Compose:

$ sudo docker-compose build

3. Run your Docker containers:

$ sudo docker-compose up -d

4. Verify the Deployment ( Check running containers )

$ sudo docker ps

5. Verify Connectivity from Another Machine

開啟瀏覽器網址輸入 https://<your_domain> 或使用 curl

$ curl -I https://<your-main>

有看到初始畫面就成功囉 🎊

Docker Command

💻 List all Docker images:

$ sudo docker images
$ sudo docker image ls

💻 Stop the Container

Use the docker stop or docker rm command followed by the container ID or name to stop the container.

# container ID
$ sudo docker stop xxx
# container name
$ sudo docker stop xxx
# container ID
$ sudo docker rm xxx
# container name
$ sudo docker rm xxx

💻 Restart Services

Restart Docker services to ensure all configurations are applied correctly:

$ sudo systemctl restart docker

💻 Rebuild and Restart Docker Containers:

$ sudo docker-compose down
$ sudo docker-compose up --build -d

💻 Check Application Logs

Inspect the logs of your running container to see if there are any error in the application itself:

$ sudo docker logs <container_id>

--

--

Conrad
Conrad KU

Remember, happiness is a choice, so choose to be happy.