Deploying and Managing Docker Applications
Once you have created your custom Docker image, you can deploy and manage your Docker-based applications. This section will cover the key concepts and steps involved in deploying and managing Docker applications.
Deploying Docker Containers
To deploy a Docker container, you can use the docker run
command. This command starts a new container based on a specified image and allows you to configure various options, such as port mapping, environment variables, and volume mounts.
Here's an example of running a container based on the "my-custom-image" image:
docker run -d -p 8080:8080 -e DB_HOST=192.168.1.100 -v /data:/app/data my-custom-image
This command will:
- Run the container in detached mode (
-d
)
- Map the container's port 8080 to the host's port 8080 (
-p 8080:8080
)
- Set an environment variable
DB_HOST
with the value 192.168.1.100
(-e DB_HOST=192.168.1.100
)
- Mount a host directory
/data
to the container's /app/data
directory (-v /data:/app/data
)
- Use the "my-custom-image" image to start the container
Managing Docker Containers
Docker provides several commands to manage running containers:
docker ps
: List all running containers
docker stop <container_id>
: Stop a running container
docker start <container_id>
: Start a stopped container
docker logs <container_id>
: View the logs of a container
docker exec -it <container_id> /bin/bash
: Access the shell of a running container
Docker Compose
Docker Compose is a tool that allows you to define and manage multi-container Docker applications. It uses a YAML file to configure the services, networks, and volumes for your application.
Here's an example docker-compose.yml
file:
version: "3"
services:
web:
build: .
ports:
- "8080:8080"
environment:
- DB_HOST=database
depends_on:
- database
database:
image: mysql:5.7
environment:
- MYSQL_DATABASE=myapp
- MYSQL_ROOT_PASSWORD=secret
volumes:
- db-data:/var/lib/mysql
volumes:
db-data:
This Compose file defines two services: a web service and a database service. The web service is built from the current directory, while the database service uses the official MySQL 5.7 image. The services are connected through a network, and the database service's data is persisted in a named volume.
To deploy this application, you can run docker-compose up -d
in the same directory as the docker-compose.yml
file.
Scaling Docker Applications
Docker makes it easy to scale your applications by adding or removing containers. You can use Docker Compose to scale your services:
docker-compose up -d --scale web=3
This command will start three instances of the "web" service.
Monitoring and Logging
Docker provides built-in tools for monitoring and logging your containers. You can use the docker stats
command to view real-time resource usage, and the docker logs
command to access the logs of a container.
Additionally, you can integrate your Docker applications with external monitoring and logging tools, such as Prometheus, Grafana, and Elasticsearch, to gain more advanced insights and capabilities.