In Half 1 of our Docker Deep Dive Sequence, we acquired Docker up and operating and ran our first container. Now, in Half 2, we’ll discover Docker pictures and containers in additional element. Understanding these basic ideas is essential for mastering Docker.
Docker Photos
Docker pictures are the blueprints for containers. They comprise all the pieces wanted to run an software, together with the code, runtime, libraries, and system instruments. Docker pictures 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 to your venture 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 accessible to the world outdoors this container
EXPOSE 80
# Outline surroundings 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 surroundings 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 situations of Docker pictures. They’re remoted environments that run functions. 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 operating containers, use:
docker ps
To cease a container, use:
docker cease <container_id>
Conclusion
On this a part of the Docker Deep Dive Sequence, we explored Docker pictures 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 functions with Docker Compose.