Sunday, September 24, 2023
HomeSoftware EngineeringDocker Deep Dive Collection - Half 2: Docker Pictures and Containers

Docker Deep Dive Collection – Half 2: Docker Pictures and Containers


In Half 1 of our Docker Deep Dive Collection, we bought Docker up and working and ran our first container. Now, in Half 2, we’ll discover Docker photos and containers in additional element. Understanding these basic ideas is essential for mastering Docker.

Docker Pictures

Docker photos are the blueprints for containers. They include all the things wanted to run an software, together with the code, runtime, libraries, and system instruments. Docker photos are constructed from a set of directions referred to as a Dockerfile.

Let’s create a easy Docker picture to get began. Create a brand new listing on your mission and inside it, create a file named Dockerfile (no file extension) with the next content material:

# Use an official Python runtime as a mum or dad picture
FROM python:3.8-slim

# Set the working listing to /app
WORKDIR /app

# Copy the present listing contents into the container at /app
COPY . /app

# Set up any wanted packages laid out in necessities.txt
RUN pip set up -r necessities.txt

# Make port 80 out there to the world outdoors this container
EXPOSE 80

# Outline atmosphere variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

On this Dockerfile:

  • We use an official Python 3.8 picture as our base picture.
  • Set the working listing to /app.
  • Copy the present listing into the container.
  • Set up Python packages from necessities.txt.
  • Expose port 80.
  • Outline an atmosphere variable NAME.
  • Specify the command to run our software.

Constructing a Docker Picture

To construct the Docker picture out of your Dockerfile, navigate to the listing containing the Dockerfile and run:

docker construct -t my-python-app .

This command tags the picture as my-python-app. The . on the finish specifies the construct context (present listing).

Docker Containers

Containers are cases of Docker photos. They’re remoted environments that run purposes. To create and run a container from our my-python-app picture:

docker run -p 4000:80 my-python-app

This command maps port 4000 in your host machine to port 80 contained in the container. Now you can entry your Python software at http://localhost:4000.

Itemizing Docker Containers

To listing working containers, use:

docker ps

To cease a container, use:

docker cease <container_id>

Conclusion

On this a part of the Docker Deep Dive Collection, we explored Docker photos and containers, and also you created your first Docker picture and ran a container. These basic ideas are the constructing blocks of Docker, and understanding them is essential for superior utilization.

Keep tuned for Half 3: Docker Compose, the place we’ll discover ways to handle multi-container purposes with Docker Compose.



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments