Getting Started with Docker – A Beginner's Guide:
What is Docker ?
Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers. The service has both free and premium tiers. The software that hosts the containers is called Docker Engine.
Why Docker ?
Consistency: Docker containers encapsulate an application and its dependencies, ensuring that it runs consistently across different environments.
Efficiency: Containers are lightweight, sharing the host system’s OS kernel, which means they use fewer resources than traditional virtual machines.
Scalability: Docker makes it easy to scale your application horizontally by running multiple container instances.
Portability: A Docker container can run on any system that supports Docker, making it highly portable.
Essential Docker commands and RUN a mysql container :
To debug the following issue:
Step 1: Add user in docker group docker sudo usermod -aG docker $USER
Step2: Change the permissions sudo chmod 757 /var/run/docker.sock
docker pull hello-world
: It will pull the hell-world image from docker hub
docker images
: will display the pulled images
docker ps
: will display running containers
Pull the mysql image:- Compiled version of filedocker pull mysql
RUN the command docker run -d -p 80:80 nginx:latest
It will pull and run the ngnix image
-d: It will run in background
-p: Use to publish and bind the port
80:80 Your system port( can modifiy ) : Container port
Open 80 port in firewall.
Hit the system ip address serve with 80 port.
Nginx is working
RUN the docker run hello-world:latest
RUN the docker run mysql:latest
docker run -d
command will RUN your containers in Background
Mysql is asking to pass the environment variables
docker run -e MYSQL_ROOT_PASSWORD=****** mysql:latest
* denotes the passwd
Mysql container is up and running...
To Get into Mysql container RUN it in detached mode using -d :
Now its running in detached mode or running in backend.
Now you are inside the mysql container
docker system prune
This command will remove all running containers forcefully.
--> docker build -t image-name:tag path to file
This is the command used to build a Docker image.
--> docker exec -it cont_ID bash
This command is used to go inside the container.
exec: execute
-it: iterative terminal
--> docker logs cont_ID
Here's an overview of what you can expect:
Application output
Startup messages
Error messages
Access logs
System messages
Custom logging
Timestamps
--> docker inspect cont_ID
The output is in Key=Value format and includes a wide range of information, such as:
ID: The unique identifier of the container.
Image: The image from which the container was created.
State: The current state of the container (e.g. running, exited).
Config: Configuration options like environment variables, command to run, exposed ports, etc.
NetworkSettings: Information about the container's networking configuration, including IP address, ports, etc.
The docker inspect
command is a powerful tool for troubleshooting, auditing, and understanding the detailed configuration and state of Docker objects.
To kill a running Docker container, you have a few options. Here's a step-by-step process:
List running containers using docker ps
command
Kill the container using docker kill cont_ID or cont_name
command
Alternative: Stop the container gracefully using docker stop cont_ID
which will send a SIGTERM signal.
Remove the container using docker rm cont_ID
command
If you want to stop and remove a container in one command then use docker rm -f cont_ID
If you remove all the stop conatiner the use docker container prune
To kill all the unused containers in one command
docker system prune
Remember:
docker kill
is immediate and doesn't allow for graceful shutdown.docker stop
is generally preferred as it allows the application to shut down cleanly.Always be cautious when killing containers, especially in production environments.
"Thank you for reading my blog! Happy Learning!!!😊
Stay tuned for more DevOps articles follow me on Hashnode and connect on LinkedIn (https://www.linkedin.com/in/ashish-karad)
for the latest updates and discussions.
Subscribe to my newsletter
Read articles from Ashish Karad directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Ashish Karad
Ashish Karad
Stay tuned for Devops techno..!!