Skip to content

Instantly share code, notes, and snippets.

@joaquimds
Last active February 29, 2024 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joaquimds/3b4d8bc3b6035d58a1b2ca3d7ac81ed9 to your computer and use it in GitHub Desktop.
Save joaquimds/3b4d8bc3b6035d58a1b2ca3d7ac81ed9 to your computer and use it in GitHub Desktop.
Docker Intro

Docker Intro

  • An image is a set of instructions, and the environment you get after running the instructions.
  • A container is a command running in an image.

Analogy: an image is the Ikea instructions for building shelves, and the assembled shelves, before you start using them.

A container is the shelves in-use, with books on them etc.

Containers are ephemeral - when the command finishes, the container stops and all changes are lost. This includes all files created in the container.

To keep the files, you must use a "volume" - this can be a local folder, or a "Docker volume". Most docker containers will have a volume attached to them, either to share local code with the container, or to store data and not lose it when the container stops.

When you're working with containers, you can think of them as a separate computer running inside your computer. They are supposed to behave like this, and generally do, except for some strange and unimportant edge cases.

Docker App Example

Dockerfile example:

# Start with a lightweight Ubuntu Linux operating system
FROM ubuntu

# Commands we need to run to install nodejs, which we need to run nextjs
# This will be permanent, as we are building the image
# Remember the image gives us the final product of running all the commands
# in the Dockerfile
RUN apt-get update
RUN apt-get install -y curl
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && apt-get install -y nodejs

# Start the container in the /app directory
WORKDIR /app
# The default command when running a container. For us: start the nextjs dev server
CMD ["npm", "run", "dev"]

Commands to start up NextJS:

  1. Install NextJS in the directory: npx create-next-app@latest. This will install NextJS in a directory called my-app.
  2. Build the Docker image: docker build . -t example-image
  3. Run the Docker container: docker run -it -v ./my-app:/app -p 3000:3000 joaquim
  • docker run starts a container
  • -it makes it interactive, which means you can type into it
  • -v ./my-app:/app shares the my-app local directory with the /app directory in the container (the actual verb used here is "mount" - local folder my-app is mounted at /app in the container)
  • -p 3000:3000 connects host port 3000 (port 3000 on the machine running docker, e.g. your Macbook) to the container port 3000 (where nextjs is listening)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment