Programming languages
Python Working Environment for Beginners: Part 1
FROM python:3.6-slimHow do we find out what exact OS this image is using? We can find the Dockerfile of the 3.6-slim version from the official Python Docker images, which specifies how this base image was built. The first line of that Dockerfile states:
FROM debian:stretch-slimThis tells us that the OS is a Debian Linux distribution.
pip freeze > requirements.txtAfter we have our requirements.txt ready, we can add the following commands to our Dockerfile:
COPY requirements.txt requirements.txt RUN pip install -r requirements.txtThe first line will copy the requirements.txt file in our local working directory to the Docker image’s working directory. The second line runs the command pip install -r requirements.txt, which will install all the libraries you need in your Docker image.
RUN apt-get update && apt-get install -y curl gitAny other CLI tools you need can be installed in a similar fashion. Just add the commands that you need to run into the Dockerfile.
WORKDIR /workdirYou can replace /workdir with any working directory you prefer. WORKDIR does multiple things including setting the default directory of running further Dockerfile commands and setting the default entry point to the container.
FROM python:3.6-slim FROM apt-get update && apt-get install -y git COPY requirements.txt requirements.txt RUN pip install -r requirements.txt WORKDIR /workdirAfter defining all the above dependencies in the Dockerfile, it’s time to build it. Run the following command:
docker build . -t my_project_imageThis searches your current directory for a Dockerfile and tags the image with the name my_project_image.
docker run -it -v $(pwd):/workdir my_project_image bashLine by line explanation of the above command:
Disclaimer: The statements and opinions expressed in this article are those of the author(s) and do not necessarily reflect the positions of Thoughtworks.