Giter Site home page Giter Site logo

docker-quickstart's Introduction

Here is a quick start guide for using Docker:

Installing Docker

  1. Download and install Docker Desktop for your operating system (Windows, macOS, or Linux) from the official Docker website: https://www.docker.com/products/docker-desktop

  2. Once installed, open a terminal and verify Docker is running correctly by running:

docker run hello-world

You should see a message printed indicating that Docker is installed and running successfully. [1]

Building Your First Docker Image

  1. Create a new directory and navigate into it.

  2. Create a file named Dockerfile with the following content:

# Use the official Node.js image 
FROM node:18

# Create app directory
WORKDIR /app

# Copy package.json and package-lock.json
COPY package*.json ./

# Install app dependencies
RUN npm install

# Copy app source code 
COPY . .

# Expose the port the app runs on
EXPOSE 3000

# Start the app
CMD [ "npm", "start" ]
  1. Create a package.json file for your Node.js app.

  2. Build the Docker image by running:

docker build -t my-node-app .
  1. Run the Docker container with:
docker run -p 3000:3000 my-node-app

This will start your Node.js app in a Docker container mapped to port 3000 on your host machine. [2]

Using Docker Hub

Docker Hub is a cloud-based registry service for finding and sharing container images.

  1. Create a free Docker Hub account at https://hub.docker.com

  2. Log in to the Docker Hub from the command line:

docker login
  1. After building an image, you can push it to Docker Hub:
docker push username/my-node-app

This allows you to share your Docker images publicly or pull them from any Docker environment. [5]

This covers the basics of installing Docker, building your first image, running a container, and using Docker Hub to share images. The Docker documentation has many more guides to explore more advanced Docker features and workflows. [1][2][4][5]

Citations: [1] https://docker-curriculum.com [2] https://blog.packagecloud.io/docker-quick-start-guide/ [3] https://www.simplilearn.com/tutorials/docker-tutorial/getting-started-with-docker [4] https://docs.docker.com/get-started/ [5] https://docs.docker.com/docker-hub/quickstart/ [6] https://docs.docker.com/desktop/extensions-sdk/quickstart/ [7] https://github.com/docker/getting-started [8] https://www.reddit.com/r/webdev/comments/fca48o/docker_quickstart_guide_for_developers/


将Python脚本进行Docker化是一个将应用程序及其依赖项打包在一个轻量级、可移植的容器中的过程,这样可以确保应用程序在任何环境中都能以相同的方式运行。以下是一个快速入门教程,用于将一个简单的Python脚本(输出print("Hello World"))进行Docker化。

步骤1: 创建Python脚本

首先,创建一个名为hello.py的Python脚本,内容如下:

print("Hello World")

步骤2: 创建Dockerfile

Dockerfile是一个文本文件,包含了运行应用所需的所有命令。在与hello.py相同的目录下创建一个名为Dockerfile的文件,内容如下:

# 使用官方Python运行时作为父镜像
FROM python:3.8-slim

# 设置工作目录为/app
WORKDIR /app

# 将当前目录内容复制到位于/app中的容器中
COPY . /app

# 通过运行python命令来运行脚本
CMD ["python", "./hello.py"]

这个Dockerfile执行了以下操作:

  • 从Docker Hub上获取官方的Python 3.8镜像。
  • 设置容器内的工作目录为/app
  • 将当前目录(包含你的hello.py脚本)的内容复制到容器的/app目录。
  • 设置容器启动时执行的命令为python ./hello.py

步骤3: 构建Docker镜像

打开终端或命令提示符,导航到包含hello.pyDockerfile的目录。运行以下命令来构建Docker镜像,其中hello-world-image是你给镜像起的名字:

docker build -t hello-world-image .

这个命令会读取当前目录的Dockerfile,并根据其内容构建一个新的Docker镜像。

步骤4: 运行Docker容器

构建完成后,使用以下命令运行Docker容器:

docker run hello-world-image

这条命令会启动一个基于hello-world-image镜像的容器。你应该会在终端看到输出Hello World

通过以上步骤,你已经成功地将一个简单的Python脚本Docker化了。这个过程展示了如何使用Dockerfile定义容器的环境,以及如何构建和运行基于这个Dockerfile的Docker镜像。Docker化可以让你的应用在任何支持Docker的环境中无缝运行,极大地提高了应用的可移植性和灵活性。

docker-quickstart's People

Contributors

zgimszhd61 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.