3
FROM python:
/app
COPY . /app
WORKDIR
-r requirements.txt
RUN pip install
"python", "app.py"] CMD [
Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package.
To start using Docker, you will first need to install it on your system. You can find installation instructions for various operating systems on the Docker website.
Once Docker is installed, you can start using it to build and run your applications. The first step is to create a Dockerfile
, which is a text file that contains all of the instructions needed to build an image.
Here’s an example Dockerfile
for a simple Python application:
This Dockerfile
starts with the FROM
directive, which specifies the base image that the rest of the instructions will be built on top of. In this case, we’re using a base image that contains the latest version of Python 3.
Next, the COPY
and WORKDIR
directives are used to copy the contents of the current directory into the /app
directory in the container and set that as the working directory.
The RUN
directive is used to run a command in the container, in this case installing the dependencies specified in the requirements.txt
file.
Finally, the CMD
directive is used to specify the command that will be run when the container is started. In this case, we’re running the app.py Python script.
To build an image based on this Dockerfile
, you can use the docker build
command. For example:
-t my-app . $ docker build
This will build an image with the tag my-app
based on the instructions in the Dockerfile
.
Once the image is built, you can use the docker run
command to start a container based on that image. For example:
-p 8000:8000 my-app $ docker run
This will start a container based on the my-app
image and map the container’s port 8000 to the host’s port 8000, so that the application can be accessed on http://localhost:8000.
There are many more options and directives that you can use in a Dockerfile
and with the docker command, but this should give you a basic understanding of how to use Docker
to build and run a simple application.