Lists running containers

docker ps


Running a container from an image

docker run <image name> <command>

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


Docker run is a combination of create and start.

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


Execute command in a running container

docker exec -it <pid> <command>


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.


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'
    container_name: web
        context: .
    restart: on-failure
        - "3000:3000"
        - /app/node_modules
        - .:/app
        command: ["yarn", "test"]

Docker Compose commands

Run all services:

docker-compose up


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

docker-compose down

Restart policies