Docker is a platform for developing, shipping, and running applications in containers:
# Basic Docker commands
docker pull nginx # Download image
docker run -p 80:80 nginx # Run container
docker ps # List running containers
docker stop container_id # Stop containerDocker is a platform that solves the classic development problem: “It works on my machine, why doesn’t it work on the server?” It allows packaging an application with all its dependencies into a standardized unit — a container. 🐳
Containers are lightweight, standalone, executable packages of software that include everything needed to run an application:
# Creating a container from an image
docker run --name my-app -d -p 3000:3000 my-app-imageDocker ensures that your application will run the same in any environment:
# Running a container with a mounted local directory
docker run -v $(pwd):/app -p 8080:80 nginxUnlike virtual machines, containers use a shared operating system kernel:
# Scaling with Docker Compose
docker-compose up -d --scale web=5Docker allows tracking image versions and creating microservice architecture:
# Tagging images
docker build -t my-app:1.0 .
docker build -t my-app:latest .Text file with instructions for building an image:
FROM node:14
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]Read-only template with instructions for creating a container:
# Building an image
docker build -t my-app .
# Viewing images
docker imagesRunning instance of an image:
# Running a container
docker run -p 3000:3000 my-app
# Viewing running containers
docker ps
# Stopping a container
docker stop container_idStorage for Docker images:
# Publishing an image to Docker Hub
docker push username/my-app:1.0
# Downloading an image from Docker Hub
docker pull username/my-app:1.0# Running a development environment
docker run -v $(pwd):/app -p 3000:3000 node:14 npm start# Running tests in a container
docker run my-app npm test# Running in production mode
docker run -d --restart=always -p 80:3000 my-app:1.0Tool for defining and running multi-container applications:
# docker-compose.yml
version: '3'
services:
web:
build: .
ports:
- "3000:3000"
db:
image: mongo
volumes:
- db-data:/data/db
volumes:
db-data:# Starting all services
docker-compose up -dDocker is an essential tool in modern development that:
Use Docker to create reliable, portable, and scalable applications! 🚀