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"]
相关推荐
muyun28005 小时前
Docker 下部署 Elasticsearch 8 并集成 Kibana 和 IK 分词器
elasticsearch·docker·容器
傻傻虎虎10 小时前
【Docker】常用帮忙、镜像、容器、其他命令合集(2)
运维·docker·容器
2401_8979300618 小时前
使用Docker轻松部署Neo4j图数据库
数据库·docker·neo4j
LeeZhao@19 小时前
【AI推理部署】Docker篇04—Docker自动构建镜像
人工智能·docker·容器
程思扬19 小时前
利用JSONCrack与cpolar提升数据可视化及跨团队协作效率
网络·人工智能·经验分享·docker·信息可视化·容器·架构
落日漫游20 小时前
dockercompose和k8s区别
docker·kubernetes
lllsure1 天前
【Docker】存储卷
运维·docker·容器
纤瘦的鲸鱼1 天前
Docker 从入门到实践:容器化技术核心指南
java·docker·容器
邂逅星河浪漫1 天前
【Docker+Nginx+Ollama】前后端分离式项目部署(传统打包方式)
java·nginx·docker·部署
咖啡Beans1 天前
Docker安装ELK(Elasticsearch + Logstash + Kibana)
后端·elasticsearch·docker