Intro
Lists running containers
docker ps
Options
-a,--alllists all containers (even those not currently running).-l,--latestshow the latest created container.
Running a container from an image
docker run <image name> <command>
<command> optionally overrides the default command defined on the image.
Options
-p,--publish listpublish container’s ports to the host.- format:
local_port:container_port, e.g.8080:8080.
- format:
--nameassign a name to the container.--rmautomatically remove the container when it exits.-d,--detachRun container in background and print container ID.-v,--volume listbind mount a volume.- format:
local_path:container_path. - If
local_pathis not provided, it will not be mapped.
- format:
Docker run is a combination of create and start.
# Returns a `pid`
docker create <image name>
docker start <pid>
Options
-a,--attachshows outputs from container.
Execute command in a running container
docker exec -it <pid> <command>
Options
-i,--interactivekeep Input open.-t,--ttyallocate a pseudo-TTY.
Get logs from a container
docker logs <pid>
Stop containers
Send SIGTERM to process:
docker stop <pid>
Send SIGKILL to process:
docker kill <pid>
Remove a docker container with conflicting names
docker rm <container name>
Delete containers
docker system prune
Remove all stopped containers, dangling images, build cache and networks not used by at least one container:
Returns the pid of deleted containers and shows total reclaimed space.
Create a Dockerfile
On a file simply named Dockerfile:
# Use an existing docker image as a base
FROM alpine
# Donwload and install a dependency
RUN apk add --update redis
# Tell the image what to do when it starts as a container
CMD ["redis-server"]
Build a docker image from the Dockerfile
docker build <directory>
Returns a pid.
Options
-t,--tag listNamelistin the formatname:tag.<docker id>/<project name>:<version>.- Example:
docker build -t stephengrider/redis:latest redis-server. - Then run with
docker run stephengrider/redis.
-f,--file nameName of the Dockerfile (default isPATH/Dockerfile).
List all docker images on the machine
docker image ls
Run the docker image
docker run <pid>
Create an image from a running container example
docker run -it alpine sh
docker ps # check the pid for the running container
docker commit -c 'CMD ["redis-server"]' <pid>
docker run <pid>
Dockerfile commands
Copy files from local to container:
COPY <path relative to build context> <path on container>
Any following comand will be executed relative to this path in the container:
WORKDIR <path>
EXPOSE <port number>
VOLUME <container path>
Docker Compose
On a file named docker-compose.yml:
version: '3'
services:
web:
container_name: web
build:
context: .
dockerfile: Dockerfile.dev
restart: on-failure
ports:
- "3000:3000"
volumes:
- /app/node_modules
- .:/app
command: ["yarn", "test"]
Docker Compose commands
Run all services:
docker-compose up
Options
--buildbuild images before starting containers.-d, -detachRun containers in background, print new container names.
Stop and remove containers, networks, images, and volumes:
docker-compose down
Restart policies
"no"alwayson-failureonly restart if the container exists with an error code.unless-stoppedalways restart unless forced withdocker stop.