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"]
相关推荐
luck_me52 小时前
k8s v1.26 实战csi-nfs 部署
linux·docker·云原生·容器·kubernetes
邪恶的贝利亚2 小时前
《Docker 入门与进阶:架构剖析、隔离原理及安装实操》
docker·容器·架构
知其_所以然3 小时前
使用docker安装clickhouse集群
clickhouse·docker·容器
.生产的驴6 小时前
Docker 部署Nexus仓库 搭建Maven私服仓库 公司内部仓库
java·运维·数据库·spring·docker·容器·maven
知行026 小时前
MySQL的Docker版本,部署在ubantu系统
数据库·mysql·docker
搬砖的工人7 小时前
Docker环境下的Apache NiFi安装实践踩坑记录
docker·容器·apache
QX_hao10 小时前
【docker】--镜像管理
运维·docker·容器
Auc2411 小时前
OJ判题系统第6期之判题逻辑开发——设计思路、实现步骤、代码实现(策略模式)
java·开发语言·docker·容器·策略模式
快乐肚皮11 小时前
深入解析Docker:核心架构与最佳实践
java·运维·docker·容器
上天_去_做颗惺星 EVE_BLUE14 小时前
Docker入门教程:常用命令与基础概念
linux·运维·macos·docker·容器·bash