...

Docker Python Enviroment

Frank Casanova

Oct. 5, 2023

...

First of all you need to create a Dockerfile:

touch Dockerfile

Next you must to fill that file with this:

FROM python:3.11-slim

 

WORKDIR /code

 

COPY ./requirements.txt ./

RUN pip install --no-cache-dir -r requirements.txt

 

COPY ./src ./src

CMD ["list-of-comands", "you-want-run", "inside-continer"]

After that, to build your image, type this in your command line:

 docker build -t any-name-you-want .

 Check if your image has been created with:

docker images

 To run that container type:

docker run --name container-name -p 80:80 any-imagen

You can now see all logs from your running container.

If you don't want to see the logs, type the same command but with the -d flag.

docker run --name container-name -p 80:80 -d any-imagen

To check if the container still running type: 

docker ps

The problem is that any changes you make to your code are not reflected in the system. This is because the changes exist on your local machine but not on your container, to solve that we're going to use a volume, volume are the prefer mechanism to persist data:

docker run --name container-name -p 80:80 -d -v $(pwd):/code any-imagen

If you run the command again with changes to your code, those changes will be reflected in the container.

The ideal solution is to run the code editor inside the container. To do this, follow these steps:

  1. Install the Docker extension in Visual Studio Code.
  2. Run the container.
  3. In the button to the left of the Visual Studio Code interface, click the Attach to container button.
  4. Install Python extension.

...

There are a better fancy way to do that, is called docker-compose file:

version: '3'

services:

  app:

    build:

      context: .

      dockerfile: Dockerfile

    ports:

      - 80:80

    volumes:

      -.:/code

 

thats all.