Azure VM安装docker

本文详细介绍了如何在AzureVM上安装、配置和使用Docker,包括安装验证、运行Nginx服务器、创建自定义网站、制作并上传Docker镜像到GHCR,以及部署PythonFlask应用的过程。
摘要由CSDN通过智能技术生成

1.安装

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

2.检验

首先切换至root权限

//如果是第一次切换,之前没有设置过密码,可以根据以下步骤
//进入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

检测是否成功安装

$ docker pull docker.io/library/hello-world
$ docker run hello-world

3.尝试使用container

// 获取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

docker pull nginx

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

$ docker run -p 8080:80 nginx
$ curl http://localhost:8080

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

5.建立自己的网站

// 进入容器
$ 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

$ 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

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

如果是最新的mac m1想要部署,则需要运行下面的命令:
docker buildx build --platform linux/amd64 -t my-image:latest .

7.上传自定义image至GHCR

  1. 生成PAT,并设置权限
    有 write:packages、read:packages 和 delete:packages 权限
  2. 上传
// 登录到 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
$ docker pull python
# 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"]

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
安装Docker的方法会因不同的云服务提供商而异,但基本步骤大致相同。以下是一些常见云服务提供商的安装步骤: 1. AWS EC2:在EC2实例上安装Docker需要在Amazon Linux 2 AMI上运行以下命令: ``` sudo yum update -y sudo amazon-linux-extras install docker -y sudo service docker start ``` 2. Azure VM:在Azure虚拟机安装Docker需要在Linux操作系统上运行以下命令: ``` sudo apt-get update sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common -y curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" sudo apt-get update sudo apt-get install docker-ce docker-ce-cli containerd.io -y ``` 3. Google Cloud Compute Engine:在Google云计算引擎上安装Docker需要在Debian或Ubuntu操作系统上运行以下命令: ``` sudo apt-get update sudo apt-get install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" sudo apt-get update sudo apt-get install -y docker-ce docker-ce-cli containerd.io ``` 以上命令都是安装Docker最基本的步骤,具体安装过程可能会因云服务提供商的不同而有所变化。如果需要更详细的安装说明,请参考相应云服务提供商的文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值