ra2 studio - Fotolia

Tip

Follow this Dockerfile tutorial to create a dev environment

Containers can help establish consistent dev environments. In this step-by-step tutorial, learn how to write a Dockerfile and run containerized applications.

A standardized development environment ensures that everyone who works on the software has the same versions of tools and languages. Docker images enable developers to share applications simply, without time-consuming setup work. In this Dockerfile tutorial, we will help you become more familiar with development in Docker.

Docker creates a standardized development environment for project contributors. The Docker environment enables developers to transform a list of programming languages and tools into an executable file. That file yields a self-contained operating system with those specific languages and tools configured to the project's specifications.

To work with Docker, you must understand the Dockerfile, image and container:

  • A Dockerfile is a text file that contains ordered instructions software needs to build a given image.
  • A Docker image is the self-contained executable system configured per the Dockerfile's instructions.
  • A container runs the image -- or, typically, many layers of images. An instance is one Docker container. You can run multiple instances of the same container image.

Follow this Dockerfile tutorial and learn how to build a Python-based application with the Flask microframework. You can even use this example as a model when developing other containerized applications. Once you create the Docker image of the application, any developer can run it from the source code; they simply need the latest version of Docker. There's no need to download and set up specific versions of Python and Flask.

Create the Dockerfile

To make this simple Python Flask-based application, start with the Dockerfile. The example project is stored on GitHub. In this Dockerfile tutorial, the first instruction is: FROM python:alpine3.11.

This optional instruction specifies the images that will provide a base for the container. The Docker Hub repository provides many container images to use. I used a lightweight Linux distribution -- Alpine Linux -- for Python. With a light distribution, the image size is miniscule, only containing what is necessary to run Python, and therefore the image doesn't impede performance. Consider the application under development and choose an image accordingly. For example, node:14.2-alpine is a suitable image for a node application.

This page on Docker Hub lists the most popular containers on the repository, and filters can help you narrow down the options.

Move on to the next instruction in the Dockerfile: WORKDIR /simple-flask-app.

Use the WORKDIR instruction to create a working directory in the container named simple-flask-app. This setup keeps application code separate from other files already on the container.

Next is the COPY . . instruction, which takes all of the files in the current directory, denoted by '.', where you ran the docker build command. This instruction copies the files to the current directory in the docker container, denoted by '.' again. We'll discuss docker build later in the walkthrough. Remember that we set simple-flask-app as the current directory with WORKDIR. To move application source files into the container, do something similar here.

Next, install the Python libraries for the project, using: RUN pip install -r requirements.txt.

The RUN instruction calls pip, the Python package manager. The tool works with the project's requirements, found in the requirements.txt file. Again, ensure your project's libraries are also loaded appropriately, which can be done with any shell command passed to the RUN instruction.

Finally, call: ENTRYPOINT ["python","app.py"].

The ENTRYPOINT instruction specifies a command to run when the Docker instance starts, making the container executable. This ENTRYPOINT instruction starts the application with Python.

For more information on the different instructions available for writing a Dockerfile, visit the documentation page.

Run the container

To run the container from this Dockerfile you created, install Docker. Then, run a few simple commands.

From the root directory of the project, run: docker build -t simple-flask-app .

This command tells Docker to build the image from the Dockerfile in the current directory, which the '.' specifies at the end of the command. The '-t' flag tells Docker to tag the image with the name simple-flask-app.

The next command to run the Docker image uses this name: docker run -p 5000:5000 simple-flask-app.

The '-p' flag tells Docker to publish the container's ports to the local machine's ports. Flask runs on port 5000 by default. So, in this example, we've mapped port 5000 from the Flask app's container to the same port on the host, where the docker run command executed. Now, you can interact with the application by going to http://localhost:5000/. You should see the text Hello, World! on the page.

Development with Docker

When developers create Docker images to run an application, they typically follow the same overall pattern as the Dockerfile build example above.

Follow this process to create applications on Docker:

  1. Choose a base image with the tools to build and run your application.
  2. Pull in the dependencies.
  3. Configure and load those dependencies.
  4. Run the application.

This image is now available for other Docker users to build and deploy, without the time-consuming setup of the application's dependencies. Dockerized applications also prevent conflicts on the operating system, where different dependencies arise for different applications. Each image has its own dependencies separated from the others.

Next Steps

Use Docker for testing to get more done in less time

Dig Deeper on Software design and development

Cloud Computing
App Architecture
ITOperations
TheServerSide.com
SearchAWS
Close