Docker初学者指南-如何创建您的第一个Docker应用程序
您是一名開發(fā)人員,并且想要開始使用Docker? 本文是為您準(zhǔn)備的。 (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創(chuàng)建第一個(gè)應(yīng)用程序。
什么是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.開發(fā)的免費(fèi)軟件。它于2013年3月13日向公眾展示,從那一天開始,它已成為IT開發(fā)領(lǐng)域的必備工具。
It allows users to create independent and isolated environments to launch and deploy its applications. These environments are then called containers.
它允許用戶創(chuàng)建獨(dú)立的隔離環(huán)境來啟動(dòng)和部署其應(yīng)用程序。 這些環(huán)境然后稱為容器。
This will let the developer run a container on any machine.
這將使開發(fā)人員可以在任何計(jì)算機(jī)上運(yùn)行容器。
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,不再有依賴或編譯問題。 您所要做的就是啟動(dòng)容器,您的應(yīng)用程序?qū)⒘⒓磫?dòng)。
但是,Docker是虛擬機(jī)嗎? (But, is Docker a virtual machine?)
Here is one of the most asked question about Docker. The answer is: actually, not quite.
這是關(guān)于Docker的最常見問題之一。 答案是:實(shí)際上并不完全。
It may look like a virtual machine at first but the functionality is not the same.
乍一看可能看起來像是虛擬機(jī),但功能并不相同。
Unlike Docker, a virtual machine will include a complete operating system. It will work independently and act like a computer.
與Docker不同,虛擬機(jī)將包含完整的操作系統(tǒng)。 它可以獨(dú)立工作并像計(jì)算機(jī)一樣工作。
Docker will only share the resources of the host machine in order to run its environments.
Docker將僅共享主機(jī)的資源以運(yùn)行其環(huán)境。
為什么要使用Docker作為開發(fā)人員? (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:
該工具確實(shí)可以改變開發(fā)人員的日常生活。 為了最好地回答這個(gè)問題,我寫了一份詳盡的清單,列出了您會(huì)發(fā)現(xiàn)的好處:
- Docker is fast. Unlike a virtual machine, your application can start in a few seconds and stop just as quickly. Docker很快。 與虛擬機(jī)不同,您的應(yīng)用程序可以在幾秒鐘內(nèi)啟動(dòng),然后停止一樣快。
- Docker is multi-platform. You can launch your container on any system. Docker是多平臺(tái)的。 您可以在任何系統(tǒng)上啟動(dòng)容器。
- Containers can be built and destroyed faster than a virtual machine. 可以比虛擬機(jī)更快地構(gòu)建和銷毀容器。
- 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. 設(shè)置工作環(huán)境不再困難。 配置了Docker之后,您將無需再次手動(dòng)重新安裝依賴項(xiàng)。 如果您要更換計(jì)算機(jī)或員工加入公司,則只需為他們提供配置。
- 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. 您可以保持工作空間整潔,因?yàn)槊總€(gè)環(huán)境都會(huì)被隔離,您可以隨時(shí)刪除它們而不會(huì)影響其余環(huán)境。
- It will be easier to deploy your project on your server in order to put it online. 將項(xiàng)目部署到服務(wù)器以使其聯(lián)機(jī)更容易。
現(xiàn)在讓我們創(chuàng)建您的第一個(gè)應(yīng)用程序 (Now let’s create your first application)
Now that you know what Docker is, it’s time to create your first application!
現(xiàn)在您已經(jīng)知道Docker是什么了,是時(shí)候創(chuàng)建您的第一個(gè)應(yīng)用程序了!
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.
本簡短教程的目的是創(chuàng)建一個(gè)顯示句子的Python程序。 該程序必須通過Dockerfile啟動(dòng)。
You will see, it’s not very complicated once you understand the process.
您將看到,一旦了解了過程,它并不是很復(fù)雜。
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.注意:您無需在計(jì)算機(jī)上安裝Python。 Docker環(huán)境將由Python來執(zhí)行代碼。1.在您的機(jī)器上安裝Docker (1. Install Docker on your machine)
For Ubuntu:
對(duì)于Ubuntu:
First, update your packages:
首先,更新您的軟件包:
$ sudo apt updateNext, install docker with apt-get:
接下來,使用apt-get安裝docker:
$ sudo apt install docker.ioFinally, verify that Docker is installed correctly:
最后,驗(yàn)證Docker是否正確安裝:
$ sudo docker run hello-worldFor MacOSX: you can follow this link.
對(duì)于MacOSX:您可以點(diǎn)擊此鏈接 。
For Windows: you can follow this link.
對(duì)于Windows:您可以點(diǎn)擊此鏈接 。
2.創(chuàng)建您的項(xiàng)目 (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:
為了創(chuàng)建您的第一個(gè)Docker應(yīng)用程序,我邀請(qǐng)您在計(jì)算機(jī)上創(chuàng)建一個(gè)文件夾。 它必須包含以下兩個(gè)文件:
A ‘main.py’ file (python file that will contain the code to be executed).
一個(gè)“ main.py ”文件(將包含要執(zhí)行的代碼的python文件)。
A ‘Dockerfile’ file (Docker file that will contain the necessary instructions to create the environment).
“ Dockerfile ”文件(將包含創(chuàng)建環(huán)境的必要說明的Docker文件)。
Normally you should have this folder architecture:
通常,您應(yīng)該具有以下文件夾體系結(jié)構(gòu):
. ├── Dockerfile └── main.py 0 directories, 2 files3.編輯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就是魔術(shù)! ”顯示在您的終端上,您將知道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.
一些理論:要?jiǎng)?chuàng)建Dockerfile時(shí)要做的第一件事是問自己要做什么。 我們的目標(biāo)是啟動(dòng)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必須包含啟動(dòng)Python所需的所有依賴項(xiàng)。 安裝了Python的Linux(Ubuntu)應(yīng)該足夠了。
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).
創(chuàng)建Docker文件時(shí)采取的第一步是訪問DockerHub網(wǎng)站。 該站點(diǎn)包含許多預(yù)先設(shè)計(jì)的映像,以節(jié)省您的時(shí)間(例如: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”。 第一個(gè)結(jié)果是創(chuàng)建執(zhí)行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.創(chuàng)建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.
一旦代碼準(zhǔn)備就緒并編寫了Dockerfile,您要做的就是創(chuàng)建映像以包含您的應(yīng)用程序。
$ 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選項(xiàng)允許您定義圖像的名稱。 在我們的例子中,我們選擇了“ python-test ”,但是您可以放置??所需的內(nèi)容。
5.運(yùn)行Docker映像 (5. Run the Docker image)
Once the image is created, your code is ready to be launched.
創(chuàng)建映像后,即可啟動(dòng)代碼。
$ docker run python-testYou 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.
到了,就是這樣。 您通常應(yīng)該看到“ 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.
如果您想檢索完整的代碼以輕松發(fā)現(xiàn)或執(zhí)行它,我已將其放在我的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.
在離開您之前,我已經(jīng)準(zhǔn)備了一份命令列表,這些命令可能對(duì)您在Docker上有用。
- List your images. 列出您的圖像。
- Delete a specific image. 刪除特定的圖像。
- Delete all existing images. 刪除所有現(xiàn)有圖像。
- List all existing containers (running and not running). 列出所有現(xiàn)有容器(正在運(yùn)行和未運(yùn)行)。
- Stop a specific container. 停止特定的容器。
- Stop all running containers. 停止所有正在運(yùn)行的容器。
- Delete a specific container (only if stopped). 刪除特定的容器(僅在停止時(shí))。
- Delete all containers (only if stopped). 刪除所有容器(僅在停止時(shí))。
- Display logs of a container. 顯示容器的日志。
下一步是什么? (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.
在收到您的所有反饋后,我決定編寫本初學(xué)者指南的下一部分。 在本文中,您將發(fā)現(xiàn)如何使用docker-compose和Docker創(chuàng)建您的第一個(gè)客戶端/服務(wù)器端應(yīng)用程序。
-> A beginner’s guide to Docker?—?how to create a client/server side with docker-compose
-> Docker初學(xué)者指南-如何使用docker-compose創(chuàng)建客戶端/服務(wù)器端
結(jié)論 (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.
每當(dāng)您需要有關(guān)如何創(chuàng)建第一個(gè)Docker應(yīng)用程序的簡單而具體的示例時(shí),都可以參考這篇文章。 如果您有任何問題或反饋,請(qǐng)隨時(shí)提出。
Don't miss my content by following me on Twitter and Instagram.
在Twitter和Instagram上關(guān)注我,不要錯(cuò)過我的內(nèi)容。
You can find other articles like this on my website: herewecode.io.
您可以在我的網(wǎng)站上找到其他類似的文章: 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.
每周都會(huì)收到勵(lì)志名言,并提供一些建議,簡短的教程,幾張幻燈片以及Instagram上一位開發(fā)人員的照片。
Sign-up for the newsletter and get the latest articles, courses, tutorials, tips, books, motivation, and other exclusive content.
注冊(cè)時(shí)事通訊并獲取最新文章,課程,教程,技巧,書籍,動(dòng)機(jī)和其他獨(dú)家內(nèi)容。
翻譯自: https://www.freecodecamp.org/news/a-beginners-guide-to-docker-how-to-create-your-first-docker-application-cc03de9b639f/
總結(jié)
以上是生活随笔為你收集整理的Docker初学者指南-如何创建您的第一个Docker应用程序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我如何在昌迪加尔大学中心组织Google
- 下一篇: 梦到被天鹅咬什么意思