Docker初學者指南-如何創建您的第一個Docker應用程序

您是一名開發人員,并且想要開始使用Docker? 本文是為您準備的。 (You are a developer and you want to start with Docker? This article is made for you.)

After a short introduction on what Docker is and why to use it, you will be able to create your first application with Docker.

在簡要介紹了什么是Docker以及為什么使用它之后,您將能夠使用Docker創建第一個應用程序。

什么是Docker? (What is Docker?)

Docker is a free software developed by Docker Inc. It was presented to the general public on March 13, 2013 and has become since that day a must in the world of IT development.

Docker是由Docker Inc.開發的免費軟件。它于2013年3月13日向公眾展示,從那一天開始,它已成為IT開發領域的必備工具。

It allows users to create independent and isolated environments to launch and deploy its applications. These environments are then called containers.

它允許用戶創建獨立的隔離環境來啟動和部署其應用程序。 這些環境然后稱為容器。

This will let the developer run a container on any machine.

這將使開發人員可以在任何計算機上運行容器。

As you can see, with Docker, there are no more dependency or compilation problems. All you have to do is launch your container and your application will launch immediately.

如您所見,有了Docker,不再有依賴或編譯問題。 您所要做的就是啟動容器,您的應用程序將立即啟動。

但是,Docker是虛擬機嗎? (But, is Docker a virtual machine?)

Here is one of the most asked question about Docker. The answer is: actually, not quite.

這是關于Docker的最常見問題之一。 答案是:實際上并不完全。

It may look like a virtual machine at first but the functionality is not the same.

乍一看可能看起來像是虛擬機,但功能并不相同。

Unlike Docker, a virtual machine will include a complete operating system. It will work independently and act like a computer.

與Docker不同,虛擬機將包含完整的操作系統。 它可以獨立工作并像計算機一樣工作。

Docker will only share the resources of the host machine in order to run its environments.

Docker將僅共享主機的資源以運行其環境。

為什么要使用Docker作為開發人員? (Why use Docker as a developer?)

This tool can really change a developer’s daily life. In order to best answer this question, I have written a non-exhaustive list of the benefits you will find:

該工具確實可以改變開發人員的日常生活。 為了最好地回答這個問題,我寫了一份詳盡的清單,列出了您會發現的好處:

  • Docker is fast. Unlike a virtual machine, your application can start in a few seconds and stop just as quickly.

    Docker很快。 與虛擬機不同,您的應用程序可以在幾秒鐘內啟動,然后停止一樣快。
  • Docker is multi-platform. You can launch your container on any system.

    Docker是多平臺的。 您可以在任何系統上啟動容器。
  • Containers can be built and destroyed faster than a virtual machine.

    可以比虛擬機更快地構建和銷毀容器。
  • No more difficulties setting up your working environment. Once your Docker is configured, you will never have to reinstall your dependencies manually again. If you change computers or if an employee joins your company, you only have to give them your configuration.

    設置工作環境不再困難。 配置了Docker之后,您將無需再次手動重新安裝依賴項。 如果您要更換計算機或員工加入公司,則只需為他們提供配置。
  • You keep your work-space clean, as each of your environments will be isolated and you can delete them at any time without impacting the rest.

    您可以保持工作空間整潔,因為每個環境都會被隔離,您可以隨時刪除它們而不會影響其余環境。
  • It will be easier to deploy your project on your server in order to put it online.

    將項目部署到服務器以使其聯機更容易。

現在讓我們創建您的第一個應用程序 (Now let’s create your first application)

Now that you know what Docker is, it’s time to create your first application!

現在您已經知道Docker是什么了,是時候創建您的第一個應用程序了!

The purpose of this short tutorial is to create a Python program that displays a sentence. This program will have to be launched through a Dockerfile.

本簡短教程的目的是創建一個顯示句子的Python程序。 該程序必須通過Dockerfile啟動。

You will see, it’s not very complicated once you understand the process.

您將看到,一旦了解了過程,它并不是很復雜。

Note: You will not need to install Python on your computer. It will be up to the Docker environment to contain Python in order to execute your code.
注意:您無需在計算機上安裝Python。 Docker環境將由Python來執行代碼。

1.在您的機器上安裝Docker (1. Install Docker on your machine)

For Ubuntu:

對于Ubuntu:

First, update your packages:

首先,更新您的軟件包:

$ sudo apt update

Next, install docker with apt-get:

接下來,使用apt-get安裝docker:

$ sudo apt install docker.io

Finally, verify that Docker is installed correctly:

最后,驗證Docker是否正確安裝:

$ sudo docker run hello-world
  • For MacOSX: you can follow this link.

    對于MacOSX:您可以點擊此鏈接 。

  • For Windows: you can follow this link.

    對于Windows:您可以點擊此鏈接 。

