您是一名開發人員,并且想要開始使用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/