Welcome to the Docker Quick Start Guide! This README.md will help you understand the basics of Docker, including essential commands and concepts, to get you up and running quickly.
The core of Docker, allowing you to create and run containers.
The command-line tool to interact with Docker.
A storage and distribution system for Docker images. The most commonly used registry is Docker Hub.
A Docker image is a lightweight, standalone, executable package that includes everything needed to run a piece of software, including the code, a runtime, libraries, environment variables, and config files.
A container is a running instance of an image. It encapsulates the application or service and its dependencies, running in an isolated environment.
- List images:
docker images
- List running containers:
docker ps
- Run a container:
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
- Build an image:
docker build -t image_name .
A Dockerfile is a script containing a series of instructions on how to build a Docker image. Here are some common Dockerfile commands:
- WORKDIR: Sets the working directory for any
RUN
,CMD
,ENTRYPOINT
,COPY
instructions that follow it. - RUN: Executes any commands in a new layer on top of the current image and commits the results.
- CMD: Provides defaults for executing a container. There can only be one
CMD
instruction in a Dockerfile. - EXPOSE: Informs Docker that the container listens on the specified network ports at runtime.
- ENV: Sets environment variables.
- COPY: Copies files from the Docker host to the Docker image.
FROM node:20
# Set the working directory
WORKDIR /app
# Copy package.json and package-lock.json
COPY package* .
# Install dependencies
RUN npm install
# Copy the rest of the files
COPY . .
# Build the project
RUN npm run build
# Expose port 3000
EXPOSE 3000
# Start the application
CMD ["node", "dist/index.js"]
To build a Docker image from a Dockerfile, use:
docker build -t image_name .
To run a Docker image as a container, use:
docker run -d -p 3000:3000 image_name
You can pass environment variables to a container using the -e
flag:
docker run -d -p 3000:3000 -e ENV_VAR_NAME=value image_name
- Kill a container:
docker kill container_id
- Execute a command inside a container:
docker exec -it container_id command
Docker images are made up of layers. Each instruction in a Dockerfile adds a new layer to the image.
Volumes are used to persist data generated by and used by Docker containers. To create and use a volume, follow these steps:
-
Create a volume:
docker volume create my_volume
-
Run a container with a volume:
docker run -d -p 3000:3000 -v my_volume:/path/in/container image_name
Bind mounts allow you to mount a file or directory from your host machine into a container. This can be useful for development when you want the container to use the latest code from your host. (Ex. NextJS app hot reloading)
-
Run a container with a bind mount:
docker run -d -p 3000:3000 -v /path/on/host:/path/in/container image_name
/path/on/host
: Path to the file or directory on your host machine./path/in/container
: Path where the file or directory will be mounted inside the container.
Docker networking allows containers to communicate with each other and with other non-Docker workloads. To create and use a network, follow these steps:
-
Create a network:
docker network create my_network
-
Run a container and connect it to the network:
docker run -d --network my_network image_name
Docker Compose is a tool for defining and running multi-container Docker applications. A docker-compose.yml
file allows you to configure your application’s services, networks, and volumes.
version: '3.8'
services:
mongodb:
image: "mongo"
container_name: mongodb
ports:
- "27017:27017"
volumes:
- "mongodb_data:/data/db"
backend:
image: "backend22"
container_name: backend_application
ports:
- "3000:3000"
environment:
MONGO_URL: "mongodb://mongodb:27017/test"
volumes:
mongodb_data:
This quick start guide covers the basics of Docker to help you get started. For more detailed information, check out the Docker documentation.
Happy Dockering! 🐳🚀