List Containers / Images.
# List all Containers.
docker ps -a
# List all Images.
docker images -a
docker image ls
Working with Images.
# Search for Image.
docker search <image>
docker search -f is-official=true <image>
# Pull Image from DockerHub.
docker pull <image[:tag|@digest]>
# View Image info.
docker inspect <image>
docker history <image>
# Remove Image.
docker rmi <image>
docker image remove <image>
Create Containers.
# Just create.
docker create -it <image>
# Create, start and attach.
docker run -it <image>
# Create, start and detach immediately.
docker run -itd <image>
# Create with name, start, attach and remove after detachment.
docker run -it --rm --name <container> <image>
Start / Unpause Containers.
# Start in the background.
docker start <container>
docker restart <container>
# Start and attach.
docker start -ia <container>
# Unpause Container.
docker unpause <container>
Stop / Pause / Restart Containers.
# Gracefully stop running Container.
docker stop <container>
# Forcefully stop unresponsive Container.
docker kill <container>
# Put Container on hold.
docker pause <container>
# Restart Container.
docker restart <container>
# Stop all running Containers (Unix, Linux ... etc).
docker stop $(docker ps -q)
# Stop all running Containers (Win CMD).
for /f "tokens=*" %i in ('docker ps -q') do docker stop %i
Delete Containers.
# Remove Container that is not running.
docker rm <container>
docker remove <container>
# Force stop and remove.
docker remove -f <container>
Attach Containers.
# Just attach.
docker attach <container>
# Attach and execute shell (bash as ex.) on running Container.
docker exec -it <container> bash
Check where the hell are you at and what’s going on.
# Current user
whoami
# Info about OS (Linux, Unix ... etc.)
cat /etc/issue
# Info about System (Win CMD)
systeminfo
# Info about docker runtime.
docker info
# View running processes of a Container.
docker top <container>
# View Container logs with time.
docker logs -t <container>
Docker file basics.
FROM <image>
USER <user>
RUN <command>
WORKDIR <path>
COPY [--chown=<user>:<group>|--from=<stage>] <host/stage_path> <container_path>
EXPOSE <ports>
CMD ["cmd 1", "cmd 2", ...]
# Build Image from dockerfile.
docker build -t <image> .
# Runs new container with exposed port to host.
docker run -dit -p <host_port>:<container_port> <image>
# Runs new container with exposed port to host and removes it after stop.
docker run -it -p <host_port>:<container_port> --name <container> --rm <image>
# Runs new container and expose all ports (see EXPOSE in docker file).
docker run -dit -P --name <container> <image>
Mount types:
# Runs container with mounted folder from the host. Removes container after stop.
docker run -it -p <h_port>:<c_port> --mount type=bind,source=<h_path>,target=<c_path> --rm <image>
# Runs container with mounted volume. Removes container after stop.
docker run -it -p <h_port>:<c_port> --mount type=volume,src=<volume>,target=<c_path> --rm <image>
# List networks
docker network ls
# Create new network
docker network create --driver=bridge <network>
# Run container with network
docker run -d --network=<network> -p <h_port>:<c_port> --name=<container> --rm <image>
Example of docker-compose.yml
file.
version: <Docker Compose Ver (for ex. 3)>
services:
<container #1>:
build: <dockerfile_path>
ports:
- "<h_port>:<c_port>"
volumes:
- <h_path>:<c_path>
links:
- <container #2>
environment:
- <env_var>
<container #2>:
image: <image>
Commands.
// Build ans spins-up all container.
docker-compose up --build
// Will spin-up numerous amount of containers.
docker-compose up --scale <container>=<count>