Skip to content

Instantly share code, notes, and snippets.

@berndbausch
Last active February 28, 2018 01:00
Show Gist options
  • Save berndbausch/290448d9e3be3113795a9c064c6e21ee to your computer and use it in GitHub Desktop.
Save berndbausch/290448d9e3be3113795a9c064c6e21ee to your computer and use it in GitHub Desktop.
Creating Docker containers

Creating Docker containers

To write an application contained by Docker, one may start with an existing container that has the required environment. If the application is written in Python, a Python programming environment would make sense.

Docker's layered images allow us to take a Python image and layer the application on top of it. A Dockerfile describes what has to be done to create the new application container image. The following is a summary of the instructions on the Docker Get Started site:

  • get a Python image
  • specify the directory in the container where the application lives - its work directory
  • add files to the container
  • execute code, e.g. install other components or compile something
  • map a network port from the container to the host
  • set up some environment variables
  • run the application

Corresponding Dockerfile:

FROM python:2.7-slim
WORKDIR /app
ADD . /app
RUN pip install --trusted-host pypi.python.org -r requirements.txt
EXPOSE 80
ENV NAME World
CMD ["python", "app.py"]

The container is then created with the docker build command. Create a directory that contains the application and the Dockerfile, then

$ cd APPLICATION_DIRECTORY
$ docker build -t tag-for-my-app .

The tag will help finding the app on Docker hub or other repositories.

The build command adds the new container to the local repository, from where it can be executed:

docker run -p 4000:80 tag-for-my-app

The -p option maps the container's port 80 to the host's port 4000.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment