Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called container
In linux os, two version of same software maintains by using Namespace & ControlGroups(cgroups)
Namespaces: allowing multiple instances of the same software to run simultaneously without interference. Control Groups (cgroups): Manage resource allocation (CPU, memory, etc.) for containers.
The Docker Engine is a client-server application that consists of:
Command | Description |
---|---|
-v, --version |
Print version information and quit |
-D, --debug |
Enable debug mode |
--help |
Print usage information |
--name |
User-defined name for the container. Example: docker run --name=rajCont redis:alpine |
-d |
Detached mode: runs container in the background, even after ctrl+c |
-a |
Attached mode: prints output to the terminal |
-it |
Interactive terminal mode |
-e |
Set environment variables |
-p |
Maps ports from the host to the container |
--net |
Specifies the network where the container will run |
Command | Description |
---|---|
docker images |
List all images present on the local machine |
docker create [IMAGE] |
Create a container layer over the specified image, printing the container ID |
docker rmi [IMAGE] |
Remove a Docker image |
docker start [CONTAINER] |
Start the specified container |
docker run [IMAGE] |
Run an image from local or Docker Hub. Example: docker run busybox |
docker run [IMAGE]:[VERSION] |
Pull a specific version of a Docker image |
docker run --name [CONTAINER] [IMAGE] |
Run an image with a user-defined container name |
docker run [IMAGE] [COMMAND] |
Run a command in a new container |
docker run -it [CONTAINER] [COMMAND] |
Run a command in an interactive terminal |
docker run -a [CONTAINER] [COMMAND] |
Run a command in attached mode |
docker run -d [IMAGE] |
Run a container in detached mode |
docker run -p [HOST_PORT]:[CONTAINER_PORT] [CONTAINER] |
Map host port to container port |
docker exec [CONTAINER] [COMMAND] |
Execute a command in an already running container |
docker exec -it [CONTAINER] /bin/bash |
Open a shell in the running container |
Command | Description |
---|---|
docker ps |
Show running containers |
docker ps -a or docker ps --all |
Show history of all containers |
docker start -a [CONTAINER] |
Start and attach to the specified container |
docker --restart [CONTAINER] |
Restart the specified container |
docker stop [CONTAINER] |
Gracefully stop a running container |
docker kill [CONTAINER] |
Forcefully stop a running container |
docker pause [CONTAINER] |
Pause a running container |
docker unpause [CONTAINER] |
Unpause a paused container |
docker rm [CONTAINER] |
Remove a stopped container |
docker attach [CONTAINER] |
Attach to a running container’s shell |
docker run [HOST_PORT]:[CONTAINER_PORT] [CONTAINER] |
Map ports from host to container |
Command | Description |
---|---|
docker ps --all |
List all previously run containers |
docker run --rm [CONTAINER] |
Remove container after execution |
docker system prune |
Remove stopped containers and unused resources |
docker inspect [CONTAINER] |
Show low-level details about a container, like IP |
Command | Description |
---|---|
docker network ls |
List all networks |
docker network create [NETWORKNAME] |
Create a new network |
docker network create --driver bridge [NETWORKNAME] |
Create a new network with a bridge driver |
Run a container with no network:
docker run -d --net none [CONTAINER]
docker network ls
eth0
- Bridge privatelo
- LoopbackConnect a container to a specific network:
docker run --net [NETWORK_NAME] [CONTAINER]
Disconnect a container from a network:
docker network disconnect [NETWORK_NAME] [CONTAINER]
docker run --net host [CONTAINER]
A Dockerfile is a text file containing instructions to assemble a Docker image.
It consists of three main parts:
Command | Description |
---|---|
docker build [DOCKERFILE_PATH] |
Build an image using the Dockerfile from the specified path, generating an image ID. |
docker build -t [DOCKER_ID]/[PROJECT_ID]:[VERSION] [DOCKERFILE_PATH] |
Build an image with a customized name. Example: docker build -t rajDock/redis:latest . |
docker run [IMAGE_NAME] |
Run a container from the specified image (default version is latest). |
docker commit -c 'CMD ["redis-server"]' [RUNNING_CONTAINER_ID] |
Create a new image from an existing running container. |
FROM node:alpine # Alpine is a lightweight version of an image
COPY ./ ./ # Copy files from the current directory to the container
RUN apt-get update # Update package index
RUN npm install # Install dependencies
CMD ["npm", "start"] # Command to run when the container starts
Instruction | Description | Example |
---|---|---|
FROM |
Set the base image. | FROM alpine |
RUN |
Executed during the image build process, on top of the current image layer. | RUN apt-get update |
CMD |
Command executed when the container starts. | CMD echo 'hello world' |
ENTRYPOINT |
Similar to CMD, but cannot be overridden by command line arguments. | ENTRYPOINT ["redis-server"] |
COPY |
Copy files from the host file system into the container. | COPY composer.json ./ |
ADD |
Similar to COPY, but can also copy files from URLs and extract tar files. | ADD http://example.com/file.tar.gz ./ |
ENV |
Set environment variables. | ENV name=rajesh |
EXPOSE |
container listens on the specified network ports at runtime. | EXPOSE 80/tcp |
LABEL |
Add metadata to the image. | LABEL description="this is cool" |
USER |
Specify the username or user group to use when running the image. | USER user:group |
VOLUME |
Create a mount point with specified path and mark it as holding externally mounted volumes. | VOLUME /var/log/ |
WORKDIR |
Set the working directory for any subsequent instructions. | WORKDIR /var/log/ |
ONBUILD |
Adds a trigger instruction to image that will be executed at a later time. | ONBUILD RUN echo "This runs on build" |
STOPSIGNAL |
Sets the system call signal that will be sent to the container to stop it. | STOPSIGNAL SIGKILL |
cloud-based registry service for sharing and managing Docker image
Command | Description |
---|---|
docker login --username=rajesh |
Log in to Docker Hub with the specified username. |
docker pull username/imageName |
Pull an image from Docker Hub to your local machine. |
docker push username/imageName |
Push an image from your local machine to Docker Hub. |
docker tag user/image:tag user/image:newtag |
Add a new tag to an existing image. |
docker search searchterm |
Search for images on Docker Hub using the specified search term. |
docker commit <container_id> <image_name>:<tag>
FROM debian:jessie
RUN apt-get update
COPY abc.txt /src/abc.txt
ADD abc.txt /src/abc.txt
WORKDIR /src
USER admin
CMD ["initial command"]
To build an image from a Dockerfile, use the following command:
docker build -t <image_name>
.
docker build -t <image_name> --no-cache=true
To link a container to another (e.g., a Redis container), use:
docker run --link redis <container_id>
tool that simplifies the process of defining and running multi-container Docker applications.
docker-compose.yml
.Command | Description |
---|---|
docker-compose -f [YMLFILENAME] up |
Start services defined in YML file |
docker-compose -f [YMLFILENAME] down |
Stop and remove services |
docker-compose start |
Start existing services |
docker-compose stop |
Stop running services |
docker-compose pause |
Pause services |
docker-compose unpause |
Resume paused services |
docker-compose build |
Rebuild all images |
docker logs -f [CONTAINERID] |
View logs for specific container |
docker logs -f [CONTAINERNAME] |
View logs using container name |
docker-compose ps |
List running services |
docker-compose log [CONTAINER] |
Show logs for a specific service |
version: '3'
services:
mongodb:
image: mongo
ports:
- "27017:27017"
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=password
volumes:
- db-data:var/lib/mongo/data
build: .
# build from Dockerfile
context: ./Path
dockerfile: Dockerfile
volumes:
- .db-data
Volume Type | Command Example | Description |
---|---|---|
Host Volume | docker run -v [HOST_DIR]:[CONTAINER_DIR] [CONTAINER] |
Useful for sharing files between the host and container. |
Anonymous Volume | docker run -v [CONTAINER_DIR] [CONTAINER] |
Automatically managed by Docker; less control over storage. |
Named Volume | docker run -v [ANYNAME]:[CONTAINER_DIR] [CONTAINER] |
Creates a named volume for easier data management. |