2.創建您的項目 (2. Create your project)

In order to create your first Docker application, I invite you to create a folder on your computer. It must contain the following two files:

為了創建您的第一個Docker應用程序,我邀請您在計算機上創建一個文件夾。 它必須包含以下兩個文件:

  • A ‘main.py’ file (python file that will contain the code to be executed).

    一個“ main.py ”文件(將包含要執行的代碼的python文件)。

  • A ‘Dockerfile’ file (Docker file that will contain the necessary instructions to create the environment).

    Dockerfile ”文件(將包含創建環境的必要說明的Docker文件)。

Normally you should have this folder architecture:

通常,您應該具有以下文件夾體系結構:

.
├── Dockerfile
└── main.py
0 directories, 2 files

3.編輯Python文件 (3. Edit the Python file)

You can add the following code to the ‘main.py’ file:

您可以將以下代碼添加到“ main.py ”文件中:

#!/usr/bin/env python3print("Docker is magic!")

Nothing exceptional, but once you see “Docker is magic!” displayed in your terminal you will know that your Docker is working.

沒什么特別的,但是一旦您看到“ Docker就是魔術! ”顯示在您的終端上,您將知道Docker正在工作。

3.編輯Docker文件 (3. Edit the Docker file)

Some theory: the first thing to do when you want to create your Dockerfile is to ask yourself what you want to do. Our goal here is to launch Python code.

一些理論:要創建Dockerfile時要做的第一件事是問自己要做什么。 我們的目標是啟動Python代碼。

To do this, our Docker must contain all the dependencies necessary to launch Python. A linux (Ubuntu) with Python installed on it should be enough.

為此,我們的Docker必須包含啟動Python所需的所有依賴項。 安裝了Python的Linux(Ubuntu)應該足夠了。

The first step to take when you create a Docker file is to access the DockerHub website. This site contains many pre-designed images to save your time (for example: all images for linux or code languages).

創建Docker文件時采取的第一步是訪問DockerHub網站。 該站點包含許多預先設計的映像,以節省您的時間(例如:Linux或代碼語言的所有映像)。

In our case, we will type ‘Python’ in the search bar. The first result is the official image created to execute Python. Perfect, we’ll use it!

在本例中,我們將在搜索欄中鍵入“ Python”。 第一個結果是創建執行Python 的官方映像 。 完美,我們將使用它!

# A dockerfile must always start by importing the base image.
# We use the keyword 'FROM' to do that.
# In our example, we want import the python image.
# So we write 'python' for the image name and 'latest' for the version.
FROM python:latest# In order to launch our python code, we must import it into our image.
# We use the keyword 'COPY' to do that.
# The first parameter 'main.py' is the name of the file on the host.
# The second parameter '/' is the path where to put the file on the image.
# Here we put the file at the image root folder.
COPY main.py /# We need to define the command to launch when we are going to run the image.
# We use the keyword 'CMD' to do that.
# The following command will execute "python ./main.py".
CMD [ "python", "./main.py" ]

4.創建Docker映像 (4. Create the Docker image)

Once your code is ready and the Dockerfile is written, all you have to do is create your image to contain your application.

一旦代碼準備就緒并編寫了Dockerfile,您要做的就是創建映像以包含您的應用程序。

$ docker build -t python-test .

The ’-t’ option allows you to define the name of your image. In our case we have chosen ’python-test’ but you can put what you want.

-t選項允許您定義圖像的名稱。 在我們的例子中,我們選擇了“ python-test ”,但是您可以放置??所需的內容。

5.運行Docker映像 (5. Run the Docker image)

Once the image is created, your code is ready to be launched.

創建映像后,即可啟動代碼。

$ docker run python-test

You need to put the name of your image after ‘docker run’.

您需要將圖像的名稱放在“ docker run ”之后。

There you go, that’s it. You should normally see “Docker is magic!” displayed in your terminal.

到了,就是這樣。 您通常應該看到“ Docker是不可思議的!” 顯示在您的終端中。

代碼可用 (Code is available)

If you want to retrieve the complete code to discover it easily or to execute it, I have put it at your disposal on my GitHub.

如果您想檢索完整的代碼以輕松發現或執行它,我已將其放在我的GitHub上供您使用。

-> GitHub: Docker First Application example

-> GitHub:Docker First Application示例

Docker的有用命令 (Useful commands for Docker)

Before I leave you, I have prepared a list of commands that may be useful to you on Docker.

在離開您之前,我已經準備了一份命令列表,這些命令可能對您在Docker上有用。

  • List your images.

    列出您的圖像。
$ docker image ls
  • Delete a specific image.

    刪除特定的圖像。
$ docker image rm [image name]
  • Delete all existing images.

    刪除所有現有圖像。
