Skip to content

[P1] 5. Docker

Kim Seohyun edited this page Jun 28, 2022 · 8 revisions

Lecture Summary

Concepts

Docker

Docker is a Linux-based, open-source containerization platform. Developers use to build, run, and package applications for deployment using containers.

Images

Images are like blueprints containing instructions for creating a Docker container. Images define:

  • Application dependencies
  • The processes that should run when the application launches

You can get images from DockerHub or create your own images by including specific instructions within a file called Dockerfile.

Containers

Containers are live instances of images on which an application or its independent modules are run. In an object-oriented programming analogy, an image is a class and the container is an instance of that class. This allows operational efficiency by allowing to you to multiple containers from a single image.

Building With Dockerfile

Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.

Writing Dockerfile

# Comment
INSTRUCTION arguments

The instruction is not case-sensitive. However, convention is for them to be UPPERCASE to distinguish them from arguments more easily.
Docker runs instructions in a Dockerfile in order. A Dockerfile must begin with a FROM instruction. This may be after parser directives, comments, and globally scoped ARGs. The FROM instruction specifies the Parent Image from which you are building. FROM may only be preceded by one or more ARG instructions, which declare arguments that are used in FROM lines in the Dockerfile.
Environment variables are notated in the Dockerfile either with $variable_name or ${variable_name}. They are treated equivalently and the brace syntax is typically used to address issues with variable names with no whitespace, like ${foo}_bar.

Commands

Using docker build users can create an automated build that executes several command-line instructions in succession. The docker build command builds an image from a Dockerfile and a context. The build’s context is the set of files at a specified location PATH or URL. The PATH is a directory on your local filesystem. The URL is a Git repository location.

Managing With docker-compose

The Compose file is a YAML file defining services, networks, and volumes for a Docker application.

Writing docker-compose.yml

Commands

Run docker-compose up and the Docker compose command starts and runs your entire app. You can alternatively run docker-compose up using the docker-compose binary.

  • docker-compose up -d
  • docker-compose run
  • docker-compose stop
  • docker-compose down

Things That Were Confusing

  • USER command
    The USER instruction sets the user name (or UID) and optionally the user group (or GID) to use when running the image and for any RUN, CMD and ENTRYPOINT instructions that follow it in the Dockerfile.
    Initially, the Dockerfile had a command of USER user which resulted the error below. The user was not added and had no authority to create a directory. Screen Shot 2022-06-27 at 4 50 29 PM
  • How to install and user docker compose on ubuntu 20.04
  • Error: connect ECONNREFUSED 127.0.0.1:5432 at TCPConnectWrap.afterConnect [as oncomplete]
    The error above was fixed by changing the DB_HOST environment from 'localhost' to 'db'. However, I do not know the reason why this was the solution.

What Else I Found Out

  • choosing alternative python version
    > sudo update-alternatives --install /usr/bin/python python /usr/bin/python${v1} 1
    > sudo update-alternatives --install /usr/bin/python python /usr/bin/python${v2} 1
    > sudo update-alternatives --config python
    
  • RFC 2119

Curriculum Page
Related PR

Clone this wiki locally