Docker Guide
Docker Guide
will help you get started with Docker real quickly. Go ahead
Happy Dockering!
The Ultimate
Next 14 Course
Image
Docker Image
1. Build an image from a Dockerfile:
Terminal
docker images
# Example
docker image ls
Or
Terminal
docker rm [image_name/image_id]
# Example
docker rm fd484f19954f
5. Tag an image:
Terminal
Container
Docker Container
Terminal
# Example
Terminal
# Example
Terminal
docker ps
Terminal
docker ps -a
Terminal
# Example
Terminal
# Example
Terminal
# Example
Terminal
# Example
Terminal
docker rm container_name_or_id
# Example
docker rm my_container
Terminal
docker rm -f container_name_or_id
# Example
docker rm -f my_container
Terminal
# Example
Terminal
# Example
Terminal
# Example
Terminal
# Example
Volumes
and Network
Docker Volumes and Network
Volumes:
Terminal
# Example
Terminal
docker volume ls
Terminal
# Example
4. Remove a volume:
Terminal
# Example
Terminal
# Example
Terminal
# Example
Terminal
# Example
Terminal
docker network ls
Terminal
# Example
Terminal
# Example
Terminal
# Example
Terminal
# Example
Compose
Docker Compose
1. Create and start containers defined in a docker-
compose.yml file:
Terminal
docker compose up
compose.yml file:
Terminal
project:
Terminal
docker compose ps
This command shows the logs for all services defined in the
docker-compose.yml file.
docker volume ls
Docker
Latest Docker
docker init
Reference
Dockerfile Reference
What is a Dockerfile?
A Dockerfile is a script that contains instructions for
building a Docker image. It defines the base image, sets up
environment variables, installs software, and configures
the container for a specific application or service.
Dockerfile Syntax
FROM:
Specifies the base image for the Docker image.
FROM image_name:tag
# Example
FROM ubuntu:20.04
WORKDIR /path/to/directory
# Example
WORKDIR /app
COPY:
Copies files or directories from the build context to the
container.
ENV:
Sets environment variables in the image.
ENV KEY=VALUE
# Example
ENV NODE_VERSION=14
EXPOSE port
# Example
EXPOSE 8080
CMD:
Provides default commands or parameters for an
executing container.
CMD ["executable","param1","param2"]
# Example
CMD ["npm", "start"]
Or,
ENTRYPOINT:
Configures a container that will run as an executable.
ENTRYPOINT ["executable","param1","param2"]
# Example
ENTRYPOINT ["node", "app.js"]
Or,
ARG VARIABLE_NAME=default_value
# Example
ARG VERSION=latest
VOLUME:
Creates a mount point for external volumes or other
containers.
VOLUME /path/to/volume
# Example
VOLUME /data
LABEL key="value"
# Example
LABEL version="1.0" maintainer="Adrian"
USER:
Specifies the username or UID to use when running the
image.
USER user_name
# Example
USER app
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
# Install dependencies
COPY . .
EXPOSE 8080
ENV NODE_ENV=production
Compose
File Reference
Docker Compose File Reference
What is a Docker Compose File?
A Docker Compose file is a YAML file that defines a multi-
container Docker application. It specifies the services,
networks, and volumes for the application, along with any
additional configuration options.
version:
Specifies the version of the Docker Compose file format.
Example:
version: '3.8'
services:
web:
image: nginx:latest
networks:
Configures custom networks for the application.
Example:
networks:
my_network:
driver: bridge
volumes:
my_volume:
environment:
Sets environment variables for a service.
Example:
environment:
- NODE_ENV=production
ports:
- "8080:80"
depends_on:
Specifies dependencies between services, ensuring one
service starts before another.
Example:
depends_on:
- db
build:
context: .
dockerfile: Dockerfile.dev
volumes_from:
Mounts volumes from another service or container.
Example:
volumes_from:
- service_name
version: '3.8'
services:
# MongoDB service
mongo:
image: mongo:latest
ports:
- "27017:27017"
volumes:
- mongo_data:/data/db
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: admin
api:
build:
context: ./api
dockerfile: Dockerfile
- "5000:5000"
depends_on:
- mongo
environment:
MONGO_URI: mongodb://admin:admin@mongo:27017/
mydatabase
networks:
- mern_network
client:
build:
context: ./client
dockerfile: Dockerfile
ports:
- "3000:3000"
depends_on:
- api
networks:
- mern_network
volumes:
mongo_data:
networks:
mern_network: