Basic Docker Images

I am going through the introduction to containers and docker on acloud.guru. Here are the notes
Image is the source of a container.

Images are made of layers: libraries, variables code, configs, etc

The layers are cached: After the inital build the layer is stored and can be re-used as needed

Everything you need to do is stored in the image

Images are generally based on a parent image. If they aren’t based on an image they are base images

COPY/ADD: Copy works inside the container, ADD can pull from any URL: So pulling from github would use ADD. COPY is easier to use

Steps to build a docker file

The Dockerfile should be in the same DIR as the config files and such

  • Define parent image: Using FROM: FROM node:10-alpine
  • Create directory structure Use RUN to create the dir and set owner: ex RUN mkdir -p /home/node/app/ && chown -R node:node /home/node/app
  • Move directories: Use WORKDIR to set the active directory: WORKDIR /home/node/app
  • Copy files: Use COPY to move the files to the correct dir: COPY package*.json ./
  • Use RUN to run any commands you want to run: (You can have many diffrent “RUN” commands)
  • Use USER to switch the node user: USER node
  • Use EXPOSE to ensure we can hit the port on the container: EXPOSE 8080
  • Use CMD to actually run the command that the container will be running: CMD [‘node’, ‘index.js”]
  • Build the image: docker build . -t my_image (docker build /location of Dockerfile if it’s not in ./ ). The -t gives the image a name
  • Start the image: docker run -dt –name my_container my_image