$ docker image rm $(docker images -a -q)
  • List all existing containers (running and not running).

    列出所有現有容器(正在運行和未運行)。
$ docker ps -a
  • Stop a specific container.

    停止特定的容器。
$ docker stop [container name]
  • Stop all running containers.

    停止所有正在運行的容器。
$ docker stop $(docker ps -a -q)
  • Delete a specific container (only if stopped).

    刪除特定的容器(僅在停止時)。
$ docker rm [container name]
  • Delete all containers (only if stopped).

    刪除所有容器(僅在停止時)。
$ docker rm $(docker ps -a -q)
  • Display logs of a container.

    顯示容器的日志。
$ docker logs [container name]

下一步是什么? (What’s next?)

After all your feedback, I decided to write the next part of this beginner’s guide. In this article, you will discover how to use docker-compose to create your first client/server-side application with Docker.

在收到您的所有反饋后,我決定編寫本初學者指南的下一部分。 在本文中,您將發現如何使用docker-compose和Docker創建您的第一個客戶端/服務器端應用程序。

-> A beginner’s guide to Docker?—?how to create a client/server side with docker-compose

-> Docker初學者指南-如何使用docker-compose創建客戶端/服務器端

結論 (Conclusion)

You can refer to this post every time you need a simple and concrete example on how to create your first Docker application. If you have any questions or feedback, feel free to ask.

每當您需要有關如何創建第一個Docker應用程序的簡單而具體的示例時,都可以參考這篇文章。 如果您有任何問題或反饋,請隨時提出。

Don't miss my content by following me on Twitter and Instagram.

在Twitter和Instagram上關注我,不要錯過我的內容。

You can find other articles like this on my website: herewecode.io.

您可以在我的網站上找到其他類似的文章: herewecode.io 。

想要更多? (Want more?)

  • Each week get a motivational quote with some advice, a short tutorial into a few slides, and one developer's picture on Instagram.

    每周都會收到勵志名言,并提供一些建議,簡短的教程,幾張幻燈片以及Instagram上一位開發人員的照片。

  • Sign-up for the newsletter and get the latest articles, courses, tutorials, tips, books, motivation, and other exclusive content.

    注冊時事通訊并獲取最新文章,課程,教程,技巧,書籍,動機和其他獨家內容。

翻譯自: https://www.freecodecamp.org/news/a-beginners-guide-to-docker-how-to-create-your-first-docker-application-cc03de9b639f/

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391875.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391875.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391875.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

mybatis if-else(寫法)

mybaits 中沒有else要用chose when otherwise 代替 范例一 <!--批量插入用戶--> <insert id"insertBusinessUserList" parameterType"java.util.List">insert into business_user (id , user_type , user_login )values<foreach collection…

spring—攔截器和異常

SpringMVC的攔截器 SpringMVC攔截器-攔截器的作用 Spring MVC 的攔截器類似于 Servlet 開發中的過濾器 Filter&#xff0c;用于對處理器進行預處理和后處理。 將攔截器按一定的順序聯結成一條鏈&#xff0c;這條鏈稱為攔截器鏈&#xff08;InterceptorChain&#xff09;。在…

29/07/2010 sunrise

** .. We can only appreciate the miracle of a sunrise if we have waited in the darkness .. 人們在黑暗中等待著&#xff0c;那是期盼著如同日出般的神跡出現 .. 附&#xff1a;27/07/2010 sunrise ** --- 31 July 改動轉載于:https://www.cnblogs.com/orderedchaos/archi…

密度聚類dbscan_DBSCAN —基于密度的聚類方法的演練

密度聚類dbscanThe idea of having newer algorithms come into the picture doesn’t make the older ones ‘completely redundant’. British statistician, George E. P. Box had once quoted that, “All models are wrong, but some are useful”, meaning that no model…

node aws 內存溢出_在AWS Elastic Beanstalk上運行生產Node應用程序的現實

