Intro
Lists running containers
docker ps
Options
-a,
--
all
lists all containers (even those not currently running).-l,
--
latest
show 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 list
publish container’s ports to the host.- format:
local_port:container_port
, e.g.8080:8080
.
- format:
--name
assign a name to the container.--rm
automatically remove the container when it exits.-d
,--detach
Run container in background and print container ID.-v
,--volume list
bind mount a volume.- format:
local_path:container_path
. - If
local_path
is 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,
--
attach
shows outputs from container.
Execute command in a running container
docker exec -it <pid> <command>
Options
-i,
--
interactive
keep Input open.-t,
--
tty
allocate 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 list
Namelist
in 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 name
Name 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
--build
build images before starting containers.-d, -detach
Run containers in background, print new container names.
Stop and remove containers, networks, images, and volumes:
docker-compose down
Restart policies
"
no
"
always
on-failure
only restart if the container exists with an error code.unless-stopped
always restart unless forced withdocker stop
.