Now that Docker is installed, let's learn the commands you'll use every day.
Pulling an Image
docker pull nginx:latest
Running a Container
docker run -d -p 8080:80 --name my-nginx nginx:latest
This runs Nginx in the background (-d) and maps port 8080 on your host to port 80 in the container.
Listing Containers
docker ps # running containers
docker ps -a # all containers, including stopped
Stopping and Removing Containers
docker stop my-nginx
docker rm my-nginx
Viewing Logs
docker logs -f my-nginx
Building Your Own Image
A minimal Dockerfile for a Node.js app looks like this:
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["node", "index.js"]
Build and run it with:
docker build -t my-node-app .
docker run -p 3000:3000 my-node-app
Next, we'll bring multiple containers together with Docker Compose.