Docker
Introduction
Docker is a platform for developing, shipping, and running applications inside lightweight, portable containers. It enables developers to package applications with all dependencies and run them in any environment.
Installation
Follow the guide on the official Docker website .
Basic Commands
Check Docker Version
docker --version
Running a Container
docker run -d -p 80:80 nginx
Listing Running Containers
docker ps
Stopping a Container
docker stop <container_id>
Removing a Container
docker rm <container_id>
Listing Images
docker images
Removing an Image
docker rmi <image_id>
Dockerfile
A Dockerfile
is a script containing a series of instructions on how to build a Docker image.
Example Dockerfile
Dockerfile
FROM node:18
WORKDIR /app
COPY . .
RUN npm install
CMD ["npm", "start"]
Building an Image
docker build -t myapp .
Running a Container from an Image
docker run -p 3000:3000 myapp
Docker Compose
Docker Compose allows managing multi-container applications using a YAML file.
Example docker-compose.yml
version: "3"
services:
web:
image: nginx
ports:
- "80:80"
db:
image: postgres
environment:
POSTGRES_PASSWORD: example
Running Docker Compose
docker-compose up -d
Stopping Docker Compose
docker-compose down
Networking in Docker
Listing Networks
docker network ls
Creating a Network
docker network create mynetwork
Connecting a Container to a Network
docker network connect mynetwork mycontainer
Volumes in Docker
Creating a Volume
docker volume create myvolume
Listing Volumes
docker volume ls
Using a Volume in a Container
docker run -v myvolume:/data busybox
Last updated on