Docker Commands
To create a Docker Image -
docker build -t <image-name> .
To run the image in a container -
docker run -d -p <container_port>:<system_port> <image-name>
example -
docker run -d -p 5000:5000 currents_app
-d is for detached
-p is for port binding
Docker is a popular platform for containerization, allowing developers to package an application and its dependencies into a standardized unit called a container. Here are some commonly used Docker commands:
docker run
: Run a container from a Docker image.docker build
: Build a Docker image from a Dockerfile.docker pull
: Pull an image or a repository from a registry.docker push
: Push an image or a repository to a registry.docker stop
: Stop a running container.docker start
: Start a stopped container.docker restart
: Restart a running container.docker ps
: List running containers.docker images
: List available Docker images.docker exec
: Run a command in a running container.
These are just a few examples of the many Docker commands available. Docker provides a powerful and flexible environment for managing and deploying applications using containers. It is widely used in the software development industry for its ease of use and portability.
Docker Volume
Docker volumes are a way to persist data generated by and used by Docker containers. They provide a convenient and efficient way to manage data in Dockerized applications.
To create a Docker volume, you can use the following command:
docker volume create <volume-name>
Once the volume is created, you can use it to mount data to a container by specifying the volume name in the docker run
command:
docker run -d -v <volume-name>:<container-path> <image-name>
This allows the container to read and write data to the specified volume, ensuring that the data remains persistent even if the container is stopped or removed.
Docker volumes are useful for scenarios where you need to store and share data between containers, or when you want to separate the data from the container itself.
Remember to clean up unused volumes using the docker volume prune
command to avoid unnecessary disk usage.
Docker Compose
Docker Compose is a tool that allows you to define and manage multi-container Docker applications. With Docker Compose, you can use a YAML file to configure your application’s services, networks, and volumes, making it easier to deploy and scale your application.
Here are some key commands and concepts related to Docker Compose:
docker-compose up
: Start and run your application from the configuration specified in thedocker-compose.yml
file.docker-compose down
: Stop and remove containers, networks, and volumes defined in thedocker-compose.yml
file.docker-compose build
: Build or rebuild services defined in thedocker-compose.yml
file.docker-compose start
: Start services defined in thedocker-compose.yml
file.docker-compose stop
: Stop services defined in thedocker-compose.yml
file.docker-compose restart
: Restart services defined in thedocker-compose.yml
file.docker-compose logs
: View the logs of services defined in thedocker-compose.yml
file.
Docker Compose allows you to define multiple services, their dependencies, and configuration options, all in a single YAML file. It simplifies the process of managing complex applications with multiple containers, making it easier to orchestrate and scale your application.
By using Docker Compose, you can define your application’s infrastructure as code, enabling you to version and reproduce your application’s environment consistently across different environments.
For more information on how to use Docker Compose, refer to the official documentation.
The syntax for the docker-compose build
command is as follows:
docker-compose build [options] [SERVICE...]
This command is used to build or rebuild the services defined in the docker-compose.yml
file. You can specify one or more services to build, or you can build all services if no service is specified.
Here are some commonly used options for the docker-compose build
command:
f, --file FILE
: Specify an alternate compose file (default:docker-compose.yml
).-no-cache
: Do not use cache when building the image.-pull
: Always attempt to pull a newer version of the image.-parallel
: Build images in parallel.
For more information about the docker-compose build
command and its options, you can refer to the official Docker documentation.
Here are some commonly used flags for the docker-compose
command:
f, --file FILE
: Specify an alternate compose file (default:docker-compose.yml
).p, --project-name NAME
: Specify an alternate project name (default: directory name).v, --verbose
: Increase verbosity.-log-level LEVEL
: Set log level (debug
,info
,warning
,error
,critical
).q, --quiet
: Only display IDs.-no-ansi
: Do not print ANSI control characters.h, --help
: Show help message and exit.v, --version
: Print version information and exit.
These flags provide additional functionality and customization options for the docker-compose
command. For more detailed information and additional flags, please refer to the official Docker Compose documentation.