My Docker learning short notes
Why Docker?
Docker enables containerization - running applications in containers instead of virtual machines. Important notes:
- Containers are not VMs - they have different benefits
- Provides standardization across environments
- Enables continuous deployment from development to production
- Ensures consistency using the same container image throughout the deployment process
Key Benefits:
- Self-contained environments
- Isolated from other applications
- Runs almost everywhere (especially in the Cloud)
- Small and lightweight
- Highly scalable
- Fast deployment
Core Concepts
Images
An image is an executable package containing everything needed to run an application:
- Application code
- Runtime environment
- Libraries
- Environment variables
- Configuration files
Containers
A container is a runtime instance of an image - what the image becomes in memory when executed (an image with state, or a user process).
Dockerfile
A Dockerfile is a text document containing all commands needed to build a Docker image. It's the blueprint for creating Docker images.
Essential Docker Commands
Version Information
docker --version # or docker -v
docker-compose --version
docker-machine --version
Basic Container Operations
# Run containers
docker run hello-world # Test installation
docker run -d -p 80:80 --name webserver nginx # Run nginx server
docker run -p 4000:80 friendlyhello # Run with port mapping
docker run -d -p 4000:80 friendlyhello # Run in detached mode
# Container management
docker container ls # List running containers
docker container ls -a # List all containers
docker container stop [container-id] # Stop container
docker container rm [container-id] # Remove container
docker logs [container-name] # View container logs
# Image management
docker image ls # List images
docker image rm [image-id] # Remove image
docker build -t [name] . # Build image from Dockerfile
Container Access
# Enter container shell
docker exec -it [container-id/name] sh
docker exec -it [container-id] bash
# Get container IP
docker inspect [container-id] | grep "IPAddress"
Docker Swarm Commands
docker stack ls # List stacks/apps
docker stack deploy -c [compose-file] [app-name] # Deploy app
docker service ls # List running services
docker swarm leave --force # Leave swarm cluster
Image Registry Operations
docker login # Login to Docker Hub
docker tag [image] username/repository:tag # Tag image
docker push username/repository:tag # Push to registry
docker pull username/repository:tag # Pull from registry
Note: Replace text in [brackets] with your actual values when using these commands.
Best Practices
- Always tag your images with meaningful versions
- Use appropriate base images
- Remove unused containers and images regularly
- Use docker-compose for multi-container applications
- Keep container images small
Comments