Intro

Lists running containers

docker ps

Options

Running a container from an image

docker run <image name> <command>

<command> optionally overrides the default command defined on the image.

Options

Docker run is a combination of create and start.

# Returns a `pid`
docker create <image name>
docker start <pid>

Options

Execute command in a running container

docker exec -it <pid> <command>

Options

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

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

Stop and remove containers, networks, images, and volumes:

docker-compose down

Restart policies