docker
docker
docker
The -d option in Docker is used to run a container in detached mode. When you start a
container with -d, it means the container runs in the background, and you can continue using
your terminal or shell without being attached to the container’s console. Here’s how you can use
it:
5. Restarting a Running Container: Occasionally, you might need to restart a running container.
Use:
docker ps -a
docker image ls
docker rm [CONTAINER_NAME]
1. docker inspect:
o The docker inspect command provides detailed information about Docker objects
(containers, images, volumes, networks, etc.).
o You can specify a custom template using the -f or --format option to extract specific
details from the output.
2. docker logs:
o Use the --follow flag to stream new output from the container’s STDOUT and STDERR.
3. docker stats:
o The docker stats command displays live resource usage statistics for running containers.
o docker stats
4. -p and -n options:
o However, -p is commonly used to specify port mappings when running containers (e.g., -
p 8080:80).
o -n is not a standard Docker option; it might be specific to a particular use case or tool.
o The docker exec command runs a new command inside an existing container.
o -it allows you to interact with the container’s terminal (interactive mode).
A Docker tag is a human-readable label assigned to a Docker image. It allows you to identify and
manage different versions or variants of an image.
You can create tags using the docker image tag command. For example:
Docker file
The first instruction must be FROM, which specifies the parent image from which you’re
building.
Other common instructions include RUN, COPY, ENV, EXPOSE, and CMD.
1. Example: Let’s say you want to create a Docker image for a Python application.
Here’s a simple Dockerfile:
2. # Use an official Python runtime as the base image
3. FROM python:3.9
4.
5. # Set the working directory inside the container
6. WORKDIR /app
7.
8. # Copy the current directory contents into the container at /app
9. COPY . /app
10.
11. # Install any needed dependencies
12. RUN pip install -r requirements.txt
13.
14. # Make port 80 available to the world outside this container
15. EXPOSE 80
16.
17. # Define the command to run when the container starts
18. CMD ["python", "app.py"]
In this example:
3. Bind Mounts:
shared folder شبه ال-
o Bind mounts can be stored anywhere on the host system.
o They may even be important system files or directories.
o Non-Docker processes can modify bind mounts.
o Use bind mounts when you need flexibility but understand the
risks.
o Example:
docker run -v /host/path:/container/path my-image
1. User-Defined Networks:
o Containers within the same network can communicate using container IP addresses or
names.
o The following example creates a network using the bridge network driver and runs a
container in the created network:
2. Network Drivers:
none: Completely isolates a container from the host and other containers.
3. Container Networks:
o Some flags are not supported for containers using the container: networking mode
(e.g., --add-host, --hostname, etc.).
4. What is Docker Compose?
o Docker Compose allows you to define and manage multi-container
applications.
o It streamlines development and deployment by providing a single
configuration file.
o With Compose, you can create, start, and stop all services defined in your
configuration with a single command.
5. How Does Compose Work?
o You define your services (containers), networks, and volumes in a docker-
compose.yaml file.
o Each service specifies its image, environment variables, ports, and
dependencies.
o Compose creates a network for your services, allowing them to
communicate.
o Running docker-compose up starts all services defined in the file.
6. Key Benefits of Using Compose:
o Simplicity: Define your entire application stack in one file.
o Consistency: Ensure consistent environments across development,
testing, and production.
o Efficiency: Easily manage services, networks, and volumes.
o Lifecycle Management: Start, stop, and rebuild services with ease.
7. Getting Started with Docker Compose:
o Install Docker Compose (if not already installed).
o Create a docker-compose.yaml file with your service definitions.
o Run docker-compose up to start your application stack.
8. Example:
o Here’s a simple example of a Compose file for a Python web application
and a MySQL database:
o version: '3'
o services:
o db:
o image: mysql:5.7
o environment:
o MYSQL_ROOT_PASSWORD: secret
o MYSQL_DATABASE: mydb
o web:
o image: python:3.9
o ports:
o - "8080:80"
o environment:
DB_HOST: db