- Why You Should Know This (2 min)
- Learning Objectives (3 min)
- Overview / TT (30 min)
- BREAK (10 min)
- Lab Time
- Additional Resources
Today's plan focuses on tips and tricks I learned using docker-compose IRL!
Let's go over some strategies and best practices I've personally leveraged in the real world when architecting containers to deploy my applications to my developers and stakeholders.
A Dockerfile contains everything needed to set up the environment to run your application. Dockerfiles typically follow this recipe:
- Start with an image.
- Create a directory for your project inside the container.
- Copy the project's code to the directory inside the container.
- Install the project's dependencies using a package manager (
npm,pip, etc). - Expose the port(s) the application will run on.
- Run a command (
npm start,flask run, etc).
docker-compose.yml is a supplement to a Dockerfile that enables developers to configure and launch one to many containers at once --- your application included!
When a new developer clones your repository, they should only need to run docker-compose up to get started.
Imagine you have a docker-compose.yml file in your project with the following configuration:
version: '2'
services:
ghost:
image: ghost:${GHOST_VERSION}
ports:
- ${GHOST_PORT}:2368Set GHOST_VERSION and GHOST_PORT all in one command by running GHOST_VERSION=2 GHOST_PORT=8080 docker-compose up!
Compose supports declaring default environment variables in an environment file named .env placed in the folder where the docker-compose command is executed (current working directory).
Syntax Rules:
- Compose expects each line in the
.envfile to be inVAR=VALformat. - Lines beginning with
#are processed as comments and ignored. - Blank lines are ignored.
- There is no special handling of quotation marks.
- This means that they are part of the
VAL.
- This means that they are part of the
- Add
*.override.ymlto the.gitignorefile in the root of the repository. - Create a
docker-compose.override.ymlfile in order to set custom settings and override the rootdocker-compose.ymlfile. - Level up your DevOps strategy by adding a
docker-compose.override.sample.ymlfile to your repository. Make sure there are lots of comments that clearly explain which values can be customized for each developer's environment!
Compose files can get messy and difficult to read quickly, especially in microservice architectures. Use this handy compose_format command to check your Compose file and auto-format it based on best practices for readability.
cat docker-compose.yml | docker run -i funkwerk/compose_formatmongo, postgres, and many other dependencies take time to start up.
How do you get a container to start before another?
Dockerfile:
FROM node:latest
RUN mkdir -p /usr/src/project_name
WORKDIR /usr/src/project_name
COPY package*.json /usr/src/project_name/
RUN npm install
COPY . /usr/src/project_name
EXPOSE 3000
# Launch the wait tool, then your application.
# Source: https://dev.to/hugodias/wait-for-mongodb-to-start-on-docker-3h8b
ADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.2.1/wait /wait
RUN chmod +x /wait
CMD /wait && npm startdocker-compose.yml:
version: "3.3"
services:
mongo:
container_name: mongo
image: mongo
volumes:
- ./data:/data/db
ports:
- "27017:27017"
app:
container_name: app
build: .
ports:
- "3000:3000"
links:
- mongo
depends_on:
- mongo
environment:
WAIT_HOSTS: mongo:27017
volumes:
mongo_data:Continue working on your final project, presentation, or blog post. Your instructor will be placing you into framework-based breakout rooms for better support from your peers!