Docker Python enviroment

 

    Published Sept. 28, 2024, 4:30 p.m. by frank_casanova  

 

Here’s a cleaner, more elegant markdown version of the HTML content:


First of all, you need to create a Dockerfile:

touch Dockerfile

Next, you must 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-commands", "you-want-run", "inside-container"]

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-image

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-image

To check if the container is 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 in your container. To solve this, we're going to use a volume. Volumes are the preferred mechanism to persist data:

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

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 bottom-left of the Visual Studio Code interface, click the Attach to container button.
  4. Install the Python extension.

There’s a better and fancier way to do this, called a docker-compose file:

version: '3'

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - 80:80
    volumes:
      - .:/code

That’s all!


This markdown format is now clean, readable, and maintains all the content. Let me know if you'd like to tweak anything!

 

Similar posts

Docker Python enviroment

Demystifying NAT: A Deep Dive into Network Address Translation

Congestion Control: A Journey Through TCP's Wisdom

Delve into the depths of TCP's flow control mechanism and discover how it ensures smoot

0 comments

There are no comments yet.

Add a new comment