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

Getting inside a container

docker attach will let you connect to your Docker container, but this isn't really the same thing as ssh. If your container is running a webserver, for example, docker attach will probably connect you to the stdout of the web server process. It won't necessarily give you a shell.

docker exec -it <CONTAINER ID> bash

This is useful when you have for example a MySQL server running inside a container. Once you're inside with docker exec you can just connect to your db with mysql -h 127.0.0.1 -P 3306 -u root -p or whatever ports/user/pass you are using.

more details

Other resources

Liz Rice - Containers from scratch