Diving into Docker: Docker Commands and Docker Compose Demystified
Let's explore the world of Docker together! In this blog, we'll dive into essential Docker commands, Docker Compose, and YAML. These tools are key players in managing containers and streamlining development. Join me as we uncover their roles and how they make container orchestration a breeze.
Docker Compose
Docker Compose simplifies the orchestration of multi-container Docker apps. By using a single YAML file, docker-compose.yml, it enables defining services, networks, and volumes for a multi-container setup. This tool streamlines complex environment configurations, allowing easy launch, management, and scaling of multiple services with a single command.
What is YAML?
YAML (YAML Ain't Markup Language) stands as a human-readable data serialization format, widely preferred for configuration files and data exchange. Its simplicity and readability make it a go-to choice. YAML employs indentation and whitespace for structuring data, ensuring ease for both humans and machines to interpret. It's extensively used alongside configuration files for tools like Docker Compose, Kubernetes, and more, whenever readable data representation is crucial.
Task-1
Learn how to use the docker-compose.yml file, to set up the environment, configure the services and links between different containers, and also to use environment variables in the docker-compose.yml file.
For docker-compose installation follow these steps:
Linux
Install via Package Manager: Use the following commands to download the latest version of Docker Compose:
sudo curl -L "
[https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname
](github.com/docker/compose/releases/latest/d..-s)-$(uname -m)" -o /usr/local/bin/docker-compose
Apply Execute Permissions: Apply execute permissions to the binary:
sudo chmod +x /usr/local/bin/docker-compose
Verify Installation: Check if Docker Compose is correctly installed:
docker-compose --version
Navigate to the Directory: Use the
cd
command to move to the directory containing yourdocker-compose.yml
file.Run Docker Compose: Once in the directory, execute the following command:
docker-compose up
Run in Detached Mode (Optional): To run in detached mode (in the background), use the
-d
flag:
docker-compose up -d
Stop Services: To stop the services defined in the Compose file, run:
docker-compose down
This stops and removes the containers, networks, and volumes.
Task-2
Pull a pre-existing Docker image from a public repository (e.g. Docker Hub) and run it on your local machine. Run the container as a non-root user (Hint- Use
usermod
command to give the user permission to docker). Make sure you reboot the instance after permitting the user.
docker pull image_name
docker run -d -p host_port:container_port --name container_name image_name
Inspect the container's running processes and exposed ports using the docker inspect command.
docker inspect container_name
Use the docker logs command to view the container's log output.
docker logs container_name
Use the docker stop and docker start commands to stop and start the container.
docker stop container_name
docker start container_name
Use the docker rm command to remove the container when you're done.
docker rm container_name
How to run Docker commands without sudo?
Make sure docker is installed and system is updated.
sudo usermod -a -G docker $USER
Reboot the machine.
Thank you!