Azure VM安装docker

Azure VM 安装docker

1.安装

bash 复制代码
使用官方安装脚本
 curl -fsSL https://test.docker.com -o test-docker.sh
 sudo sh test-docker.sh

2.检验

首先切换至root权限

bash 复制代码
//如果是第一次切换,之前没有设置过密码,可以根据以下步骤
//进入root模式
sudo su -
//设置root密码
sudo passwd root
//为了之后可以使用root和密码登录
sudo vim /etc/ssh/sshd_config
//找到这一行 #PermitRootLogin prohibit-password  
//在它的后面添加一行
PermitRootLogin yes   //允许root登录,设为yes。
//重启ssh服务
sudo service ssh restart

检测是否成功安装

bash 复制代码
$ docker pull docker.io/library/hello-world
$ docker run hello-world

3.尝试使用container

bash 复制代码
// 获取ubuntu
$ docker pull ubuntu
// 第一次建立ubuntu container
$ docker run -it ubuntu bash
// 看都有哪些可用的container
$ docker container ls -a

// 重启之前停止的container
$ docker start container xxx
// 在一个正在运行的container执行命令
$ docker exec -it xxx bash
// 清除旧container重新获取磁盘空间
$ docker container prune

其他命令:https://yeasy.gitbook.io/docker_practice/container/attach_exec

4.在container中运行一个server:nginx

bash 复制代码
docker pull nginx

nginx在80端口运行,我们需要使用http://localhost:80去使用这个web server。但是我们的浏览器会使用本机作为localhost,而不是这个container。所以需要加一个port forwarding的过程,比如将本机的8080端口与container的80端口相映射。

bash 复制代码
$ docker run -p 8080:80 nginx
$ curl http://localhost:8080

如果显示Welcome to nginx!之类的html代码,就证明上面的步骤都成功啦!

5.建立自己的网站

bash 复制代码
// 进入容器
$ docker exec -it xxxx bash
// 下载vim
$ apt-get update
$ apt-get install vim
vim /usr/share/nginx/html/index.html
// 修改该html后再次访问http://localhost:8080,若有更新则成功

6.建立自己的image

bash 复制代码
$ mkdir mydocker
$ cd mydocker
$ vim index.html
$ vim Dockerfile

示例:

• 使用Nginx作为base

• 设置工作目录至/usr/share/nginx/html

• 将自己写的HTML files复制进container的/usr/share/nginx/html

• 开放port 80

• Sets up a command to run nginx when we run the container

bash 复制代码
FROM nginx
RUN apt update
RUN apt install -y vim curl
WORKDIR /usr/share/nginx/html
EXPOSE 80
COPY index.html .
bash 复制代码
docker build -t nginx-with-my-page .

7.上传自定义image至GHCR

  1. 生成PAT,并设置权限
    有 write:packages、read:packages 和 delete:packages 权限
  2. 上传
bash 复制代码
// 登录到 GitHub 容器注册表
echo "YOUR_GITHUB_PAT" | docker login ghcr.io -u YOUR_GITHUB_USERNAME --password-stdin
// 构建docker镜像
docker build -t your-image-name .
// 标记docker镜像
docker tag your-image-name ghcr.io/YOUR_GITHUB_USERNAME/your-image-name:tag
// 推送
docker push ghcr.io/YOUR_GITHUB_USERNAME/your-image-name:tag
  1. 设置访问权限

8.上传一个python flask app至GHCR

  1. 准备一个python flask app
  2. 准备好requirements.txt
    pip freeze > requirements.txt
  3. 生成Dockerfile
bash 复制代码
$ docker pull python
bash 复制代码
# Use an official Python runtime as a base image
FROM python
RUN mkdir app
# Set the working directory in the container
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Make port 5000 available to the world outside this container
EXPOSE 5000

# Define environment variable
ENV NAME World
RUN cd api
# Run app.py when the container launches
#CMD ["python", "app.py"]
CMD ["flask", "run"]
相关推荐
营赢盈英10 分钟前
Give azure openai an encyclopedia of information
ai·openai·azure
全云在线allcloudonline22 分钟前
微软 Azure AI 服务免费试用及申请:语音识别、文本转语音、基于视觉、语言处理、文档分析等10大场景
人工智能·microsoft·azure
shelby_loo1 小时前
通过 Docker 部署 WordPress 服务器
服务器·docker·容器
minsin2 小时前
【linux】【docker】Docker默认网段配置导致无法访问
docker
悲伤的创可贴4 小时前
Docker安装以及简单使用
linux·docker·centos
方圆师兄4 小时前
docker快速搭建kafka
docker·容器·kafka
小的~~5 小时前
k8s使用本地docker私服启动自制的flink集群
docker·flink·kubernetes
诚诚k5 小时前
docker存储
运维·docker·容器
sorel_ferris6 小时前
Ubuntu-24.04中Docker-Desktop无法启动
linux·ubuntu·docker
多多*6 小时前
OJ在线评测系统 登录页面开发 前端后端联调实现全栈开发
linux·服务器·前端·ubuntu·docker·前端框架