node aws 內存溢出by Jared Nutt賈里德努特(Jared Nutt) 在AWS Elastic Beanstalk上運行生產Node應用程序的現實 (The reality of running a production Node app on AWS Elastic Beanstalk) 從在AWS的ELB平臺上運行生產Node應用程序兩年的經驗教訓 (Lessons learned from 2 y…

Day2-數據類型

數據類型與內置方法 數據類型 數字字符串列表字典元組集合字符串 1.用途 用來描述某個物體的特征&#xff1a;姓名&#xff0c;性別&#xff0c;愛好等 2.定義方式 變量名 字符串 如&#xff1a;name huazai 3.常用操作和內置方法 1.按索引取值&#xff1a;&#xff08;只能取…

嵌套路由

父組件不能用精準匹配&#xff0c;否則只組件路由無法展示 轉載于:https://www.cnblogs.com/dianzan/p/11308146.html

leetcode 992. K 個不同整數的子數組(滑動窗口)

給定一個正整數數組 A&#xff0c;如果 A 的某個子數組中不同整數的個數恰好為 K&#xff0c;則稱 A 的這個連續、不一定獨立的子數組為好子數組。 &#xff08;例如&#xff0c;[1,2,3,1,2] 中有 3 個不同的整數&#xff1a;1&#xff0c;2&#xff0c;以及 3。&#xff09; …

從完整的新手到通過TensorFlow開發人員證書考試

I recently graduated with a bachelor’s degree in Civil Engineering and was all set to start with a Master’s degree in Transportation Engineering this fall. Unfortunately, my plans got pushed to the winter term because of COVID-19. So as of January this y…

微信開發者平臺如何編寫代碼_編寫超級清晰易讀的代碼的初級開發者指南

微信開發者平臺如何編寫代碼Writing code is one thing, but writing clean, readable code is another thing. But what is “clean code?” I’ve created this short clean code for beginners guide to help you on your way to mastering and understanding the art of c…

【轉】PHP面試題總結

PHP面試總結 PHP基礎1&#xff1a;變量的傳值與引用。 2&#xff1a;變量的類型轉換和判斷類型方法。 3&#xff1a;php運算符優先級&#xff0c;一般是寫出運算符的運算結果。 4&#xff1a;PHP中函數傳參&#xff0c;閉包&#xff0c;判斷輸出的echo&#xff0c;print是不是函…

Winform控件WebBrowser與JS腳本交互

1&#xff09;在c#中調用js函數 如果要傳值&#xff0c;則可以定義object[]數組。 具體方法如下例子&#xff1a; 首先在js中定義被c#調用的方法: function Messageaa(message) { alert(message); } 在c#調用js方法Messageaa private void button1_Click(object …

從零開始擼一個Kotlin Demo

####前言 自從google將kotlin作為親兒子后就想用它擼一管app玩玩&#xff0c;由于工作原因一直沒時間下手&#xff0c;直到項目上線后才有了空余時間&#xff0c;期間又由于各種各樣煩人的事斷了一個月&#xff0c;現在終于開發完成項目分為服務器和客戶端&#xff1b;服務器用…

移動平均線ma分析_使用動態移動平均線構建交互式庫存量和價格分析圖

移動平均線ma分析I decided to code out my own stock tracking chart despite a wide array of freely available tools that serve the same purpose. Why? Knowledge gain, it’s fun, and because I recognize that a simple project can generate many new ideas. Even t…

敏捷開發創始人_開發人員和技術創始人如何將他們的想法轉化為UI設計

敏捷開發創始人by Simon McCade西蒙麥卡德(Simon McCade) 開發人員和技術創始人如何將他們的想法轉化為UI設計 (How developers and tech founders can turn their ideas into UI design) Discover how to turn a great idea for a product or service into a beautiful UI de…

在ubuntu怎樣修改默認的編碼格式

ubuntu修改系統默認編碼的方法是&#xff1a;1. 參考 /usr/share/i18n/SUPPORTED 編輯/var/lib/locales/supported.d/* gedit /var/lib/locales/supported.d/localgedit /var/lib/locales/supported.d/zh-hans如&#xff1a;more /var/lib/locales/supported.d/localzh_CN GB18…

JAVA中PO,BO,VO,DTO,POJO,Entity

https://my.oschina.net/liaodo/blog/2988512轉載于:https://www.cnblogs.com/dianzan/p/11311217.html

【Lolttery】項目開發日志 (三)維護好一個項目好難

項目的各種配置開始出現混亂的現象了 在只有一個人開發的情況下也開始感受到維護一個項目的難度。 之前明明還好用的東西&#xff0c;轉眼就各種莫名其妙的報錯&#xff0c;完全不知道為什么。 今天一天的工作基本上就是整理各種配置。 再加上之前數據庫設計出現了問題&#xf…

leetcode 567. 字符串的排列(滑動窗口)

給定兩個字符串 s1 和 s2&#xff0c;寫一個函數來判斷 s2 是否包含 s1 的排列。 換句話說&#xff0c;第一個字符串的排列之一是第二個字符串的子串。 示例1: 輸入: s1 “ab” s2 “eidbaooo” 輸出: True 解釋: s2 包含 s1 的排列之一 (“ba”). 解題思路 和s1每個字符…

靜態變數和非靜態變數_統計資料:了解變數

靜態變數和非靜態變數Statistics 101: Understanding the different type of variables.統計101&#xff1a;了解變量的不同類型。 As we enter the latter part of the year 2020, it is safe to say that companies utilize data to assist in making business decisions. F…