您是一名开发人员,并且想要开始使用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/

Docker初学者指南-如何创建您的第一个Docker应用程序相关推荐

  1. docker新建Linux虚拟机,RHEL/CentOS 7下创建你的第一个Docker容器

    Docker容器人气一直在增长.他们现在正在快速采用,正在取代虚拟机,特别是在应用程序开发方面. 在本教程中,我们将讨论Docker容器的内容,并将学习在RHEL/CentOS 7上安装它的过程. D ...

  2. windows docker 设置镜像源_Windows10下搭建第一个Docker应用(Demo)

    1. 打开Windows虚拟化,需要Windows10Pro以上版本,部分机器还需设置BIOS打开 2. 安装Windows10 Hyper-v 3. 安装Docker Desktop,很简单,两步搞 ...

  3. 能从入门到精通的 Docker 学习指南

    原文链接:https://blog.opskumu.com/docker.html 作者:Kumu 这是一份我一直参考的 Docker 学习指南,最近感觉访问慢了很多,发在公众号里留作备份也分享给大家 ...

  4. Web 开发初学者指南

    Web 开发初学者指南 Web 开发是构建网站和 Web 应用程序的过程.它涉及多种语言.工具和框架.在这篇博文中,我们将提供 Web 开发初学者指南. HTML HTML 代表超文本标记语言.它是 ...

  5. docker拉镜像、创建容器、停止容器、移除容器、构建镜像

    导语:最近发现好多面试者不会docker,虽然不是开发必备,但是偶尔会弄个demo,或者解析个什么自己搭建项目也很方便,以我微薄的docker经验在这里留下记录并供自己以后巩固 以下内容可学会dock ...

  6. Docker 从入门到实践系列一 - 什么是Docker

    文章目录 虚拟机和容器 虚拟机和容器的区别 什么是 Docker Docker 的优点 Docker 的应用场景 Docker 核心概念 docker 镜像(image) docker 容器(cont ...

  7. 【NLP】创建强大聊天机器人的初学者指南

    作者 | Louis Teo 编译 | VK 来源 | Towards Data Science 你是否面临着太多来自客户的标准要求和问题,并且难以应对?你是否在寻找一种既不增加成本又扩大客户服务的方 ...

  8. 亚马逊云服务开通指南_亚马逊弹性容器服务初学者指南

    亚马逊云服务开通指南 This article is a beginner's high level look at Amazon ECS. We'll cover core concepts, te ...

  9. Unity增强现实初学者指南视频教程 A Beginner’s Guide to Augmented Reality with Unity

    Unity增强现实初学者指南视频教程 A Beginner's Guide to Augmented Reality with Unity MP4 |视频:h264,1280×720 (部分1920X ...

最新文章

  1. Winform中对xml文件进行保存时空白节点自动换行问题的解决
  2. 使用mybatis-generator自动生成代码的方法介绍及踩坑
  3. innodb存储引擎 - 锁
  4. 算法--排序--大小写字母数字分离(桶排序思想)
  5. Android xmlns 的作用及其自定义
  6. java基础知识---IO常用基础操作(二)
  7. python工作目录,如何使用python 3获取当前工作目录?
  8. AutoCAD2012从入门到精通中文视频教程 第13课 正多边形(个人收藏)
  9. java常用设计模式详解及应用
  10. mathtype删除注册表的方法
  11. UE4相机贴图,屏幕标注
  12. 学习分布式存储应该从哪几方面着手?
  13. 32位cpu和64位cpu对内存地址和内存单元的差别
  14. ubuntu 20.04 耳机没有声音
  15. 高并发解决方案——提升高并发量服务器性能解决思路
  16. CCF201712-4 行车路线(最短路)
  17. css——三角形的实现
  18. ARouter踩坑指南
  19. 面试管:Zookeeper在项目的典型应用场景请你回答一下
  20. S11、反射系数、回损、VSWR之间的换算

热门文章

  1. 如何保证消息队列的高可用?透彻分析源码
  2. 腾讯T2亲自讲解!搞懂开源框架设计思想真的这么重要吗?系列篇
  3. 浪里个浪 FZU - 2261
  4. Android_Event Bus 的基本用法
  5. Objective-C模版方法(TemplateMethod)
  6. IPHONE 开发 7 -- Object C 02 字符串NSString 与 char* ,字符串的遍历,字符串的比较,截取与大小写改变,搜索字符串与替换字符串...
  7. 计算机中的数(一):数在计算机中的表示
  8. VC++中进程与多进程管理的方法[转]
  9. 各种IE(IE6-IE10)兼容问题一行代码搞定
  10. centos7.4二进制安装mysql