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"]
相关推荐
七度光阴;6 小时前
Docker入门手册
运维·docker·容器
weixin_405023378 小时前
使用docker 安装部署easy-mock
运维·docker·容器
Asuncion0078 小时前
Docker核心揭秘:轻量级虚拟化的革命
服务器·开发语言·docker·云原生
ZLRRLZ8 小时前
【Docker】Docker Image(镜像)
运维·docker·容器
Akshsjsjenjd11 小时前
Docker资源限制详解
运维·docker·容器
雲帝13 小时前
1panel docker开启swap内存
运维·docker·容器
tryCbest20 小时前
Linux使用Docker部署项目后期更新
linux·运维·docker
缘的猿1 天前
Docker 与 K8s 网络模型全解析
docker·容器·kubernetes
运维栈记1 天前
使用Grafana监控K8S中的异常Pod
docker·kubernetes·grafana
东城绝神1 天前
《Linux运维总结:基于X86_64+ARM64架构CPU使用docker-compose一键离线部署consul 1.21.5容器版集群》
linux·运维·docker·架构·consul