Skip to main content

Docker

Dockerfiles, Images and Containers

Dockerfile is a file containing a set of instructions to build an Image which can be used to start a Docker container.

Analogy:

Dockerfile = Makefile

Image == binary

Container == running process

Example of a Dockerfile:

FROM node

WORKDIR /app

COPY . /app

RUN npm install

EXPOSE 80

CMD ["node", "server.js"]

Running docker build . inside a directory containing this Dockerfile will build the image.

EXPOSE 80 line is just for documentation purposes. To actually expose and bind to a port use: docker run -p 3333:80 IMAGE.

COPY . . copies from local directory into the image WORKDIR.

If RUN npm install runs during image building, CMD ["node", "server.js"] runs when the container is started.

Commands

docker build . Build an image from a Dockerfile located in working directory

docker run IMAGE Run a built image

docker ps Show running containers

docker ps -a Show all containers (default shows just running)

docker run -p 3000:80 IMAGE Run a container and bind to local 3000 exposed container 80

Resources

Liz Rice - Containers from scratch