Docker Notes
Docker Notes
Docker Notes
https://www.linkedin.com/in/mubeen507/
DOCKER
Docker Overview:
1. Docker Engine:
2. Docker Image:
3. Container:
4. Docker Hub:
1. Consistent Environment:
2. Dependency Management:
Follow for more DevOps
https://www.linkedin.com/in/mubeen507/
3. Isolation:
4. Resource Efficiency:
5. Portability:
6. Scalability:
1. Web Applications:
2. Microservices Architecture:
4. Database Containers:
Follow for more DevOps
https://www.linkedin.com/in/mubeen507/
5. Development Environments:
7. Serverless Architectures:
Dockerfile
A Dockerfile is a script that contains instructions for building a Docker image. It
specifies the base image, sets up the environment, installs dependencies, and defines
how the application should run. Let's go through the key components of a Dockerfile
using a simple example.
Example Dockerfile:
# Use an official Python runtime as a parent image
FROM python:3.8-slim
Explanation:
1. FROM:
o Specifies the base image for the Docker image. In this case, it uses the official
Python 3.8 image as a starting point.
2. WORKDIR:
3. COPY:
o Copies the current directory (the directory where the Dockerfile is located)
into the container at /app. This includes your application code.
Follow for more DevOps
https://www.linkedin.com/in/mubeen507/
4. RUN:
o Executes commands in the container during the build process. In this example,
it installs the Python dependencies specified in requirements.txt.
5. EXPOSE:
o Informs Docker that the container will listen on the specified network ports at
runtime. It does not actually publish the ports; it's more of a documentation
feature.
6. ENV:
o Sets an environment variable, in this case, NAME with the value "World". This
variable can be accessed by the application inside the container.
7. CMD:
o Provides the default command to run when the container starts. In this
example, it runs python app.py. You can override this command when running
the container.
Usage:
1. Save the above Dockerfile in a file named Dockerfile in your project directory.
2. Create a file named requirements.txt if your application has Python
dependencies. Add the necessary dependencies.
3. Create your Python application file (e.g., app.py) in the same directory.
This example assumes a simple Python web application. Adjust the Dockerfile based
on your specific application, its dependencies, and how it needs to be executed
within the container.
Follow for more DevOps
https://www.linkedin.com/in/mubeen507/
Docker Image
A Docker image is a lightweight, standalone, and executable software package that
includes everything needed to run a piece of software, including the code, runtime,
libraries, dependencies, and system tools. Docker images are built from a set of
instructions called a Dockerfile, which specifies the steps needed to create the image.
3. Layers: Docker images are built in layers, and each instruction in the
Dockerfile adds a new layer to the image. Layers are cached, and if a
Dockerfile hasn't changed, Docker can reuse existing layers, making the build
process more efficient.
4. Registry: Docker images can be stored and shared through a registry. Docker
Hub is a popular public registry, but you can also set up private registries for
more control over image distribution.
5. Base Image: The starting point for a Docker image is often a base image that
provides a minimal operating system environment. Common base images
include Alpine Linux, Ubuntu, and others.
6. Tag: Docker images can have tags to differentiate between different versions
or configurations of the same application. For example, an image might have
tags like "latest," "v1.0," or "development."
docker images
a. Removing an Image:
a. Tagging an Image:
These commands cover various aspects of working with Docker images, including
pulling, listing, removing, tagging, building, inspecting, and managing images. Adjust
these examples based on your specific use cases and requirements.
Follow for more DevOps
https://www.linkedin.com/in/mubeen507/
Docker Container
Docker containers are instances of Docker images, and they encapsulate applications
and their dependencies, allowing them to run consistently across different
environments. Here are some basic Docker container commands with examples:
Docker Compose
Docker Compose is a tool for defining and running multi-container Docker
applications. It allows you to define a multi-container application in a single file, then
spin up the entire application stack with a single command. Here's an overview of
Docker Compose with examples:
1. Compose File:
Docker Compose configurations are written in YAML format and typically
named docker-compose.yml. This file defines services, networks, and volumes for your
application.
Example docker-compose.yml for a simple web application with a web server and a
database:
```yaml
version: '3'
services:
web:
image: nginx:latest
ports:
- "8080:80"
db:
image: postgres:latest
environment:
POSTGRES_DB: mydatabase
POSTGRES_USER: myuser
POSTGRES_PASSWORD: mypassword
```
2. Services:
Each service in the docker-compose.yml file corresponds to a container. In the example
above, there are two services: web and db.
4. Stopping Containers:
To stop the running containers:
```bash
docker-compose down
```
• The down command stops and removes containers, networks, and volumes defined in
the docker-compose.yml file.
5. Scaling Services:
You can scale services easily. For example, to run three instances of the web service:
```bash
docker-compose up --scale web=3 -d
```
6. Environment Variables:
You can define environment variables for services in the docker-compose.yml file. In
the example, the db service uses environment variables for configuring the
PostgreSQL database.
7. Volumes:
Docker Compose allows you to define volumes to persist data between container
restarts. For example, adding a volume for the web service:
```yaml
services:
web:
image: nginx:latest
ports:
- "8080:80"
volumes:
- ./html:/usr/share/nginx/html
```
In this case, the ./html directory on the host is mounted to
the /usr/share/nginx/html directory in the web container.
8. Networks:
Docker Compose automatically creates a default network for your services to
communicate. You can define custom networks and specify which services should be
connected to them.
Follow for more DevOps
https://www.linkedin.com/in/mubeen507/
networks:
custom_network:
services:
web:
networks:
- custom_network
db:
networks:
- custom_network
In this example, both the web and db services are connected to the custom_network.
Docker Network
Docker provides a networking feature that allows containers to communicate with
each other and with the outside world. Docker networking enables seamless
connectivity between containers on the same host or across multiple hosts in a
cluster. Here's an overview of Docker networking concepts along with examples:
2. Host Network:
3. Overlay Network:
o Used for connecting containers across multiple Docker hosts (in a swarm
cluster).
o Provides network abstraction, allowing containers to communicate as if they
are on the same host.
o Commonly used in Docker Swarm mode.
4. Macvlan Network:
o Allows containers to have their own MAC address, making them appear as
physical devices on the network.
o Useful when containers need to be part of an existing network.
6. List Networks:
docker network ls
5. Communication Between
Containers: Containers container1 and container2 can communicate with each
other using their container names as hostnames.
6.
Docker networking is a powerful feature that facilitates communication between
containers, and it becomes especially crucial in scenarios where you have multiple
containers or services working together.
Follow for more DevOps
https://www.linkedin.com/in/mubeen507/
Docker Volumes
Docker volumes are a way to persistently store and manage data used by Docker
containers. Volumes provide a mechanism for sharing data between containers, as
well as persisting data beyond the lifecycle of a single container. Here's an overview
of Docker volumes along with examples:
2. Bind Mounts:
o Bind mounts allow you to directly mount a directory from the host into a
container.
o Data is shared between the host and the container in real-time.
o Docker does not manage the content of bind mounts.
2. List Volumes:
docker volume ls
This command runs an Nginx container and creates/uses the named volume
"mydata."
Follow for more DevOps
https://www.linkedin.com/in/mubeen507/
This command runs an Nginx container and mounts the host directory
"/host/path" into the container.
Look for the "Mounts" section to see the volume or bind mount details.
This command runs an Nginx container with the named volume "mydata"
mounted to the container's "/usr/share/nginx/html" directory.
6. Verify Data Persistence: Stop and remove the container, then run a new
container using the same named volume:
7. docker rm -f mycontainer
docker run -d --name newcontainer -v mydata:/usr/share/nginx/html nginx
The new container should have the same data written to the volume.
This command runs an Nginx container with a bind mount, linking the host
directory "/host/path" to the container's "/usr/share/nginx/html" directory.
Follow for more DevOps
https://www.linkedin.com/in/mubeen507/
2. Write Data to the Bind Mount: You can add or modify files directly on the
host in the "/host/path" directory.
3. Verify Data Sharing: Changes made on the host in "/host/path" are reflected
inside the container at "/usr/share/nginx/html."