Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

docker

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

1.

Creating a Container: To create a simple container, use the following command,

docker create [IMAGE_NAME]

2. Creating and Running a Container: To create and run a container,

docker run [IMAGE_NAME]

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:

docker run -d [IMAGE_NAME]

3. Starting a Stopped Container: If you have a stopped container, start it using:

docker start [CONTAINER_NAME]

4. Stopping a Running Container: To stop a running container, use:

docker stop [CONTAINER_NAME]

5. Restarting a Running Container: Occasionally, you might need to restart a running container.
Use:

docker restart [CONTAINER_NAME]

6. Pausing a Running Container: Temporarily pause a process within a running container:

docker pause [CONTAINER_NAME]

7. Resuming a Paused Container: After pausing a container, resume it with:

docker unpause [CONTAINER_NAME]

8. Listing Running Containers: View a list of running containers on your system:

docker ps -a

9. Listing images: View a list of running containers on your system:

docker image ls

10. Removing a Container: To delete a container, run:

docker rm [CONTAINER_NAME]
1. docker inspect:

o The docker inspect command provides detailed information about Docker objects
(containers, images, volumes, networks, etc.).

o By default, it returns results in a JSON array format.

o You can specify a custom template using the -f or --format option to extract specific
details from the output.

o Example: To get an instance’s IP address, use:

o docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'


[CONTAINER_ID]

2. docker logs:

o The docker logs command fetches logs from a container.

o By default, it retrieves logs present at the time of execution.

o Use the --follow flag to stream new output from the container’s STDOUT and STDERR.

o Example: To view logs for a container named “mycontainer,” run:

o docker logs -f mycontainer

3. docker stats:

o The docker stats command displays live resource usage statistics for running containers.

o It shows CPU, memory, network I/O, and other metrics.

o Example: To view stats for all running containers, use:

o docker stats

4. -p and -n options:

o These options are not directly related to Docker commands.

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.

5. docker exec -it:

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).

o Example: To execute a shell inside a running container named “mycontainer,” use:

docker exec -it mycontainer sh


Docker Tags:

 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 tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

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:

o We start with the official Python image.


o Set the working directory to /app.
o Copy the local files into the container.
o Install dependencies using pip.
o Expose port 80.
o Specify the default command to run when the container starts.

To build an image from a Dockerfile, use:


docker build -t my-python-app .
Container Data Storage:
o By default, files created inside a container are stored in
a writable container layer.
o However, this has limitations:
 Data doesn’t persist when the container stops or is removed.
 Retrieving data from a stopped container can be challenging.
 The container’s writable layer is tightly coupled to the
host machine.
o Docker provides better options for data storage:
2. Volumes:
o Volumes are created and managed by Docker.
o They are stored within a directory on the Docker host.
o When you mount a volume into a container, this directory is
accessible inside the container.
o Volumes are isolated from the core functionality of the host
machine.
o Use volumes to persist data reliably in Docker.
o Example:
docker volume create mydata
docker run -v mydata:/app/data my-image

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 You can create custom, user-defined networks in Docker.

o Multiple containers can be connected to the same network.

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:

o docker network create -d bridge my-net

o docker run --network=my-net -itd --name=container3 busybox

2. Network Drivers:

o Docker offers several built-in network drivers:

 bridge: The default network driver.


 host: Removes network isolation between the container and the Docker host.

 none: Completely isolates a container from the host and other containers.

 overlay: Connects multiple Docker daemons together.

 ipvlan: Provides control over both IPv4 and IPv6 addressing.

 macvlan: Assigns a MAC address to a container.

 For more details, see Network drivers overview.

3. Container Networks:

o In addition to user-defined networks, you can attach a container directly to another


container’s networking stack using --network container:<name|id> flag.

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

You might also like