所需积分/C币:9 2019-04-07 09:43:51 498KB PDF
收藏 收藏

Docker初学指南:如何创建第一个Docker应用程序,A beginner’s guide to Docker — how to create your first Docker application。
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 is multi-platform. You can launch your container on any 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 You keep your work-space clean, as each of your environments ill 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! 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 Dockerfilo 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 1. Install Docker on your machine for ubuntu First, update your packages s sudo apt update Next, install docker with apt-get s sudo apt-get install docker-ce Finally, verify that Docker is installed correctly s sudo docker run hello-world For MacOSX: you can follow this link For windows: you can follow this link 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' file (python file that will contain the code to be executee A Dockerfile' file(docker file that will contain the necessary instructions to create the environment normally you should have this folder architecture Dockerfile main. py o directories 2 files 3. Edit the Python file You can add the following code to the 'main. py'file 1 !/usr/bin/env pythons 3 print("Docker is magic! Nothing exceptional, but once you see "Docker is magic displayed in your terminal you will know that your Docker is working 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 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 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) In our case we will type 'Python'in the search bar. The first result is the official image created to execute Python Perfect, we'l use it! 1 #a dockerfile must always start by importing the base 2 we use the keyword from to do that. 3 In our example, we want import the python image so we write python for the image name and ' latest 5 FROM python latest 7 In order to launch our python code, we must import 8 We use the keyword add' to do that. 9 the first parameter main. py is the name of the fil 10 The second parameter is the path where to put th 11 Here we put the file at the image root folder. 12 ADD main. py 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 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. 5. Run the Docker image Once the image is created, your code is ready to be launched s docker run python -test You need to put the name of your image after ' docker run There you go, that' s it. You should normally see"Docker is magic displayed in your terminal Useful commands for docker Before I leave you, I have prepared a list of commands that may be useful to you on docker List your images. s docker image ls Delete a specific image s docker image rm [image name Delete all existing image docker image rm $(docker images -a-q) List all existing containers (running and not running s docker ps -a Stop a specific container. s docker stop [container name Stop all running containers s docker stop $(docker ps -a -g) Delete a specific container (only if stopped) s docker rm [container name l Delete all containers (only if stopped s docker rm (docker ps -a -g) display logs of a container s docker logs [container name] Before you go This embedded content is from a site that does not comply with the Do Not Track (DNT) setting now enabled on your browser. Please note, if you click through and view it anyway, you may be tracked by the website hosting the embed Learn More about Medium's DNT policy Thanks for reading! I regularly share articles on the Medium platform, you can check out my profile Do not hesitate to give me some feedback to improve my future articles in the comments below

试读 10P Docker初学指南:如何创建第一个Docker应用程序
立即下载 低至0.43元/次 身份认证VIP会员低至7折
  • 签到新秀

  • 分享王者

关注 私信
Docker初学指南:如何创建第一个Docker应用程序 9积分/C币 立即下载

试读结束, 可继续读1页

9积分/C币 立即下载